我在一个名为“pk_tcustomersessions”的表“tcustomersessions”中设置了主键约束。
下面是我的问题-

SELECT *
FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS
WHERE TABLE_NAME = 'tCustomerSessions'
    AND Constraint_Type = 'PRIMARY KEY'

在这里,如何获取设置了主键约束的列的名称?

最佳答案

SELECT
    tc.CONSTRAINT_NAME
  , ccu.COLUMN_NAME
FROM
    INFORMATION_SCHEMA.TABLE_CONSTRAINTS AS tc
    JOIN INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE AS ccu ON ccu.CONSTRAINT_NAME = tc.CONSTRAINT_NAME
WHERE
    tc.TABLE_NAME = 'tCustomerSessions'
    AND tc.CONSTRAINT_TYPE = 'PRIMARY KEY'

关于sql-server - 在SQL中使用INFORMATION_SCHEMA.TABLE_CONSTRAINTS查找约束列名称,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/39536134/

10-10 13:09