How to properly create a trigger in workbench? - mysql

I am trying to create a simple triger functionality in my DB, so I can change a value in one of my tables when the other one is updated.
I have an oversimplified library database and the tables in question are boakloan and bookcopy.
In bookloan I have a foreign key to bookcopy, where I have an isAvailable column. The defaul value of this column is 1(true), and having a new entry in my bookloan table would mean that a copy of a book is borrowed, and it's no longer available, so I want to change the value of isAvailable to 0(false) for this record.
I tried to write a trigger for that in a couple of ways, but nothing worked.
Here is my last code :
USE `libdb`;
DELIMITER $$
DROP TRIGGER IF EXISTS libdb.bookcopy_update$$
USE `libdb`$$
CREATE DEFINER=`root`#`localhost` TRIGGER `libdb`.`bookcopy_update`
BEFORE INSERT
ON `bookloan` FOR EACH ROW
BEGIN
UPDATE bookcopy
SET bookcopy.isAvailable = 0
WHERE bookcopy.isAvailable = 1
AND bookcopy.idBookCopy = bookloan.BookCopy_idBookCopyFK;
END$$
DELIMITER ;
Can someone help make this work?
With the current version I get the following error : ERROR 1054: 1054: Unknown column 'bookloan.BookCopy_idBookCopyFK' in 'where clause'

You get that error because in this query (from your trigger body):
UPDATE bookcopy
SET bookcopy.isAvailable = 0
WHERE bookcopy.isAvailable = 1
AND bookcopy.idBookCopy = bookloan.BookCopy_idBookCopyFK;
there's no bookloan defined.
Replace bookloan.BookCopy_idBookCopyFK with NEW.BookCopy_idBookCopyFK.

Related

How to add a trigger in Oracle SQL to update a value after a row was created

I am trying to create triggers for my Oracle database and I always get compiled with errors when trying to add it.
The statement is as follows
CREATE OR REPLACE TRIGGER update_open_amount
AFTER INSERT ON Payment
FOR EACH ROW
BEGIN
UPDATE Booking SET open_amount = open_amount - NEW.amount WHERE Booking.booking_id = NEW.booking_id;
END;
and I get the following error
2/5 PL/SQL: SQL Statement ignored
2/90 PL/SQL: ORA-00904: "NEW"."BOOKING_ID": invalid identifier
The trigger should subtract the amount in the newly created payment row from the open_amount field in the booking row and store the new value.
I managed to get it to work in mysql with the following statement
DELIMITER $$
CREATE TRIGGER `update_open_amount` AFTER INSERT ON `Payment`
FOR EACH ROW
BEGIN
UPDATE `Booking` SET `open_amount` = `open_amount` - NEW.`amount` WHERE `Booking`.`booking_id` = NEW.`booking_id`;
END;
$$
DELIMITER ;
but when I try to get it to work in oracle I get stuck.
How can I get the trigger to work because this is all that is still missing for migrating the database from mysql to oracle.
How can I fix this?
Thanks in advance
Please check syntax for referring new and old values. It should have preceeding colon (:)
CREATE OR REPLACE TRIGGER update_open_amount
AFTER INSERT ON Payment
FOR EACH ROW
BEGIN
UPDATE Booking
SET open_amount = open_amount - :NEW.amount
WHERE Booking.booking_id = :NEW.booking_id;
END;

How to insert row into table when update event occurs in another table?

I have two tables named del_stu and del_emp.
I want to insert a row into the del_emp table when updating the del_stu table. and I wan to insert the row which updated by the del_emp. I have been created a trigger for that as follows.
create trigger before_update_stu
before update
on del_stu
for each row
begin
insert into del_emp
set
del_emp.emp_id = del_stu.stu_id,
del_epm.emp_name = del_stu.stu_name,
del_emp.salary = stu_emp.salary;
end
But this shows the following error.
ERROR 1054 (42S22): Unknown column 'del_epm.emp_name' in 'field list'
So how to do this task? That means how to change my trigger in order to do this task?
If you not clear about the task,
The task
I want to insert a row(updated by the del_stu table) into the del_emp table
It seems to be a spelling issue for that particular column.
You should have
del_emp.emp_name.
Otherwise syntax looks fine for MySQL.
Need to use "OLD" keyword for that. The following code worked.
create trigger before_update_stu
before update
on del_stu
for each row
begin
insert into del_emp
set
emp_id = OLD.stu_id,
emp_name = OLD.stu_name,
salary = OLD.salary;
end

Unknown table in where clause trigger

Im trying to make a trigger to update my product status. After it gets delivered. So when something is inserted in entrega_viaturas it triggers and updates estado in viatura.
Here are the tables and code for the trigger, please if you see something wrong, do tell.
Tabel entrega_viaturas(id_entrega,data_entrega,funcionario,viatura(fk id_viatura))
Tabel viatura(id_viatura,estado,*other columns*)
CREATE TRIGGER `Update_Status` AFTER INSERT ON `entrega_viaturas`
FOR EACH ROW BEGIN
AS
begin
update viatura
SET estado='Indisponivel'
Where viatura.id_viatura = entrega_viaturas.viatura
end
But it says that entrega_viaturas.viaturas is a unknown column. help
I am assuming that you intend to use new:
CREATE TRIGGER `Update_Status` AFTER INSERT ON `entrega_viaturas`
FOR EACH ROW
AS
begin
update viatura
set estado = 'Indisponivel'
where viatura.id_viatura = new.viatura
end;

MySQL Trigger Error: Updating the one table's field value based upon another table updates or insertions

I want to update one table in MySQL based upon another table update and insertions. Scenario is like this, when someone change the status of the column in main table the child table's column field should be update for their matching ID.
Tables look like as below:
po_request
Id po_id status
1 E0001 Requested
2 E0002 Received
PO_LINE
Id po_id is_received
6 E0001 0
7 E0002 0
Need to update the Po_line table each time when status has changed to "Received" OR directly insert the "received" in the table. I have made trigger but it's not working. Trigger
DROP TRIGGER IF EXISTS `t1`;
DELIMITER $$
CREATE TRIGGER `t1`
AFTER UPDATE ON po_request FOR EACH ROW
BEGIN
IF NEW.`status` = 'Received'
THEN
UPDATE po_line JOIN po_request ON po_request.po_id = po_line.po_id SET is_received = '1' WHERE po_request.status = 'Received'; END IF;
END$$
DELIMITER ;
Trigger loaded into table successfully but when I update the table it throws very weird error:
Error Code: 1054. Unknown column 'date_received' in 'field list'.
I think you need to update it by avoid joining the table and match in where clause with the reference of NEW keyword. which refers to the other table id like :
UPDATE po_line
SET po_line.is_received = '1'
WHERE new.po_id = po_id
AND po_request.status = 'Received';

MySQL Trigger to update another table

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.