简体   繁体   English

如何删除相关表的行

[英]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

referential integrity. 参照完整性。 check this web 检查这个网站

This is the concept you are looking for "ON DELETE CASCADE". 这就是您要寻找的“ ON DELETE CASCADE”的概念。

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 分两步完成

  1. 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删除

  2. 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.

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