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 8933761
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T09:37:27+00:00 2026-06-15T09:37:27+00:00

I have three PK columns (staff, the_date and work_time ), two of which are

  • 0

I have three PK columns (staff, the_date and work_time), two of which are reliant on each other: work_time and the_date.

PK staff is linked to Staff table, and PK the_date and work_time is linked to Company table. But all three PK described are in Admission table.

I need to make a trigger that prevents deletion of a staff, if the work_time and the_date are the remaining one for the table Admission.

For example:

ROW 1: staff: 2 | the_date: 2012-01-20 | work_time : 13:20:02
ROW 2: staff: 3 | the_date: 2012-01-20 | work_time : 13:20:02
ROW N: ............

To clarify, each time and date can be the same but can have many UNIQUE staffs working there at the same time.

From the example above, if I tried to delete staff 3 (ROW 2), then no problem – that is permitted. However, if I tried to delete again, but this time for staff 2 (ROW 1) then the trigger would fire and prevent me from deleting that remaining staff (row).

I’m new to triggers, but I had a crack at it but wasn’t able to make it work. From my interpretation on how the code goes is: IF the_date and work_time are DISTINCT then DON’T DELETE ELSE DELETE.

Here’s what I did:

CREATE TRIGGER preventLastStaffDeletion
BEFORE DELETE 
ON Admission 
FOR EACH ROW
BEGIN
SELECT DISTINCT the_date, work_time FROM Admission;
END

Thanks in advance.

  • 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-06-15T09:37:27+00:00Added an answer on June 15, 2026 at 9:37 am

    From the trigger documentation:

    The trigger cannot use statements that explicitly or implicitly begin or end a transaction such as START TRANSACTION, COMMIT, or ROLLBACK.

    So usually this would be not possible.
    What you can do is to force an error in your trigger when you want to fail the whole transaction: e.g.

    CREATE TRIGGER t1 BEFORE DELETE ON table1
    FOR EACH ROW
    BEGIN
          IF (failCondition) THEN
               SELECT 1/0 FROM table1 LIMIT 1
          END IF;
    END
    

    This would trigger a error and rollback the whole transaction

    EDIT:

    So for your problem this would be something like this

    CREATE TRIGGER preventLastStaffDeletion BEFORE DELETE ON Admission
    FOR EACH ROW
    BEGIN
          DECLARE remaining INT DEFAULT 0;
          SET remaining := (SELECT COUNT(staff) FROM Admission WHERE the_date = OLD.the_date AND work_time = OLD.work_time );
          IF remaining = 1 THEN
               SELECT 1/0 FROM Admission LIMIT 1
          END IF;
    END
    

    Didn’t test so excuse syntax errors, but the idea should be clear.

    Allows deletion of staff with this work time at this date till there is only one left

    EDIT 2
    Re-read your question and updated the fail condition

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

Sidebar

Related Questions

I have a table with three columns, and two cells contain select boxes with
I have a table ABR with three columns: two lookup values, A and B
I have three columns in mysql table: tax_id, company_name, store_name. I need that tax_id
Consider i have a user table and I have three columns mobilePhone , homePhone
I have three tables with 5 columns each. They can grow upto 15 rows
I have three tables, Name - columns Custpackingslipjour(table 1) - (deliverydate, dateclosed,, salesid) Inventrans
i have a mysql db, and i have one table which has many columns
I'm new to php and sql. I have a table with three columns. One
I have a staff database table containing staff members, with user_no and user_name columns.
I have three columns, y, m, and d (year, month, and day) and want

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.