MySQL syntax error while creating a UPDATE Trigger - mysql

I am trying to create a trigger on updating the salary field of this table.
customers(ID, Name, Age, Address, Salary) from this site SQL Trigger
While creating it shows the following error
MySQL said:
#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'OR INSERT OR UPDATE ON customers
FOR EACH ROW
WHEN (NEW.ID > 0)
DECLARE
' at line 2
Here is the code snippet:
BEFORE DELETE OR INSERT OR UPDATE ON customers
FOR EACH ROW
WHEN (NEW.ID > 0)
DECLARE
sal_diff number;
BEGIN `enter code here`
sal_diff := :NEW.salary - :OLD.salary;
dbms_output.put_line('Old salary: ' || :OLD.salary);
dbms_output.put_line('New salary: ' || :NEW.salary);
dbms_output.put_line('Salary difference: ' || sal_diff);
END
Note that, I'm using phpMyAdmin. Does dbms_ouput.put_line() works there?

As said in the comments, you are using Oracle syntax, it can't work on MySQL.
Then, there's no reason to have this trigger on DELETE and INSERT because your aim is to compute the salary difference on a specific row before and after it get updated. You can't have a value NEW on DELETE, and you can't have a value OLD on INSERT.
Thus your computation is only meaningful on UPDATE.
Here's the correct MySQL syntax (I am assuming that you have a column sal_diff on your table)
DELIMITER $$
CREATE TRIGGER `update_sal_diff`
BEFORE UPDATE ON `customers `
FOR EACH ROW
BEGIN
SET NEW.sal_diff = NEW.salary - OLD.salary;
END $$
DELIMITER ;
If this is not what you are trying to achieve, edit your question and add clear requirements

Related

How to decrement a value in another table via a trigger? (MySQL)

I want my trigger to decrement the quantity by 1 in the table "quantity" when a new row is added to "rented_equipment_log" where date_returned has a value of NULL.
A basic synopsis of the database is that there is an equipment table with columns; model, maker, type and quantity. (E.g. 1001, 'Jackson', 'oar', 10)
The table rented_equipment_log has the columns; member_id, model, type, date_taken, date_returned. (E.g. 17225663, 1001, oar, 2018-11-26, 2018-11-27)
So when a member takes out a piece of equipment but has not yet returned it (date_returned is null), the quantity of the same model decrements by 1 in the table equipment.
However I'm getting a syntax error. I've looked at other questions similar to this and still can't figure out what the error is.
Here's the trigger:
delimiter //
CREATE TRIGGER UpdateQuantity
AFTER INSERT ON rented_equipment_log
FOR EACH ROW BEGIN
DECLARE q integer;
SELECT quantity INTO q FROM equipment WHERE model = NEW.model;
IF (date_returned IS NULL) THEN
UPDATE equipment
SET quantity = q -1 WHERE model = NEW.model
END IF;
END;//
And here's the 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
'END IF;
END' at line 9
I'm not sure why you are getting an error there. But your query is more complicated than necessary:
delimiter //
CREATE TRIGGER UpdateQuantity
AFTER INSERT ON rented_equipment_log
FOR EACH ROW
BEGIN
UPDATE equipment e
SET e.quantity = e.quantity - 1
WHERE e.model = NEW.model and ?.date_returned IS NULL;
END;//
I'm not sure if date_returned should be new.date_returned or e.date_returned. I don't even understand that condition. I would be expecting e.equipment > 0.

MYSQL Trigger to update different table based on inserted value

I need to update oc_product_attribute with a value obtained from an inserted row in vehicles. How can I get this specific value? (xxxCOLUMN_VALUExxx)
CREATE TRIGGER vehicle_attributes AFTER INSERT ON vehicles
FOR EACH ROW
BEGIN
UPDATE oc_product_attribute SET oc_product_attribute.vehicle_name =
(SELECT CONCAT(vehicles.make_name, ' ', vehicles.model_name, ' ',
vehicles.chassis) FROM vehicles WHERE vehicles.id = xxxCOLUMN_VALUExxx)
WHERE oc_product_attribute.id = xxxCOLUMN_VALUExxx
END
Even simplified below: I still get an error in MYSQL:
CREATE TRIGGER vehicle_attributes AFTER INSERT ON vehicles
FOR EACH ROW
BEGIN
UPDATE oc_product_attribute
SET oc_product_attribute.vehicle_name =
CONCAT(NEW.make_name,' ',NEW.model_name,' ',NEW.chassis);
END;
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 7
And Simplified further:
CREATE TRIGGER vehicle_attributes AFTER INSERT ON vehicles
FOR EACH ROW
BEGIN
UPDATE oc_product_attribute
SET oc_product_attribute.vehicle_name = 'test' WHERE 1
END;
I still get the same error - Could it be MYSQL doesn't have triggers enabled?
I guess you need to update oc_product_attribute like this:
CREATE TRIGGER vehicle_attributes AFTER INSERT ON vehicles
FOR EACH ROW
BEGIN
UPDATE oc_product_attribute
SET oc_product_attribute.vehicle_name =
CONCAT(NEW.make_name,' ',NEW.model_name,' ',NEW.chassis)
WHERE oc_product_attribute.id = NEW.id;
END
Although I am no sure if oc_product_attribute.id is holding the vehicles.id. However, the important poart is that in an INSERT AFTER TRIGGER you can access the values of the inserted row by NEW.columnName.
N.B. In update triggers you also can access the value before the change with OLD.columnName

