MYSQL 无法更新 parent table - 关系数据库设计、外键约束和级联更新/删除
MYSQL Unable to update parent table - Relational Database Design, foreign key constraints and cascading update / delete
我有一个用于管理电视节目的非常简单的数据库设计:
- Parenttable:
tv_shows
。主键:show_id
- Childtable:
seasons
。主键:season_id
,外键:show_id
- Childtable:
episodes
。主键:episode_id
,外键:season_id
Table 创作 SQL:
$sql = "DROP TABLE IF EXISTS ".$table_shows.";
CREATE TABLE $table_shows (
show_id int(11) NOT NULL,
name varchar(120) DEFAULT NULL,
PRIMARY KEY (show_id),
UNIQUE KEY show_id_UNIQUE (show_id)
) $charset_collate;
DROP TABLE IF EXISTS ".$table_seasons.";
CREATE TABLE $table_seasons (
season_id int(10) NOT NULL,
show_id int(11) DEFAULT NULL,
name varchar(120) DEFAULT NULL,
PRIMARY KEY (season_id),
KEY seasons_ibfk_1 (show_id),
CONSTRAINT seasons_ibfk_1 FOREIGN KEY (show_id) REFERENCES $table_shows (show_id) ON DELETE CASCADE ON UPDATE CASCADE
) $charset_collate;
DROP TABLE IF EXISTS ".$table_episodes.";
CREATE TABLE $table_episodes (
episode_id int(10) NOT NULL,
season_id int(10) DEFAULT NULL,
name varchar(120) DEFAULT NULL,
PRIMARY KEY (episode_id),
KEY season_id (season_id),
CONSTRAINT episodes_ibfk_1 FOREIGN KEY (season_id) REFERENCES $table_seasons (season_id)
) $charset_collate;";
我想要发生的是,如果从 tv_shows
table 中删除了一个节目,那么删除级联,因此 seasons
和 episodes
也被删除来自他们各自的 tables。
我可以很好地插入这些 table。但是,如果我尝试从 tv_shows table 中删除一行,则会收到 SQL 错误:
ERROR 1451: 1451: Cannot delete or update a parent row: a foreign key constraint fails
那么我的 FK 约束哪里出了问题?
您还需要将 ON DELETE CASCADE
添加到剧集中。这样可以确保它们都相互跟随进入垃圾箱。
CONSTRAINT episodes_ibfk_1
FOREIGN KEY (season_id)
REFERENCES $table_seasons (season_id)
ON DELETE CASCADE
我有一个用于管理电视节目的非常简单的数据库设计:
- Parenttable:
tv_shows
。主键:show_id
- Childtable:
seasons
。主键:season_id
,外键:show_id
- Childtable:
episodes
。主键:episode_id
,外键:season_id
Table 创作 SQL:
$sql = "DROP TABLE IF EXISTS ".$table_shows.";
CREATE TABLE $table_shows (
show_id int(11) NOT NULL,
name varchar(120) DEFAULT NULL,
PRIMARY KEY (show_id),
UNIQUE KEY show_id_UNIQUE (show_id)
) $charset_collate;
DROP TABLE IF EXISTS ".$table_seasons.";
CREATE TABLE $table_seasons (
season_id int(10) NOT NULL,
show_id int(11) DEFAULT NULL,
name varchar(120) DEFAULT NULL,
PRIMARY KEY (season_id),
KEY seasons_ibfk_1 (show_id),
CONSTRAINT seasons_ibfk_1 FOREIGN KEY (show_id) REFERENCES $table_shows (show_id) ON DELETE CASCADE ON UPDATE CASCADE
) $charset_collate;
DROP TABLE IF EXISTS ".$table_episodes.";
CREATE TABLE $table_episodes (
episode_id int(10) NOT NULL,
season_id int(10) DEFAULT NULL,
name varchar(120) DEFAULT NULL,
PRIMARY KEY (episode_id),
KEY season_id (season_id),
CONSTRAINT episodes_ibfk_1 FOREIGN KEY (season_id) REFERENCES $table_seasons (season_id)
) $charset_collate;";
我想要发生的是,如果从 tv_shows
table 中删除了一个节目,那么删除级联,因此 seasons
和 episodes
也被删除来自他们各自的 tables。
我可以很好地插入这些 table。但是,如果我尝试从 tv_shows table 中删除一行,则会收到 SQL 错误:
ERROR 1451: 1451: Cannot delete or update a parent row: a foreign key constraint fails
那么我的 FK 约束哪里出了问题?
您还需要将 ON DELETE CASCADE
添加到剧集中。这样可以确保它们都相互跟随进入垃圾箱。
CONSTRAINT episodes_ibfk_1
FOREIGN KEY (season_id)
REFERENCES $table_seasons (season_id)
ON DELETE CASCADE