Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6018809
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T03:19:42+00:00 2026-05-23T03:19:42+00:00

I have the trigger: create or replace TRIGGER JACKET_DELETE BEFORE DELETE ON JACKET FOR

  • 0

I have the trigger:

create or replace
TRIGGER JACKET_DELETE 
BEFORE DELETE ON JACKET 
FOR EACH ROW 
BEGIN
  DELETE FROM PORT
  WHERE EXISTS
    (SELECT * FROM port LEFT JOIN device on port.fkdevice = device.pkid
     where port.fkjacket = :old.pkid
     and device.fkdevice_type = 1);

  UPDATE PORT
  set port.fkjacket = null, port.fkport = null
  WHERE EXISTS
(SELECT port.fkjacket, port.fkport FROM port LEFT JOIN device on port.fkdevice = device.pkid
     where port.fkjacket = :old.pkid
     and device.fkdevice_type <> 1);
END;

For some reason, when the where in the delete matches, it deletes the WHOLE port table! I thought my SQL was correct, but obviously it’s not, and I can’t see what’s wrong with it. Can anyone see the issue that is making it do this?

When the update matches, everything works as expected.


table structure:
port links to device, jacket, and port

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T03:19:43+00:00Added an answer on May 23, 2026 at 3:19 am

    Your DELETE is referencing the PORT table twice. To clarify, let’s 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);
    

    Notice that the subquery is not correlated to p1. In other words, the result of this subquery will be identical for every row in PORT that is being considered for deletion. So you’re either going to delete all rows or no rows.

    (It’s also odd that you use a LEFT JOIN when you have a non-join predicate on the outer table. But that’s at worst an efficiency problem and more likely just confusing to anyone reading your code.)

    I believe that what you want is:

    DELETE FROM PORT
    WHERE fkjacket = :old.pkid
    AND EXISTS
      (SELECT NULL FROM device
       WHERE device.pkid = port.fkdevice
         AND device.fkdevice_type=1);
    

    And the UPDATE seems to have the same issue; even if it’s currently giving you expected results, I bet that’s just luck due to the data you’re testing with. I think it can be simplified to:

    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 an EXISTS operator doesn’t care what if any columns are returned by its subquery; just whether rows are returned at all.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is what I currently have: CREATE OR REPLACE TRIGGER MYTRIGGER AFTER INSERT ON
I have this trigger: create or replace trigger t_calctotal after insert or update on
I have a table for which i have written a trigger: CREATE OR REPLACE
If I have a trigger before the update on a table, how can I
I have this trigger procedure CASE_A as UPDATE table1 SET field1 = (SELECT bus
I have the following code for a trigger: create trigger trPassDat ON men FOR
When I try create a trigger as given below, CREATE TRIGGER FiscalYearTable1_bi BEFORE INSERT
I have to create a trigger (not procedure) to update subtotal, shipping_charge, tax and
Is it possible to delete a file from the filesystem in a SQL trigger
I have this Trigger in Postgresql that I can't just get to work (does

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.