鉴于以下T-SQL语句,我如何(通过检查)确定主键?如果没有列出(就像我怀疑的那样)它会是什么样子?
CREATE TABLE [dbo].[BLDGINFO](
[LBLDGRUNNO] [int] NULL,
[LBLDGNO] [int] NOT NULL,
[FBIVOLUME] [float] NULL,
[FBIACOND] [float] NULL,
[NBIHTYPE] [float] NULL,
[NBILTYPE] [float] NULL,
[NBISTORIES] [float] NULL,
[NBIFTYPE] [float] NULL,
[NBIBEDS] [float] NULL,
[NBIUNITS] [float] NULL,
[SBIRATENO] [nvarchar](31) NULL,
[NBICTYPE] [float] NULL,
[NBIYEARBLT] [int] NULL DEFAULT ((0)),
[NBITHBNDRY] [int] NULL
) ON [PRIMARY]
答案 0 :(得分:2)
表中唯一可能作为主键使用的字段是LBLDGNO
,因为主键列不能为空。
答案 1 :(得分:1)
您问题中的表定义不包含主键,以下是将主键定义为id列的示例:
CREATE TABLE [dbo].[BLDGINFO](
id int NOT NULL,
[LBLDGRUNNO] [int] NULL,
[LBLDGNO] [int] NOT NULL,
[FBIVOLUME] [float] NULL,
[FBIACOND] [float] NULL,
[NBIHTYPE] [float] NULL,
[NBILTYPE] [float] NULL,
[NBISTORIES] [float] NULL,
[NBIFTYPE] [float] NULL,
[NBIBEDS] [float] NULL,
[NBIUNITS] [float] NULL,
[SBIRATENO] [nvarchar](31) NULL,
[NBICTYPE] [float] NULL,
[NBIYEARBLT] [int] NULL DEFAULT ((0)),
[NBITHBNDRY] [int] NULL,
PRIMARY KEY (id)
) ON [PRIMARY]
答案 2 :(得分:1)
如果已经定义了主键,则它将是LBLDGNO
。但是,很可能根本没有定义主键。您可以按如下方式确定:
SELECT c.name
FROM sys.indexes i, sys.index_columns ic, sys.objects o, sys.columns c
WHERE o.object_id = i.object_id
AND ic.index_id = i.index_id AND ic.object_id = i.object_id
AND c.object_id = o.object_id AND c.column_id = ic.column_id
AND i.is_primary_key = 1
AND o.name = 'BLDGINFO'
此查询将返回主键中包含的所有列。如果它返回一个空的结果集,则没有定义主键。