SQL Server错误 - 用于定义' TABLE'的语法不正确约束

时间:2015-07-02 10:48:33

标签: sql-server tsql

运行此代码时SQL 2008上的T-SQL错误。

这种类型的问题过去曾经在Stack Overflow上被问过几次,并且通常与逗号位于错误的位置有关,但对于我的生活,我无法看到错误使用以下SQL

CREATE TABLE IntroducerSupportLink
( 
IntroducerSupportLinkId int identity, 
IntroducerId nvarchar(12) not null, 
SupportStaffId nvarchar(12) not null, 
Status numeric(5),
SupportStaffType numeric(5),
CreatedDateTime datetime not null,
CONSTRAINT PK_IntroducerSupportLink_IntroducerSupportLinkId Primary key(IntroducerSupportLinkId),
CONSTRAINT FK_IntroducerSupportLink_IntroducerId foreign key(IntroducerId)    REFERENCES Introducer(introducerId),
CONSTRAINT FK_IntroducerSupportLink_SupportStaffId foreign key(SupportStaffId) REFERENCES Introducer(introducerId),
CONSTRAINT DF_IntroducerSupportLink_CreatedDateTime DEFAULT (GETDATE())
)

如果最后一个(默认)约束被注释掉,它就会运行

3 个答案:

答案 0 :(得分:3)

尝试以下,

CREATE TABLE IntroducerSupportLink
( 
    IntroducerSupportLinkId int identity, 
    IntroducerId nvarchar(12) not null, 
    SupportStaffId nvarchar(12) not null, 
    Status numeric(5),
    SupportStaffType numeric(5),
    CreatedDateTime datetime not null DEFAULT GETDATE(),

    CONSTRAINT PK_IntroducerSupportLink_IntroducerSupportLinkId Primary key(IntroducerSupportLinkId),
    CONSTRAINT FK_IntroducerSupportLink_IntroducerId foreign key(IntroducerId)    REFERENCES Introducer(introducerId),
    CONSTRAINT FK_IntroducerSupportLink_SupportStaffId foreign key(SupportStaffId) REFERENCES Introducer(introducerId)
)

您也可以内联

CreatedDateTime datetime not null CONSTRAINT "constraints_CreatedDateTime" DEFAULT GETDATE(),

您无法按设计在表级定义DEFAULT约束,请参阅here

答案 1 :(得分:3)

你没有"附加"任何列的最后一个约束 要使用DEFAULT约束执行此操作,必须在列定义本身上使用它(请参阅完整语法here

答案 2 :(得分:0)

尝试以下代码..

END