MySQL: ON UPDATE SET DEFAULT 似乎不应该引发错误
MySQL: ON UPDATE SET DEFAULT raises an error when it seems like it shouldn't
我有这个代码:
CREATE TABLE CarDrivers (
driver_name VARCHAR (50) UNIQUE
);
INSERT INTO CarDrivers (driver_name) VALUES ("John"), ("Jarvis");
CREATE TABLE Cars (
driver_name VARCHAR (50) DEFAULT "John",
-- driver_names are connected
FOREIGN KEY (driver_name) REFERENCES CarDrivers (driver_name)
-- but we can always return to value "John", which is present in CarDrivers
ON UPDATE SET DEFAULT
);
INSERT INTO Cars SET driver_name = 'Jarvis';
-- this is where the error happens
UPDATE CarDrivers SET driver_name = 'Jarvease' WHERE driver_name = 'Jarvis';
代码导致错误:
Cannot delete or update a parent row. A foreign key constraint fails:
...
.Cars
, CONSTRAINT Cars_ibfk_1
FOREIGN KEY (driver_name
) REFERENCES CarDrivers
(driver_name
)
当我们告诉 Cars
table CarDrivers.driver_name
Cars.driver_name
的 ON UPDATE
应该设置为 [=22 时,为什么会出现此错误=],这是一个值 CarDrivers.driver_name
?
顺便说一句,约束 ... ON UPDATE SET NULL
... ON UPDATE CASCADE
正常工作并允许我们更新 CarDrivers.driver_name
!
感谢您的宝贵时间!
https://dev.mysql.com/doc/refman/8.0/en/create-table-foreign-keys.html 说:
SET DEFAULT: This action is recognized by the MySQL parser, but both InnoDB and NDB reject table definitions containing ON DELETE SET DEFAULT or ON UPDATE SET DEFAULT clauses.
换句话说,这个功能在MySQL的当前版本中不起作用。
我有这个代码:
CREATE TABLE CarDrivers (
driver_name VARCHAR (50) UNIQUE
);
INSERT INTO CarDrivers (driver_name) VALUES ("John"), ("Jarvis");
CREATE TABLE Cars (
driver_name VARCHAR (50) DEFAULT "John",
-- driver_names are connected
FOREIGN KEY (driver_name) REFERENCES CarDrivers (driver_name)
-- but we can always return to value "John", which is present in CarDrivers
ON UPDATE SET DEFAULT
);
INSERT INTO Cars SET driver_name = 'Jarvis';
-- this is where the error happens
UPDATE CarDrivers SET driver_name = 'Jarvease' WHERE driver_name = 'Jarvis';
代码导致错误:
Cannot delete or update a parent row. A foreign key constraint fails:
...
.Cars
, CONSTRAINTCars_ibfk_1
FOREIGN KEY (driver_name
) REFERENCESCarDrivers
(driver_name
)
当我们告诉 Cars
table CarDrivers.driver_name
Cars.driver_name
的 ON UPDATE
应该设置为 [=22 时,为什么会出现此错误=],这是一个值 CarDrivers.driver_name
?
顺便说一句,约束 ... ON UPDATE SET NULL
... ON UPDATE CASCADE
正常工作并允许我们更新 CarDrivers.driver_name
!
感谢您的宝贵时间!
https://dev.mysql.com/doc/refman/8.0/en/create-table-foreign-keys.html 说:
SET DEFAULT: This action is recognized by the MySQL parser, but both InnoDB and NDB reject table definitions containing ON DELETE SET DEFAULT or ON UPDATE SET DEFAULT clauses.
换句话说,这个功能在MySQL的当前版本中不起作用。