维护约束
维护约束是由表所有者来完成的,如果以其他用户身份维护约束则需要具有alter any table 系统权限或者alter对象权限
1.增加约束
注:如果增加not null 约束,必须使用alter table语句的modify子句
如果增加unique、primary key、foreign key和check约束,必须使用alter table语句的add子句
例:增加not null约束
alter table i_top modify name not null;
例:增加primary key 约束
alter table i_top add primary key(i_id);
例:增加 foreign key约束
alter table i_top add i_don number(2) references i_tops(don);
例:增加unique约束
alter table i_top add constraint i_name unique(name);
例:增加check约束
alter table i_top add check(i_top between 1000 and 5000);
2.修改约束名
/*当使用IMPDP工具或者IMP工具导入其他用户对象时,为避免约束同名导致导入失败*/
例:修改i_top表中的SYS_id约束约束名为 ck_top_id
alter table i_top rename constraint SYS_id to ck_top_id;
3.删除约束
例:删除表i_top表中的约束 ck_top_id
alter table i_top drop constraint ck_top_id;
注:当删除特定表的主键约束时如果该表具有相关的从表,那么在删除主键约束时必须带有cascade选项,否则会报错
例:
alter table i_top drop primary key cascade;
4.禁止约束
/*在使用SQL*Loader或者insert 装载数据之前,为了加快数据装载速度应该先禁止约束再装载数据*/
例:禁止i_top表中SYS_id约束
alter table i_top disable constraint SYS_id;
5.激活约束
例:激活i_top表中SYS_id约束
alter table i_top enable constraint SYS_id;
6.显示约束信息<数据字典 USER_CONSTRAINTS>
--查询i_top表中的所有约束信息
select constraint_name as 约束名,
constraint_type as 约束类型
from user_constraints
where table_name='i_top';
/*注:constraint_type 列值说明
P:主键约束
R:外部键约束
C:check约束或not null约束
U:唯一约束*/
转载请注明原文地址: https://ju.6miu.com/read-39824.html