Rumah > Artikel > pangkalan data > Oracle 数据完整性的维护
1.删除约束 alter table table_name drop constraint constraint_name; 2.创建主键 alter table table_name add constrain
1.删除约束
alter table table_name drop constraint constraint_name;
2.创建主键
alter table table_name add constraint constraint_name primary key(column_name1,column_name2);
3.创建唯一约束
alter table table_name add constraint constraint_name unique(column_name1,column_name2);
4.创建外键约束
alter table table_name1 add constraint constraint_name foreign key(column_name1) references table_name2(column_name1);
enable/disable:约束/不约束新数据
novalidate/validate:不对/对老数据进行验证
alter table table_name add constraint constraint_name check(column_name like 'B%') enable/disable novalidate/validate;
5.修改约束条件,延时验证,commit时验证
alter table table_name modify constraint constraint_name initially deferred;
6.修改约束条件,立即验证
alter table table_name modify constraint constraint_name initially immediate;
7.设置约束延迟/立即验证
alter session set constraints=deferred/immediate;
8.删除表时删除约束
drop一个有外键的主键表,带cascade constraints参数级联删除
drop table table_name cascade constraints;
9.当truncate外键表时,,先将外键设为无效,再truncate;
truncate table table_name;
设约束条件无效
alter table table_name disable constraint constraint_name;
alter table table_name enable novalidate constraint constraint_name;