Here is a trigger i created and i want to compare new.utype with employee as given below but dont know the actual way to compare it in mysql .. how can i achieve this and also please tell if condition syntax is correct or not
delimiter $$
CREATE TRIGGER insdetail_tomaster
AFTER INSERT ON tempdetail
FOR EACH ROW BEGIN
IF(new.utype =='employee') THEN
INSERT INTO master_employee values(new.field1,new.filed2);
ELSE
INSERT INTO master_manager values(new.field1,new.fiel2);
END $$
DELIMITER ;
Use "like" key word as below
IF new.utype like 'employee'
Entire trigger looks like this
delimiter $$
CREATE TRIGGER insdetail_tomaster
AFTER INSERT ON tempdetail
FOR EACH ROW BEGIN
IF new.utype like 'employee' THEN
INSERT INTO master_employee values(new.field1,new.filed2);
ELSE
INSERT INTO master_manager values(new.field1,new.fiel2);
END $$
DELIMITER ;
I assumed trigger you wrote other than the string comparison is correct
Thanks
You need to use single = instead of ==
IF new.utype = 'employee'
DELIMITER $$
CREATE TRIGGER insdetail_tomaster
AFTER INSERT ON tempdetail
FOR EACH ROW BEGIN
IF new.utype = 'employee' THEN
INSERT INTO master_employee values(new.field1,new.filed2);
ELSE
INSERT INTO master_manager values(new.field1,new.fiel2);
END $$
DELIMITER ;
You can also use <> for comparing not equal value.
IF new.utype <> 'employee'
DELIMITER $$
CREATE TRIGGER insdetail_tomaster
AFTER INSERT ON tempdetail
FOR EACH ROW BEGIN
IF new.utype <> 'employee' THEN
INSERT INTO master_employee values(new.field1,new.filed2);
ELSE
INSERT INTO master_manager values(new.field1,new.fiel2);
END $$
DELIMITER ;
Related
I want to create a trigger in mysql which insert a row in a table change_history when there is any update in another table event_data if event_data.title='event_media'.
DELIMITER $$
CREATE TRIGGER `after_evdata_update`
AFTER UPDATE ON event_data
FOR EACH ROW
BEGIN
if OLD.title <=> 'event_media'
INSERT INTO change_history (badge,city,country,venue,company,
date,rehosted,type,cancelled,name,tagline,description,agenda,pricing,
edition,photo_video,exhibitors,speakers,official_url,twitter_handle,
twitter_hashtag,facebook_url,contact,post_review_id) values(1,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,null);
end if;
END $$
DELIMITER ;
I think this should work. Please try.
BEGIN
IF (OLD.title <> 'event_media')
THEN
INSERT INTO `change_history` (`xxx_id`,`and_all_other_fields_similar_way`)
VALUES (old.xxx_id,old.and_all_other_fields_similar_way);
END IF;
END
I have table "book" and "store_order" have relation
I want to make trigger(but it contain error):
DELIMITER $$
CREATE TRIGGER t1
before delete ON store_order
FOR EACH ROW
BEGIN
update book set number = number + NEW.quantity where ISBN = NEW.ISBN;
END
$$
DELIMITER ;
DELIMITER $$
CREATE
TRIGGER t2 AFTER delete
ON library.store_order
FOR EACH ROW BEGIN
update library.book
set library.book.number = (library.book.number + OLD.quantity)
where library.book.ISBN = OLD.ISBN;
END$$
DELIMITER ;
Use OLD instead of NEW when you want to get the deleted object.
for an example of my case. I'm getting the id of the newly added role by calling
NEW.id
and getting the same field's value while deleting by calling
OLD.id
Example:
DELIMITER $$
CREATE TRIGGER after_insert_role
AFTER INSERT ON role FOR EACH ROW
BEGIN
INSERT INTO `sync_mapping`
(`operation_type`, `table_name`, `oid`, `end_point`)
VALUES
('insert', 'role', NEW.id, 'new/role');
END $$
DELIMITER $$
CREATE TRIGGER after_delete_role
AFTER DELETE ON role FOR EACH ROW
BEGIN
INSERT INTO `sync_mapping` (`operation_type`, `table_name`, `oid`, `end_point`) VALUES
('delete', 'role', OLD.id, 'delete/role');
END $$
Whenever we are deleting the data
USE
OLD. instead of NEW.
CREATE TRIGGER user_history_delete AFTER DELETE ON user FOR EACH ROW
INSERT INTO user_history(action , name )
VALUES ("Delete" , OLD.name );
I have created the following trigger in mysql:
DELIMITER $$
CREATE TRIGGER `some_trigger` AFTER INSERT ON db1.table
FOR EACH ROW
BEGIN
IF NEW.userid = 'certain_id' THEN
INSERT INTO db2.table
SET
value1 = NEW.value,
value2 = NEW.value;
END IF;
END $$
DELIMITER ;
The above statement works if I remove the if statement. Additionally no syntax errors are encountered when this trigger is added to the db. Any idea what is wrong with the if statement that is not allowing it to insert entries with value 'certain_id' in column userid when added to db1.table??
Try this it should work..
DELIMITER $$
CREATE TRIGGER `some_trigger` AFTER INSERT ON `db1`.`table`
FOR EACH ROW
BEGIN
IF (NEW.userid = 'certain_id') THEN
INSERT INTO `db2`.`table` (value1, value2)
VALUES (NEW.value1,NEW.value2);
END IF;
END $$
DELIMITER ;
I have query for crete trigger:
CREATE TRIGGER `Insert` AFTER INSERT ON `HistoryBalance`
FOR EACH ROW INSERT INTO Winners (name) VALUES (new.ProductID)
But now i woul like get condition:
if (new.Status = 'user'){
CREATE TRIGGER `Insert` AFTER INSERT ON `Balance`
FOR EACH ROW INSERT INTO Winners (name) VALUES (new.ProductID)
}
Tell me please how right crete trigger with my condition ?
You could do something like this:
delimiter $$
CREATE TRIGGER balance_insert AFTER INSERT ON Balance
FOR EACH ROW
BEGIN
if (new.Status = 'user') then
INSERT INTO Winners(name) VALUES (new.ProductID);
end if;
END; $$
delimiter ;
You can read more Here
I would like to check if a record is being created with a user id, if not then generate one. I can't figure out what I'm doing wrong?
delimiter $$
DROP TRIGGER IF EXISTS init_uuid_users;
CREATE TRIGGER init_uuid_users BEFORE INSERT ON `users`
FOR EACH ROW BEGIN
IF (NEW.username IS NULL) THEN
SET NEW.username = UUID();
END IF;
END;
$$
delimiter ;
DROP TRIGGER IF EXISTS init_uuid_users;
put it before delimiter.
And remove ; after END.