2

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

2 Answers 2

2

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 ;
Sign up to request clarification or add additional context in comments.

4 Comments

It tells which schema to use for the session you are in. Even though if you try to execute to create a trigger without USING a schema, it should give the error that 'no database selected'
It was most likley not using any SCHEMA , also, try again like this now, se if it doesnt work.
CREATE TRIGGER agecheck BEFORE INSERT ON raptor1_5.people Ive added the database name.
Nice explanation.I understand the problem. thank you
0

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.

5 Comments

Still its not working. Its shows like Executing Query... but nothing happens
Maybe add a delete trigger if exists before?
@KayNelson i cant get you
Add this before the CREATE TRIGGER ....... DROP TRIGGER IF EXISTS raptor1_5.agecheck//
@KayNelson check my updated question. I tried this query but nothing happen

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.