MySQL Trigger to update another table - mysql

I have the following two tables in a MySql database:
Bookings
BookingID | ClientID | SeatID
SeatAvailability
SeatAvailabilityID | BookingID | ShowID | Available
They are linked on SeatID/SeatAvailabilityID.
I'm trying to write a trigger which updates the SeatAvailability table each time a row is inserted in Bookings. The trigger should change SeatAvailability.Available to 0 and also enter the BookingID from Bookings into the BookingID field in SeatAvailability with the same SeatAvailabilityID.
I've written this trigger, MySql accepts it but gives an error when inserting
"ERROR 1054: Unknown column 'cinemax.bookings.SeatID' in 'where clause'".
DELIMITER $$
USE `cinemax`$$
CREATE
DEFINER=`root`#`localhost`
TRIGGER `cinemax`.`update_available`
AFTER INSERT ON `cinemax`.`bookings`
FOR EACH ROW
UPDATE cinemax.seatavailability
SET cinemax.seatavailability.Availabe=0, cinemax.seatavailability.BookingID=cinemax.bookings.BookingID
WHERE cinemax.bookings.SeatID=cinemax.seatavailability.SeatAvailabilityID$$

try
AFTER INSERT ON `cinemax`.`bookings`
instead of
AFTER UPDATE ON `cinemax`.`bookings`

It's a couple of months late, but I decided to give it a quick shot before handing in the overall assignment. In the meantime I switched to postgres as it seemed to offer more functionality (albeit not as user friendly). I first had to create a trigger function:
CREATE OR REPLACE FUNCTION updateseatavailable()
RETURNS trigger AS
$BODY$
BEGIN
IF (TG_OP = 'INSERT') THEN
UPDATE "SeatAvailability"
SET "Available"='FALSE' AND "BookingID"=NEW."BookingID" WHERE "SeatAvailabilityID"=NEW."SeatID";
ELSIF (TG_OP = 'DELETE') THEN
UPDATE "SeatAvailability"
SET "Available"='TRUE' WHERE "SeatAvailabilityID"=OLD."SeatID";
END IF;
RETURN NEW;
END;
$BODY$
LANGUAGE plpgsql VOLATILE
and then simply call the function/procedure from a trigger:
CREATE TRIGGER UpdateSeatAvailable
AFTER INSERT OR DELETE ON "Bookings"
FOR EACH ROW
EXECUTE PROCEDURE updateSeatAvailable();
I wasn't able to get the BookingID in SeatAvailability to update for some reason (on Insert nothing happened and on Delete I got an error telling me Available cannot be null, even though I was changing the BookingID) so I omitted that in postgres,and implemented it with Java instead. It's not the best way but still better than nothing.
I decided to post my solution just in case someone has a similar problem and stumbles upon this question.

Related

MYSQL fires the trigger only when no value manually inserted - not working

Hi I just need help as I am just new to mysql trigger.
I'm trying to create a trigger that if the project id is NULL then it will set it to new value but if someone has manually insert a value then it won't do anything.
I performed this but it ain't working on mysql.
CREATE TRIGGER custom_autonums_pdl BEFORE INSERT ON project_details_logs
FOR each ROW
WHEN (new.projectid IS NULL)
BEGIN
SET new.projectid = getNextCustomSeqPl(year(now()),year(now()));
END//
delimiter ;
If someone can direct me to correct this, appreciate it.
Thank you.
I haven't seen a MariaDb/MySQL trigger that uses the WHEN syntax available in other major RDBMS - you'd need to run the trigger for every insert and conditionally act on the id:
DELIMITER |
CREATE TRIGGER custom_autonums_pdl
BEFORE INSERT ON project_details_logs
FOR each ROW
BEGIN
IF new.projectid IS NULL THEN
SET new.projectid = getNextCustomSeqPl(year(now()),year(now()));
END IF;
END|
DELIMITER ;

stored procedure returning `ASCII \0` error but can't find what that is referring to

I am writing my first stored procedure as a trigger. I am doing this in a dev migration as we have two systems which don't speak to each other in dev, so I need to mock the data which would normally come from the other system.
My procedure is added as part of our dev migration script.
DELIMITER |;
CREATE TRIGGER `activity_insert` AFTER INSERT ON `activity`
FOR EACH ROW
BEGIN
UPDATE `activity` AS `a` JOIN `handle` AS `h` on `a.handle_id` = `h.handle_id` SET `path` = CONCAT(`h.handle`,'/',`a.activity_handle`) WHERE `a.path` IS NULL;
END;
|
DELIMITER;
I would expect the logic to be:
DELIMITER $$
CREATE TRIGGER activity_insert BEFORE INSERT ON activity
FOR EACH ROW
BEGIN
IF new.path IS NULL THEN
SET new.path = (SELECT CONCAT(h.handle, '/', new.activity_handle)
FROM handle h
WHERE new.handle_id = h.handle_id
);
END IF;
END;$$
DELIMITER;
There are numerous problem with your code:
You don't update the table being modified using update.
You want a "before" triggers, not an "after trigger".
Don't use | for the the delimited. It is a valid MySQL operator.
You have over-used the backtick, including putting the table alias in with the column alias.
This assumes that handle.handle_id is unique. This seems like a reasonable assumption based on the names, but you can add limit 1 to guarantee no more than one row is returned.

How to write a 'Before Delete' trigger in SQL

