oracle查看表外键信息代码

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_36411874/article/details/85051954

目录

(1)查看数据库表外键信息

(2)查询表的约束

(3)查询表的字段信息


ORA-02291: integrity constraint () violated - parent key not found

原因:外键关联。所谓外键关系就是一个表中的列引用了其他表中的列。例如,products表中的product_type_id列引用了 product_types表中的product_type_id列。product_types表称为父表(parent table),而products表则称为子表(child table),这是因为products表中的product_type_id列依赖于product_types表中的product_type_id 列。如果试图向products表中插入一行,但此行的product_type_id不存在,数据库就会返回ORA-02291错误。这个错误说明数据库无法找到一个匹配的父键值(此处父键就是product_types表中的product_type_id列)。

那么刚拿到一个数据库,如何拿到这个表的外键信息呢?

(1)查看数据库表外键信息

代码如下:

--根据所有主外键关联的表
select (select a.table_name
          from user_constraints a
         where a.constraint_name = c.r_constraint_name) 主表表名,
       c.r_constraint_name 主表主键键约束名,
       c.table_name 从表表名,
       c.constraint_name 从表外键约束名,c.*
  from user_constraints c
 where c.constraint_type = 'R'  and c.table_name= ?(问号填写表的名字)

也就是,假如,我需要查询ES_PYMT表名字的外键,那么 c.table_name='ES_PYMT'

--根据所有主外键关联的表
select (select a.table_name
          from user_constraints a
         where a.constraint_name = c.r_constraint_name) 主表表名,
       c.r_constraint_name 主表主键键约束名,
       c.table_name 从表表名,
       c.constraint_name 从表外键约束名,c.*
  from user_constraints c
 where c.constraint_type = 'R'  and c.table_name='ES_PYMT';

结果截图:

(2)查询表的约束

select * from user_cons_columns where table_name='ES_PYMT'(table_name表名字)

(3)查询表的字段信息


select * from user_tab_columns

查询指定表的名字

select * from user_tab_columns where  table_name='ES_PYMT'

猜你喜欢

转载自blog.csdn.net/qq_36411874/article/details/85051954