1

I have 2 tables, Customer and Order.

I wish to delete the rows in which that particular Customer appears in. I have tried this,

DELETE FROM Customer
WHERE Email ='2ManySnakes@gmail.com' -- I was asked to associate the customer with an email. 
UNION
DELETE FROM `Order`
WHERE O_CustomerID = 2;

Not sure if this will work. But I just want to know if the syntax is correct.

LovesPie
  • 117
  • 10

3 Answers3

2

union used for joining 2 queries vertically, so it is no sense here in your query, you run 2 queries like this:

DELETE FROM Customer WHERE Email ='2ManySnakes@gmail.com';
DELETE FROM `Order` WHERE O_CustomerID = 2;
Gouda Elalfy
  • 6,888
  • 1
  • 26
  • 38
1

You just use two SQL statements. I presume these would be:

DELETE c FROM Customer c
WHERE CustomerId = 2;

DELETE o FROM `Order` o
WHERE O_CustomerID = 2;

There is no need to use the email for one table and the customer id for the other -- use the same identifier for both.

Note that you can also use cascading foreign key constraints. That would make it possible to delete the customer record from the customer table, and all related tables for that customer would also be deleted.

Gordon Linoff
  • 1,242,037
  • 58
  • 646
  • 786
  • As always, very good answer of yours! I always feel confident when I see your name :-) Given the structure of the OP's tables shown [here](http://stackoverflow.com/q/35970170/5830574), the constraints are `on delete restrict`. – PerlDuck Mar 13 '16 at 14:34
  • It's a school assignment I was told to use the email as an identifier to delete the row. – LovesPie Mar 13 '16 at 14:34
  • @LovesPie . . . You can use the email, it just looks really awkward. – Gordon Linoff Mar 14 '16 at 01:45
0

Is there a foreign key between the Customer and Order table?

If yes, you can delete from Order where (select from customer where customer_id = 2); Commit;

Then delete from customer where customer_id = 2; Commit;