TRIGGER CREATED successfully , but no triggers in information_schema TRIGGERS table - php

hi i am trying to use triggers , my MySQL version is 5.5.8
the thing is when i create the trigger , PHPMyadmin says it is created successfully ,
this is my trigger
DELIMITER $$
CREATE TRIGGER `check_pupil_before_insert` BEFORE INSERT ON `pupil_data`
FOR EACH ROW BEGIN
IF CHAR_LENGTH( NEW.DateOfBirth ) < 4 THEN
SIGNAL SQLSTATE '12345'
SET MESSAGE_TEXT := 'check constraint on pupil_data.DateOfBirth failed';
END IF;
END$$
DELIMITER ;
it says
Your SQL query has been executed successfully
DELIMITER $$ CREATE TRIGGER `check_pupil_before_insert` BEFORE INSERT ON `pupil_data`
FOR EACH
ROW BEGIN
IF CHAR_LENGTH( NEW.DateOfBirth ) <4
THEN SIGNAL SQLSTATE '12345'
SET MESSAGE_TEXT := 'check constraint on pupil_data.DateOfBirth failed';
END IF ;
but when i
select * from information_schema.triggers
MySQL returned an empty result set (i.e. zero rows). ( Query took 0.0843 sec )
SELECT *
FROM information_schema.triggers
LIMIT 0 , 30
why is this happening , please help me , thanks in advance.

Check this.. It will help you ... and make sure query is same as is shown in image change ":=" to "="

Related

PHP MYSQL and Trigger

I'm looking for the solution to a problem. I have a form that sends two fields (ID, info_data) and I'm trying to add a third field (secuencia) by a trigger but I can not do it.
table, result and expected results
Trigger:
DELIMITER $$
CREATE TRIGGER regi_tg
BEFORE INSERT ON registros
FOR EACH ROW
BEGIN
SET NEW.secuencia = new.id;
END$$
DELIMITER;
PD: I work info_data copy data in secuencia, but not copy the ID achievement in secuencia
Test CODE:
DELIMITER $$
CREATE TRIGGER regi_tg AFTER INSERT ON registros FOR EACH ROW
BEGIN
SET OLD.secuencia = NEW.id;
END;
CREATE TRIGGER regi_tg AFTER UPDATE ON registros FOR EACH ROW
BEGIN
SET OLD.secuencia = NEW.id;
END;
END$$
DELIMITER ;
I use this trigger and it worked, I did recently thought for a while and seeing many post but i dont know if a correct form.enter image description here
Trigger
DELIMITER $$
CREATE TRIGGER regi_tg
BEFORE INSERT ON registros
FOR EACH ROW
BEGIN
SET new.secuencia = md5((SELECT ID FROM registros ORDER BY id DESC LIMIT 1)+1);
END$$
DELIMITER ;

MySQL Stored Procedure that compares each row's date field and updates based on curdate()

This might be a noob question, but I tried googling about this issue and I haven't found any solution for it (I think). Anyway, I need to compare a date in a row from a mysql table to the current date and update the row depending on the results. I made a stored procedure that will be called by an event every x minutes. Here is my code for the sp:
delimiter //
CREATE PROCEDURE checkPromoValidity()
BEGIN
DECLARE done INT DEFAULT FALSE;
DECLARE id INT;
DECLARE lvn_status INT;
DECLARE lvn_validUntil DATE;
DECLARE cur1 CURSOR FOR SELECT promos.PROMO_NUMBER,promos.VALID_UNTIL,promos.status FROM MDCH_NEW.promos;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE;
open cur1;
check_loop: LOOP
fetch cur1 into id,lvn_validUntil,lvn_status;
IF done THEN
LEAVE check_loop;
END IF;
IF (lvn_validUntil > CURDATE()) THEN
update mdch_new.promos set MDCH_NEW.PROMOS.status = 0 where mdch_new.promos.PROMO_NUMBER = id;
END IF;
end LOOP;
close cur1;
END //
MDCH_NEW = database name
PROMOS= table name
EDIT: I CHANGED my delimiter to // just to see
edit2: added space after the final word, still getting the error below.. :(
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 23
delimiter $$
create procedure return7 ()
BEGIN
select 7;
END
$$
call return7();
you are free to re-use this code

how to update mysql procedure in php mysql?

select #i := $priority;
update todo,category set priority = (select #i := #i + 1) where cateid=category.id and status='new' and groups=0 and siteid=1 and priority>=$priority and todo.id!=190;
This statement runs mysql. But not execute in php mysql_query() command. Kindly help me.
Another way tries somthing,
Add Routine : stored procedure concept is not updated my phpmyadmin. It shows #1064 error on indicate the line 'select #'.
DELIMITER //
CREATE PROCEDURE FirstPro(IN st VARCHAR(10), IN grp INT(11),IN sid INT(11),IN pri INT(11), IN tid INT(11))
BEGIN
select #i := pri;
update todo,category set priority = (select #i := #i + 1) where cateid=category.id and status='st' and groups=grp and siteid=sid and priority>=pri and todo.id!=tid
END //
DELIMITER;
It shows
#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 'END' at line 5

MYSQL Trigger Not Working. Not sure why

I am trying to create a trigger for my table named quailmiles. When the total miles hits a certain number I want the persons status (which is a attribute in that same table) to change. For some reason my code is not working.
The error currently reads:
#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 9.
delimiter $$
CREATE TRIGGER status_update
AFTER UPDATE ON quailmiles
FOR EACH ROW BEGIN
IF (total_miles > 2000) THEN
UPDATE quailmiles SET status = 'gold';
ELSE IF (total_miles > 4000) THEN
UPDATE quailmiles SET status = 'platinum';
END IF;
END $$
delimiter;
Your logic is backwards. You need to test against 4000 before 2000:
delimiter $$
CREATE TRIGGER status_update
BEFORE UPDATE ON qualmiles
FOR EACH ROW
BEGIN
IF (NEW.total_miles > 4000) THEN
SET NEW.status = 'platinum';
ELSEIF (NEW.total_miles > 2000) THEN
SET NEW.status = 'gold';
END IF;
END $$
delimiter;
I also changed the syntax to be a "before" update trigger. Also, I made the name "qualmiles". "Quailmiles" sounds funny.
I think you want ELSEIF rather than ELSE IF. (Remove the space.)
The error is being returned because that second IF isn't being ended with an END IF.
Also, you don't really want to issue an UPDATE against the same table. You'd rather this be handled in a BEFORE UPDATE trigger, and set the value of the column.
e.g.
CREATE TRIGGER status_update
BEFORE UPDATE ON quailmiles
FOR EACH ROW
BEGIN
IF (NEW.total_miles > 4000) THEN
SET NEW.status = 'platinum';
ELSIF (NEW.total_miles > 2000) THEN
SET NEW.status = 'gold';
END IF;
END$$

how to validate a string using mysql when inserting data

I have a data called registration number like follows
COL/A-000001,
KAL/B-000023,
BAL/A-000452,
I know how to validate this type of format using php.But i want to do it when i create the table.IS it possible ?
Try this:
change (example_tbl / field_name) to your (table / field) names respectively.
DELIMITER $$
CREATE TRIGGER example_before_insert
BEFORE INSERT ON example_tbl FOR EACH ROW
BEGIN
IF NEW.field_name NOT_REGEXP '^[A-Z]{3}\/[A-Z]-\d{6}$' THEN
SIGNAL SQLSTATE '45000'
SET MESSAGE_TEXT = 'Cannot add or update row: regex failed';
END IF;
END;
$$

Categories