MySQL TRIGGER ON INSERT issue

i'm going mad with a MySQL trigger. MySQL says there's an error in the code but i can't figure out what's the problem.
this is the TRIGGER
CREATE TRIGGER UPDATE_COUNTRY
AFTER INSERT
ON `dog`FOR EACH ROW
BEGIN
IF (NEW.export=1)
THEN
IF (NEW.year > (SELECT MAX(`updated_year`) FROM `dog` WHERE `code`=NEW.origin))
THEN
UPDATE `dog` SET `updated_year`= NEW.year, `updated_month`= NEW.month WHERE `code`= NEW.origin;
ELSEIF (NEW.year = (SELECT MAX(`updated_year`) FROM `dog` WHERE `code`=NEW.origine)
AND NEW.month > (SELECT MAX(`updated_month`) FROM `dog` WHERE `code`=NEW.origine AND `updated_year`=NEW.year))
THEN UPDATE `dog` SET `updated_month`=NEW.month WHERE `code`=NEW.origin;
ELSE
RETURN NEW;
END IF;
END IF;
RETURN NEW;
END;
My SQL says
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 13
Thank you all!
Your trigger is:
AFTER INSERT ON `dog`FOR EACH ROW
And your line 13 is
UPDATE `dog` SET `updated_year`= NEW.year, `updated_month`= NEW.month WHERE `code`= NEW.origin;
This kinda suck, but you cannot update/insert/delete with a trigger on the same table that you are currently inserting/updating/deleting data because the table is locked.
This is a limitation from MySQL (yeah and not all SGDB actually have this limitation...(
You will need to work around a different way if you want to update your "dog" table. You could use a store procedure to do both the insert and the update.

SQL Trigger solve

CREATE TRIGGER Print
Before UPDATE ON employ
FOR EACH ROW
WHEN (NEW.Employe_ID>0)
DECLARE
salary int;
BEGIN
salary:= :NEW.salary-:OLD.salary;
dbms_output.put('Old salary:'||:OLD.salary);
dbms_output.put('New salary:'||:NEW.salary);
dbms_output.put_line('Difference'||salary);
END;
/
Shows
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 'WHEN (NEW.Employe_ID>0)
DECLARE
salary int' at line 4
I have a table name employe which has 4 columns Employe_ID, E_name,
Department_ID,
salary
What is the problem with this sql and how can I solve it?
I don't get why you added the condition on employee_id > 0, but in case you actually need it
delimiter //
CREATE OR REPLACE TRIGGER print BEFORE UPDATE ON employee
FOR EACH ROW
IF ( NEW.employe_id > 0 ) THEN
SELECT NEW.salary - OLD.salary INTO #delta;
END IF;
//
delimiter ;
Insert and update your data, then read #delta
insert into employee select 1,'Me',1,2400;
update employee set salary = 2800 where id = 1;
Select #delta;
I don't think it's possible to automatically output #delta to the screen as it would be on Oracle using dbms_output.put_line().

Trigger does not work

Good day all,
I am trying to keep some historical test data if a certain field changes after every test. I have the data in a table called gypsum and wish to track changes in a table called history_gypsum.
The trigger I have created shows errors which I do not understand,your help will be appreciated.Many thanks.G Styles
DELIMITER $$
CREATE TRIGGER `pattesti_testing`.`history_gypsum`
BEFORE UPDATE
ON `pattesti_testing`.`gypsum`
FOR EACH ROW
BEGIN
IF OLD.leakage != NEW.leakage
THEN
INSERT INTO history_gypsum
(
id,
register_number,
equipment_description,
make,
class,
location,
continuity_value,
insulation_value,
leakage,
polarity,
test_frequency,
date_of_test,
comments,
passed,
tester_initials,
next_test,
current,
)
VALUES
(
NEW.id,
NEW.register_number,
NEW.equipment_description,
NEW.make,
NEW.class,
NEW.location,
NEW.continuity_value,
NEW.insulation_value,
NEW.leakage,
NEW.polarity,
NEW.test_frequency,
NEW.date_of_test,
NEW.comments,
NEW.passed,
NEW.tester_initials,
NEW.next_test,
NEW.current,
);
END IF ;
END;
$$
DELIMITER ;
ERRORS
Query : CREATE TRIGGER `pattesti_testing`.`history_gypsum` BEFORE UPDATE ON `pattesti_testing`.`gypsum` FOR EACH ROW BEGIN ...
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 ')
VALUES
(
NEW.id,
NEW.register_n' at line 26
Execution Time : 00:00:00:000
Transfer Time : 00:00:00:000
Total Time : 00:00:00:000
Remove the comma after current
current, /*this comma is too much*/
)
VALUES