Cannot create trigger in mysql at phpmyadmin - mysql

I'm trying to create an ID by combining fields after a row has been inserted into the database in phpmyadmin. I get an error on line 3 of the following trigger statement:
DROP TRIGGER IF EXISTS `scanID2`;
CREATE DEFINER=`root`#`localhost`
TRIGGER `scanID2`
AFTER INSERT ON db_name.`scan_data`
FOR EACH ROW
BEGIN
UPDATE db_name.scan_data
SET #data1 = concat(RIGHT(scanContent,2), RIGHT(operatorID,2), RIGHT(deviceID,2), RIGHT (scanDate, '-', ''), REPLACE (scanTime, ':', ''));
INSERT INTO db_name.scan_data(scanID) VALUES (#data1);
END
//
The error is:
#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 'CREATE DEFINER=`root`#`localhost`
TRIGGER `scanID2`
AFTER INSERT ON db_name' at line 2
OK, so I figured how to get the query executed. The trigger has been created but I don't think it does what it's supposed to do. The records don't get updated. Here's the new code:
BEGIN
SET #data1 = concat(RIGHT(scanContent,2), RIGHT(operatorID,2), RIGHT(deviceID,2), REPLACE (scanDate, '-', ''), REPLACE (scanTime, ':', ''));
INSERT INTO vacseen1_connect.scan_data(scanID) VALUES (#data1);
END

There are several things to consider:
Use BEFORE event for the trigger instead of AFTER. This way you can set the value of scanID the same time a row is being inserted.
Use NEW keyword to access fields of a row being inserted.
Since it's one statement trigger now you don't need to change delimiter and use BEGIN ... END block.
You most likely meant to use REPLACE for scanDate.
Try
CREATE TRIGGER scanID2
BEFORE INSERT ON scan_data
FOR EACH ROW
SET NEW.scanID = CONCAT(
RIGHT(NEW.scanContent, 2),
RIGHT(NEW.operatorID, 2),
RIGHT(NEW.deviceID, 2),
REPLACE(NEW.scanDate, '-', ''),
REPLACE (NEW.scanTime, ':', '')
);
Here is a SQLFiddle demo

Related

How can i add trigger event in mysql?

I have trigger statement but its not working
CREATE TRIGGER tg_table3_insert BEFORE INSERT ON rd_stonepanel_pricing
FOR EACH ROW BEGIN
INSERT INTO table3_seq VALUES (NULL);
SET NEW.option_id = CONCAT('R', LPAD(LAST_INSERT_ID(), 3, '0'));
But it shows an error like 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 3
I am assuming you are trying to update a sequence when an insert is done. See this(http://en.latindevelopers.com/ivancp/2012/custom-auto-increment-values/).
The below code snippet may work.
CREATE TRIGGER tg_table3_insert BEFORE INSERT ON rd_stonepanel_pricing
FOR EACH ROW
BEGIN
INSERT INTO table3_seq SET option_id = CONCAT('R', LPAD(LAST_INSERT_ID(), 3, '0');
END;
Remove the semicolon at line 3 if it is a single statement and add end. Also, the above code will only work if the table "table3_seq" has one column.

MySQL syntax error while creating a UPDATE Trigger

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

Syntax error on creating trigger for concat two columns value

I'm trying to create a trigger by concat two fields
CREATE TRIGGER format_goods_sn
BEFORE INSERT ON tp_goods
FOR EACH ROW
BEGIN
SET NEW.goods_sn = CONCAT('TP', LPAD(NEW.goods_id, 7, '0'))
END
but I got alert said SET NEW.goods_sn = CONCAT('TP', LPAD(NEW.goods_id, 7, '0')) has syntax error?
I found that just use single line format (remove 'BEGIN' and 'END') would resolve this issue:
CREATE TRIGGER format_goods_sn
BEFORE INSERT ON tp_goods
FOR EACH ROW
BEGIN
SET NEW.goods_sn = CONCAT('TP', LPAD(NEW.goods_id, 7, '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

Trigger in MySQL

I am trying to create a trigger wherein it will insert a new row to a ADMIN_EMAIL_DOMAINS table.
CREATE TRIGGER email_domain_insert AFTER INSERT ON USERS
FOR EACH ROW
BEGIN
IF NOT EXISTS (SELECT * FROM (SELECT DISTINCT SUBSTRING_INDEX(USR_EMAIL, '#', -1) as domain FROM USERS) as a WHERE a.domain = SUBSTRING_INDEX(NEW.USR_EMAIL, '#', -1)) THEN
INSERT INTO ADMIN_EMAIL_DOMAINS (DOMAIN_NAME, VISIBLE) VALUES (SUBSTRING_INDEX(NEW.USR_EMAIL, '#', -1), 1);
END IF;
END //
What I would like to do is everytime I create a new user, i would check its email extension and then insert it to ADMIN_EMAIL_DOMAINS table if that DOMAIN_NAME is not yet existing.
My problem is it has an SQL error when i tried to execute it
#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 5
Am I missing something?
I could suggest you another way - make field DOMAIN_NAME as unique and try to insert new domains into the table ADMIN_EMAIL_DOMAINS with IGNORE option, e.g. -
DELIMITER $$
CREATE TRIGGER email_domain_insert
AFTER INSERT
ON users
FOR EACH ROW
BEGIN
INSERT IGNORE INTO ADMIN_EMAIL_DOMAINS (DOMAIN_NAME, VISIBLE) VALUES (SUBSTRING_INDEX(NEW.USR_EMAIL, '#', -1), 1);
END
DELIMITER ;