1. 程式人生 > >Oracle資料庫語句大全(一)

Oracle資料庫語句大全(一)

一.入門部分

1. 建立表空間create tablespace schooltbs datafile D:\oracle\datasource\schooltbs.dbf size 10M autoextend on;

2. 刪除表空間drop tablespace schooltbs[including contents and datafiles];

3. 查詢表空間基本資訊select *||tablespace_name from DBA_TABLESPACES;

4. 建立使用者create user  lihuaidentified by lihuadefault tablespace schooltbs

temporary tablespace temp;

5. 更改使用者alter user lihua identified by 123default tablespace users;

6. 鎖定使用者alter user lihua account lock|unlock;

7. 刪除使用者drop user lihua cascade;--刪除使用者模式

8. oracle資料庫中的角色connect,dba,select_catalog_role,delete_catalog_role,execute_catalog_role,exp_full_database,imp_full_database,

resource

9. 授予連線伺服器的角色grant connect to lihua;

10. 授予使用表空間的角色grant resource to lihua with grant option;--該使用者也有授權的許可權

11. 授予操作表的許可權grant select,insert on user_tbl to scott;--當前使用者grant delete,update on lihua.user_tbl to scott;--系統管理員

二.SQL查詢和SQL函式

1.SQl支援的命令:資料定義語言(DDL:create,alter,drop資料操縱語言(DML

:insert,delete,update,select資料控制語言(DCL:grant,revoke事務控制語言(TCL:commit,savepoint,rollback

2.Oracle資料型別字元,數值,日期,RAWLOB字元型char:1-2000位元組的定長字元varchar2:1-4000位元組的變長字元long:2GB的變長字元 注意:一個表中最多可有一列為long

Long列不能定義唯一約束或主鍵約束

long列上不能建立索引

過程或儲存過程不能接受long型別的引數。

數值型number:最高精度38日期時間型date:精確到sstimestamp:秒值精確到小數點後6

函式sysdate,systimestamp返回系統當前日期,時間和時區。更改時間的顯示alter session set nls_date_language=american;alter session set nls_date_format=yyyy-mm-dd;

Oracle中的偽列像一個表列,但沒有儲存在表中

偽列可以查詢,但不能插入、更新和修改它們的值常用的偽列:rowidrownumrowid:表中行的儲存地址,可唯一標示資料庫中的某一行,可以使用該列快速定位表中的行。rownum:查詢返回結果集中的行的序號,可以使用它來限制查詢返回的行數。

3.資料定義語言

用於操作表的命令create tablealter tabletruncate tabledrop table

修改表的命令alter table stu_table rename to stu_tbl;--修改表名alter table stu_tbl rename column stu_sex to sex;--修改列名alter table stu_tbl add (stu_age number);--新增新列alter table stu_tbl drop(sex);--刪除列alter table stu_tbl modify(stu_sex varchar2(2));--更改列的資料型別alter table stu_tbl add constraint pk_stu_tbl primary key(id);--新增約束

4.資料操縱語言

select,update,delete,insert利用現有的表建立表create table stu_tbl_log as select id,stu_name,stu_age from stu_tbl;--選擇無重複的行

select distinct stu_name from stu_tbl;--插入來自其他表中的記錄insert into stu_tbl_log select id,stu_name,stu_age from stu_tbl;

5.資料控制語言grant,revoke6.事務控制語言commit,savepoint,rollback7.SQL操作符算術操作符:L+-*/比較操作符:L=,!=,<>,>,<,>=,<=,between-and,in,like,is null邏輯操作符:Land,or,not集合操作符:Lunion,union all,intersect,minus連線操作符:L||示例中stu_tbl_log中的資料如下:        ID STU_NAME                STU_AGE

---------- -------------------- ----------

      1000 李華                          20

      1001 accp                         20

      1003 nimda                         3stu_tbl中的資料如下:        ID STU_NAME             ST    STU_AGE

---------- -------------------- -- ----------

      1000 李華                  男         20

      1001 accp                 男         20

      1002 admin                男         30示例:select (3+2)/2 from dual;--算術操作符,結果:2.5select * from stu_tbl where stu_age>=20;--比較操作符select * from stu_tbl where stu_name like '%a%';--比較操作符:likeselect * from stu_tbl where stu_name like 'a___';--比較操作符:likeselect * from stu_tbl where stu_age in(20,30);--比較操作符:inselect * from stu_tbl where stu_age between 20 and 30;--比較操作符:betweenselect stu_name from stu_tbl union allselect stu_name from stu_tbl_log;--集合操作符:union all,測試結果具體如下:STU_NAME

-----------

李華

accp

admin

李華

accp

nimda

已選擇6行。select stu_name from stu_tbl union select stu_name from stu_tbl_log;--集合操作符:union,測試結果具體如下:STU_NAME

---------

accp

admin

nimda

李華select stu_name from stu_tbl intersect select stu_name from stu_tbl_log;--集合操作符:intersect,測試結具體如下:STU_NAME

----------

accp

李華select stu_name from stu_tbl minus select stu_name from stu_tbl_log;--集合操作符:minus,測試結果如下:STU_NAME

----------

Admin從中可以看出:minus是獲取第一張表獨有的資料intersect是獲取兩張表中都有的資料union是整合兩張表的資料,都有的只顯示一次union all是純粹的兩張表資料整合select id,stu_name||' '||stu_sex as name_sex,stu_age from stu_tbl;--連線操作符||,測試結果具體如下:        ID NAME_SEX                   STU_AGE

---------- ----------------------- ----------

      1000 李華    男                         20

      1001 accp   男                         20

      1002 admin  男                         30

8.SQL函式單行函式:從表中查詢的每一行只返回一個值,可出現在select子句,where子句中

日期函式

數字函式

字元函式

轉換函式:ToChar(),ToDate(),ToNumber()

其他函式:

Nvl(exp1,exp2):表示式一為null時,返回表示式二

Nvl2(exp1,exp2,exp3):表示式一為null時返回表示式三,否則返回表示式二

Nullif(exp1,exp2):兩表示式相等時,返回null,否則返回表示式一分組函式:基於一組行來返回

Avg,Min,Max,Sum,Count

Group by,having分析函式

Row_number,rank,dense_rank示例:select u.user_name,sum(oi.order_num*oi.order_price) as total,row_number() over (order by sum(oi.order_num*oi.order_price) desc) as sort from order_item_tbl

  oi,user_tbl  u,order_tbl  o where oi.order_id = o.id and o.user_id = u.id group by u.user_name;

三.鎖和資料庫物件

1.鎖:資料庫用來控制共享資源併發訪問的機制。鎖的型別:行級鎖,表級鎖行級鎖:對正在被修改的行進行鎖定。行級鎖也被稱之為排他鎖。在使用下列語句時,Oracle會自動應用行級鎖:insert,update,delete,select…… for updateselect……for update允許使用者一次鎖定多條記錄進行更新。使用commit or rollback釋放鎖。表級鎖:lock table user_tbl in mode mode;表級鎖型別:行共享 row share行排他 row exclusive共享 share共享行排他 share row exclusive排他 exclusive死鎖:兩個或兩個以上的事務相互等待對方釋放資源,從而形成死鎖2.資料庫物件oracle資料庫物件又稱模式物件資料庫物件是邏輯結構的集合,最基本的資料庫物件是表資料庫物件:

表,序列,檢視,索引序列用於生成唯一,連續序號的物件。建立語法:create sequence user_id_seqstart with 1000increment by 1maxvalue 2000minvalue 1000nocyclecache 1000;--指定記憶體中預先分配的序號訪問序列:select user_id_seq.currval from dual;select user_id-seq.nextval from dual;

更改刪除序列:alter sequence user_id_seq maxvalue 10000;--不能修改其start with drop sequence user_id_seq;Hibernate中訪問序列:

<generator class="sequence">

<param name="sequence">      user_id_seq

</param>

</generator>

檢視以經過定製的方式顯示來自一個或多個表的資料建立檢視:create or replace view user_tbl_view (vid,vname,vage)as select id,user_name,age from user_tbl[with check option]|[with read only];建立帶有錯誤的檢視:create force view user_tbl_force_view asselect * from user_table;--此時user_table可以不存在建立外聯接檢視:create view user_stu_view asselect u.id,u.user_name,u.password,s.ddressfrom user_tbl u,stu_tbl swhere u.s_id(+)=s.id;--哪一方帶有(+),哪一方就是次要的刪除檢視:drop user_stu_view;

索引用於提高SQL語句執行的效能索引型別:唯一索引,點陣圖索引,組合索引,基於函式的索引,反向鍵索引

建立標準索引:create index user_id_index on user_tbl(id) tablespace schooltbs;重建索引:alter index user_id_index rebuild;刪除索引:drop index user_id_index;

建立唯一索引:create unique index user_id_index on user_tbl(id);建立組合索引:create index name_pass_index on user_tbl(user_name,password);建立反向鍵索引:create index user_id_index on user_tbl(id) reverse;