简体   繁体   English

mysql ON DELETE CASCADE

[英]mysql ON DELETE CASCADE

network table网络表

"CREATE TABLE network("
 . "n_id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,"
 . "nname VARCHAR(50) NOT NULL,"
 . "ndate DATE NOT NULL"
 . ");";

locality table位置表

"CREATE TABLE locality("
 ."l_id int NOT NULL AUTO_INCREMENT PRIMARY KEY,"
 ."lname varchar(50) NOT NULL,"
 ."ldate varchar(50) NOT NULL,"
 ."n_id int NOT NULL,"
 . "CONSTRAINT locality_fk_1 FOREIGN KEY (n_id) REFERENCES network(n_id)ON DELETE CASCADE ON UPDATE CASCADE"
 . ");"; 

Added from comments :从评论中添加

It gives me an following error:它给了我以下错误:

Network not deletedCannot delete or update a parent row: a foreign key constraint fails (accelore.locality, CONSTRAINT locality_ibfk_1 FOREIGN KEY (n_id) REFERENCES network (n_id))网络未删除不能删除或更新父行:外键约束失败(accelore.locality, CONSTRAINT locality_ibfk_1 FOREIGN KEY (n_id) REFERENCES network (n_id))

You have added a foreign key constraint so it'll not get deleted directly,you have to drop the constraint first.您添加了一个外键约束,因此它不会被直接删除,您必须先删除该约束。
alter table locality drop constraint locality_ibfk_1 will drop the constraint and you can delete network OR alter table locality drop constraint locality_ibfk_1将删除约束,您可以删除网络或
If you just want to delete data from both the table, use:如果您只想从两个表中删除数据,请使用:

delete from network where n_id = n_id; 

will delete all data in both the table without dropping any of the table.将删除两个表中的所有数据而不删除任何表。 Or或者
If you use specific n_id it'll delete that particular data in both the tables.如果您使用特定的n_id它将delete两个表中的特定数据。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM