我试图创建一个包含几个具有相同FK约束的列的表。我的语法有什么问题?我收到了一个msg 102,错误的语法错误围绕" fk_fighterID"
" FIGHTERS"是一个用" fighterID"设置的表格。作为主键
CREATE TABLE FIGHTS
(
FIGHTID INT PRIMARY KEY,
CONSTRAINT fk_fighterID FOREIGN KEY REFERENCES FIGHTERS(fighterID),
FIGHTER_ID_CHALLANGER int fk_fighterID,
FIGHTER_ID_CHALLANGED int fk_fighterID,
CONSTRAINT fk_weightclass FOREIGN KEY (classID) REFERENCES WEIGHT_CLASSES
(classID)
WEIGHTCLASS NOT NULL fk_wightclass,
IS_MAIN BIT NOT NULL,
IS_CO_MAIN BIT NOT NULL,
SCHEDULED ROUNDS TINYINT(1),
CONSTRAINT fk_WINMETH FOREIGN KEY (WINID) REFERENCES WINNING_METHODS (WINID)
WINNING METHOD NOT NULL fk_WINMETH
)
;
答案 0 :(得分:1)
正确陈述
CONSTRAINT fk_weightclass FOREIGN KEY (classID) REFERENCES WEIGHT_CLASSES (classID)
声明不正确
CONSTRAINT fk_fighterID FOREIGN KEY REFERENCES FIGHTERS(fighterID),
您忘记提及'(classId)'等内容。
编辑: 我不确定您要实现的目标,但您的上述查询是错误的,如果有意义,请尝试以下查询。
CREATE TABLE FIGHTS
(
FIGHTID INT PRIMARY KEY,
FIGHTER_ID_CHALLANGER INT,
FIGHTER_ID_CHALLANGED INT,
IS_MAIN BIT NOT NULL,
IS_CO_MAIN BIT NOT NULL,
SCHEDULED_ROUNDS TINYINT(1),
WEIGHTCLASS INT NOT NULL,
CLASS_ID INT NOT NULL,
WINNING_METHOD INT NOT NULL,
CONSTRAINT fk_fighterID FOREIGN KEY (FIGHTER_ID_CHALLANGER) REFERENCES FIGHTERS (fighterID),
CONSTRAINT fk_weightclass FOREIGN KEY (CLASS_ID) REFERENCES WEIGHT_CLASSES (classID),
CONSTRAINT fk_WINMETH FOREIGN KEY (WINNING_METHOD) REFERENCES WINNING_METHODS (WINID)
);
答案 1 :(得分:0)
不应该将约束放在表创建语句之外,如下所示:
CREATE TABLE Sales.TempSalesReason (TempID int NOT NULL, Name nvarchar(50),
CONSTRAINT PK_TempSales PRIMARY KEY NONCLUSTERED (TempID),
CONSTRAINT FK_TempSales_SalesReason FOREIGN KEY (TempID)
REFERENCES Sales.SalesReason (SalesReasonID)
ON DELETE CASCADE
ON UPDATE CASCADE
);