查看oracle关于主/外键约束的元数据

发布于 2024-09-08 14:47:22 字数 147 浏览 4 评论 0原文

哪个表包含有关约束的详细信息(例如外键引用的表)?表 'all_cons_columns' 、 'all_constraints' 仅包含约束的名称,这不是很有帮助。 我目前正在使用 dbms_metadata.get_ddl() 但它不适用于所有数据库。

谢谢。

Which table contains detailed information(For example the table the foreign key is referring to) about the constraints? The tables 'all_cons_columns' , 'all_constraints' contains only the name of the constraints which isn't very helpful.
I am currently using dbms_metadata.get_ddl() but it doesn't work on all the databases.

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

百善笑为先 2024-09-15 14:47:22

一切都在其中:ALL_CONSTRAINTS 中的 R_CONSTRAINT_NAME 列包含外键引用的 PK/UK 约束的名称。然后,您可以查找该约束以获取引用表的 TABLE_NAME。

查看 ALL_CONS_COLUMNS 时,外键中列的位置将与主/唯一键中列的位置匹配。

It is all in there: the column R_CONSTRAINT_NAME in ALL_CONSTRAINTS contains the name of the referenced PK/UK constraint for a foreign key. You can then look up that constraint to get the TABLE_NAME of the reference table.

When looking at ALL_CONS_COLUMNS, the POSITION of the column in the foreign key will match the POSITION of the column in the primary/unique key.

北恋 2024-09-15 14:47:22

此语句列出表、约束名称和外键表名称:

select c.table_name,c.constraint_name,  --c.r_constraint_name, 
  cc.table_name
from all_constraints c
inner join all_constraints cc on c.r_constraint_name = cc.constraint_name

This statement lists tables, constraint names, and foreign key table names:

select c.table_name,c.constraint_name,  --c.r_constraint_name, 
  cc.table_name
from all_constraints c
inner join all_constraints cc on c.r_constraint_name = cc.constraint_name
八巷 2024-09-15 14:47:22

为了检索外键并生成脚本来创建它们,您可以使用以下查询:

SELECT 
   'ALTER TABLE ' || a.table_name || ' ADD CONSTRAINT ' || a.constraint_name 
   || ' FOREIGN KEY (' || a.column_name || ') REFERENCES ' || jcol.table_name 
   || ' (' || jcol.column_name || ');' as commandforeign
FROM
   (SELECT 
       uc.table_name, uc.constraint_name, uc.r_constraint_name, col.column_name
    FROM 
       USER_CONSTRAINTS uc, USER_CONS_COLUMNS col
    WHERE 
       uc.constraint_type='R' and uc.constraint_name=col.constraint_name) a
 INNER JOIN 
    USER_CONS_COLUMNS jcol
 ON 
    a.r_constraint_name=jcol.constraint_name;

In order to retrieve the foreign key and generate a script to create these, you can use the following query:

SELECT 
   'ALTER TABLE ' || a.table_name || ' ADD CONSTRAINT ' || a.constraint_name 
   || ' FOREIGN KEY (' || a.column_name || ') REFERENCES ' || jcol.table_name 
   || ' (' || jcol.column_name || ');' as commandforeign
FROM
   (SELECT 
       uc.table_name, uc.constraint_name, uc.r_constraint_name, col.column_name
    FROM 
       USER_CONSTRAINTS uc, USER_CONS_COLUMNS col
    WHERE 
       uc.constraint_type='R' and uc.constraint_name=col.constraint_name) a
 INNER JOIN 
    USER_CONS_COLUMNS jcol
 ON 
    a.r_constraint_name=jcol.constraint_name;
深居我梦 2024-09-15 14:47:22

看一下:对数据模型进行逆向工程。基于此,我做了 将 Oracle 数据库架构转储为文本的 Python 程序。有 PRIMARY_KEYS_INFO_SQLFOREIGN_KEYS_INFO_SQL 可以执行您感兴趣的操作。

Have a look at: Reverse Engineering a Data Model. Based on this I did a Python program that dumps Oracle db schema to text. There is PRIMARY_KEYS_INFO_SQL and FOREIGN_KEYS_INFO_SQL that do what you are interested in.

娇妻 2024-09-15 14:47:22

使用 Oracle XE 示例数据库。我想识别架构/表上的主键和外键。 在

SELECT acc.column_name, ac.constraint_type, ac.status, fkc.table_name, fkc.column_name 
FROM all_cons_columns acc 
LEFT JOIN all_constraints ac ON ac.constraint_name = acc.constraint_name 
LEFT OUTER JOIN all_cons_columns fkc ON fkc.constraint_name = ac.r_constraint_name 
WHERE acc.owner = 'OT' 
AND acc.table_name = 'ORDERS' 
AND ac.constraint_type IN ('P', 'R');

输出(格式化列宽后)看起来像这样...

COLUMN_NAME          C STATUS   TABLE_NAME           COLUMN_NAME
-------------------- - -------- -------------------- --------------------
ORDER_ID             P ENABLED
CUSTOMER_ID          R ENABLED  CUSTOMERS            CUSTOMER_ID
SALESMAN_ID          R ENABLED  EMPLOYEES            EMPLOYEE_ID    

这里我们可以看到(对于 ORDERS 表)ORDER_ID 是主键,并且有两个引用 CUSTOMERS 的外键,并且分别是 EMPLOYEES 表。

Using the Oracle XE sample database. I wanted to identify the primary and foreign keys on a schema/table. Here's how...

SELECT acc.column_name, ac.constraint_type, ac.status, fkc.table_name, fkc.column_name 
FROM all_cons_columns acc 
LEFT JOIN all_constraints ac ON ac.constraint_name = acc.constraint_name 
LEFT OUTER JOIN all_cons_columns fkc ON fkc.constraint_name = ac.r_constraint_name 
WHERE acc.owner = 'OT' 
AND acc.table_name = 'ORDERS' 
AND ac.constraint_type IN ('P', 'R');

The output (after formatting the column widths) looks like this...

COLUMN_NAME          C STATUS   TABLE_NAME           COLUMN_NAME
-------------------- - -------- -------------------- --------------------
ORDER_ID             P ENABLED
CUSTOMER_ID          R ENABLED  CUSTOMERS            CUSTOMER_ID
SALESMAN_ID          R ENABLED  EMPLOYEES            EMPLOYEE_ID    

Here we can that see that (for the ORDERS table) ORDER_ID is the primary key, and that there are two foreign keys referencing the CUSTOMERS, and the EMPLOYEES tables respectively.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文