简体   繁体   English

从MySQL中的父表和子表中删除记录

[英]Delete records from parent table and child table in mysql

I want to delete record from table with foreign key. 我想用外键从表中删除记录。 Table structure is: 表结构为:

CREATE TABLE `employees` (
`employeeNumber` int(11) NOT NULL,
`lastName` varchar(50) NOT NULL,
`firstName` varchar(50) NOT NULL,
`extension` varchar(10) NOT NULL,
`email` varchar(100) NOT NULL,
`officeCode` varchar(10) NOT NULL,
`reportsTo` int(11) DEFAULT NULL,
`jobTitle` varchar(50) NOT NULL,
PRIMARY KEY (`employeeNumber`),
KEY `reportsTo` (`reportsTo`),
KEY `officeCode` (`officeCode`),
CONSTRAINT `employees_ibfk_1` FOREIGN KEY (`reportsTo`) REFERENCES   `employees` (`employeeNumber`),
CONSTRAINT `employees_ibfk_2` FOREIGN KEY (`officeCode`) REFERENCES `offices` (`officeCode`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1

Second table: 第二张表:

Create Table">CREATE TABLE `offices` (
`officeCode` varchar(10) NOT NULL,
`city` varchar(50) NOT NULL,
`phone` varchar(50) NOT NULL,
`addressLine1` varchar(50) NOT NULL,
`addressLine2` varchar(50) DEFAULT NULL,
`state` varchar(50) DEFAULT NULL,
`country` varchar(50) NOT NULL,
`postalCode` varchar(15) NOT NULL,
`territory` varchar(10) NOT NULL,
PRIMARY KEY (`officeCode`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1

I'm facing this error: 我遇到这个错误:

delete from offices where officeCode=7 Error Code: 1451. Cannot delete or update a parent row: a foreign key constraint fails (`classicmodels`.`employees`, CONSTRAINT `employees_ibfk_2` FOREIGN KEY (`officeCode`) REFERENCES `offices` (`officeCode`)) 0.093 sec 从officeCode = 7的办公室删除,错误代码:1451。无法删除或更新父行:外键约束失败(`classicmodels`.employees`,CONSTRAINT`employees_ibfk_2` FOREIGN KEY(`officeCode`)参考`offices( `officeCode`))0.093秒

You have one option to delete child table entry first then after delete to master table. 您可以选择先删除子表条目,然后再删除到主表。 like this 像这样

delete from employees where officeCode=7
delete from offices where officeCode=7

Since employees.officeCode is declared as a foreign key to offices.officeCode , all office codes that are used in the employees table have to exist in the offices table. 由于将employees.officeCode声明为offices.officeCode的外键,因此employees表中使用的所有office代码都必须存在于offices表中。 You can't delete an office if there are any employees in that office. 如果该办公室有任何员工,则无法删除该办公室。

You either have to delete those employees first, or you can tell MySQL to do this automatically by adding the ON DELETE CASCADE option to the FOREIGN KEY constraint. 您要么必须先删除这些雇员,要么可以通过将ON DELETE CASCADE FOREIGN KEY约束添加ON DELETE CASCADE选项来告诉MySQL自动执行此操作。

If you allow employees.officeCode to be NULL , you could also use ON DELETE SET NULL to leave the employees there, but set their officeCode to NULL . 如果允许employees.officeCodeNULL ,则还可以使用ON DELETE SET NULL来将员工留在此处,但将其officeCode设置为NULL

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

相关问题 使用父表从子表中删除不必要的记录 - Delete unnecessary records from child table by using parent table MySQL仅在子表中的记录与另一个子表中的记录匹配时才从父表中选择行 - MySQL select rows from parent table only if records in child table match records in another child table MySql 触发删除同表子记录 - MySql Triggers to delete child records in the same table Hibernate:如果子节点链接到父节点多对一,如何在删除父节点时从子表中删除Hibernate记录? - Hibernate: How to make Hibernate delete records from child table when deleting parent if child is linked to parent with many-to-one? mysql:使用FK从父表中删除与子表不匹配的行 - mysql : using FK to delete rows from parent table that dont match with child table 从MySQL中的表中删除特定的日期记录 - Delete specific date records from table in mysql 父表中的 Select 记录取决于子表中的条件 - Select records from parent table depending on a condition in child table 遍历父表中的记录,其中子表中的parentTableID和 - Loop through records from parent table where parentTableID in child table and 显示父表中子记录少于4个的记录 - Show Records from parent table that have child records less than 4 从MySQL中的同一表中选择父子 - selecting parent child from same table in mysql
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM