creating trigger inserting UNIX_TIMESTAMP() to a log table - mysql

Hi im stuck on the syntax of making this trigger to insert values to my log table. Using the sql tab inside phpmyadmin an error appears before executing the sql statement
`#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 8 `
here is my sql statement in creating a trigger
CREATE TRIGGER after_insert_list
AFTER INSERT ON list FOR EACH ROW
BEGIN
INSERT INTO log (user_id, action, date_log)
VALUES (
NEW.user_id,
NEW.action,
UNIX_TIMESTAMP()
);
END

Create your trigger like follows:
delimiter |
CREATE TRIGGER testref
BEFORE INSERT ON test1FOR EACH ROW BEGIN
INSERT INTO test2 SET a2 = NEW.a1;
DELETE FROM test3 WHERE a3 = NEW.a1;
UPDATE test4 SET b4 = b4 + 1 WHERE a4 = NEW.a1;
END;
|
delimiter ;

Here is a 100% working example:
Table demo
CREATE TABLE `demo` (
`id` int NOT NULL AUTO_INCREMENT,
`text` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci DEFAULT NULL,
`updated_at` int NOT NULL DEFAULT '0',
PRIMARY KEY (`id`)
);
Trigger on INSERT
DELIMITER ;;
CREATE TRIGGER `updated_at__on_insert`
BEFORE INSERT
ON `demo`
FOR EACH ROW
BEGIN
SET new.updated_at = UNIX_TIMESTAMP();
END;;
DELIMITER ;
Trigger on UPDATE
DELIMITER ;;
CREATE TRIGGER `updated_at__on_update`
BEFORE UPDATE
ON `demo`
FOR EACH ROW
BEGIN
SET new.updated_at = UNIX_TIMESTAMP();
END;;
DELIMITER ;
p.s. I was looking for such a solution for my CMS EFFCORE.

Related

'WHERE' condition in an UPDATE TRIGGER giving syntax error :mysql version 5.7

