【问题标题】:MariaDB "Foreign key constraint is incorrectly formed" errorMariaDB“外键约束格式不正确”错误
【发布时间】:2020-03-31 12:11:45
【问题描述】:

浏览其他类似问题没有帮助,我正在创建两个简单的表,然后弹出这个错误,不知道哪里出错了:

create table department (
    dept_name varchar(20),  
    building varchar(15),  
    budget numeric(12,2),  
    primary key (dept_name)
) COLLATE='utf8_general_ci' ENGINE=INNODB;

现在当我创建第二个表时:

create table course (
    course_id varchar(7), 
    title varchar(50), 
    dept_name varchar(20), 
    credits numeric(2,0), 
    primary key (course_id), 
    foreign key (dept_name) references department
) COLLATE='utf8_general_ci' ENGINE=INNODB;

它给了我错误。我在这里错过了什么?

【问题讨论】:

  • 欢迎来到 StackOverflow!请提及您遇到的错误,以便其他人可以提供更好的帮助!
  • 请使您的帖子正文独立,不要只在标题中包含任何内容。在考虑发布斩波代码直到没有错误之前,然后添加回——基本调试。而当你没有这样做并发布代码时,它会失败minimal reproducible example 中的“最小”。也请研究和反映它。特别是对于编译器错误,请参阅手册重新语法并尝试在许多 SO 帖子中的每一个的许多答案中的每一个案例 - 这将是重复或错字。

标签: mysql foreign-keys mariadb create-table


【解决方案1】:

外键语法为:

foreign key (dept_name) references department(dept_name)
                       --  column name here --^

【讨论】:

    猜你喜欢
    • 2019-11-02
    • 2019-09-15
    • 2017-05-29
    • 2019-03-08
    • 2019-08-08
    • 2020-02-19
    • 2014-11-20
    • 1970-01-01
    • 2020-11-12
    相关资源
    最近更新 更多