我有一个主键标识表:如何查找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???
...
任何帮助是非常赞赏。
从SQL Server ** 2005 **或更新版本开始,建议使用'sys.'模式目录视图 - 不能旧的'sysobjects'和'syscolumns'等(这些将被逐步淘汰) –