mysql 创建表的时候发生errno:150错误 这是表的内容
createtablecourse(courseIdchar(5)notnull,primarykey(courseId));createtablestudent(ssn...
create table course(
courseId char(5) not null,
primary key (courseId)
);
create table student(
ssn char(9) not null,
primary key (ssn)
);
create table Enroll(
ssn char(9) not null,
courseId char(5) not null,
primary key (ssn,courseId),
foreign key (ssn) references student,
foreign key (courseId) references course
); 展开
courseId char(5) not null,
primary key (courseId)
);
create table student(
ssn char(9) not null,
primary key (ssn)
);
create table Enroll(
ssn char(9) not null,
courseId char(5) not null,
primary key (ssn,courseId),
foreign key (ssn) references student,
foreign key (courseId) references course
); 展开
2个回答
展开全部
SQL Server 或者 Oracle 外键的时候,可以不指定 主表的列名称,例如:
SQL> -- 创建外键(默认选项)
SQL> ALTER TABLE test_sub ADD CONSTRAINT main_id_cons FOREIGN KEY (main_id) REFERENCES test_main;
Table altered.
注意,MySQL 外键,必须指定 主表的列名称
mysql> ALTER TABLE test_sub
-> ADD CONSTRAINT main_id_cons
-> FOREIGN KEY (main_id)
-> REFERENCES test_main(id);
-> //
Query OK, 2 rows affected (0.17 sec)
Records: 2 Duplicates: 0 Warnings: 0
SQL> -- 创建外键(默认选项)
SQL> ALTER TABLE test_sub ADD CONSTRAINT main_id_cons FOREIGN KEY (main_id) REFERENCES test_main;
Table altered.
注意,MySQL 外键,必须指定 主表的列名称
mysql> ALTER TABLE test_sub
-> ADD CONSTRAINT main_id_cons
-> FOREIGN KEY (main_id)
-> REFERENCES test_main(id);
-> //
Query OK, 2 rows affected (0.17 sec)
Records: 2 Duplicates: 0 Warnings: 0
展开全部
第三个表的建表语句不规范.我改了一下并测试可以了.
CREATE TABLE `enroll` (
`ssn` char(9) NOT NULL,
`courseId` char(5) NOT NULL,
PRIMARY KEY (`ssn`,`courseId`),
FOREIGN KEY (`ssn`) REFERENCES `student` (`ssn`),
FOREIGN KEY (`courseId`) REFERENCES `course` (`courseId`)
)
CREATE TABLE `enroll` (
`ssn` char(9) NOT NULL,
`courseId` char(5) NOT NULL,
PRIMARY KEY (`ssn`,`courseId`),
FOREIGN KEY (`ssn`) REFERENCES `student` (`ssn`),
FOREIGN KEY (`courseId`) REFERENCES `course` (`courseId`)
)
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询