How to calculate using trigger? - mysql

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;

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 ;

Updating the status of a bonus on updating the used point through a trigger in mysql

I've a table tbl_bonus. I want to update the status of bonus when point==used_point. Want to update the status to used at that time. otherwise the status will remain unused.
This is the table.
CREATE TABLE `tbl_bonus` (
`id` BIGINT(20) NOT NULL AUTO_INCREMENT,
`point` INT(5) NOT NULL,
`used_point` INT(5) DEFAULT NULL,
`status` ENUM('unused','used') NOT NULL,
PRIMARY KEY (`id`),
);
This is the trigger I'm trying to run.
DELIMITER $$
CREATE TRIGGER xyz BEFORE UPDATE ON tbl_bonus FOR EACH ROW
BEGIN
IF (new.used_point <=> new.point) THEN
SET new.status = 'used':
END IF:
END $$
DELIMITER :
How shall I write the trigger that will suffice my requirement? Will appreciate any sort of help.

I can't use triggers to update a column based on changes in another column

I need to automatically update the value of AdoptionDate every time AdopterID receives a value different from NULL. I tried to do this using the code below, but it gives me Error Code: 1362.
DROP SCHEMA IF EXISTS `Assignment`;
CREATE SCHEMA `Assignment` DEFAULT CHARACTER SET utf8;
USE `Assignment`;
DROP TABLE IF EXISTS `Dog`;
CREATE TABLE `Dog` (
`DogID` INT NOT NULL AUTO_INCREMENT,
`Race` VARCHAR(32) NOT NULL,
`RescueDate` DATE NOT NULL,
`AdoptionDate` DATE DEFAULT NULL,
`AdopterID` INT(11) DEFAULT NULL,
PRIMARY KEY (`DogID`)
);
DELIMITER //
CREATE TRIGGER CustomTrigger AFTER UPDATE ON `Dog`
FOR EACH ROW
BEGIN
IF NEW.`AdopterID` <=> NULL THEN
SET NEW.`AdoptionDate` = NOW();
END IF;
END; //
DELIMITER ;

MySQL: Integrity error when inserting into database

So, I have this one column in my table that gets filled by a trigger when a new entry is inserted.
CREATE TABLE `users` (
`idusers` int(11) NOT NULL AUTO_INCREMENT,
`uid` char(64) DEFAULT NULL,
`uname` varchar(80) NOT NULL,
`password` char(128) NOT NULL,
`mail` varchar(120) NOT NULL,
PRIMARY KEY (`idusers`),
UNIQUE KEY `uname_UNIQUE` (`uname`),
UNIQUE KEY `mail_UNIQUE` (`mail`),
UNIQUE KEY `uid_UNIQUE` (`uid`)
);
DELIMITER $$
TRIGGER `flask`.`users_BEFORE_INSERT` BEFORE INSERT ON `users` FOR EACH ROW
BEGIN
set new.uid = sha2(new.idusers, 256);
END$$
DELIMITER ;
The problem now is that when I try to add a new row (only have a test one yet because of the error), in the trigger the value of new.idusers is somehow always 0 instead of the current auto_increment value.
What do I need to change in my trigger code so that the value used for generating the uid is the actual id and not always 0?
Since idusers is an AUTO_INCREMENT field, its value is known only after the record is inserted in the table.
Use an AFTER INSERT trigger instead of a BEFORE INSERT trigger, and update the newly inserted record:
DELIMITER $$
CREATE TRIGGER `flask`.`users_AFTER_INSERT` AFTER INSERT ON `users` FOR EACH ROW
BEGIN
UPDATE users SET uid = sha2(NEW.idusers, 256) WHERE idusers = NEW.idusers;
END $$
DELIMITER ;

syntax error in mysql during tringger definition

I'm trying to use a trigger defined as follows
-- the table
CREATE TABLE codici_ddt(
id_ordine VARCHAR(15) NOT NULL,
id_invoice VARCHAR(15) NOT NULL,
ddt_numero INT(8) NOT NULL,
fatturazione DATE NOT NULL,
ddt VARCHAR(20) NOT NULL,
FOREIGN KEY(id_ordine) REFERENCES ordini_dal_web(id_ordine),
PRIMARY KEY(id_ordine)
);
--the_trigger
DELIMITER $$
CREATE TRIGGER genera_numero_ddt BEFORE INSERT ON codici_ddt FOR EACH ROW
BEGIN
DECLARE ultimo_ddt INT(8);
SELECT COALESCE(max(ddt_numero),1) INTO ultimo_ddt
FROM codici_ddt
WHERE data_fatturazione >= MAKEDATE(YEAR(NEW.data_fatturazione) ,1)
AND data_fatturazione < MAKEDATE(YEAR(NEW.data_fatturazione)+1,1);
SET NEW.ddt_numero = (ultimo_ddt+1)
SET NEW.ddt = CONCAT(NEW.ddt_numero,'/',(SUBSTRING(SUBSTRING_INDEX(NEW.data_fatturazione,'-',1),-2)),'c');
END $$
DELIMITER ;
the message returned from mysql is
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 'SET NEW.ddt =
CONCAT(NEW.ddt_numero,'/',(SUBSTRING(SUBSTRING_INDEX(NEW.data_fatt' at
line 11
the CONCAT should be right, where is my error?
many thanks!
You miss ';' at the end of line.
SET NEW.ddt_numero = (ultimo_ddt+1);
Missing semi-colon. Also you are referencing data_fatturazione instead of just fatturazione.
Try this:
DELIMITER $$
CREATE TRIGGER genera_numero_ddt BEFORE INSERT ON codici_ddt FOR EACH ROW
BEGIN
DECLARE ultimo_ddt INT(8);
SELECT COALESCE(max(ddt_numero),1) INTO ultimo_ddt FROM codici_ddt
WHERE fatturazione >= MAKEDATE(YEAR(NEW.fatturazione) ,1)
AND fatturazione < MAKEDATE(YEAR(NEW.fatturazione)+1,1);
SET NEW.ddt_numero = (ultimo_ddt+1);
SET NEW.ddt = CONCAT(NEW.ddt_numero,'/',(SUBSTRING(SUBSTRING_INDEX(NEW.fatturazione,'-',1),-2)),'c');
END $$
DELIMITER ;