【问题标题】:ERROR 1005 (HY000): Can't create table 'test.sports' (errno: 150)错误 1005 (HY000): 无法创建表 'test.sports' (errno: 150)
【发布时间】:2017-04-09 09:10:06
【问题描述】:

抛出错误:

错误 1005 (HY000):无法创建表“test.sports”(错误号:150)。

我需要帮助来解决它。

 CREATE TABLE sports(
-> interest text,
-> prize_money int,
-> sp_id int NOT NULL,
-> CONSTRAINT fk_sports 
-> FOREIGN KEY(sp_id)
-> REFERENCES records(id)
-> ON DELETE CASCADE
-> ON UPDATE CASCADE
-> ) ENGINE=INNODB;

【问题讨论】:

  • 只是另一个机器人..
  • 可以发一下records表的结构吗?
  • +------------+-------------+------+-----+- --------+--------+ |领域 |类型 |空 |钥匙 |默认 |额外 | +------------+-------------+------+-----+--------- +--------+ |学生ID |整数(11) |否 |优先级 | 0 | | |主题 |正文 |是 | |空 | | |标记|整数(11) |是 | |空 | | |老师 | varchar(20) |是 | |空 | | |性别 |小文本 |是 | |空 | | |年龄 |整数(11) |是 | |空 | | +------------+-------------+------+-----+--------- +
  • @TimBiegeleisen:看不懂……评论区能不能上传个图??
  • records 表没有 idcolumn。尝试改用REFERENCES records(student_id)

标签: mysql foreign-keys


【解决方案1】:

您的records 表没有名为id 的列。相反,它的主键列是student_id,因此您可能打算从sports 的外键中引用它。试试这个定义:

CREATE TABLE sports(
    interest text,
    prize_money int,
    sp_id int NOT NULL,
    CONSTRAINT fk_sports 
    FOREIGN KEY(sp_id)
    REFERENCES records(student_id)
    ON DELETE CASCADE
    ON UPDATE CASCADE
) ENGINE=INNODB;

【讨论】:

    猜你喜欢
    • 2016-10-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-08-07
    • 1970-01-01
    • 2011-02-17
    • 2019-02-26
    相关资源
    最近更新 更多