MySQL create trigger declare - mysql

I am trying to complete this question
**
Produce an audit trail (in a separate table) that records the current username, system date & grade change when someone attempts to update a Students grade
**
Here are the tables and columns
Module (code,credits,cost,name)
session (code,date,room)
Exam (no,code,grade)
course (code,credits,name)
Student (no,name,cell)
Here is the code I have so far
DELIMITER $$
USE `HarlemHS`$$
CREATE
DEFINER=`HarlemHS`#`%`
TRIGGER `HarlemHS`.`ExamChange`
AFTER UPDATE ON `HarlemHS`.`Exam`
FOR EACH ROW
BEGIN
INSERT INTO NEW.GradeUpdateLog Date_Of_Change,old_grade)
VALUES (CURDATE(), grade);
END$$
I know I have to use DECLARE somewhere but not too sure where to use it and what to put in the DECLARE statement.
If i have missed anything let me know,
Thank you for the help :)

Related

MYSQL - Advice on populating FIELD options from another TABLE COLUMN using ENUM

I'm a MySql newbie so sorry if this has been asked before.
I am going round in circles trying to get a solution to this question.
I have a MySql database with two tables.
TERRITORIES
This table lists all nations of the world and has a column for listing the currency of each country.
CURRENCIES
This is essentially a "lookup" table including a country ID and a list of world currencies.
When inputting data into the TERRITORIES.currency cell I would like a drop-down list to appear from the CURRENCIES.country column.
I've spent hours googling to see if an ENUM field can call that data from the other table or whether one should use SET etc.
If anyone can assist me with reference to a good CLEAR tutorial on this issue I'd appreciate it.
There appear to be 2 things you want to happen 1) Populate 2) validate
For both triggers may be good for you.
delimiter $$
Create trigger t before insert on territories
for each row
begin
set new.currency = (Select currency from currencies where country = new.country);
end $$
delimiter ;
It doesn't then matter if an insert tries to insert to currency as well as country.
You also need an update trigger
delimiter $$
Create trigger t before update on territories
for each row
begin
set new.currency = (Select currency from currencies where country = new.country);
end $$
delimiter ;
To stop an update changing the currency to an invalid currency.

MySQL TRIGGER to INSERT BEFORE INSERT using SELECT, IF, etc

I am trying to write a trigger that combines an insert & select, I've found numerous topics online, but, none seem to relate to my exact problem, maybe I am missing something with my structure?
The aim of this is that on the event of a cancellation in our audit log, then I define a cancellation reason based on a series of business logic in another table, this logic is drawn together in a SELECT using CASE & subqueries.
I want to expand the following trigger that currently works and replace the SET cancellation_point='test' element with the SELECT query I just mentioned.
DELIMITER $$
CREATE
TRIGGER `cancellation_stage` BEFORE INSERT ON `log`
FOR EACH ROW
BEGIN
IF (NEW.status='cancel' AND NEW.type='0') THEN
INSERT INTO cancellation_stage
SET
id=NEW.id,
property_id=NEW.entity_id,
cancellation_date=NOW(),
cancellation_point='test';
END IF;
END;
$$
DELIMITER ;
I did try to construct this myself using various guidance from here, but, its just not working. I got this code to physically save as a trigger, but, it did not populate the data in the database (I have replaced my SELECT with a basic query example below):
DELIMITER $$
CREATE
TRIGGER `cancellation_stage` BEFORE INSERT ON `log`
FOR EACH ROW
BEGIN
DECLARE cancellation_point VARCHAR(255);
SET cancellation_point = ( SELECT * FROM x);
IF (NEW.transition='cancel' AND NEW.entity_type='property') THEN
INSERT INTO cancellation_stage
SET
id=NEW.id,
property_id=NEW.entity_id,
cancellation_date=NOW(),
cancellation_point=NEW.cancellation_point;
END IF;
END;
$$
DELIMITER ;
Any help would be greatly appreciated :)

Creating Trigger on Insert Command MYSQL

I am trying to create a trigger for when someone enters a salary number that is less than a number already present in my database. I know how to make it in Java and C, just not in SQL.
CREATE TRIGGER InsertError
ON dbo.Employee_audit
AFTER Insert
AS
BEGIN
IF (SELECT Salary FROM dbo.Employee_audit) < 25000
BEGIN
RAISERROR('Salary is too low')
END
END
GO
I know this is incorrect, but there seems to be little resources or examples of specific scenarios.

MySQL write a trigger to prevent from buying too much

I have a table warehouse where I have information about articles in my store (article id as foreign key and quantity). Then, I have another table, shoppinglist where I have a clients id, article id and quantity. Lets say, that client wants to buy 3 articles but theres only one article available. How to write a trigger which help me to prevent from buying too much?
I tried this:
DELIMITER $$ CREATE TRIGGER check BEFORE INSERT ON shoppinglist FOR EACH ROW BEGIN IF warehouse.quantity < shoppinglist.quantity THEN CALL fail('You cant buy that much'); END IF; END $$ DELIMITER;
but this seems not to work. I mean, when I do:
INSERT INTO shoppinlist (clients_id, article_id, quantity) VALUES (1, 2, 100);
having only 2 articles with id = 2 on warehouse its ok, its possible. What did I do wrong?
What specific article would warehouse.quantity or shoppingList.quantity refer to in your code?
Also, check is a reserved keyword.
Try this:
DELIMITER $$
CREATE TRIGGER qtyCheck BEFORE INSERT ON shoppinglist
FOR EACH ROW
BEGIN
SET #qty = (SELECT quantity FROM warehouse WHERE article_id = NEW.article_id);
IF #qty < NEW.quantity THEN
CALL fail('You cant buy that much');
END IF;
END $$
DELIMITER ;
Note that I renamed the trigger, I'm guessing the name of the article_id column on the warehouse table, I used the NEW variable instead of shoppingList within the body of the trigger, and you need a space before the semicolon in DELIMITER ;, though this might've been a typo when posting.
Finally, you may get the following error if the fail function isn't defined. It doesn't exist on my system...
ERROR 1305: PROCEDURE testing.fail does not exist

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.