我如何知道 CONSTRAINT_NAME 是主键还是外键?

发布于 2024-07-14 04:00:11 字数 254 浏览 7 评论 0原文

在 SQL Server 2005 上使用此 SQL,

SELECT CONSTRAINT_NAME
FROM INFORMATION_SCHEMA.KEY_COLUMN_USAGE 
WHERE TABLE_NAME = @TableName
  AND COLUMN_NAME=@ColumnName

我得到主键和外键。

如何只获取外键? 如何查看约束是主键还是外键?

谢谢

Using this SQL on SQL Server 2005

SELECT CONSTRAINT_NAME
FROM INFORMATION_SCHEMA.KEY_COLUMN_USAGE 
WHERE TABLE_NAME = @TableName
  AND COLUMN_NAME=@ColumnName

I get the Primary Keys AND the Foreign Keys.

How can I get only Foreign Keys?
How can I see if a Constraint is a Primary or a Foreign Key?

Thanks

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

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

发布评论

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

评论(6

失去的东西太少 2024-07-21 04:00:11

找到了一个更优雅的解决方案 此处

为了完整性起见,在下面添加了代码,但所有功劳都归功于 Pinal Dave

SELECT f.name AS ForeignKey,
   OBJECT_NAME(f.parent_object_id) AS TableName,
   COL_NAME(fc.parent_object_id, 
   fc.parent_column_id) AS ColumnName,
   OBJECT_NAME (f.referenced_object_id) AS ReferenceTableName,
   COL_NAME(fc.referenced_object_id, 
   fc.referenced_column_id) AS ReferenceColumnName
FROM sys.foreign_keys AS f
LEFT JOIN sys.foreign_key_columns AS fc
   ON f.OBJECT_ID = fc.constraint_object_id

如果您只想要主键...

SELECT i.name AS IndexName,
        OBJECT_NAME(ic.OBJECT_ID) AS TableName,
        COL_NAME(ic.OBJECT_ID,ic.column_id) AS ColumnName
FROM sys.indexes AS i
        INNER JOIN sys.index_columns AS ic
        ON i.OBJECT_ID = ic.OBJECT_ID 
    AND i.index_id = ic.index_id
WHERE i.is_primary_key = 1

将此添加为附加答案,因为它与我的上一个答案相去甚远:)

Found a much more elegant solution here

Code added below for completeness but all credit goes to Pinal Dave

SELECT f.name AS ForeignKey,
   OBJECT_NAME(f.parent_object_id) AS TableName,
   COL_NAME(fc.parent_object_id, 
   fc.parent_column_id) AS ColumnName,
   OBJECT_NAME (f.referenced_object_id) AS ReferenceTableName,
   COL_NAME(fc.referenced_object_id, 
   fc.referenced_column_id) AS ReferenceColumnName
FROM sys.foreign_keys AS f
LEFT JOIN sys.foreign_key_columns AS fc
   ON f.OBJECT_ID = fc.constraint_object_id

And if you just want the primary keys...

SELECT i.name AS IndexName,
        OBJECT_NAME(ic.OBJECT_ID) AS TableName,
        COL_NAME(ic.OBJECT_ID,ic.column_id) AS ColumnName
FROM sys.indexes AS i
        INNER JOIN sys.index_columns AS ic
        ON i.OBJECT_ID = ic.OBJECT_ID 
    AND i.index_id = ic.index_id
WHERE i.is_primary_key = 1

Added this as an additional answer because it's so far removed from my previous one :)

故事和酒 2024-07-21 04:00:11

我在 SQL Server 2005 中使用以下 SQL 来按主键获取约束名称:

SELECT Constraint_Name 
FROM INFORMATION_SCHEMA.REFERENTIAL_CONSTRAINTS 
WHERE UNIQUE_CONSTRAINT_NAME = 
   (SELECT CONSTRAINT_NAME 
    FROM INFORMATION_SCHEMA.KEY_COLUMN_USAGE 
    WHERE TABLE_NAME = @TableName AND COLUMN_NAME = @ColumnName)

按外键获取约束名称:

SELECT CONSTRAINT_NAME 
FROM INFORMATION_SCHEMA.KEY_COLUMN_USAGE 
WHERE TABLE_NAME = @TableName AND COLUMN_NAME = @ColumnName

按约束名称获取外键的表和字段:

SELECT TABLE_NAME, COLUMN_NAME 
FROM INFORMATION_SCHEMA.KEY_COLUMN_USAGE 
WHERE CONSTRAINT_NAME = @ConstraintName

按约束名称获取主键的表和字段:

SELECT TABLE_NAME, COLUMN_NAME 
FROM INFORMATION_SCHEMA.KEY_COLUMN_USAGE 
WHERE CONSTRAINT_NAME = 
    (SELECT UNIQUE_CONSTRAINT_NAME 
     FROM INFORMATION_SCHEMA.REFERENTIAL_CONSTRAINTS 
     WHERE CONSTRAINT_NAME = @ConstraintName)

