Oracle數據庫語句大全

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