oracle常规操作

2023-05-10,,

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提交,否则在系统处是禁止操作数据库的,因为此时数据库已经被锁死,这是数据库为了防止多人同时修改数据库数据造成混乱的一种防范机制。  

《oracle常规操作.doc》

下载本文的Word格式文档,以方便收藏与打印。