2015-07-02 2 views
0

Ошибка SQL-SQL в SQL 2008 при запуске этого кода.Ошибка SQL Server - неверный синтаксис для определения ограничения TABLE

Этот вопрос был задан несколько раз в прошлом в переполнении стека, и обычно это делается с запятой, находящейся в неправильном месте, но для жизни меня я не вижу, что не так с следующий 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

Попробуйте следующее,

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

+0

Я хотел назвать ограничение по умолчанию, что это ответ не будет – jazza1000

+0

См. обновленный ответ –

3

Вы не «присоединить» последнее ограничение для любого столбца.
Чтобы сделать это с помощью DEFAULT ограничения, вы должны использовать его на самом определении столбца (см полный синтаксис here)

+0

Я думаю, что это правильно. Мне нужно иметь определение CONSTRAINT с определением столбца – jazza1000

0

Try ниже код ..

create TABLE IntroducerSupportLink5 
( 
IntroducerSupportLinkId int identity, 
IntroducerId nvarchar(12) not null, 
SupportStaffId nvarchar(12) not null, 
Status numeric(5), 
SupportStaffType numeric(5), 
CreatedDateTime datetime not null constraint DF_IntroducerSupportLink_CreatedDateTime 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) 
) 
Смежные вопросы