MySQL workbench error 1064 when adding trigger - mysql

I am new to SQL and more specifically using MySQL Workbench.
I have created a database with two tables Grades and GPAList.
I want to take the average value of every entry in Grades (grouped by student id [sid is the name of the column]) once I have those averages, store them in the GPAList table with their corresponding students. This is intended to trigger every time a new entry is added to Grades.
This is the trigger that I have created:
CREATE DEFINER = CURRENT_USER TRIGGER `ProjectName`.`Grades_AFTER_UPDATE_1` AFTER UPDATE ON `Grades` FOR EACH ROW
BEGIN
SELECT sid, SUM ( CASE grade
when 'A' then 4.0
when 'B' then 3.5
when 'C' then 3.0
when 'D' then 2.5
when 'F' then 1.0
else 0
end
) / COUNT(*) Grades
INTO GPAList
FROM Grades
GROUP BY sid;
END
The error that I am encountering is:
Error 1064: You have an error in your SQL syntax; check the manual that
corresponds to your MySQL server version for the right syntax to use near '' at
line 1 SQL Statement: CREATE DEFINER = CURRENT_USER TRIGGER
`ProjectName`.`Grades_AFTER_UPDATE_1` AFTER UPDATE ON `Grades` FOR EACH ROW
FOLLOWS `Grades_AFTER_UPDATE`
When I write this code in the workbench, no syntax errors are shown. When I try to apply this trigger, my code is converted to:
DROP TRIGGER IF EXISTS `ProjectName`.`Grades_AFTER_UPDATE_1`;
DELIMITER $$
USE `ProjectName`$$
CREATE DEFINER = CURRENT_USER TRIGGER `ProjectName`.`Grades_AFTER_UPDATE_1` AFTER UPDATE ON `Grades` FOR EACH ROW FOLLOWS `Grades_AFTER_UPDATE`
$$
DELIMITER ;
Only after clicking apply once more do I see the 1064 error listed above. Is there some easy fix that I am missing?
There is a Grades_AFTER_UPDATE that Grades_AFTER_UPDATE_1 is following, so that shouldn't be the problem.

I'm not sure why you would want to store all averages after a single insert, but the correct insert logic is something like this:
INSERT INTO GPALIST(sid, grades)
SELECT sid,
AVG (CASE grade
when 'A' then 4.0
when 'B' then 3.5
when 'C' then 3.0
when 'D' then 2.5
when 'F' then 1.0
else 0
end) as Grades
FROM Grades
GROUP BY sid;
I suspect the trigger would want to access new. somewhere along the way.

Related

Ambiguous error in creating BEFORE INSERT trigger

I have a Reservation table and am creating a BEFORE INSERT trigger that checks whether a user's reservation is already in the database, i.e. prevent him from making duplicate reservations. This is done to signal the front-end that the user is unable to make a duplicate reservation.
However, mySQL raises this error: ERROR 1064 (42000) at line 3: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 5. For some reason when the body between BEGIN and END is empty, it doesn't raise an error.
CREATE TABLE reservation (
customer_user_id integer,
date integer,
time integer,
name varchar(100)
);
CREATE TRIGGER trigger_name
BEFORE INSERT -- error raised in this line
ON reservation FOR EACH ROW
BEGIN
DECLARE sum INT;
SELECT COUNT(*)
INTO sum
FROM reservation r
WHERE (r.customer_user_id = new.customer_user_id)
AND (r.date = new.date)
AND (r.time = new.time);
IF sum = 0 THEN
INSERT INTO reservation values (new.customer_user_id, new.date, new.time, new.name);
ELSE
RAISE NOTICE 'duplicate'
END IF;
END;
Additionally, could this trigger be a constraint on the table Reservation? E.g. (customer_user_id, date, time) UNIQUE of sorts, which would prevent adding of duplicates, but I am clueless on how to "signal" the front-end which in turn prompts the user that they cannot add a duplicate reservation.
Sorry if some parts of the code might be weird, I'm new to SQL and was taught pSQL.
Thanks in advance!

MySQL: Trying to Create a Trigger Where Another Table is Updated

