MySQL Trigger Creation. What's wrong with my trigger? - mysql

I'm trying to create a trigger, however I keep getting back a syntax error.
Here's the statement:
DELIMITER $$
CREATE TRIGGER `swtalentbank`.`after_candidate_insert`
AFTER INSERT ON `Candidates` FOR EACH ROW
BEGIN
INSERT INTO useradmin (username, talent)
VALUES (NEW.email, 1);
UPDATE `Candidates` SET UserID = useradmin.userid where useradmin.username = NEW.email;
END
DELIMITER ;
I have a registration form on my site. When a person registers it populates the Candidates table with their profile information.
In the Candidates table, there are various fields, two of them being 'email' and 'UserID'.
UserID is also the PK in 'useradmin', so I'm linking the two up.
So when a user registers, I need to insert a record into 'useradmin' with the email address that's just been used to register, and then update the 'Candidates' table, with UserID that's just been created in 'useradmin'.
I hope this makes sense?
NB. I am changing the delimiter before running the statement.

Besides properly using DELIMITER when creating a trigger you have at least two fundamental issues with your current code:
In MySQL you can't use issue a DML statement (in your case UPDATE) against a table (candidates) on which you defined a trigger (also candidates). Your only option is to use BEFORE trigger and set a value of userid column of a row being inserted to a proper value.
You can't arbitrarily reference a column (useradmin.userid) of a table out of the context like you did in your UPDATE. You didn't joined useradmin table or used it in a subquery.
That being said and assuming that userid in useradmin table is an auto_increment column your trigger might look like this
DELIMITER $$
CREATE TRIGGER after_candidate_insert
BEFORE INSERT ON candidates
FOR EACH ROW
BEGIN
INSERT INTO useradmin (`username`, `talent`) VALUES (NEW.email, 1);
SET NEW.userid = LAST_INSERT_ID();
END$$
DELIMITER ;
Here is SQLFiddle demo

You should use semicolon after end your insert query.
You can use INSERT ... ON DUPLICATE KEY UPDATE syntax for your purpose

try out this...
DELIMITER $$
CREATE TRIGGER `swtalentbank`.`after_candidate_insert`
AFTER INSERT ON `Candidates` FOR EACH ROW
BEGIN
INSERT INTO useradmin (username, talent)
VALUES (NEW.email, 1);
UPDATE `Candidates` SET UserID = useradmin.userid where useradmin.username = NEW.email;
END $$
DELIMITER ;

Related

MySQL Insert trigger not working: Count not matched

Creating a trigger is not working as expected, whenever I try to insert data into master table it give me error that count does't match. I am unable to identify where I'm doing wrong.
I have attached error image please look for further demonstration
DELIMITER $$
DROP TRIGGER IF EXISTS `trg_apl_b_info_after_insert`
CREATE
TRIGGER `trg_apl_b_info_after_insert` AFTER INSERT ON `tbl_appli_basic_info`
FOR EACH ROW BEGIN
DECLARE vApplicant VARCHAR(256);
-- Find appli_basic_info_id & apli_reg_no of Applicant performing the INSERT into table
SELECT USER() INTO vApplicant;
-- Insert record into tbl_appli_basic_info_after_insert table
INSERT INTO tbl_appli_basic_info_after_insert
( appli_basic_info_id,
apli_reg_no,
full_name,
after_insert_datetime)
VALUES
( NEW.appli_basic_info_id,
NEW.apli_reg_no,
NEW.full_name,
SYSDATE(),
vApplicant );
END;
$$
DELIMITER ;
Error in phpMyAdmin
Your insert statement lists 4 fields however you provided 5 values. Hence count not matched.

Create My sql trigger for After insert , update another table column using inserted record [duplicate]

What is the error in the following code. I am executing in mysql
CREATE TRIGGER tg_order_insert
BEFORE INSERT
ON `order` FOR EACH ROW
BEGIN
INSERT INTO `grocery`.`order_seqid` VALUE(NULL);
SET NEW.order_id = CONCAT('#GNC', LPAD(LAST_INSERT_ID(),3,'0'));
END;
Grocery is the database and order_seqid and order are 2 table.
order_seqid is a table with only 1 attribute if type int and auto increment.
Am trying to put a prefix on the id which we insert into order table.
I am getting 2 errors in INSERT INTO..... and END; line
Did you declare a delimiter before your trigger definition? Something like
DELIMITER //
CREATE TRIGGER tg_order_insert
BEFORE INSERT
ON `order` FOR EACH ROW
BEGIN
INSERT INTO `grocery`.`order_seqid` VALUE(NULL);
SET NEW.order_id = CONCAT('#GNC', LPAD(LAST_INSERT_ID(),3,'0'));
END
//
Because if you don't, then MySQL thinks you're trying to end your trigger definition when it sees that first ; and calls syntax error.