I am using mysql database(VERSION 5.7).
I created a table with the following CREATE command:
CREATE TABLE `attendance` (
`id` varchar(11) NOT NULL,
`hash` char(8) NOT NULL,
`time_in` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
`time_out` datetime DEFAULT NULL,
`time_spent` time DEFAULT NULL,
`purpose` char(10) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
I have a trigger with the following query:
DELIMITER $$
CREATE TRIGGER test_trigger
BEFORE UPDATE ON attendance
FOR EACH ROW
BEGIN
SET NEW.time_spent=TIMEDIFF(NEW.time_out,OLD.time_in) WHERE OLD.time_spent IS NULL;
END$$
DELIMITER ;
But mysql gives the following 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 'WHERE OLD.time_spent IS NULL; END' at line 1
(The where condition makes sure that only current days records get updated)
How am I supposed to write the query?
You can't use a WHERE clause with SET inside a trigger. Use IF instead:
DELIMITER $$
CREATE TRIGGER test_trigger
BEFORE UPDATE ON attendance
FOR EACH ROW
BEGIN
IF OLD.time_spent IS NULL THEN
SET NEW.time_spent = TIMEDIFF(NEW.time_out, OLD.time_in);
END IF;
END$$
DELIMITER ;

MySQL If else construct syntax

Im having some trouble with using the if else construct in sql triggers in MySQL. The tables below are just used for testing.
First Table:
CREATE TABLE `test2`.`t1` (
`c1` VARCHAR(5) NOT NULL,
`c2` VARCHAR(45) NULL,
`c3` VARCHAR(45) NULL,
PRIMARY KEY (`c1`));
My second table:
CREATE TABLE `test2`.`t2` (
`cc1` VARCHAR(5) NOT NULL,
`cc2` VARCHAR(45) NULL,
`cc3` VARCHAR(45) NULL,
PRIMARY KEY (`cc1`));
and the third:
CREATE TABLE `test`.`t3` (
`ccc1` VARCHAR(5) NOT NULL,
`ccc2` VARCHAR(45) NULL,
`ccc3` VARCHAR(45) NULL,
PRIMARY KEY (`cc1`));
c1 is the primary key for both cc1 and ccc1.
The trigger I am using is:
DROP TRIGGER IF EXISTS `test`.`t1_AFTER_UPDATE`;
DELIMITER $$
USE `test`$$
CREATE DEFINER=`root`#`localhost` TRIGGER `t1_AFTER_UPDATE` AFTER UPDATE ON
`t1` FOR EACH ROW
BEGIN
DECLARE start varchar(1);
set start=substring((c2),1,1);
IF(start='S') THEN
UPDATE t2
SET cc2 = NEW.c2
WHERE cc1 = NEW.c1;
else
UPDATE t3
SET ccc2 = NEW.c2
WHERE ccc1 = NEW.c1;
END IF;
END$$
DELIMITER ;
Basically, if the user updates t1.c2, then t2.cc2 OR t3.ccc2 must be updated. If t1.c2 of the NEW.c2 starts with the letter S, then t2.cc2 must be updated, else t3.ccc2 must be updated.
The query executes without a hitch but wont let me update the value of c2:
Executing:
UPDATE `test2`.`t1` SET `c2` = '24' WHERE (`c1` = 'M123');
Operation failed: There was an error while applying the SQL script to the database.
ERROR 1054: 1054: Unknown column 'c1' in 'field list'
SQL Statement:
In a Trigger, we refer to columns in the rows being updated/inserted/deleted by NEW (after operation), and OLD (before operation) keywords.
You will need to use these keywords in order to be able to access the values. You can access the c2 value from the current table using NEW keyword. You can also optimize further by doing the operation only, when the c2 value is actually modified.
DROP TRIGGER IF EXISTS `test`.`t1_AFTER_UPDATE`;
DELIMITER $$
USE `test`$$
CREATE DEFINER=`root`#`localhost` TRIGGER `t1_AFTER_UPDATE` AFTER UPDATE ON
`t1` FOR EACH ROW
BEGIN
DECLARE start varchar(1) DEFAULT NULL; -- default it to NULL
-- Check if there is any update on c2 column
IF (NEW.c2 <> OLD.c2)
-- We access the updated value of c2 using NEW keyword
SET start = SUBSTRING(NEW.c2,1,1);
END IF;
-- Do update operation on other tables only when start is not null
IF (start IS NOT NULL) THEN
IF (start = 'S') THEN
UPDATE t2
SET cc2 = NEW.c2
WHERE cc1 = NEW.c1;
ELSE
UPDATE t3
SET ccc2 = NEW.c2
WHERE ccc1 = NEW.c1;
END IF;
END IF;
END$$
DELIMITER ;
Some noteworthy points from Trigger docs:
Within the trigger body, the OLD and NEW keywords enable you to access
columns in the rows affected by a trigger. OLD and NEW are MySQL
extensions to triggers; they are not case-sensitive.
In an INSERT trigger, only NEW.col_name can be used; there is no old
row. In a DELETE trigger, only OLD.col_name can be used; there is no
new row. In an UPDATE trigger, you can use OLD.col_name to refer to
the columns of a row before it is updated and NEW.col_name to refer to
the columns of the row after it is updated.
Your trigger basically looks okay, except for the definition of start. The column reference is missing a reference to new. I am puzzled by the error message, which should be on c2 rather than c1, though.
In any case, I would simply dispense with it:
DELIMITER $$
USE `test`$$
CREATE DEFINER=`root`#`localhost` TRIGGER `t1_AFTER_UPDATE` AFTER UPDATE ON
`t1` FOR EACH ROW
BEGIN
IF (new.c2 LIKE 'S%') THEN
UPDATE t2
SET cc2 = NEW.c2
WHERE cc1 = NEW.c1;
ELSE
UPDATE t3
SET ccc2 = NEW.c2
WHERE ccc1 = NEW.c1;
END IF;
END$$
DELIMITER ;
You also need to fix the definition of t3. The primary key column does not exist.

SQL After Update Trigger

Im having some trouble with sql triggers in MySQL. The tables below are just used for testing.
First table:
CREATE TABLE `test`.`t1` (
`c1` INT NOT NULL,
`c2` VARCHAR(45) NULL,
`c3` VARCHAR(45) NULL,
PRIMARY KEY (`c1`));
Second Table:
CREATE TABLE `test`.`t2` (
`cc1` INT NOT NULL,
`cc2` VARCHAR(45) NULL,
`cc3` VARCHAR(45) NULL,
PRIMARY KEY (`cc1`));
cc1 references c1
ALl i want to do is, if the values in c2 are updated, then the corresponding field should be updated in cc2.
The trigger i came up with is:
DROP TRIGGER IF EXISTS `test`.`t1_AFTER_UPDATE`;
DELIMITER $$
USE `test`$$
CREATE DEFINER = CURRENT_USER TRIGGER `test`.`t1_AFTER_UPDATE` AFTER UPDATE ON `t1` FOR EACH ROW
BEGIN
update t2
set cc2=c2
where t2.cc1=t1.c1;
END$$
DELIMITER ;
This executes with no errors but if i try to update the values of c2,
UPDATE `test`.`t1` SET `c2` = '23' WHERE (`c1` = '11');
Operation failed: There was an error while applying the SQL script to
the database. ERROR 1054: 1054: Unknown column 't1.c1' in 'where
clause'
The update goes through if i remove the trigger.
In a Trigger, we refer to columns in the rows being updated/inserted/deleted by NEW (after operation), and OLD (before operation) keywords.
You will need to use these keywords in order to be able to update the other table.
Also, we can optimize the Trigger to initiate UPDATE operation only when there has been a change observed in the c2 value.
DROP TRIGGER IF EXISTS `test`.`t1_AFTER_UPDATE`;
DELIMITER $$
USE `test`$$
CREATE DEFINER = CURRENT_USER TRIGGER `test`.`t1_AFTER_UPDATE`
AFTER UPDATE ON `t1` FOR EACH ROW
BEGIN
-- Check if there has been any change in the c2 value or not
IF (NEW.c2 <> OLD.c2) THEN
-- Update only when there is some change
UPDATE t2
SET cc2 = NEW.c2 -- access the recent updated value of c2 using NEW keyword
WHERE cc1 = NEW.c1; -- access the c1 value of updated row using NEW keyword
END IF;
END$$
DELIMITER ;

How to calculate using trigger?

CREATE TABLE table_test_trigger (
id INT(11) NOT NULL AUTO_INCREMENT,
a INT(11) DEFAULT NULL,
b INT(11) DEFAULT NULL,
c INT(11) DEFAULT NULL,
PRIMARY KEY (id)
);
DELIMITER $$
CREATE TRIGGER trigger1
BEFORE INSERT
ON table_test_trigger
FOR EACH ROW
BEGIN
SET NEW.c = NEW.a + NEW.b;
END
$$
I've tried this code.
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 6
Try this:
DELIMITER $$
CREATE
/*!50017 DEFINER = 'root'#'%' */
TRIGGER `trigger1` BEFORE INSERT ON `table_test_trigger`
FOR EACH ROW BEGIN
SET NEW.c = NEW.a + NEW.b;
END;
$$
DELIMITER ;
It works fine ..there is no error in the trigger or table. Data will be inserted peacefully..!May be you have given some extra spaces in between while trying...Just check out that
Other than that there is no error it is fine...! (I tried it in my machine It worked fine..!)
Do one thing just neatly take it up into a notepad remove all extra spaces or character if any you see, then paste it into the terminal, It works fine.
The script is correct. It is possible that your MySQL client does not support delimiters. Your trigger has only one statement, so you can omit BEGIN-END clause and DELIMITER command.
CREATE TABLE table_test_trigger (
id INT(11) NOT NULL AUTO_INCREMENT,
a INT(11) DEFAULT NULL,
b INT(11) DEFAULT NULL,
c INT(11) DEFAULT NULL,
PRIMARY KEY (id)
);
CREATE TRIGGER trigger1
BEFORE INSERT
ON table_test_trigger
FOR EACH ROW
SET NEW.c = NEW.a + NEW.b;

MySQL Trigger for after insert and update table

i was created a table for content management.I am not familiar with Mysql trigger.
Key Default
a varchar(50)
b varchar(50)
c text
status varchar(100)
This my table.I was created a trigger When each table update table change the status.
DELIMITER $$
CREATE TRIGGER tr2 BEFORE insert or UPDATE ON p
FOR EACH ROW BEGIN
SET NEW.status = '1';
END;
$$
DELIMITER ;
This trigger only update when the row will be modified .How can create trigger for change the "status" during every insert and update.. Please help me any one
This is not correct syntax. Just create a trigger before update
DELIMITER $$
CREATE TRIGGER tr2 BEFORE UPDATE ON p
FOR EACH ROW BEGIN
SET NEW.date = '1';
END;
$$
DELIMITER ;
and make the default value for column status = '1'.
CREATE TABLE p(
a varchar(50),
b varchar(50),
c text,
status varchar(100) NOT NULL DEFAULT '1'
);
That way whenever you insert a row into p and don't specify a value for status, status will be 1. You could create another trigger BEFORE INSERT but I don't recommend that. And the above solution does the same.