查询oracle数据库约束条件以及删除约束下内容

查询所有约束条件:
select * from USER_CONSTRAINTS  ;

constraint_type='R' 外键约束
constraint_type='P' 唯一性约束

查询外键约束:
select CONSTRAINT_NAME,TABLE_NAME from user_constraints

t where  CONSTRAINT_TYPE ='R'  ;

查询带有外键约束以及表名为'JS_RESOURCE'的所有约束 :
  select a.constraint_name,a.table_name,a.column_name
from user_cons_columns a,user_constraints b
where a.constraint_name=b.constraint_name  and
b.constraint_type='R' and b.table_name='JS_RESOURCE';

查询带有外键约束以及列名为'TYPEID'的约束:
  select  a.constraint_name,a.table_name,a.column_name
from user_cons_columns a,user_constraints b
where a.constraint_name=b.constraint_name  and
b.constraint_type='R' and a.column_name ='TYPEID';

删除带有外键约束的表中的内容:
   根据父表中主键的id值,先删除子表中的对应字段的内容,再删除父表中对应id值的内容;

使约束失效:
ALTER TABLE 表名 DISABLE CONSTRANT 约束名;
使约束条件生效:
ALTER TABLE 表名 ENABLE CONSTRANT 约束名;


问题:在有完整性约束条件下删除一行数据;
解决步骤:
1.查询出该行数据所有内容;select * from user;
2.试着根据id删除数据;delete from user where id='213';
3.如果有完整性约束,会报SQL错误:ORA-02292:违反完整性约束条件(TEST.FKFA236FEC23B0DE15)-已找到子记录等,根据约束名和表名查询
   select * from user_cons_columns a,user_constraints b
   where b.constraint_name='FKCDD5512A23B0DE15' and  
   a.table_name='USER';
4.按上述方法应该能查询到子表名,例如我的是contact_group ;
5.查询出user表的外键userid: select * from contact_group;
6.删除子表内容:delete from contact_group where userid='213';
7.删除主表内容:delete from user where id='213';
至此,完整性约束条件下删除一行数据完成。

猜你喜欢

转载自lafecat.iteye.com/blog/1927502