回答

收藏

MySQL拒绝使用ON DELETE SET NULL而不是ON DELETE CASCADE的列

技术问答 技术问答 150 人阅读 | 0 人回复 | 2023-09-12

我有一个简单的MySQL在某些表列中,数据库中有一些外键。一些外键列被设置为ON DELETE SET! r8 N# }: B9 t- N9 Z- W
NULL,而且看起来工作正常。然而,我无法配置其中一张表。ON DELETE SET NULL-我只能配置ON DELETE CASCADE。- M, G- A5 o7 Y3 r& K! c
初始表如下:" k3 Z/ b' u4 X
CREATE TABLE sessions(session_id int NOT NULL AUTO_INCREMENT PRIMARY KEY,name varchar(50) NOT NULL,UNIQUE KEY uk_sessions(name))COLLATE utf8_unicode_ci;CREATE TABLE blocks(block_id int NOT NULL AUTO_INCREMENT PRIMARY KEY,name varchar(50) NOT NULL,UNIQUE KEY uk_blocks(name))COLLATE utf8_unicode_ci;CREATE TABLE edot(edah_id int NOT NULL AUTO_INCREMENT PRIMARY KEY,name varchar(50) NOT NULL,UNIQUE KEY uk_edot(name))COLLATE utf8_unicode_ci;CREATE TABLE campers(camper_id int NOT NULL AUTO_INCREMENT PRIMARY KEY,edah_id int,FOREIGN KEY fk_edah_id(edah_id) REFERENCES edot(edah_id)ON DELETE SET NULLON UPDATE CASCADE,session_id int,FOREIGN KEY fk_session_id(session_id) REFERENCES sessions(session_id)ON DELETE SET NULLON UPDATE CASCADE,first varchar(50) NOT NULL,last varchar(50) NOT NULL,email varchar(50) NOT NULL,needs_first_choice bool DEFAULT 0,active bool NOT NULL DEFAULT 1)COLLATE utf8_unicode_ci;所有这些工作都很正常。当我试图这样做时,问题来了:/ R1 @: z, Y  Q* f$ L. R* e
CREATE TABLE block_instances(block_id int NOT NULL,FOREIGN KEY fk_block_id(block_id) REFERENCES blocks(block_id)ON DELETE CASCADEON UPDATE CASCADE,session_id int,FOREIGN KEY fk_session_id(session_id) REFERENCES sessions(session_id)ON DELETE SET NULLON UPDATE CASCADE,PRIMARY KEY pk_block_instances(block_id,session_id))COLLATE utf8_unicode_ci;这返回' B. Z& U6 t4 D6 x
ERROR 1005 (HY000): Can't create table 'chugbot_db.block_instances' (errno: 150)假如第二个ON DELETE SET NULL更改为ON DELETE CASCADE,错误消失:
) C9 v* [2 J$ X) z; I3 f1 ]9 e: hCREATE TABLE block_instances(block_id int NOT NULL,FOREIGN KEY fk_block_id(block_id) REFERENCES blocks(block_id)ON DELETE CASCADEON UPDATE CASCADE,session_id int,FOREIGN KEY fk_session_id(session_id) REFERENCES sessions(session_id)ON DELETE **CASCADE**ON UPDATE CASCADE,PRIMARY KEY pk_block_instances(block_id,session_id))COLLATE utf8_unicode_ci;工作正常。
* Y1 ~2 T  s6 p# m+ y, b谁能告诉我在这里block_instances表中使用ON DELETE SET NULL有什么问题吗?campers表中的ON DELETE
$ a0 l9 M1 M* }) VSET NULL完全相同。: u# ]/ [2 g$ c9 J9 O
                                                                ; M3 A. C+ Y' _( _! v
    解决方案:
分享到:
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则