Using MySQL Database Triggers

I have three tables: users, account and accountinfo and I am trying to make a trigger that will add the id from users to the UserID column in the account table. Here is what I tried:
CREATE TRIGGER catchUser BEFORE INSERT ON defaultdatabase.users
FOR EACH ROW
BEGIN
INSERT INTO defaultdatabase.account(UserID) VALUES (new.id);
END
However, I get an error right after my INSERT statement that says,
Syntax Error: insert 'semicolon'
Why am I getting this error is I have the semicolon or is my trigger just wrong?
I'm using MySQL 5.6 if that makes any difference as well.
You need to specify the delimiter:
delimiter //
CREATE TRIGGER catchUser BEFORE INSERT ON defaultdatabase.users
FOR EACH ROW
BEGIN
INSERT INTO defaultdatabase.account(UserID) VALUES (new.id);
END; //
delimiter ;
Try this:
CREATE TRIGGER catchUser BEFORE INSERT ON defaultdatabase.users
FOR EACH ROW
BEGIN
INSERT INTO defaultdatabase.account(UserID) VALUES (new.id)
END;
Please insert a semicolon after END.

Incorrect syntax in trigger

I have two tables called account and transaction. I need to insert data to transaction automatically when account inserts data. I created this trigger in MySQL. It gives error 1064 (Syntax error). What is the problem?
CREATE TRIGGER upTransaction AFTER INSERT ON account FOR EACH ROW
BEGIN
INSERT INTO transaction VALUES (2,account.openDate,CURTIME(),
account.deposit,account.accNo,
"Teller","Cash","Deposit");
END
MySQL gets confused with semicolons inside the BEGIN END block, so you must use DELIMITER to temporarily force MySQL to use a different delimiter.
Documentation: http://dev.mysql.com/doc/refman/5.5/en/create-trigger.html
DELIMITER //
CREATE TRIGGER upTransaction AFTER INSERT ON account FOR EACH ROW
BEGIN
INSERT INTO transaction VALUES (2,account.openDate,CURTIME(),account.deposit,account.accNo,"Teller","Cash","Deposit");
END//
DELIMITER ;
You probably need to replace:
INSERT INTO transaction VALUES (2,account.openDate,CURTIME(),account.deposit,account.accNo,"Teller","Cash","Deposit");
with:
INSERT INTO transaction VALUES (2,NEW.openDate,CURTIME(),NEW.deposit,NEW.accNo,"Teller","Cash","Deposit");
Here is you original trigger
CREATE TRIGGER upTransaction AFTER INSERT ON account FOR EACH ROW
BEGIN
INSERT INTO transaction VALUES (2,account.openDate,CURTIME(),
account.deposit,account.accNo,
"Teller","Cash","Deposit");
END
Replace account. with NEW. (optional : replace " with ')
DELIMITER $$
CREATE TRIGGER upTransaction AFTER INSERT ON account FOR EACH ROW
BEGIN
INSERT INTO transaction VALUES (2,NEW.openDate,CURTIME(),
NEW.deposit,NEW.accNo,
'Teller','Cash','Deposit');
END $$
DELIMITER ;
CREATE
TRIGGER `event_name` AFTER INSERT
ON `database`.`table`
FOR EACH ROW BEGIN
-- trigger body
-- this code is applied to every
-- inserted/updated/deleted row
END;
Seems like you're missing a table name after 'account'. (edit: or a database name before account, I'm not certain as to what account pertains to)

Trigger syntax error

I have two tables with the following column names.
users
ui | email | pass
user_profiles
_email | qty | lvl
I am attempting to make sure that everytime a new user is added to the users table that the user_profiles table creates a new row for the new user. However it fails. After reading through many sample trigger statements I can't seem to find the error.
CREATE TRIGGER updatUserProfilesTBL AFTER INSERT ON users
BEGIN
INSERT INTO user_profiles (_email)
VALUES (new.email)
END;
Looks like you are missing the FOR EACH ROW. Additionally, you will need to alter the DELIMITER if executing this through the MySQL command line and add a ; after the insert statement.
DELIMITER $$
CREATE TRIGGER updatUserProfilesTBL AFTER INSERT ON users
FOR EACH ROW BEGIN
INSERT INTO user_profiles (_email)
VALUES (new.email);
END$$
DELIMITER ;
According to the CREATE TRIGGER spec, it is not optional.