I used the following SQL with SQL Server 2005 to get the Constraint Names by Primary Key:

SELECT Constraint_Name 
FROM INFORMATION_SCHEMA.REFERENTIAL_CONSTRAINTS 
WHERE UNIQUE_CONSTRAINT_NAME = 
   (SELECT CONSTRAINT_NAME 
    FROM INFORMATION_SCHEMA.KEY_COLUMN_USAGE 
    WHERE TABLE_NAME = @TableName AND COLUMN_NAME = @ColumnName)

To get the Constraint Name by Foreing Key:

SELECT CONSTRAINT_NAME 
FROM INFORMATION_SCHEMA.KEY_COLUMN_USAGE 
WHERE TABLE_NAME = @TableName AND COLUMN_NAME = @ColumnName

To get the Foreign Key's Table and Field by Constraint Name:

SELECT TABLE_NAME, COLUMN_NAME 
FROM INFORMATION_SCHEMA.KEY_COLUMN_USAGE 
WHERE CONSTRAINT_NAME = @ConstraintName

To get the Primary Key's Table and Field by Constraint Name:

SELECT TABLE_NAME, COLUMN_NAME 
FROM INFORMATION_SCHEMA.KEY_COLUMN_USAGE 
WHERE CONSTRAINT_NAME = 
    (SELECT UNIQUE_CONSTRAINT_NAME 
     FROM INFORMATION_SCHEMA.REFERENTIAL_CONSTRAINTS 
     WHERE CONSTRAINT_NAME = @ConstraintName)
我家小可爱 2024-07-21 04:00:11

这取决于您的 DBMS,但约束应该在某处具有目标表和列。

如果是外键约束,则目标表将与当前表不同。 尝试

select *

查看

select constraint_name

您可以使用的所有神奇列。

It depends on your DBMS but a constraint should have the target table and column somewhere.

If it's a foreign key constraint, the target table will be different to the current table. Try

select *

instead of

select constraint_name

to see all the magical columns you can work with.

久伴你 2024-07-21 04:00:11
SELECT CONSTRAINT_NAME
FROM INFORMATION_SCHEMA.KEY_COLUMN_USAGE
WHERE TABLE_NAME = @TableName
  AND COLUMN_NAME=@ColumnName
  AND REFERENCED_COLUMN_NAME IS NOT NULL
SELECT CONSTRAINT_NAME
FROM INFORMATION_SCHEMA.KEY_COLUMN_USAGE
WHERE TABLE_NAME = @TableName
  AND COLUMN_NAME=@ColumnName
  AND REFERENCED_COLUMN_NAME IS NOT NULL
橘虞初梦 2024-07-21 04:00:11

这不是最优雅的解决方案,但 MS SQL Server 使用 PK & 键的 FK 前缀命名约定,以便您可以使用类似的内容...

SELECT CONSTRAINT_NAME 
FROM INFORMATION_SCHEMA.KEY_COLUMN_USAGE 
WHERE TABLE_NAME = @TableName
AND LEFT(CONSTRAINT_NAME,2) = 'FK'

如果那是您的 DBMS :)

编辑

当我尝试 Quassnoi 的答案时,我得到了“无效的列名称 'REFERENCED_COLUMN_NAME'”。 错误。

It's not the most elegant of solutions but MS SQL Server uses the PK & FK prefix naming convention for keys so you could use something like...

SELECT CONSTRAINT_NAME 
FROM INFORMATION_SCHEMA.KEY_COLUMN_USAGE 
WHERE TABLE_NAME = @TableName
AND LEFT(CONSTRAINT_NAME,2) = 'FK'

If that's your DBMS :)

EDIT

When I tried the answer by Quassnoi I got an "Invalid column name 'REFERENCED_COLUMN_NAME'." error.

怎言笑 2024-07-21 04:00:11

我用类似的东西。 它也适用于 SQL Server 2000。

select  object_name(fkx.constid) as fk_name,
        fkx.keyno as num,
        object_name(fkx.fkeyid) as child_table,
        col_name(fkx.fkeyid, fkx.fkey) as child_column,
        object_name(fkx.rkeyid) as parent_table,
        col_name(fkx.rkeyid, fkx.rkey) as parent_column
from sysforeignkeys fkx
order by fk_name, fkx.keyno

这一切都在系统表和视图中,并且有很好的记录。

I use something like that. It works in SQL Server 2000 as well.

select  object_name(fkx.constid) as fk_name,
        fkx.keyno as num,
        object_name(fkx.fkeyid) as child_table,
        col_name(fkx.fkeyid, fkx.fkey) as child_column,
        object_name(fkx.rkeyid) as parent_table,
        col_name(fkx.rkeyid, fkx.rkey) as parent_column
from sysforeignkeys fkx
order by fk_name, fkx.keyno

It's all in system tables and views which are pretty well documented.

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