oracle 详细建表语句

1.创建一张表格
create table test(id number,name varchar2(20),class_name varchar2(20));
1.1创建表格的时候直接创建主外键
create table test(id number primary key,name varchar2(20) constraint t_fk references table_name(column_name));
create table test(id number,name varchar2(20)
                       constraint pk primary key(id),
                        constraint fk foreign key(id) references 关联表(关联表主键);
--增加主键
alter table test add constraint constraint_name primary key(column_name);
--增加外键
alter table test add constraint constraint_name foreign key(column_name) references 关联表(column_name);--这里需要注意一个表的外键必须是另一个表的主键
--增加唯一索引
create unique index index_name on table_name(column_name);
--修改列的属性
alter table test modify column_name varchar2(10);--如果以前column_name varchar2(20);
--修改表的名称
rename table_name to table_name;
--为列设定默认值
alter table test modify name varchar2(20) default 'hello');--这个不会影响已经插入的数据只会对新数据造成影响
create table test (id number default 2);
--增加check
alter table test add constraint ck check ((id>20) and (id <30));
alter table test add constraint ck check(name in ('hello','world');
--unique
alter table test add constraint cuq unique(id);
--修改列的默认值
alter table test modify id default sysdate+1;
--添加列
alter table test add class varchar2(20);
alter table test add user varchar2(20) unique not null;
--删除列
alter table test drop column class;
--对表添加注释
commont on table test is '测试表';
--对列添加注释
commont on column table.id is '主键';
--删除表
drop table test;
--清除表的数据
truncate table test;
delete from test;

truncate 操作与 delete 操作对比区别
操作
回滚
高水线
空间
效率
Truncate
不能
下降
回收
delete
可以
不变
不回收
也就是说我们delete掉一个表格时其表格所占用的表空间时不会释放的。
--禁用约束
alter table test disable constraint constraint_name;
--启用约束
alter table test enable constraint constraint_name;
--延迟约束
alter table test drop constraint constraint_name;
alter table test add constraint constraint_name foreign key(id) references table_name(column_name) deferrable initially deferred;

deferrable的两个选项区别

deferrable表示该约束是可延迟验证的它有两个选项:

Initially immediate(默认): 立即验证执行完一个sql后就进行验证;

Initially deferred: 延迟验证当事务提交时或调用set constraint[s] immediate语句时才验证.

区别是事务提交时验证不通过则立即回滚事务; set constraint[s] immediate只验证不回滚事务.

not deferrabledeferrable区别

区别就在于: “立即验证的可延迟约束” 是可以根据需要设置成 延迟验证的可延迟约束不可延迟验证是不能改变的.

最后我们如果想查看我们对test表格建立了那些约束以及约束所对应的列
可以通过查看user_constraints and user_cons_columns;

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/26509390/viewspace-1391373/,如需转载,请注明出处,否则将追究法律责任。

转载于:http://blog.itpub.net/26509390/viewspace-1391373/

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值