oracle常規操作

1、創建表

Sql代碼  收藏代碼

  1. create table test(  

  2.   id varchar2(10),  

  3.   age number  

  4. );  


2、備份表

Sql代碼  收藏代碼

  1. create table   

  2. as   

  3. select * from test group by id;  

 
3、刪除表

Sql代碼  收藏代碼

  1. drop table test;--刪除表結構和表數據  


4、清空表

Sql代碼  收藏代碼

  1. truncate table test;--清空數據表數據,沒有返回餘地  

  2. delete from test;---清空數據表數據,有返回餘地  

 

5、添加字段下載

Sql代碼  收藏代碼

  1. alter table test add (  

  2.   name varchar2(10),  

  3.   interest varchar2(20)  

  4. );  


6、刪除字段

Sql代碼  收藏代碼

  1. alter table test drop name;  


7、更新數據

7.1更新一條數據

Sql代碼  收藏代碼

  1. update test t   

  2. set t.id='001'   

  3. where t.id is not null;  

  4. commit;  

 

7.2從其他表更新多條數據

Sql代碼 下載 收藏代碼

  1. update test t set t.name=(  

  2.   select tm.name   

  3.     from test_common tm   

  4.   where t.id=tm.id  

  5. );  

  6. commit;  

  7. --備註:update數據時,最好將update子查詢中的sql單獨建表,提高更新速度。  

 

7.3在PL/SQL中查詢完數據直接進入編輯模式更改數據

Sql代碼  收藏代碼

  1. select * from test for update;  

  2. --備註:更新操作執行完,要鎖上數據表,同時執行commit提交操作  

 

8、查詢數據

Sql代碼  收藏代碼

  1. select * from test;  

 
9、查詢數據表數量

Sql代碼  下載

  1. select count(0) from test;  

  2. --備註:count(0)或者其他數字比count(*)更加節省數據庫資源,高效快捷  


10、插入數據

10.1插入一條數據中的多個字段

Sql代碼  收藏代碼

  1. insert into test (C1,C2) values(1,'技術部');  

Sql代碼  收藏代碼

  1. insert into test(C1,C2) select C1,C2 from test_common;  

  2. commit;  

 

10.2插入多條數據

Sql代碼  收藏代碼

  1. insert into test(  

  2.   id,  

  3.   name,  

  4.   age  

  5. )  

  6. select  

  7.   id,  

  8.   name,  

  9.   age  

  10. from test_common;  

  11. commit;  

  12. --備註:1、插入多條數據時,insert into語句後面沒有values,直接拼接數據查詢語句;2、在oracle中對數據表進行了insert、update、delete等操作後,要執行commit提交,否則在系統處是禁止操作數據庫的,因爲此時數據庫已經被鎖死,這是數據庫爲了防止多人同時修改數據庫數據造成混亂的一種防範機制。  


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章