[英]update foreign key reference with default value on delete
I m currently making a table on primary foreign key relation.... 我目前正在制作有关主外键关系的表格。
book table
bookid bookname author
0 No book no author
1 Engg Maths Greiwal
2 Java Basics James Gosling
3 Mahabharata Ved Vyasa
4 Ramayana Valmiki
5 Harry Potter JK Rowling
usertable
userid name bookid
1 Arjun 2
2 Charles 2
3 Babbage 3
Here bookid is the foreign key in usertable and is primary key in booktable..... Now i've heard about cascading, setnull , restrict and no action constraints.... 这里bookid是usertable中的外键,并且是booktable中的主键.....现在,我听说过级联,setnull,strict和没有操作约束。
0 is set as the default value for column bookid in usertable..... 用户表中列bookid的默认值设置为0。
Now i want a situation when a bookid(primary) entry is deleted from the main table ie booktable, the entires containing bookid in the user table should be updated to the default value ( ie here 0 ).....
does there exist any method...??? 是否存在任何方法...?
all i can tell is that im working on phpmyadmin mysql innodb...... 我只能说的是,我正在phpmyadmin mysql innodb上工作……
There's no built-in way, however there is a workaround. 没有内置的方法,但是有一种解决方法。 You can use a DELETE trigger to accomplish this: 您可以使用DELETE触发器来完成此操作:
DELIMITER $$
CREATE TRIGGER `book_delete` BEFORE DELETE ON `booktable`
FOR EACH ROW BEGIN
UPDATE `usertable` SET `bookid` = 0 WHERE `bookid` = OLD.`bookid`;
END $$
DELIMITER ;
However, note that 0 is not null, so if you have any referential integrity constraints on usertalbe (that is, if you have bookid set as a FOREIGN KEY column), this will throw an error. 但是,请注意0不为null,因此,如果对usertalbe进行任何引用完整性约束(即,如果将bookid设置为FOREIGN KEY列),则将引发错误。
As a best practice, you really should use NULL to mean that the row in the usertable doesn't have a corresponding row in the booktable. 作为最佳实践,您实际上应该使用NULL来表示usertable中的行在booktable中没有对应的行。
With MySQL you can use ON DELETE SET NULL
on the foreign key. 使用MySQL,您可以在外键上使用ON DELETE SET NULL
。
For instance: 例如:
CREATE TABLE t1 (
id INT UNSIGNED PRIMARY KEY AUTO_INCREMENT,
title VARCHAR(256)
) ENGINE=InnoDB;
CREATE TABLE t2 (
id INT UNSIGNED PRIMARY KEY AUTO_INCREMENT,
parent_id INT UNSIGNED,
INDEX(parent_id),
FOREIGN KEY (parent_id) REFERENCES t1(id) ON DELETE SET NULL
) ENGINE=InnoDB;
insert into t2 values (null, 3);
insert into t2 values (null, 2);
This will result in table t2
looking like: 这将导致表t2
看起来像:
+----+-----------+
| id | parent_id |
+----+-----------+
| 2 | 2 |
| 1 | 3 |
+----+-----------+
Deleting a row from t1
as follows: 从t1
删除一行,如下所示:
delete from t1 where id = 2;
Will result in: 将导致:
+----+-----------+
| id | parent_id |
+----+-----------+
| 2 | NULL |
| 1 | 3 |
+----+-----------+
Interesting enough, MySQL will parse an ON DELETE SET DEFAULT
clause - but the table creation will fail. 有趣的是,MySQL将解析一个ON DELETE SET DEFAULT
子句-但表创建将失败。
So the short answer - you can set to NULL
on delete - but currently there's no way to set the default value for the column on delete AND maintain the foreign key constraint. 因此,简短的答案-您可以在delete上将其设置为NULL
但目前无法在delete上设置列的默认值并保持外键约束。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.