Your DELETE references the PORT table twice. To clarify, first modify the statement to include table aliases:
DELETE FROM PORT p1 WHERE EXISTS (SELECT * FROM port p2 LEFT JOIN device on p2.fkdevice = device.pkid where p2.fkjacket = :old.pkid and device.fkdevice_type = 1);
Note that the subquery is not correlated with p1 . In other words, the result of this subquery will be identical for each row in PORT that is considered for deletion. This way, you are either going to delete all lines, or there are no lines.
(It's also weird that you use LEFT JOIN when you have a non-join predicate on an external table. But in the worst case, it is a performance issue and most likely just confuses anyone reading your code.)
I believe you want:
DELETE FROM PORT WHERE fkjacket = :old.pkid AND EXISTS (SELECT NULL FROM device WHERE device.pkid = port.fkdevice AND device.fkdevice_type=1);
And UPDATE seems to have the same problem; even if it is currently producing the expected results, Iβm sure itβs just luck because of the data you are testing. I think this can be simplified:
UPDATE PORT set port.fkjacket = null, port.fkport = null WHERE port.fkjacket = :old.pkid AND EXISTS (SELECT NULL FROM device WHERE port.fkdevice = device.pkid AND device.fkdevice_type <> 1);
Note that the EXISTS statement doesn't care if any columns are returned by its subquery; Are the rows just returned at all?
source share