I am trying to create in MySQL a 'custom' unique ID based on existing primary key (auto-incremented) 'id' field
delimiter //
CREATE TRIGGER update_id AFTER INSERT ON test
BEGIN
UPDATE test SET PN="PN-"+NEW.id;
END;
//
delimiter ;
I am using PhpMyAdmin and I really don't know if it's a UI problem (I encountered some problems in the past while creating triggers in PhpMyAdmin) or I really do something wrong.
What I need is a custom PN field that is automatically updated when insert new records in table, based on some text prefix "PN-"
id PN other fields
-------------------------
...
...
...
1253 PN-1253
1254 PN-1254
#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 'BEGIN
UPDATE test SET PN="PN-"+NEW.id;
END' at line 2
I read in some other StackOverflow post that this will be impossible (to update) on the same table AFTER insert.
There is a solution with this? Thanks.
Get the new value and add it before insert
delimiter //
CREATE TRIGGER update_id BEFORE INSERT ON test
BEGIN
SET #pn:= ( SELECT AUTO_INCREMENT
FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_NAME='test'
AND TABLE_SCHEMA=DATABASE() );
SET new.PN= CONCAT('PN-',#pn);
END;
//
delimiter ;
Here
Related
I have a problem with a stored procedure, both in syntax and logic. I got the error below when trying it on sqlfiddle.com.
Also I'm not sure if the logic is right. Is there anyway I can check by entering the ID and the query will return a table which row that contains the ID has been deleted?
create a procedure named delete_id which deletes the row that goes by the ID
CREATE PROCEDURE DELETE_ID (ID_INPUT IN INT)
AS
BEGIN
DELETE FROM TABLE ALBUM WHERE ID=ID_INPUT;
END;
Expected result: Enter an Id and SQL will delete the row which contains the ID
Actual Result:
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 'TABLE ALBUM WHERE ID=ID_INPUT' at line 3
You need to add "DELIMITER //" at the start on the code and "//" at the end. So your code should look like
DELIMITER //
CREATE PROCEDURE `DELETE_ID`(IN `ID_INPUT` INT)
BEGIN
DELETE FROM `ALBUM` WHERE `ID` = ID_INPUT;
END //
I have this code here:
CREATE TRIGGER testTrigger
AFTER INSERT ON users
BEGIN
DECLARE #uid VARCHAR(60)
SET #uid = (SELECT userid FROM inserted)
INSERT INTO user_locations (id,uid,lat,lng) VALUES (0,#uid,5.0,5.0)
END;
The idea is to insert generated user id into other table alongside some other data as soon as it hits the first 'users' table but phpMyAdmin gives this 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
BEGIN
DECLARE #uid VARCHAR(60)
SET #uid = (SELECT userid FROM inserted)
at line 3
Can someone clarify why this trigger is bad?
I see four problems:
You have to use DELIMITERs so that your able to finish the commands with a semicolon as usual.
FOR EACH ROW is missing.
Use new.uid to access the recently inserted uid.
I'd also suggest using procedure variables instead of session-specific user-defined #variables, the latter ones being loosely typed and not declared as you've done.
But you don't even have to declare a variable. If you don't use phpMyAdmin:
DELIMITER //
CREATE TRIGGER testTrigger
AFTER INSERT ON users FOR EACH ROW
BEGIN
INSERT INTO user_locations (id,uid,lat,lng) VALUES (0,new.uid,5.0,5.0);
END//
DELIMITER ;
Check this answer about delimiter and the MySQL 5.7 docs on triggers and this answer about variables.
Edit, I overread you're using phpMyAdmin:
I don't use phpMyAdmin. But you can (stolen from here)
In phpMyAdmin, select the database that you want to work with.
Go to the SQL tab at the top of the page.
In the "Run SQL query/queries on database" form, change the Delimiter to $$. (Located in a small box at the bottom of the form)
Enter your SQL trigger into the main dialog box on the form. The correct syntax is as follows:
CREATE TRIGGER testTrigger
AFTER INSERT ON users FOR EACH ROW
BEGIN
INSERT INTO user_locations (id,uid,lat,lng) VALUES (0,new.uid,5.0,5.0);
END;$$
Hit "GO" with Super privilege.
I would like to create a trigger to this example table when I insert, update or delete any row into the table. I have never worked with triggers before so this will be my first one.
I have this code so far:
DROP TRIGGER auditemployees
CREATE TRIGGER auditemployees AFTER INSERT ON employees
FOR EACH ROW
BEGIN
INSERT INTO employees_trigger select * from employees where emp_no = NEW.emp_no;
END;
The problem now is:
I would like to copy the affected row into another table in this case employees_trigger, but in order to copy the whole row and to pass the values all I can think of is using variables for each field which in my opinion sounds inefficient since if I had 50 fields I will have to use 50 variables and then insert all those values into the new table.
Is there any better and efficient way to do this?
Also I get the error:
> ERROR 1064 (42000): 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 TRIGGER auditemployees AFTER INSERT ON
> employees
Every statement needs to be terminated -- there is no ; at the end of the DROP statement.
You are missing a DELIMITER statement, and you are not using a different delimiter for the CREATE.
What's wrong with OLD.* for the 50 variables?
It's my first time that I try to use trigger on mysql (and generally I don't use mysql so much).
The version of mysql that I'm using is 5.5.40 and the code I'm using to create the trigger is:
CREATE TRIGGER updateTrigger
AFTER UPDATE ON tab1
FOR EACH ROW
BEGIN
UPDATE tab2
SET field1 = NEW.field1
WHERE field2 = NEW.field2;
END;
Where field1 and field2 are two fields in both tables (tab1 and tab2) both archer(120).
When I try to execute this code I receive an 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 7
(line 7 is near "WHEN" keyword).
I checked several answer on stack overflow and other web sites, I tried with and without delimiters, and I still not able to create a trigger.
What's my syntax error?
Is there some way to have some more accurate mysql debugging? Actually mysql answer (like I just posted) is just "there is an error (1064)" but it doesn't let me know what is wrong.
you have to change the delimiter to use it in your sql statement :
DELIMITER //
CREATE TRIGGER updateTrigger
AFTER UPDATE ON tab1
FOR EACH ROW
BEGIN
UPDATE tab2
SET field1 = NEW.field1
WHERE field2 = NEW.field2;
END//
DELIMITER ;
I have two tables
1. Tag
2. Triger_testing
Tag desc
id int, is_active (tinyint)
Trigger_Testing Desc
tag_id (int), is_active(tinyint)
I want to create a trigger on tag table update which update trigger_testing table. So if tag.is_active is set to 0 the trigger must fire and update trigger_testing table and set trigger_testing.is_active=0 where trigger_testing.tag_id=tag.id.
I tried to create a trigger in MYSQL but getting syntax exception. Can someone help me out in resolving that issue.
Here is the code : -
CREATE TRIGGER update_trigger_testing AFTER UPDATE ON tag
FOR EACH ROW
BEGIN
IF NEW.is_active=0 THEN
UPDATE trigger_testing SET is_Active=0 WHERE tag_id=NEW.id
END IF
END$$
DELIMITER;
The error I am getting 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 'END IF
END$$
DELIMITER' at line 6
CREATE TRIGGER update_trigger_testing AFTER UPDATE ON tag
FOR EACH ROW
BEGIN
IF NEW.is_active=0 THEN
UPDATE trigger_testing SET is_Active=0 WHERE tag_id=NEW.id;
END IF;
END;