Mysql syntax error in delete trigger - mysql

Could anyone tell me that what is the syntax error in this trigger
DELIMITER |
CREATE TRIGGER User_XEntity_Before_Delete
BEFORE DELETE
ON UserXEntity FOR EACH ROW
BEGIN
-- Insert record into Delete_UserXEntity table
INSERT INTO Delete_UserXEntity
( DeletedUserXEntityId,
UserId,
CreatedAt)
VALUES
( OLD.Id,
OLD.UserId,
NOW() );
END;
|
DELIMITER ;

I've got the solution. Actually I was entering this code in phpmyadmin Trigger window, where it asks about table name, time and event already. So we only need to write the trigger action code in that window. I was writing the whole trigger code and that's why I was giving me a syntax error.
We only need to write the following code in PHPMYADMIN add new trigger windows:
INSERT INTO Delete_UserXEntity
( DeletedUserXEntityId,
UserId,
CreatedAt)
VALUES
( OLD.Id,
OLD.UserId,
NOW() );

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 trigger in MySQL to log changes in database

UPDATE!
The Delimiter thing actually solved my original problem. But I now ended up with a new Error. I'll describe it below along with the implementet changes that solved the first problem.
I am trying to write a trigger that will log changes in the database to a seperate table. But I keep getting the same error. I have looked at the MYSQL documentation and searched this forum and found a lot of helpfull answers. Trouble is that I now have a piece of SQL code that looks exactly like the answers given in this forum but I still get an error.
The trigger I am trying to use is:
CREATE TRIGGER logInsert AFTER INSERT ON test_table
FOR EACH ROW
BEGIN
INSERT INTO datalog (action, id, timestamp, data1, data2)
VALUES ('insert', NEW.id, NOW(), NEW.data1, NEW.data2);
END;
The error message I get back is:
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
I have tried using " instead of single quotes.
someone surgested in on of the answers here that you should use backticks, so tried that as well.
No matter what, the error message is exactly the same.
A friendly soul here posted a fix for my original error. I needed to add DELIMITER to the statement so that it should look like this:
DELIMITER $$
CREATE TRIGGER logInsert AFTER INSERT ON test_table
FOR EACH ROW
BEGIN
INSERT INTO datalog (action, id, timestamp, data1, data2)
VALUES ('insert', NEW.id, NOW(), NEW.data1, NEW.data2);
END$$
DELIMITER ;
This change solved the original error, but also let to a new error.
The new error is:
Unknown Coloumn 'id' in table NEW
This is the table I'm trying to write to:
CREATE TABLE datalog (
id INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
timestamp TIMESTAMP,
data1 VARCHAR(255) NOT NULL,
data2 DECIMAL(5,2) NOT NULL
);
Hope someone here has an idea of what is wrong.
Kind regards,
Jonas
Use DELIMITER for the trigger
Like
DELIMITER $$
CREATE TRIGGER logInsert AFTER INSERT ON test_table
FOR EACH ROW
BEGIN
INSERT INTO datalog (action, id, timestamp, data1, data2)
VALUES ('insert', NEW.id, NOW(), NEW.data1, NEW.data2);
END$$
DELIMITER ;

Denied insert statement in before update trigger mysql

I'm trying to create a history table. In the case of an update I want to be able to select the "old" data and move it to the delta table. Therefore i created the following trigger:
delimiter $$
drop trigger if exists entity_url_before_update_to_delta;
create trigger entity_url_before_update_to_delta
before update on entity_url
for each row
begin
insert into delta.entity_url
(url_id, updated_at, title, description, h1, h2, content, main_entity,
entity_sentence_id, entity_id)
values(old.url_id, old.updated_at, old.title, old.description, old.h1, old.h2,
old.content, old.main_entity, old.entity_sentence_id, old.entity_id);
end $$
delimiter ;
However when an update statement is fired the database gives the following error:
"Error code 1142. Insert command denied to users "user#..." for table entity_url".
Is this due to using an insert statement in an update trigger or because i do not have the proper rights for this?
Thank you!

create trigger on update to insert old values into other table in MYSQL

I have two tables. The first is my person table which has id, name, creation_date as values, I have a old_person table (id, name, modified_date) which I want to populate the value of person before it actually changes. How would I go about that? I have tried triggers but failed.
I tried as follows
create trigger Person_Trigger Update on person
before update
as
insert into old_person(id, name, modified)
select id, new.name, getdate()
from person
It's giving me syntax errors...Not many Trigger references out there either, a little push would be greatly appreciated!
Have a look at the following example
SQL Fiddle DEMO
IO hade a bit of trouble myself, but from How to Create Triggers in MySQL there was a line that made me think
The first MySQL command we’ll issue is a little unusual:
DELIMITER $$
Our trigger body requires a number of SQL commands separated by a
semi-colon (;). To create the full trigger code we must change
delimiter to something else — such as $$.
Finally, we set the delimiter back to a semi-colon:
DELIMITER ;
So in the SQL Fiddle I changed the query terminator to GO and that seemed top work.
CREATE TABLE person
(
id INT,
name varchar(20)
)
GO
CREATE TABLE old_person
(
id INT,
name varchar(20),
modified DATETIME
)
GO
CREATE TRIGGER Person_Trigger before update on person
FOR EACH ROW BEGIN
INSERT INTO old_person(id, name, modified)
VALUES (OLD.id, OLD.name, NOW());
END
GO
INSERT INTO person VALUES (1,'TADA')
GO
UPDATE person SET name = 'FOO'
GO
Try this:
DELIMITER \\
CREATE TRIGGER `Person_Trigger`
BEFORE UPDATE ON `Person`
FOR EACH ROW
BEGIN
DECLARE date_modified datetime;
SET date_modified = NOW();
INSERT INTO old_person(id, name, modified)
VALUES (OLD.id, OLD.name, date_modified);
END\\
This syntax works for me on my own projects. You may also need to declare delimiters before you begin the trigger. Also if you want to use the NEW keyword it should be AFTER update. Switch to the OLD keyword if you are going to keep using BEFORE update on your trigger.

PhpMyAdmin freezes by creating triggers

I try to create a trigger from the SQL console of PhpMyAdmin but the navigator loads infinitely
DELIMITER |
CREATE TRIGGER before_insert_history BEFORE INSERT
ON history FOR EACH ROW
BEGIN
INSERT INTO newsfeed(user_ID, date_action, action, pg_ID, rk_ID, vote)
VALUES (NEW.user_ID, NOW(), "vote", NEW.page_ID, NEW.ranking_ID, SELECT IF(NEW.liked='1','1','-1') AS vote );
END |
DELIMITER ;
After a certain time I have this error : Fatal error: Maximum execution time of 300 seconds exceeded in C:\wamp\apps\phpmyadmin3.2.0.1\libraries\import\sql.php on line 259
Maybe it's a delimiter issue, yet I choosed ";" in the delimiter field on the bottom of console.
delimiter is a MySQL client command, not SQL. I believe you need to choose | as your delimiter from the drop-down box on PHPMyAdmin's SQL tab, then use the same code you posted here without the delimiter commands.
I just tried a simple statement within delimiter commands, and although the first delimiter command didn't seem to do much, the second one causes it to hang. Removing it solved it in my case.
CREATE TRIGGER before_insert_history BEFORE INSERT
ON history FOR EACH ROW
BEGIN
INSERT INTO newsfeed(user_ID, date_action, action, pg_ID, rk_ID, vote)
VALUES (NEW.user_ID, NOW(), "vote", NEW.page_ID, NEW.ranking_ID, SELECT IF(NEW.liked='1','1','-1') AS vote );
END |