Oracle PL/SQL常用命令

來源:互聯網
上載者:User

一、Oracle PL/SQL使用
1、使用者管理
資料庫系統許可權(DataBaseSystem Privilege)

資料庫物件許可權(DataBaseSystem Privilege)

Oracle內部使用者Sys和System

建立使用者:create user user_test identified by user_test123;

修改使用者:alter user user_test identified by user_test123;

刪除使用者:drop user user_test;

刪除使用者與其對象:drop user user_testCASCADE;

使用者授權串連:grant connect,resource to user_test;

 

在sqlplus 中使用以下命令可以查出使用者表的清單:

sqlplus scott/tiger -- 使用sqlplus 登入 oracle 資料庫

col table_name format a30; -- 指定table_name 的列寬

col table_type format a10; -- 指定table_type 的列寬

select * from cat; -- 列出使用者資料表

 

2、角色管理
1)Connect Role(串連角色):臨時使用者,不需要建表的使用者。

2)Resource Role(資源角色):更可靠和正式的資料庫使用者。

3)Dba Role(資料庫管理員角色):擁有所有的系統許可權。

操作:

grant(授權)命令:grant connect,resource to user_test;

    revoke(撤銷)命令:revoke connect,resource to user_test;

建立角色:除了系統內建的3種標準角色外使用者可以建立自己的role.

          create role user_test_role;

角色授權:grant select on t_service to user_test_role;

註:擁有student角色的使用者都具有對t_service表的select許可權。

刪除角色:droprole user_test_role;

查看使用者有哪些角色:

select grant_role fromdba_role_privs where grantee='scott';

查看使用者有哪些許可權:

select privilege from dba_sys_privs wheregrantee='scott';

select privilege fromdba_sys_privs where grantee='CONNECT';

預設使用者:Sys:oralce中的超級使用者,主要用來維護系統資訊和管理執行個體。

System:oracle中預設的系統管理員,擁有dba許可權。通常管理oracle資料庫的使用者、許可權和儲存等。

Scott:oracle資料庫的一個示範賬戶,在資料庫安裝時建立。

使用者授權: grant 許可權 [ on 對象名 ] to 使用者名稱 [ with grant option];

          grant select on scott.emp to user_test with grant option;

          grant create session to user_test; 

3、許可權管理
授予user_test使用者查看emp表資料的許可權

1.驗證user_test對scott使用者的emp表進行查詢的許可權。

SQL>select *from scott.emp; 

2.為使用者user_test授予scott使用者的emp表的查詢許可權。

SQL>conns cott/scott@test;

SQL>grant select on scott.emp to user_test;

 

3.使用user_test賬戶登入並查詢scott使用者的表emp資訊。

SQL>conn user_test/u01@test;

SQL>select *from scott.emp;

收回許可權: revoke 許可權 [ on 對象名 ] from 使用者名稱

SQL>revoke select on scott.emp from user_test;

修改使用者密碼:alter user 使用者名稱 identified by 新密碼;

與許可權相關的表:

1.dba_sys_privs(所有系統許可權)

2.user_sys_privs(使用者擁有的系統許可權)

3.user_col_privs(使用者擁有的對象許可權)

常用的系統許可權指派陳述式:

SQL>grant create session to user_test;

SQL>grant create table to user_test;

SQL>grant unlimited tablespace to user_test;

SQL>grant create session to public;

對象許可權的賦予與撤銷語句:

SQL>grant select on mytable to user_test;

SQL>grant all on mytable to user_test;

SQL>revoke select on mytable from user_test;

SQL>revoke all on mytable from user_test;

Oracle中除了能在表對象上賦予相應的許可權,還能將許可權控制到表的列上:

SQL>grant update(name)on mytable to user_test;

SQL>grant insert(id) on mytable to user_test;

4、口令管理
使用profile系統管理使用者口令

profile是口令限制,資源管理的命令集合,當建立資料庫時,Oracle會自動建立名詞為default的profile。當建立使用者沒有指定profile選項,那Oroacle就會將default分配給使用者。

關鍵字列表:

SQL>create profile 設定檔名稱 limit failed_login_attempts 嘗試次數password_lock_time 鎖定天數;

SQL>alter user 使用者名稱 profile 設定檔名稱;

例:指定tea使用者最多隻能嘗試3次登陸,鎖定時間為2天,下面可以實現。

SQL>create profile lock_account limit failed_login_attempts 3 password_lock_time 2;

SQL>alter user tea profile lock_account;

給帳號解鎖

SQL>alter user user_test account unlock;

例:為了讓使用者定期修改密碼可使用終止口令完成。

SQL>create profile myprofile limit password_life_time 10 password_grace_time 2;

給使用者解鎖

SQL>alter user user_testprofile myprofile;

刪除profile

SQL>drop profile myprofile[cascade];

5、有關表sequence
建立主鍵自增表 

1)建立表 t_test   

create table t_test(

          userid number(10) NOT NULL primary key,  /*主鍵,自動增加*/

          username varchar2(20)

          );

2)建立自動成長序列

 CREATESEQUENCE t_test_increase_sequence

 INCREMENTBY 1  -- 每次加幾個 

    START WITH 1    -- 從1開始計數 

    NOMAXVALUE      -- 不設定最大值  ,設定最大值:maxvalue 9999

    NOCYCLE          -- 一直累加,不迴圈

    CACHE 10;

 

樣本:CREATE SEQUENCE t_service_sequence INCREMENT BY1  START WITH 1  NOMAXVALUE  NOCYCLE    CACHE 10;

 

3)建立觸發器(用戶端工具添加)

 

CREATE TRIGGER t_service_triggerBEFORE

insert ON t_service FOR EACH ROW          /*對每一行都檢測是否觸發*/

begin

select t_service_sequence.nextvalinto:New.userid from dual;

end;

 

樣本:

create orreplacetrigger t_service_trigger

  before insert on t_service 

  for eachrow

declare

  -- local variables here

begin

  select t_service_sequence.nextvalinto:New.idfrom dual;

end t_service_trigger;

 

4)提交

 

commit;

5)測試

 

insert intoTest_Increase(Username) values('test');

 


6、遠端連線
在串連之前設定好監聽程式然後使用如下命令:

sqlplus usr/pwd@//host:port/sid


二、SpringJDBC配置
#flow config jdbcfor oracle

jdbc.driver=oracle.jdbc.driver.OracleDriver

 

jdbc.url=jdbc:oracle:thin:@127.0.0.1:1521:orcl

jdbc.username=user_test

jdbc.password=user_test123

 

xml裡配置方言:

<!-- 設定hibernate相關屬性 -->

        <propertyname="hibernateProperties">

            <props>

                <propkey="hibernate.dialect">org.hibernate.dialect.OracleDialect</prop>

                <propkey="hibernate.show_sql">true</prop>

                <propkey="generate_statistics">true</prop>

                <propkey="hibernate.use_sql_comments">false</prop>

                <propkey="hibernate.format_sql">true</prop>

                <propkey="hibernate.hbm2ddl.auto">update</prop>

            </props>

        </property>

rlwrap - 解決Linux下SQLPLUS退格、上翻鍵亂碼問題

SQLPLUS spool 到動態記錄檔名

Oracle SQLPLUS提示符設定

通過設定SQLPLUS ARRAYSIZE(行預取)加快SQL返回速度

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.