0

给定以下 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]
4

3 回答 3

2

表中唯一可能用作主键的字段是LBLDGNO因为主键列不能为空。

于 2012-04-24T06:43:50.983 回答
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]
于 2012-04-24T05:54:34.263 回答
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'

此查询将返回主键中包含的所有列。如果它返回一个空结果集,则没有定义主键。

于 2012-04-24T10:25:07.307 回答