當前位置:才華齋>IT認證>Oracle認證>

Oracle資料庫語句大全

Oracle認證 閱讀(3.21W)

Oracle資料庫是甲骨文公司的一款關係資料庫管理系統。它是在資料庫領域一直處於領先地位的產品。下面yjbys小編為大家分享的是Oracle資料庫查詢語句,希望能幫助到大家!

Oracle資料庫語句大全

  一.入門部分

1. 建立表空間

create tablespace schooltbs datafile ‘D:oracledatasource’ size 10M autoextend on;

2. 刪除表空間

drop tablespace schooltbs[including contents and datafiles];

3. 查詢表空間基本資訊

select *||tablespace_name from DBA_TABLESPACES;

4. 建立使用者

create user lihua

identified by lihua

default tablespace schooltbs

temporary tablespace temp;

5. 更改使用者

alter user lihua

identified by 123

default 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 _tbl to scott;--系統管理員

12.修改表的結構(alter)

Alter table 表名 add(列的名稱,列的型別);

  二查詢和SQL函式

支援的命令:

資料定義語言(DDL):create,alter,drop

資料操縱語言(DML):insert,delete,update,select

資料控制語言(DCL):grant,revoke

事務控制語言(TCL):commit,savepoint,rollback

le資料型別

字元,數值,日期,RAW,LOB

字元型

char:1-2000位元組的定長字元

varchar2:1-4000位元組的變長字元

long:2GB的變長字元

注意:一個表中最多可有一列為long型

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

long列上不能建立索引

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

數值型

number:最高精度38位

日期時間型

date:精確到ss

timestamp:秒值精確到小數點後6位

函式

sysdate,systimestamp返回系統當前日期,時間和時區。

更改時間的顯示

alter session set nls_date_language=’american’;

alter session set nls_date_format=’yyyy-mm-dd’;

Oracle中的偽列

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

偽列可以查詢,但不能插入、更新和修改它們的值

常用的偽列:rowid和rownum

rowid:表中行的儲存地址,可唯一標示資料庫中的某一行,可以使用該列快速定位表中的行。

rownum:查詢返回結果集中的行的序號,可以使用它來限制查詢返回的行數。

3.資料定義語言

用於操作表的命令

create table

alter table

truncate table

drop 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,revoke

6.事務控制語言

commit,savepoint,rollback

操作符

算術操作符: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 3

stu_tbl中的資料如下:

ID STU_NAME ST STU_AGE

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

1000 李華 男 20

1001 accp 男 20

1002 admin 男 30

示例:

select (3+2)/2 from dual;--算術操作符,結果:2.5

select * from stu_tbl where stu_age>=20;--比較操作符

select * from stu_tbl where stu_name like '%a%';--比較操作符:like

select * from stu_tbl where stu_name like 'a___';--比較操作符:like

select * from stu_tbl where stu_age in(20,30);--比較操作符:in

select * from stu_tbl where stu_age between 20 and 30;--比較操作符:between

select stu_name from stu_tbl union all

select 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

函式

單行函式:從表中查詢的每一行只返回一個值,可出現在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 _name,sum(r_num*r_price) as total,row_number() over (order by sum(r_num*r_price) desc) as sort from order_item_tbl

oi,user_tbl u,order_tbl o where r_id = and _id = group by _name;

  三.鎖和資料庫物件

1.鎖:資料庫用來控制共享資源併發訪問的機制。

鎖的型別:行級鎖,表級鎖

行級鎖:對正在被修改的行進行鎖定。行級鎖也被稱之為排他鎖。

在使用下列語句時,Oracle會自動應用行級鎖:

insert,update,delete,select…… for update

select……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_seq

start with 1000

increment by 1

maxvalue 2000

minvalue 1000

nocycle

cache 1000;--指定記憶體中預先分配的序號

訪問序列:

select user_id_val from dual;

select user_val from dual;

更改刪除序列:

alter sequence user_id_seq maxvalue 10000;--不能修改其start with 值

drop sequence user_id_seq;

在Hibernate中訪問序列:

user_id_seq

檢視

以經過定製的`方式顯示來自一個或多個表的資料

建立檢視:

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 as