SQL phpMyAdmin-找不到外键约束

时间:2018-02-15 04:59:12

标签: sql phpmyadmin

在phpMyAdmin上创建表艺术家和表专辑后,当我尝试创建表Track时,我不断收到错误 - #1215 - 无法添加外键约束。它可以来自NULL吗?

CREATE TABLE Artist (
artist_id INTEGER NOT NULL AUTO_INCREMENT,
name VARCHAR(255),
PRIMARY KEY(artist_id)
)

CREATE TABLE Album (
album_id INTEGER NOT NULL AUTO_INCREMENT,
title VARCHAR(255),
artist_id INTEGER,

PRIMARY KEY(album_id),
INDEX USING BTREE (title),

CONSTRAINT FOREIGN KEY (artist_id)
REFERENCES Artist (artist_id)
ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE = InnoDB;



CREATE TABLE Track (  <--ERROR STARTS AFTER I TRY TO CREATE TRACK.
track_id INTEGER NOT NULL AUTO_INCREMENT,
title VARCHAR(255),
len INTEGER,
rating INTEGER,
count INTEGER,
album_id INTEGER,
genre_id INTEGER,
PRIMARY KEY(track_id),
INDEX USING BTREE (title),

CONSTRAINT FOREIGN KEY (album_id) REFERENCES Album (album_id)
  ON DELETE CASCADE ON UPDATE CASCADE,
CONSTRAINT FOREIGN KEY (genre_id) REFERENCES Album (genre_id)
  ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE = InnoDB;

1 个答案:

答案 0 :(得分:0)

您在使用以下声明时遇到问题

CONSTRAINT FOREIGN KEY (genre_id) REFERENCES Album (genre_id)
   ON DELETE CASCADE ON UPDATE CASCADE

genre_id不是Album表的列。

  

此外,仅使用另一个表的主键创建外键...