【Oracle笔记】最常用的CRUD操作命令

1、创建表

create table mytest(id varchar2(2 char),name varchar2(3 char));

2、插入数据

insert into mytest(id,name) values('01','tom');

3、查询数据

select * from mytest;

4、更新数据

update mytest set name='mm' where id='03';

5、删除数据

delete from mytest where id='03';

6、新增字段

alter table mytest add (address varchar2(100 char));

7、修改字段

alter table mytest rename column address to addr;
alter table mytest modify address varchar2(200 char);

8、删除字段

alter table mytest drop (addr);

9、重命名表

alter table mytest rename to mytest2;

10、清空表

delete from mytest;--删除数据,可回滚
truncate table mytest;--删除数据,不可回滚,并重置计数器

11、删除表

drop table mytest;--删除表,可恢复(flashback table mytest to before drop )
drop table mytest purge;--删除表,无法恢复

12、表只读

alter table mytest read only;--只读
alter table mytest read write;--恢复读写

13、查看该用户的所有表

select table_name from user_tables;

14、order by 排序

      desc:降序
      asc:升序(默认)

select * from mytest order by id desc;

15、分组

select sex from mytest group by sex;

16、备份表

create table mytest_new as select * from mytest;--备份表结构和数据
create table mytest_new as select * from mytest where 1=2; --只备份表结构
insert into mytest_new select * from mytest;--只备份表数据 

17、新增主键

alter table mytest add constraint name primary key(name); 

18、删除主键

alter table mytest drop constraint id;

19、新增索引

create  index  index_name  on  table(column_name1,column_name2);

20、删除索引

drop index index_name;

21、创建同义词

create synonym tablenameB for 数据库名字.tablenameA;

22、删除同义词

drop synonym tablenameB;

你可能感兴趣的:(Oracle笔记)