Oracle常用操作表结构的语句

首先,一起来认识几个单词。

alter (改变)  rename(重命名)  column(柱子,用来表示列)  modify(修改)  comment on (评论)  truncate (截断)

1.创建表

  create table 表名(字段名1 字段1类型 默认值(是否为空),字段名2 字段2类型 默认值);

  例:create table tab_person (id varchar2(50) default sys_guid() not null, mc varchar2(20) null,createtime date null);

2.更改表名

  alter table 表名 rename to 新表名;

  例:alter table tab_person rename to tab_people;

  rename 表名 to 新表名;

  例:rename tab_people to tab_person;

3.新增表字段

  alter table 表名 add 字段名 字段类型;

  例:alter table tab_person add sex varchar2(2);

4.修改字段名称

  alter table 表名 rename column 字段名 to 新字段名称;

  例:alter table tab_person rename column sex to xb;

5.修改字段类型

  alter table 表名 modify (字段名 新的字段类型);  //加不加括号貌似无所谓

  例:alter table tab_person modify xb varchar2(4);

       注:当此列有数据时,不能修改类型,不能将字段的长度减小,只能增加长度。

6.删除字段

  alter table 表名 drop column 字段名;

  例:alter table tab_person drop column xb;

7.添加/修改/删除字段说明

  comment on column 表名.字段名 is ‘表说明';  //删除就赋空值

  例:comment on column tab_person.mc is ‘名称’;

8.清空表内容数据

  truncate table 表名;

  例:truncate table tab_person;

9.删除表

  drop table 表名;

  例:drop table tab_person;

10.为id添加唯一约束

       alter table 表名 add primary key (列名) using index;

       alter table tab_person add primary key (id) using index;

11.为多个属性添加唯一约束

       alter table 表名 add constraint 索引名(不可重复) unique (列名1,列名2,。。。。)  using index;

       alter table tab_person add constraint tab_person_ys1 unique (xb,mc) using index;

12.解锁表

        select sid ,serial# from v$session t where t.SID in (select session_id from v$locked_object);

        alter system kill session 'sid,serial#';

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值