如何转换这个t-sql脚本?

时间:2009-05-15 05:05:28

标签: sql sql-server oracle

我不熟悉Oracle,我需要拥有与Oracle相同的脚本。

脚本:


SELECT  COL_NAME(parent_object_id, parent_column_id) AS ForigneKeyField,
        COL_NAME(referenced_object_id, referenced_column_id) AS PrimaryKeyField,
        OBJECT_NAME(referenced_object_id) AS PrimaryTable
FROM    sys.foreign_key_columns
WHERE   parent_object_id = OBJECT_ID(@name)

1 个答案:

答案 0 :(得分:3)

我不熟悉t-sql,但我熟悉Oracle,所以:

您似乎在查询数据字典中是否存在给定表的所有参照完整性约束,并且每个参数都报告外键字段,以及它们引用的表的相关字段和表。

在Oracle中,此信息保存在ALL_CONSTRAINTSALL_CONS_COLUMNS数据字典视图(或DBA_CONSTRAINTS / DBA_CONS_COLUMNS or USER_CONSTRAINTS / USER_CONS_COLUMNS中,具体取决于查询的范围)。我会运行这样的查询:

SELECT fc.constraint_name   AS ForeignConstraint
      ,fc.r_constraint_name AS ReferencedConstraint
      ,fcc.column_name      AS ForeignKeyField
      ,rcc.column_name      AS ReferencedKeyField
      ,rc.table_name        AS ReferencedTable
FROM   sys.all_constraints  fc
      ,sys.all_constraints  rc
      ,sys.all_cons_columns fcc
      ,sys.all_cons_columns rcc
WHERE  fc.table_name        = :name
AND    fc.constraint_type   = 'R'
AND    fc.r_constraint_name = rc.constraint_name
AND    fc.constraint_name   = fcc.constraint_name
AND    rc.constraint_name   = rcc.constraint_name
AND    fcc.position         = rcc.position
ORDER BY fc.constraint_name, fcc.position;

我在查询中添加了约束名称(ForeignConstraint和ReferencedConstraint),这在引用约束涉及连接键时很有用。