MMMMS
MMMMS

Reputation: 2201

Problems creating TRIGGER in MySQL

I am first time using trigger concept in my MYSQL workbench like below query,

First I created people table by using below query,

CREATE TABLE people (age INT, name varchar(150));

Then I used below query for trigger

DELIMITER 
CREATE TRIGGER agecheck BEFORE INSERT ON raptor1_5.people FOR EACH ROW IF NEW.age < 0 THEN SET NEW.age = 0; END IF;

DELIMITER ;

But agecheck trigger not creating to people table. Its does not show any error message when I run this query.

Here is my table images,

enter image description here

Updated : based on the answer

enter image description here

Upvotes: 2

Views: 3544

Answers (2)

Mad Dog Tannen
Mad Dog Tannen

Reputation: 7242

Try with this.

USE `raptor1_5`;

DELIMITER $$

DROP TRIGGER IF EXISTS raptor1_5.agecheck$$
USE `raptor1_5`$$
CREATE TRIGGER agecheck BEFORE INSERT ON people 
FOR EACH ROW 
BEGIN
 IF NEW.age < 0 THEN 
   SET NEW.age = 0; 
 END IF;
END$$
DELIMITER ;

Upvotes: 2

Abhik Chakraborty
Abhik Chakraborty

Reputation: 44874

The standard trigger syntax should be as

DELIMITER  //

CREATE TRIGGER agecheck BEFORE INSERT ON raptor1_5.people 
FOR EACH ROW 
BEGIN
 IF NEW.age < 0 THEN 
   SET NEW.age = 0; 
 END IF;
END;//

DELIMITER ;

The above is when you run on mysql cli, however in phpmyadmin or workbench check if you have an option to set the delimiter if yes choose that and remove the delimiter part from the above.

Upvotes: 0

Related Questions