I have a table SCHEDULES_CLASS
, 2 of it's fields are CLASS_ID
(foreign key from the CLASS table), STATUS
and NUMBER_OF_ENROLLED_STUDENTS
. I have other table CLASS
having 1 field as MAX_SEATS.
When the NUMBER_OF_ENROLLED_STUDENTS
in a scheduled class equals the MAX_SEATS
available for the respective CLASS, I need to change the status of that SCHEDULES_CLASS
to "FULL" from the initial status which is "OPEN".
I created a trigger for this purpose as follows:
USE mydb;
DELIMITER ##
dROP TRIGGER IF EXISTS updateClassStatusTrigger ##
CREATE TRIGGER updateClassStatusTrigger
BEFORE UPDATE ON SCHEDULED_CLASS
FOR EACH ROW
BEGIN
UPDATE SCHEDULED_CLASS SET STATUS="FULL" WHERE CLASS_ID=NEW.CLASS_ID
AND EXISTS (SELECT 1 FROM SCHEDULED_CLASS sc, CLASS cl WHERE cl.CLASS_ID=NEW.CLASS_ID AND NEW.NUMBER_OF_ENROLLED_STUDENTS=cl.MAX_SEATS);
END##
I am using phpmyadmin, and I get the following error, when any update takes place on the SCHEDULED_CLASS table:
#1442 - Can't update table 'SCHEDULED_CLASS' in stored function/trigger because it is already used by statement which invoked this stored function/trigger.
Any ideas, how to accomplish this task? i.e. update the status of the scheduled class to 'FULL' whenever the enrolled students number reaches the max available seats.
Thanks
MySQL UPDATE_TRIGGER is one of the triggers that enable the update actions on the specified table. On a general note, a trigger can be defined as a set of instructions or steps which perform the intended change action automatically, on the specified table. The possible change actions can be INSERT, UPDATE, or DELETE.
To modify a DML trigger In Object Explorer, connect to an instance of Database Engine and then expand that instance. Expand the database that you want, expand Tables, and then expand the table that contains the trigger that you want to modify. Expand Triggers, right-click the trigger to modify, and then click Modify.
UPDATE. An UPDATE trigger can refer to both OLD and NEW transition variables. INSERT. An INSERT trigger can only refer to a NEW transition variable because before the activation of the INSERT operation, the affected row does not exist in the database.
Old and new values are available in both BEFORE and AFTER row triggers. A new column value can be assigned in a BEFORE row trigger, but not in an AFTER row trigger (because the triggering statement takes effect before an AFTER row trigger is fired).
The way to update same row is to simply SET NEW.column_name = some_Value
. To simplify your trigger, consider:
CREATE TRIGGER updateClassStatusTrigger
BEFORE UPDATE ON SCHEDULED_CLASS
FOR EACH ROW
SET NEW.STATUS='FULL'
Now, in addition, you need only to update value given some condition. This can't be done within the same query here since we are no longer using a standard UPDATE
. So, split:
USE mydb;
DELIMITER ##
dROP TRIGGER IF EXISTS updateClassStatusTrigger ##
CREATE TRIGGER updateClassStatusTrigger
BEFORE UPDATE ON SCHEDULED_CLASS
FOR EACH ROW
BEGIN
SELECT EXISTS (SELECT 1 FROM SCHEDULED_CLASS sc, CLASS cl WHERE cl.CLASS_ID=NEW.CLASS_ID AND NEW.NUMBER_OF_ENROLLED_STUDENTS=cl.MAX_SEATS) INTO @row_exists;
IF @row_exists THEN
SET NEW.STATUS='FULL';
END IF;
END##
I hope I don't have some syntax error in the above. I've tested it on my own tables, but have then edited to fit your schema.
Your own attempt failed because from within a trigger, MySQL does not allow you to modify the table from which the trigger executed. It could not analyze your query to determine what you essentially tried can ba accomplished in a valid way - it doesn't even try. It simply forbids modifying same table.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With