我尝试在sql server 2008中创建数据库, 并得到了这个错误:
Msg 1785,Level 16,State 0,Line 34
在表'Viewers'上引入FOREIGN KEY约束'Viewers_FK1'可能会导致循环或多个级联路径。指定ON DELETE NO ACTION或ON UPDATE NO ACTION,或修改其他FOREIGN KEY 限制。 Msg 1750,Level 16,State 0,Line 34 无法创建约束。查看以前的错误。
这是我的SQL查询:
CREATE DATABASE [ShemenHD];
GO
USE [DBNAME1]
GO
CREATE TABLE Users(
UserName VARCHAR(50) PRIMARY KEY,
FullName VARCHAR(50) NOT NULL,
PhoneNum1 VARCHAR(20) ,
PhoneNum2 VARCHAR(20)
);
CREATE TABLE Category(
CategoryNum VARCHAR(15) PRIMARY KEY,
CategoryName VARCHAR(30) NOT NULL
);
CREATE TABLE Request(
RequestNum VARCHAR(30) PRIMARY KEY,
RequestCreator VARCHAR(50) NOT NULL,
RequestCategory VARCHAR(15),
RequestSubject VARCHAR(250) NOT NULL,
RequestContent VARCHAR(2000) ,
RequestOpenDate datetime NOT NULL,
RequestCloseDate datetime ,
RequestSLA datetime ,
RequestStatus VARCHAR(50) ,
RequestPriority VARCHAR(50) ,
IsProject BIT NOT NULL,
RequestFile VARCHAR(500) ,
constraint Request_FK foreign key (RequestCreator) references Users (UserName) ON DELETE NO ACTION ON UPDATE CASCADE,
constraint Request1_FK foreign key (RequestCategory) references Category (CategoryNum) ON DELETE NO ACTION ON UPDATE CASCADE
);
CREATE TABLE Viewers(
UserName VARCHAR(50) ,
RequestNum VARCHAR(30)
constraint Viewers_PK PRIMARY KEY (UserName, RequestNum),
constraint Viewers_FK foreign key (UserName) references Users(UserName) ON DELETE NO ACTION ON UPDATE CASCADE,
constraint Viewers_FK1 foreign key (RequestNum) references Request(RequestNum) ON DELETE NO ACTION ON UPDATE CASCADE
);
CREATE TABLE AdminNotes (
NoteNumber VARCHAR(10) ,
RequestNum VARCHAR(30) ,
NoteCreator VARCHAR(50),
NoteContent VARCHAR(2000) NOT NULL,
constraint AdminNotes_PK PRIMARY KEY (NoteNumber, RequestNum),
constraint AdminNotes_FK foreign key (RequestNum) references Request(RequestNum) ON DELETE NO ACTION ON UPDATE CASCADE ,
constraint AdminNotes_FK1 foreign key (NoteCreator) references Users(UserName) ON DELETE NO ACTION ON UPDATE CASCADE
);
CREATE TABLE RequestMessage (
MessageNumber VARCHAR(10) ,
RequestNum VARCHAR(30) ,
MessageCreator VARCHAR(50),
MessageContent VARCHAR(2000) NOT NULL,
constraint RequestMessage_PK PRIMARY KEY (MessageNumber, RequestNum),
constraint RequestMessage_FK foreign key (RequestNum) references Request(RequestNum) ON DELETE NO ACTION ON UPDATE CASCADE ,
constraint RequestMessage_FK1 foreign key (MessageCreator) references Users(UserName) ON DELETE NO ACTION ON UPDATE CASCADE
);
有什么问题?我想只在更新时级联,这会循环它的原因吗? 我该如何解决呢?
谢谢!!答案 0 :(得分:1)
你好这里是带注释的固定代码: 如上所述,您不能在多个表中使用相同的列作为外键并进行级联,您只能级联一次。
CREATE DATABASE [DBNAME1];
GO
USE [DBNAME1]
GO
CREATE TABLE Users(
UserName VARCHAR(50) PRIMARY KEY,
FullName VARCHAR(50) NOT NULL,
PhoneNum1 VARCHAR(20) ,
PhoneNum2 VARCHAR(20)
);
CREATE TABLE Category(
CategoryNum VARCHAR(15) PRIMARY KEY,
CategoryName VARCHAR(30) NOT NULL
);
CREATE TABLE Request(
RequestNum VARCHAR(30) PRIMARY KEY,
RequestCreator VARCHAR(50) NOT NULL,
RequestCategory VARCHAR(15),
RequestSubject VARCHAR(250) NOT NULL,
RequestContent VARCHAR(2000) ,
RequestOpenDate datetime NOT NULL,
RequestCloseDate datetime ,
RequestSLA datetime ,
RequestStatus VARCHAR(50) ,
RequestPriority VARCHAR(50) ,
IsProject BIT NOT NULL,
RequestFile VARCHAR(500) ,
constraint Request_FK foreign key (RequestCreator) references Users (UserName) ON DELETE NO ACTION ON UPDATE CASCADE,
constraint Request1_FK foreign key (RequestCategory) references Category (CategoryNum) ON DELETE NO ACTION ON UPDATE CASCADE
);
CREATE TABLE Viewers(
UserName VARCHAR(50) ,
RequestNum VARCHAR(30)
constraint Viewers_PK PRIMARY KEY (UserName, RequestNum),
--constraint Viewers_FK foreign key (UserName) references Users(UserName) ON DELETE NO ACTION ON UPDATE CASCADE, -- This cause error
constraint Viewers_FK foreign key (UserName) references Users(UserName) ON DELETE NO ACTION ON UPDATE NO ACTION,
constraint Viewers_FK1 foreign key (RequestNum) references Request(RequestNum) ON DELETE NO ACTION ON UPDATE CASCADE
);
CREATE TABLE AdminNotes (
NoteNumber VARCHAR(10) ,
RequestNum VARCHAR(30) ,
NoteCreator VARCHAR(50),
NoteContent VARCHAR(2000) NOT NULL,
constraint AdminNotes_PK PRIMARY KEY (NoteNumber, RequestNum),
--constraint AdminNotes_FK foreign key (RequestNum) references Request(RequestNum) ON DELETE NO ACTION ON UPDATE CASCADE , --This cause error
--constraint AdminNotes_FK1 foreign key (NoteCreator) references Users(UserName) ON DELETE NO ACTION ON UPDATE CASCADE --This cause error
constraint AdminNotes_FK foreign key (RequestNum) references Request(RequestNum) ON DELETE NO ACTION ON UPDATE NO ACTION ,
constraint AdminNotes_FK1 foreign key (NoteCreator) references Users(UserName) ON DELETE NO ACTION ON UPDATE NO ACTION
);
CREATE TABLE RequestMessage (
MessageNumber VARCHAR(10) ,
RequestNum VARCHAR(30) ,
MessageCreator VARCHAR(50),
MessageContent VARCHAR(2000) NOT NULL,
constraint RequestMessage_PK PRIMARY KEY (MessageNumber, RequestNum),
--constraint RequestMessage_FK foreign key (RequestNum) references Request(RequestNum) ON DELETE NO ACTION ON UPDATE CASCADE , --This cause error
--constraint RequestMessage_FK1 foreign key (MessageCreator) references Users(UserName) ON DELETE NO ACTION ON UPDATE CASCADE --This cause error
constraint RequestMessage_FK foreign key (RequestNum) references Request(RequestNum) ON DELETE NO ACTION ON UPDATE NO ACTION ,
constraint RequestMessage_FK1 foreign key (MessageCreator) references Users(UserName) ON DELETE NO ACTION ON UPDATE NO ACTION
);