[英]How to delete rows of dependent tables
I am new in database and in my application i want when i delete any project then deleted all tasks related to this project.Following is my table structure.I want to use this query in php. 我是数据库和应用程序的新手,我想删除任何项目然后删除与该项目相关的所有任务。以下是我的表结构。我想在php中使用此查询。 please help.Thanks in Advanced.
请提供帮助。感谢使用Advanced。
Table:- Project
-Project_ID (primary)
-ProjectName
Table:- Task
-Task_ID ( primary)
-project_ID
-TaskName
You can design this in database itself ... It will delete in automatically 您可以在数据库本身中进行设计...它将自动删除
ALTER TABLE `Task` ADD FOREIGN KEY ( `project_ID` ) REFERENCES `Project`.`project_ID` (
`project_ID`) ON DELETE CASCADE ON UPDATE CASCADE ;
first delete the records from table Task
首先从表
Task
删除记录
then you can delete the records of table Project
然后您可以删除表
Project
的记录
example 例
suppose you want to delete the record from table Project
which have Project_Id=3
假定您要从表
Project
删除具有Project_Id=3
do this in two steps 分两步完成
delete records from table Task
which has foreign key of Porject_ID 从表
Task
删除具有Porject_ID外键的记录
DELETE FROM Task
WHERE Project_Id=3 从
Task
WHERE Project_Id = 3删除
then you can delete the record from table Project
然后您可以从表
Project
删除记录
DELETE FROM Project
WHERE Project_Id=3 从
Project
WHERE Project_Id = 3中删除
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.