简体   繁体   中英

How to delete foreign key as well as primary key

I want to use SQL and PHP to delete a specific row, events, and the foreign key eventlocation along with it. I tried to use an inner join function, but this gave me an error. Any ideas?

DELETE FROM Events, EventLocation JOIN ON EventLocation.EventLocationID = Events.EventLocationID WHERE EventID = '".$_POST["id"]."' AND EventLocationID = '".$_POST["id"]."'

使用内连接

DELETE Events, EventLocation FROM Events INNER JOIN EventLocation WHERE Events.EventID=EventLocation.EventID

Well bellow query works fine.

DELETE Events, EventLocation FROM Events INNER JOIN EventLocation WHERE Events.EventID=EventLocation.EventID

But in this case you handle easily if the table EventLocation setting Cascade On Delete with EventID whwres EventID is a foreign key of table EventLocation and primary key of table Events . Then you just delete row of Events . So the row of EventLocation delete autometically which along with Events .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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