GoGoAllen
GoGoAllen

Reputation: 117

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"]."'

Upvotes: 1

Views: 152

Answers (2)

Sayem
Sayem

Reputation: 104

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.

Upvotes: 0

PHP Geek
PHP Geek

Reputation: 4033

use inner join

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

Upvotes: 3

Related Questions