您的位置:首页技术文章
文章详情页

MySQL FOREIGN KEY 约束报错

【字号: 日期:2022-06-13 11:46:35浏览:23作者:猪猪

问题描述

Create table Student( Sno varchar(7) primary key, Sname varchar(10) not null, Ssex char (2) check(Ssex=‘男’or Ssex=’女’), Sage int check(Sage between 15 and 45), Sdept varchar(20) default ‘计算机系’);

Create table course( Cno varchar(10) primary key, Cname varchar(20) not null, Ccredit int check(Sctedit>0), Semester int check(Semester>0), Period int check(Period>0));

Create table SC( Sno varchar(7) foreign key references student(Sno), Cno varchar(10) foreign key references course(Cno), Grade int check(Grade between 0 and 100), Primary key (Sno,Cno));

前两个可以成功的建表,但是第三个建表报错:

MySQL FOREIGN KEY 约束报错

更改第三个:

Create table SC( Sno varchar(7), Cno varchar(10), Grade int check(Grade between 0 and 100), foreign key (Sno) references student(Sno), foreign key (Cno) references course(Cno), Primary key (Sno,Cno),);

还是报错:

MySQL FOREIGN KEY 约束报错

求解答,谢谢大佬!

问题解答

回答1:

创建外键的写法错误吧,MySQL FOREIGN KEY 约束报错

相关文章: