Ales
Ales

Reputation: 180

Delete rows from two tables with same primary key

I have two tables:

Table1: orders

idOrder, 
Blockquote
idUser

Table2: ordersinfo

idOrder,
.......,
.......

idOrder is primary a key for two tables. I have to delete from this tables rows by idUser. I tried different ways, but nothing helped me.

My Questions: What query, I should use?

I have this exception

MySQLIntegrityConstraintViolationException

Upvotes: 0

Views: 980

Answers (1)

M.Ali
M.Ali

Reputation: 69564

You will need to issue two delete statement

-- Delete OrderInfo table 
DELETE FROM ordersinfo
WHERE EXISTS (SELECT 1 
              FROM orders
              WHERE orders.idOrder = ordersinfo.idOrder 
              AND IdUser = ???)

-- Delete Orders 
DELETE FROM Orders 
WHERE IdUser = ???

Upvotes: 1

Related Questions