2016-09-28 58 views
1

我有一個主鍵標識表:如何查找SQL Server列是否是單個SQL語句中的主鍵?

SELECT 
    so.name AS TableName, 
    sc.colid AS Id, sc.name AS Name, 
    sc.xtype AS TypeId, sc.prec AS Precision, sc.scale AS Scale, 
    sc.iscomputed AS IsComputed, 
    sc.isnullable AS IsNullable, 
    sc.collation AS Collation 
FROM syscolumns sc 
JOIN sysobjects so ON sc.id = so.id 
WHERE so.xtype = 'U' AND so.name = 'Person' 

我也可以得到所有的主鍵:

IF NOT EXISTS (SELECT * FROM sysobjects WHERE name='Person' AND xtype='U') 
    CREATE TABLE Person (
    Id INT PRIMARY KEY NOT NULL, 
    Name NVARCHAR(50) NOT NULL, 
    Age INTEGER NOT NULL); 
GO 

爲此,我可以使用下面的查詢得到的所有列細節使用:

SELECT * 
FROM sysobjects so 
WHERE xtype = 'PK' 
AND so.parent_obj IN (SELECT id FROM sysobjects WHERE xtype = 'U' AND name = 'Person') 

但我無法弄清楚如何將它們全部結合起來,以便我可以有效地唱歌使用簡單的布爾或甚至,標誌指示列是否是主鍵。

喜歡的東西:

SELECT 
    so.name AS TableName, 
    sc.colid AS Id, sc.name AS Name, 
    sc.xtype AS TypeId, 
    sc.prec AS Precision, sc.scale AS Scale, 
    sc.iscomputed AS IsComputed, 
    sc.isnullable AS IsNullable, 
    sc.collation AS Collation, 
    ???isPrimaryKey??? 
    ... 

任何幫助是非常讚賞。

+0

從SQL Server ** 2005 **或更新版本開始,建議使用'sys.'模式目錄視圖 - 不能舊的'sysobjects'和'syscolumns'等(這些將被逐步淘汰) –

回答

1

你可以試試這個 - 它通過sys.key_constraints找到合適的索引,並使用sys.index_columns在索引中的列

SELECT 
    so.name AS TableName, 
    sc.colid AS Id, 
    sc.name AS Name, 
    sc.xtype AS TypeId, 
    sc.prec AS Precision, 
    sc.scale AS Scale, 
    sc.iscomputed AS IsComputed, 
    sc.isnullable AS IsNullable, 
    sc.collation AS Collation, 
    ( select count(*) from sys.key_constraints kc 
     inner join sys.indexes i on i.name = kc.name 
     inner join sys.index_columns ic on ic.object_id = kc.parent_object_id and ic.index_id = i.index_id 
     where kc.type = 'PK' and kc.parent_object_id = so.id and ic.column_id = sc.colid) as IsPrimaryKey 

FROM syscolumns sc 
JOIN sysobjects so ON sc.id = so.id 
WHERE so.xtype = 'U' 
AND so.name = 'Person' 

輸出是:

TableName Id Name TypeId Precision Scale IsComputed IsNullable Collation    IsPrimaryKey 
Person  1 Id   56  10   0  0    0 NULL     1 
Person  2 Name  231  50   NULL 0    0 Latin1_General_CI_AS 0 
Person  3 Age   56  10   0  0    0 NULL     0 
+0

從SQL Server ** 2005 **或更新版本開始,建議使用'sys.'模式目錄視圖 - 而不是舊的'sysobjects'和'syscolumns'等(這些將被逐步淘汰) –

0

試試這個

SELECT 
    sc.id, 
    so.name AS TableName, 
    sc.colid AS Id, 
    sc.name AS Name, 
    sc.xtype AS TypeId, 
    sc.prec AS Precision, 
    sc.scale AS Scale, 
    sc.iscomputed AS IsComputed, 
    sc.isnullable AS IsNullable, 
    sc.collation AS Collation, 
    cast (case when so1.xtype = 'pk' then 1 else 0 end as bit) as IsPrimaryColumn 
FROM syscolumns sc 
inner JOIN sysobjects so ON sc.id = so.id 
left join sysobjects so1 on sc.id = so1.parent_obj and sc.colid = so1.uid and so1.xType = 'PK' 
WHERE 
    so.xtype = 'U' AND --so1.xtype = 'PK' and 
    so.name = 'Person' 
+0

從SQL Server ** 2005 **或更新的版本開始,建議使用'sys.'模式目錄視圖 - 而不是舊的'sysobjects'和'syscolumns'等(這些將被逐步淘汰) –

+0

這會返回不正確的結果,請嘗試創建第二個表並將第二列指定爲主鍵。 – MaYaN

+0

你的意思是有複合主鍵的表嗎? – ash