Oracle中的SQL語句(此篇重點介紹DML)

2021-08-28 22:12:40 字數 3612 閱讀 9999

1.dml=data manipulation language 資料操縱,由dbms提供,實現對資料庫中資料的操作。dml分為兩種,過程性dml和非過程性dml。

非過程化語言就是它一次處理乙個記錄,對資料提供自動導航,不對資料結果做過多的處理,使得使用者更容易得到結果。

過程化語言就是通過sql特定的語言對結果集進行特殊的處理,使得結果更符合使用者的要求。

insert

insert into student values(3,『liming』,0);

insert into student(id,name) values(4,『liming2』);

update

update student set name=『zhangwei』,***=1 where id=2;

delete

delete from student where id=1;(刪除指定行)

delete from student;(全部刪除)

where

where子句用於過濾記錄,並提供運算子

另外where中還常用到and 且 or 或,通常like會配合統配符%和_一起使用,前者匹配多個字元,後者匹配乙個字元。

select

含義:用於從資料庫中選取資料,結果被儲存在乙個結果集中,常見寫法如下:

select * from student where 1=1;

select id,name from student where 1=1;

select distinct name,*** from student where 1=1;

distinct用於去重

as

給字段或表取別名(可用as,或不寫,建議不寫)

給字段取別名:

select name n from student where 1=1 或

select name as n from student where 1=1

給表取別名:

select * from dog a

left join student b

on a.sid=b.id

where 1=1;

這裡就不能像上面那樣也寫as,會報錯,所以建議都不要寫as,直接空格就好了。

運算子

連線運算子:你可以concat函式,在oracle裡也有concat,但是它只能有兩個引數,oracle裡連線可以使用雙豎桿||

order by用於對結果集按照1個列或多個列進行排序,預設按公升序asc,如果要降序使用desc關鍵字

select * from student order by id desc;

like

模糊查詢,尤其值得注意的是:萬用字元%替代0個或多個字元;_下劃線替代1個字元

select * from student where name like 『%z%』;

select * from student where name like 『%s_n』;

in

限定集,允許你在where子句中規定多個值

select * from student where id in(1,3);

select * from student where not id in(1,3);

between

區間集,允許你指定乙個條件區間(包左也包右)

select * from student where id between 1 and 3;

select * from student where not id between 1 and 3;

null

null值代表未知資料,它不等同於0,通過is null來判斷是否為空

select * from student where name is null;

select * from student where name is not null;

case when then else end

譬如通常性別都用1位number儲存0或1,0代表男,1代表女,節省記憶體,那取資料的時候怎麼對映成男或女呢?就用到case函式了。

select name,case *** when 0 then 『男』 else 『女』 end ***

從乙個表中複製所有的列到另乙個表中,前提當然得是這兩個表結構相同;

從乙個表中賦值某列到另乙個表中。

insert into table2 select * from table1;

insert into table2(id,name) select id,name from table1;

join

sql join用於連線多張表,基於關聯欄位的多表查詢

主要有以下四大型別:

inner join:內連線

left join:左連線

right join:右連線

full join:全連線

這個不舉例了,用的比較多,尤其是涉及多對多的關係時,通常有乙個關聯表,這時通過三表查詢可以得到最全的結果集。有必要提的是,內連線,其實就是自身關聯自身,有乙個例子,省市二級聯動,可以只需要設計一張表,通過pid欄位關聯自身的id,如果pid為null說明其為省份,如果pid不為null,則其為市。但是**聯動時,就必須有多張表了。

union

union操作符用於合併兩個或多個select語句的結果集,union內部的每個select語句必須擁有相同數列的列。列也必須擁有相似的資料型別,同時每個select語句中列的順序也必須相同。union和union all是有區別的,前者是兩張表的去重了的結果集,後者是所有的。

select * from table1

union

select * from table2

oracle資料操作篇(sql語句)

建立序列seq cqyt,按1遞增,從1開始遞增,最小值為1 序列在資料庫中很常用 create sequence seq cqyt increment by 1 start with 1 nomaxvalue minvalue 1 刪除序列 drop sequence seq cqyt 檢視指定表...

Oracle中的SQL語句

1.選擇部門30中的所有員工.select empno as 部門編號 ename as 員工名稱 job 員工工作 mgr 領導姓名 hiredate sal,comm deptno from emp where deptno 30 select from emp 2.列出所有辦事員 clerk ...

oracle中的SQL語句(一)

oracle中有些語句和標準sql有些細微差別。現在總結一些。oracle中的sql語句總結 1.建立資料表 create table scott director director id number 6 not null,name varchar2 10 not null,zhicheng va...