问题描述
我已阅读数据库系统概念,第 6 版,Silberschatz.我将在 MySQL 上的 OS X 上实现第 2 章中显示的大学数据库系统.但是我在创建表 course
时遇到了麻烦.表 department
看起来像
I have read Database system concepts, 6th edition, Silberschatz. I'm going to implement the university database system shown in chapter 2 on OS X on MySQL. But I have a trouble with creating the table course
. the table department
looks like
创建表course
导致以下错误.
Creating the table course
causes the following error.
在google上搜索外键约束后,我才知道外键约束"这个词表示表中外键列的数据course
必须存在于表中的主键列中部门
.但是我在插入数据时应该会遇到这个错误.
after searching google for foreign key constraint, I have just learned that the word 'foreign key constraint' indicates that data from foreign key column in the table course
must exist in primary key column in the table department
. But I should have met this error when inserting data.
如果不是,为什么作者让我执行那个 SQL 语句?
If not, why does author make me execute that SQL statement?
如果我真的执行了错误的SQL语句,是否需要在插入数据后指定课程表中的dept_name
作为外键?
If I really execute erroneous SQL statement, Does I have to designate dept_name
in course table as foreign key after inserting some data?
EDIT :在 mysql>
中输入 set foreign_key_checks=0
不能修复错误.
EDIT : typing set foreign_key_checks=0
into mysql>
does not fix the error.
推荐答案
CREATE TABLE
的FOREIGN KEY
的语法结构如下:
所以你的 MySQL DDL 应该是:
So your MySQL DDL should be:
另外,在department
表中dept_name
应该是VARCHAR(20)
更多信息可以在 MySQL 文档中找到
More information can be found in the MySQL documentation
这篇关于MySQL:错误 1215 (HY000):无法添加外键约束的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!