服务器: 消息 1767,级别 16,状态 1,行 1 外键 'FK__SC__Sno__47DBAE45' 引用了无效的表 'Student'。
useJW_SystemgoCREATETABLESC(SnoCHAR(10),CnoCHAR(10),GradeSMALLINTCHECK(Grade>=0ANDGra...
use JW_System
go
CREATE TABLE SC
(Sno CHAR(10),
Cno CHAR(10),
Grade SMALLINT CHECK(Grade>=0 AND Grade<=100),
PRIMARY KEY (Sno, Cno),
FOREIGN KEY (Sno) REFERENCES Student(Sno)
ON DELETE CASCADE /* 当删除Student表中的元组时,级连删除SC表中相应的元组*/
ON UPDATE CASCADE,/*当更新Student表中的Sno时,级连更新SC表中相应的元组*/
FOREIGN KEY (Cno) REFERENCES Course(Cno) /*表级完整性约束条件,Sno是外码*/
on delete no ACTION
ON UPDATE CASCADE /*当更新Course表中的Cno时,级连更新SC表中相应的元组*/
);
CREATE TABLE Teacher
(Tno CHAR(10) PRIMARY KEY,
Tname CHAR(20),
Tsex CHAR(2),
Tjob CHAR(10),
Tsdept CHAR(10),
Ttel BIGINT,
Dno CHAR(10),
FOREIGN KEY (Dno) REFERENCES Sdept(Dno)
);
CREATE TABLE Student
(Sno CHAR(10) PRIMARY KEY,
Sname CHAR(20) UNIQUE,
Ssex CHAR(2),
Sage SMALLINT,
Sdept CHAR(20),
Sbirth CHAR(10),
Class CHAR(10) REFERENCES Class(Cno) /*表级完整性约束条件,Class是外码*/
);
CREATE TABLE Sdept
(Dno CHAR(10) PRIMARY KEY,
Dname CHAR(20) UNIQUE,
Dtel BIGINT
);
CREATE TABLE Course
(Cno CHAR(10) PRIMARY KEY,
Cname CHAR(20) UNIQUE,
Ccredit SMALLINT,
Ctime SMALLINT,
Tno CHAR(10),
Cpeople BIGINT,
FOREIGN KEY (Tno) REFERENCES Teacher(Tno) /*表级完整性约束条件,Tno是外码*/
);
CREATE TABLE Class
(Cno CHAR(10) PRIMARY KEY,
Lcredit BIGINT,
Tno CHAR(10),
Dno CHAR(10),
FOREIGN KEY (Tno) REFERENCES Teacher(Tno), /*表级完整性约束条件,Tno是外码*/
FOREIGN KEY (Dno) REFERENCES Sdept(Dno)); /*表级完整性约束条件,Dno是外码*/ 展开
go
CREATE TABLE SC
(Sno CHAR(10),
Cno CHAR(10),
Grade SMALLINT CHECK(Grade>=0 AND Grade<=100),
PRIMARY KEY (Sno, Cno),
FOREIGN KEY (Sno) REFERENCES Student(Sno)
ON DELETE CASCADE /* 当删除Student表中的元组时,级连删除SC表中相应的元组*/
ON UPDATE CASCADE,/*当更新Student表中的Sno时,级连更新SC表中相应的元组*/
FOREIGN KEY (Cno) REFERENCES Course(Cno) /*表级完整性约束条件,Sno是外码*/
on delete no ACTION
ON UPDATE CASCADE /*当更新Course表中的Cno时,级连更新SC表中相应的元组*/
);
CREATE TABLE Teacher
(Tno CHAR(10) PRIMARY KEY,
Tname CHAR(20),
Tsex CHAR(2),
Tjob CHAR(10),
Tsdept CHAR(10),
Ttel BIGINT,
Dno CHAR(10),
FOREIGN KEY (Dno) REFERENCES Sdept(Dno)
);
CREATE TABLE Student
(Sno CHAR(10) PRIMARY KEY,
Sname CHAR(20) UNIQUE,
Ssex CHAR(2),
Sage SMALLINT,
Sdept CHAR(20),
Sbirth CHAR(10),
Class CHAR(10) REFERENCES Class(Cno) /*表级完整性约束条件,Class是外码*/
);
CREATE TABLE Sdept
(Dno CHAR(10) PRIMARY KEY,
Dname CHAR(20) UNIQUE,
Dtel BIGINT
);
CREATE TABLE Course
(Cno CHAR(10) PRIMARY KEY,
Cname CHAR(20) UNIQUE,
Ccredit SMALLINT,
Ctime SMALLINT,
Tno CHAR(10),
Cpeople BIGINT,
FOREIGN KEY (Tno) REFERENCES Teacher(Tno) /*表级完整性约束条件,Tno是外码*/
);
CREATE TABLE Class
(Cno CHAR(10) PRIMARY KEY,
Lcredit BIGINT,
Tno CHAR(10),
Dno CHAR(10),
FOREIGN KEY (Tno) REFERENCES Teacher(Tno), /*表级完整性约束条件,Tno是外码*/
FOREIGN KEY (Dno) REFERENCES Sdept(Dno)); /*表级完整性约束条件,Dno是外码*/ 展开
2个回答
展开全部
--楼主 你搞错顺序了 既然有那么多外键 那么肯定要先创建主键表
--帮你改了
use JW_System
go
CREATE TABLE Sdept
(Dno CHAR(10) PRIMARY KEY,
Dname CHAR(20) UNIQUE,
Dtel BIGINT
);
CREATE TABLE Teacher
(Tno CHAR(10) PRIMARY KEY,
Tname CHAR(20),
Tsex CHAR(2),
Tjob CHAR(10),
Tsdept CHAR(10),
Ttel BIGINT,
Dno CHAR(10),
FOREIGN KEY (Dno) REFERENCES Sdept(Dno)
);
CREATE TABLE Class
(Cno CHAR(10) PRIMARY KEY,
Lcredit BIGINT,
Tno CHAR(10),
Dno CHAR(10),
FOREIGN KEY (Tno) REFERENCES Teacher(Tno), /*表级完整性约束条件,Tno是外码*/
FOREIGN KEY (Dno) REFERENCES Sdept(Dno)); /*表级完整性约束条件,Dno是外码*/
CREATE TABLE Student
(Sno CHAR(10) PRIMARY KEY,
Sname CHAR(20) UNIQUE,
Ssex CHAR(2),
Sage SMALLINT,
Sdept CHAR(20),
Sbirth CHAR(10),
Class CHAR(10) REFERENCES Class(Cno) /*表级完整性约束条件,Class是外码*/
);
CREATE TABLE Course
(Cno CHAR(10) PRIMARY KEY,
Cname CHAR(20) UNIQUE,
Ccredit SMALLINT,
Ctime SMALLINT,
Tno CHAR(10),
Cpeople BIGINT,
FOREIGN KEY (Tno) REFERENCES Teacher(Tno) /*表级完整性约束条件,Tno是外码*/
);
CREATE TABLE SC
(Sno CHAR(10),
Cno CHAR(10),
Grade SMALLINT CHECK(Grade>=0 AND Grade<=100),
PRIMARY KEY (Sno, Cno),
FOREIGN KEY (Sno) REFERENCES Student(Sno)
ON DELETE CASCADE /* 当删除Student表中的元组时,级连删除SC表中相应的元组*/
ON UPDATE CASCADE,/*当更新Student表中的Sno时,级连更新SC表中相应的元组*/
FOREIGN KEY (Cno) REFERENCES Course(Cno) /*表级完整性约束条件,Sno是外码*/
on delete no ACTION
ON UPDATE CASCADE /*当更新Course表中的Cno时,级连更新SC表中相应的元组*/
);
--如果解决了楼主的问题 请采纳
--帮你改了
use JW_System
go
CREATE TABLE Sdept
(Dno CHAR(10) PRIMARY KEY,
Dname CHAR(20) UNIQUE,
Dtel BIGINT
);
CREATE TABLE Teacher
(Tno CHAR(10) PRIMARY KEY,
Tname CHAR(20),
Tsex CHAR(2),
Tjob CHAR(10),
Tsdept CHAR(10),
Ttel BIGINT,
Dno CHAR(10),
FOREIGN KEY (Dno) REFERENCES Sdept(Dno)
);
CREATE TABLE Class
(Cno CHAR(10) PRIMARY KEY,
Lcredit BIGINT,
Tno CHAR(10),
Dno CHAR(10),
FOREIGN KEY (Tno) REFERENCES Teacher(Tno), /*表级完整性约束条件,Tno是外码*/
FOREIGN KEY (Dno) REFERENCES Sdept(Dno)); /*表级完整性约束条件,Dno是外码*/
CREATE TABLE Student
(Sno CHAR(10) PRIMARY KEY,
Sname CHAR(20) UNIQUE,
Ssex CHAR(2),
Sage SMALLINT,
Sdept CHAR(20),
Sbirth CHAR(10),
Class CHAR(10) REFERENCES Class(Cno) /*表级完整性约束条件,Class是外码*/
);
CREATE TABLE Course
(Cno CHAR(10) PRIMARY KEY,
Cname CHAR(20) UNIQUE,
Ccredit SMALLINT,
Ctime SMALLINT,
Tno CHAR(10),
Cpeople BIGINT,
FOREIGN KEY (Tno) REFERENCES Teacher(Tno) /*表级完整性约束条件,Tno是外码*/
);
CREATE TABLE SC
(Sno CHAR(10),
Cno CHAR(10),
Grade SMALLINT CHECK(Grade>=0 AND Grade<=100),
PRIMARY KEY (Sno, Cno),
FOREIGN KEY (Sno) REFERENCES Student(Sno)
ON DELETE CASCADE /* 当删除Student表中的元组时,级连删除SC表中相应的元组*/
ON UPDATE CASCADE,/*当更新Student表中的Sno时,级连更新SC表中相应的元组*/
FOREIGN KEY (Cno) REFERENCES Course(Cno) /*表级完整性约束条件,Sno是外码*/
on delete no ACTION
ON UPDATE CASCADE /*当更新Course表中的Cno时,级连更新SC表中相应的元组*/
);
--如果解决了楼主的问题 请采纳
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询