I'm trying to create a trigger whereby an insertion on one table updates another. This is my SQL Query:
CREATE TRIGGER makePayment AFTER INSERT ON Payments FOR EACH ROW
BEGIN
UPDATE Invoice
SET InvoiceClientPaid = SUM(InvoiceClientPaid + NEW.PaymentAmt)
WHERE InvoiceID = NEW.PaymentInvoiceID;
END;
No matter what I do I get the following error:
Error Code: 1064. You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 6
I don't think it's related to the SUM, because trying a basic = 1 on the SET command gives me the exact error. There is no '' at line 6 which is very confusing?
If you're entering this query directly into MySQL you will need to change the delimiter prior to the query using (e.g.) DELIMITER //, otherwise it thinks the query ends at the ; at the end of your UPDATE statement. MySQL then sees an END with nothing before it and complains about the nothing (''). So try this:
DELIMITER //
CREATE TRIGGER makePayment AFTER INSERT ON Payments FOR EACH ROW
BEGIN
UPDATE Invoice
SET InvoiceClientPaid = SUM(InvoiceClientPaid + NEW.PaymentAmt)
WHERE InvoiceID = NEW.PaymentInvoiceID;
END; //
DELIMITER ;

Can't figure out what's wrong with SQL syntax

I am getting this error:
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'if (t=null) then update employee set grade='x' where empid=id; END' at line 4
I am not able to understand where the syntax is wrong.
I am creating a procedure to get a grade from the table if it grade is present. If it is not present then it should be updated as x.
CREATE PROCEDURE spGETgrade (in id int)
BEGIN
select grade as t from employee where empid=id
if (t=null) then
update employee set grade='x' where empid=id;
END $$
I think you need ";" after the first query
select grade as t from employee where empid=id; <<---
and
IF THEN
...
END IF; <<--

IF NOT EXIST NOT WORKING

IF NOT EXISTS(SELECT * FROM `user` WHERE `name`='Rutvij' AND `lang`='python')
BEGIN
INSERT INTO `user` VALUES ('Rutvij', 'python', 25)
END
ELSE
BEGIN
UPDATE user SET `name`='Kanzaria' WHERE `name`='Rutvij'
END
I am trying the above query in phpmyadmin sql area. I am using xampp. It is throwing error stating that
1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use
near 'IF EXISTS(SELECT * FROM user)
SELECT name FROM use' at line 1
I have also tried the below code
IF NOT EXISTS(SELECT * FROM user WHERE name='Rutvij' AND lang='python')
BEGIN
INSERT INTO user VALUES ('Rutvij', 'python', 25)
END
ELSE
BEGIN
INSERT INTO user VALUES ('Kanzaria', 'python', 25)
END
Struggling for so long. Kindly help. Thanks!!
MySQL doesn't permit if logic, unless you are in a programming block (stored procedure, trigger, or function).
Fortunately, you can do the same with WHERE logic:
INSERT INTO user
SELECT 'Rutvij', 'python', 25
FROM DUAL
WHERE NOT EXISTS (SELECT 1 FROM user WHERE name = 'Rutvij' AND lang = 'python')
UNION ALL
SELECT 'Kanzaria', 'python', 25
FROM DUAL
WHERE EXISTS (SELECT 1 FROM user WHERE name = 'Rutvij' AND lang = 'python');
MySQL should process the SELECT before the INSERT, so only one row should be inserted.
Or, you can do this as two INSERTs but in the opposite order:
INSERT INTO user
SELECT 'Kanzaria', 'python', 25
FROM DUAL
WHERE EXISTS (SELECT 1 FROM user WHERE name = 'Rutvij' AND lang = 'python');
INSERT INTO user
SELECT 'Rutvij', 'python', 25
FROM DUAL
WHERE NOT EXISTS (SELECT 1 FROM user WHERE name = 'Rutvij' AND lang = 'python');
This is not a query, this would be an sql script with control flow logic, which is not allowed in mysql outside stored programs (procedures, functions, triggers). Even if you encapsulated the above code into a stored procedure it would not work because exist / not exists can only be used in subqueries.
I would do the following:
Create a stored procedure
Declare an integer variable
Using select into fetch the count of rows where name='Rutvij' AND lang='python' into your variable.
Use the if statement to do the insertion based on the number of records.

Correct MySQL Syntax for this trigger?

Trying to create a simple trigger that after a table update, it checks if a count (InvalidLogins) is over 4, and if so, sets the field 'LockedOut' to 1. Below is how I am attempting to create the trigger, this is my first trigger for MySQL though and I can't get it to work.
CREATE TRIGGER update_user_lockout after update ON members
FOR EACH ROW SET LockedOut = 1 where invalidLogins < 4
The error that MySQL returns is:
Error Code: 1064. You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '.LockedOut = 1 where invalidLogins < 4' at line 2
delimiter |
CREATE TRIGGER update_user_lockout before update ON members
FOR EACH ROW
IF NEW.invalidLogins < 4 THEN
SET NEW.LockedOut = 1;
END IF;
END
|
delimiter ;
The current record can be referenced with NEW
Choose another delimiter than ;. Otherwise the trigger definition will end at the first ; which is too early
Use a before trigger to change values before updating