2016-09-28 59 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