MySQL Insert trigger not working: Count not matched - mysql

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.

Related

How to write a MySql insert trigger based on inserted data value?

I want to wrote a trigger for table users. when new user added, if his Title is IT related, then create a record in IT contact list table.
So I wrote below trigger.
CREATE TRIGGER `test1` INSERT ON `Users` FOR EACH ROW BEGIN
IF INSTR(Title,'IT') > 0
THEN
INSERT INTO IT_contact_list (name,title) value (username,Title);
END IF;
END;
It has error 'unknow column Title in the field list' But it did exist in table Users and IT_contact_list.So what's the issue?Thx.
Fixed and tested in workbench. Try this:
delimiter //
drop trigger if exists test1 //
CREATE TRIGGER `test1` AFTER INSERT ON `Users` FOR EACH ROW BEGIN
IF INSTR(new.Title,'IT') > 0
THEN
INSERT INTO IT_contact_list (name,title) value (new.username,new.Title);
END IF;
END//
insert into Users values('john','HardwareIT');
insert into Users values('bill','Hardware engineer');
select * From IT_contact_list;
--result set:
john HardwareIT

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.

Make a trigger that copies new data from table to another table after insert

I have this code for the trigger:
CREATE TRIGGER `insert_after` AFTER INSERT ON `hyk50_0001`
FOR EACH ROW BEGIN
INSERT INTO hyk50_0001_copy(Fecha)
SELECT Fecha FROM hyk50_0001
END;
but doesn't work, it says a syntax error but I didn't see it
I'm using Navicat. And hyk50_0001_copy it's a identical copy of hyk50_0001.
The target is take the new row of hyk50_0001 and INSERT in hyk50_0001_copy
I want put all the database, but if it doesn't work with only a value I can't progress.
I don't see a syntax error, but I assume you intend:
DELIMITER $$
CREATE TRIGGER `insert_after` AFTER INSERT ON `hyk50_0001`
FOR EACH ROW
BEGIN
INSERT INTO hyk50_0001_copy(Fecha)
VALUES (new.Fecha)
END;$$
DELIMITER ;
That is, you probably want to insert only the row that was just created.

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.

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

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 ;