I am currently working on a homework project where we are creating triggers user plpgsql
I've managed to figure out the trigger for update/insert but I'm having trouble with writing
a delete one and feel like im going round in circles with the online documentation.
The two tables we're given are:
MAJOR(mCode, name) with primary key mCode.
STUDENT(sId, firstName, lastName, mCode, pointsEarned) with primary key {sId} and foreign key mCode
The requirement is basically that a major cannot be deleted or changed if a student has chosen the major.
(I've already implemented the update/insert trigger)
I'm 99% sure my create trigger will just be:
CREATE TRIGGER major_delete
BEFORE UPDATE OR INSERT ON student
FOR EACH ROW EXECUTE PROCEDURE major_delete_trigger();
And I have come up with two basic ideas for the function (based off lectures notes):
CREATE OR REPLACE FUNCTION major_delete_trigger()
RETURNS trigger AS $$
DECLARE m RECORD;
BEGIN
SELECT * INTO m FROM major WHERE OLD.mcode = mcode;
IF NOT FOUND THEN RETURN OLD;
ELSE
DELETE FROM major WHERE mcode = OLD.mcode;
RETURN NULL;
END IF;
END;
$$ LANGUAGE 'plpgsql';
Which does nothing at all - when running a test on my database deletes that should be failing still succeed.
Secondly:
CREATE OR REPLACE FUNCTION major_delete_trigger()
RETURNS trigger AS $$
BEGIN
IF (TG_OP = 'DELETE') THEN
DELETE FROM major WHERE mcode=OLD.mcode;
IF NOT FOUND THEN RETURN NULL; END IF;
END IF;
END;
$$ LANGUAGE 'plpgsql';
Which executes the line 'DELETE FROM major WHERE mcode = OLD.mcode;' until there is no room left on the stack.
When doing my update function I found I was just looking at it as if it was really complicated so I'm not sure if this will be the same.
Any ideas or documentation that could help will be much appreciated!

Update foreign key of different row of the same table on UPDATE

I'm using MySql, for my Database and I have a table "Person". Amongst other thing, it contains a relation to itself "Person_MarriedTo" stored as a foreigh_key;
This relation can be null since it's not everybody that's married. What I want to do is that this field can be updated automatically.
if I add A married to B, then B married to A
if B deivorce from A, then A is also divorced from B
I thought triggers would be the way to go, but I can't get my code to work the way I want it to. Any insights would be much appreciated.
Here is my code for the trigger:
USE `mydb`;
DELIMITER $$
CREATE TRIGGER `Person_BUPD` BEFORE UPDATE ON Person FOR EACH ROW
BEGIN
IF OLD.Person_MariedTo != NULL THEN
UPDATE Person SET Person_MariedTo = NULL WHERE UID_Person = OLD.Person_MariedTo;
END IF;
UPDATE Person SET Person_MariedTo = OLD.UID_Person WHERE UID_Person = NEW.Person_MariedTo;
END$$
What you are trying to do is not possible using a trigger.
Within a stored function or trigger, it is not permitted to modify a table that is already being used (for reading or writing) by the statement that invoked the function or trigger. You need to do this some other way.
Source
See here:
MySQL - Trigger for updating same table after insert

Trigger - Error #1442 Can't update [MYSQL] [duplicate]

MySQL doesn't currently support updating rows in the same table the trigger is assigned to since the call could become recursive. Does anyone have suggestions on a good workaround/alternative? Right now my plan is to call a stored procedure that performs the logic I really wanted in a trigger, but I'd love to hear how others have gotten around this limitation.
Edit: A little more background as requested. I have a table that stores product attribute assignments. When a new parent product record is inserted, I'd like the trigger to perform a corresponding insert in the same table for each child record. This denormalization is necessary for performance. MySQL doesn't support this and throws:
Can't update table 'mytable' in stored function/trigger because it is already used by statement which invoked this stored function/trigger. A long discussion on the issue on the MySQL forums basically lead to: Use a stored proc, which is what I went with for now.
Thanks in advance!
You can actually up the rows in the same table as the trigger. The thread you linked to even has the solution.
For example:
TestTable ( id / lastmodified / random )
create trigger insert_lastmod
before insert on TestTable
for each row
set NEW.lastmodified = NOW();
insert into TestTable ( `random` ) values ( 'Random' );
select * from TestTable;
+----+---------------------+---------------------+
| id | lastmodified | random |
+----+---------------------+---------------------+
| 1 | 2010-12-22 14:15:23 | Random |
+----+---------------------+---------------------+
I suppose you could call the stored proc in your trigger. HOwever, if you want to update some fields in the same records that you are changing (such as an updatedby or lastupdated column) then you can do this in a beofre trigger according to the refernce manual. http://dev.mysql.com/doc/refman/5.0/en/trigger-syntax.html
This is a common operation for triggers and I find it difficult to believe it isn't supported.
If you want to update column that you don't read in trigger function, then as a workaround, you could put that column into separate table.
You can actually do that
The below is an example for same
DELIMITER $$
create trigger test2
before insert on ptrt
for each row
begin
if NEW.DType = "A" then
set NEW.PA = 500;
elseif NEW.DType = "B" then
set NEW.PA = 1000;
else
set NEW.PA = 0;
END IF;
END;$$
DELIMITER;
This worked for me :D
On Before / Update.
BEGIN
SET NEW.DateTimeUpdated = NOW();
END