if statements inside mysql trigger - mysql

So i have two tables practice and practice1 where both has an auto increment id and a name and surname columns.
I made an after insert trigger where i have an if statement inside, where i put a condition if name is null one insert should occur and if surname is null then the other one but it did not work for some reason. I am sharing the script i tried.
CREATE DEFINER = CURRENT_USER TRIGGER `id_information`.`practice_after_INSERT1` AFTER INSERT ON `practice` FOR EACH ROW
BEGIN
if (`name` = NULL) then
insert into id_information.practice1(surname) values(new.surname);
else
(`surname` = NULL) then
insert into id_information.practice1(name) values(new.name);
end if;
END$$
delimiter ;
Kindly correct my mistake. id_information is the name of the database.

I think the syntax you want is:
delimiter $$
create definer = current_user trigger `id_information`.`practice_after_insert1`
after insert on `practice` for each row
begin
if new.name is null then
insert into id_information.practice1(surname) values(new.surname);
elseif new.surname is null then
insert into id_information.practice1(name) values(new.name);
end if;
end$$
delimiter ;
Key points:
you need to use pseudo-table new to access the row that was just inserted
the structure of your if statement should be if ... then ... elseif ... end
to check a value against null, use is null rather than = null (which is never true)

Related

after update trigger mysql dont execute if a field is change whatever null or not null or empty

i have table person and the column is name and email.
then i have trigger after update that insert value to table logger
but in my case
i dont want to execute insert IF
name is null OR
name is not null OR
name is empty OR
so i just want execute logger when only field email has on change/update.
these my trigger
DELIMITER $$
DROP TRIGGER IF EXISTS person__aus;
CREATE TRIGGER person__aus
AFTER UPDATE
ON person FOR EACH ROW
BEGIN
IF OLD.name THEN // how to implement the three condition on above to dont execute, thats mean only execute these line when only column email has any change
INSERT INTO person_logger SELECT 'update', NULL, NOW(), d.* FROM person AS d WHERE d.id = NEW.id; // here is i copy the value with three dynamic value 'flag', 'auto-increment', dateTime, and person new value.
END IF;
END $$
DELIMITER ;

how to declare variable phpmyadmin mysql triggers properly?

I have a database where whenever residential address update in user table I want it to store in history table of user. For that I'm trying to write triggers but failing miserably in phpmyadmin. Also it's not giving me proper reason why so I can correct it. This is what I have done so far.
DROP TRIGGER IF EXISTS `record_history`;
CREATE TRIGGER `record_history` AFTER UPDATE ON `s_user`
FOR EACH ROW
BEGIN
DECLARE date_current datetime;
DECLARE residential_address varchar(1000);
SET #date_current = NOW();
SET #residential_address = NEW.residential_address;
IF (#residential_address <> OLD.residential_address AND #residential_address != "" AND #residential_address IS NOT NULL) THEN
INSERT INTO history_residential_address (`s_u_id`, `residential_address`, `status`, `date_added`, `date_updated`) VALUES
(OLD.s_u_id, #residential_address, 1, #date_current, #date_current);
END IF;
END;
delimiter ;
A cleaner version of your code
DROP TRIGGER IF EXISTS `record_history`;
delimiter $$
CREATE TRIGGER `record_history` AFTER UPDATE ON `s_user`
FOR EACH ROW
BEGIN
IF (new.residential_address <> OLD.residential_address AND new.residential_address <> "" AND new.residential_address IS NOT NULL) THEN
INSERT INTO history_residential_address (`s_u_id`, `residential_address`, `status`, `date_added`, `date_updated`) VALUES
(OLD.s_u_id, new.residential_address, 1, now(), now());
END IF;
END $$
delimiter ;
If you are still having problems please add sample data from s_user as text to the question.

MySQL Trigger After Insert Value on Another Table

I have a table with a list of my cellphone contacts this is the structure of contacts table.
And the other one is smartteleco table.
The ContactId of smartteleco table is FOREIGN KEY to the ContactId of contacts.
I'm creating a trigger that if I INSERT a value into contacts table and the ContactNumber1 value starts with 0918,0919,0920,0921 it will be stored in the CellNumber field of smartteleco table.
These are the triggers I've already tried but there is a syntax error and the error is always inside the IF STATEMENT.
CREATE TRIGGER addSmart
AFTER INSERT ON contacts
FOR EACH ROW
BEGIN
IF(substring(ContactNumber1,1,4) IN (0918,0919,0920,0921) = 1)
SET CellNumber = substring(ContactNumber1,1,11);
END IF;
END
CREATE TRIGGER addSmart
AFTER INSERT ON contacts
FOR EACH ROW
BEGIN
IF(substring(ContactNumber1,1,4) IN (0918,0919,0920,0921) = 1)
INSERT INTO smartteleco (CellNumber) VALUES (ContactNumber1);
END IF;
END
CREATE TRIGGER addSmart
AFTER INSERT ON contacts
FOR EACH ROW
BEGIN
IF(substring(ContactNumber1,1,4) IN (0918,0919,0920,0921) = 1)
INSERT INTO smartteleco (NEW.CellNumber) VALUES (NEW.ContactNumber1);
END IF;
END
CREATE TRIGGER addSmart
AFTER INSERT ON contacts
FOR EACH ROW
BEGIN
IF(substring(ContactNumber1,1,4) IN (0918,0919,0920,0921) = 1)
SET CellNumber = ContactNumber1;
END IF;
END
CREATE TRIGGER addSmart
AFTER INSERT ON contacts
FOR EACH ROW
BEGIN
IF(substring(ContactNumber1,1,4) IN (0918,0919,0920,0921) = 1)
SET NEW.CellNumber = NEW.ContactNumber1;
END IF;
END
A row trigger cannot directly access the columns of the table to which it is attached. It can access the values in the newly-inserted record by using the pseudorecord NEW. To make your triggers work, just replace the references to ContactNumber1 with references to NEW.ContactNumber1.
You also need to use the correct syntax for your IF structures, which is
IF <contition> THEN --< You need this keyword
<statement>
END IF;
You might also need to cast ContactNumber1 as CHAR to use substring() on it.
So the correct trigger syntax should be
CREATE TRIGGER addSmart
AFTER INSERT ON contacts
FOR EACH ROW
BEGIN
IF(substring(CAST(NEW.ContactNumber1 AS CHAR(11)), 1, 4) IN ('0918', '0919', '0920' ,'0921'))
THEN
INSERT INTO smartteleco (ContactId, CellNumber)
VALUES (NEW.ContactId, NEW.ContactNumber1);
END IF;
END
EDIT: Syntax corrected. I also discovered that you will need to change the column ContactNumber1 from INT(11) to CHAR(11), which is actually the preferred data type since you will never be performing arithmetic on a phone number.
Try this:
BEGIN
IF SUBSTRING(NEW.ContactNumber1,1,4) IN ('0918','0919','0920','0921') THEN
INSERT INTO smartteleco (CellNumber) VALUES (NEW.ContactNumber1);
END IF;
END

Mysql trigger to update empty field with condition

I have a MySQL table , look like this
t id lang title
1 7 en_UK my_title
1 7 kh_KH
I want write a trigger that update title to my_title with the same id is 7
Result
t id lang title
1 7 en_UK my_title
1 7 kh_KH my_title
From my understanding.
DELIMITER $$
CREATE TRIGGER upd_title BEFORE UPDATE ON `term`
FOR EACH ROW BEGIN
IF (NEW.title IS NULL OR NEW.title= '') THEN
SET NEW.title= ??? ;
END IF;
END$$
DELIMITER ;
[UPDATE1]->not works (trigger not being created)
DELIMITER $$
DROP TRIGGER IF EXISTS `update_category_after_insert`
CREATE TRIGGER `update_category` AFTER INSERT ON `categories`
FOR EACH ROW BEGIN
DECLARE loc_title text;
IF (NEW.libelle_categorie IS NULL OR NEW.libelle_categorie= '') THEN
select libelle_categorie into loc_title from categories where NEW.num_noeud= num_noeud and langue = 'en_UK';
SET NEW.libelle_categorie = loc_title;
END IF;
END
DELIMITER ;
[UPDATE2]
DELIMITER $$
CREATE TRIGGER ``update_category_after_insert`` BEFORE INSERT ON `categories`
FOR EACH ROW BEGIN
DECLARE loc_title text;
IF (NEW.libelle_categorie IS NULL OR NEW.libelle_categorie= '') THEN
select libelle_categorie into loc_title from categories where NEW.num_noeud= num_noeud and langue = 'en_UK';
SET NEW.libelle_categorie = loc_title;
END IF;
END
DELIMITER ;
Finally , I found the good solution for my case
UPDATE categories c INNER JOIN categories c2 ON (
c.num_noeud = c2.num_noeud
) SET c.libelle_categorie = c2.`libelle_categorie`
Can you clarify?
Are you trying to pull the value from the title column in the 'en_UK' row that exists when you insert an new row with the same id that HAS the title column not entered?
okay
CREATE TRIGGER upd_title BEFORE UPDATE ON `term`
FOR EACH ROW BEGIN
DECLARE loc_title VARCHAR(20);
IF (NEW.title IS NULL OR NEW.title= '') THEN
select title into loc_title from term where NEW.id = id and lang = 'en_UK';
SET NEW.title= loc_title;
END IF;
END
This should do the trick.
This was my trigger def:
CREATE DEFINER = CURRENT_USER TRIGGER `therinks`.`glreturndata_BEFORE_UPDATE` BEFORE UPDATE ON `glreturndata` FOR EACH ROW
BEGIN
DECLARE myVal VARCHAR(20);
if NEW.DESCRIPTION IS NULL or new.description = '' THEN
SELECT min(description) into myVal from glreturndata where category = NEW.category and new.idglreturndata <> idglreturndata;
Set NEW.DESCRIPTION = myval;
end if;
END
It seems that you can't do all this in a trigger. According to the documentation:
Within a stored function or trigger, it is not permitted to modify a table that is already being used (for reading or writing) by the statement that invoked the function or trigger.
According to this answer, it seems that you should:
create a stored procedure, that inserts into/Updates the target table, then updates the other row(s), all in a transaction.
With a stored proc you'll manually commit the changes (insert and update). I haven't done this in MySQL, but this post looks like a good example.

Trigger before insertion

Hi I need to write a trigger to check if the value is existing in a column, if yes then do not insert it and through and error.
There is a column which can have only two values(same as bool) DEFAULT or NONDEFAULT.
This column can have multiple NONDEFAULT value but only one DEFAULT value in column.
If the DEFAULT exists in table, we need to through an error and shall not insert the new row.
Kindly help
create trigger status_value before insert on TABLE for each row
begin
if new.status=DEFAULT AND select count(status) from TABLE where status=DEFAULT)
then
signal sqlstate = '4500'
set message_text = 'can not update, default value already present'
end if;
end;
Try it like this:
delimiter $$
create trigger status_value before insert on TABLE for each row
begin
if (new.status='DEFAULT' AND EXISTS (select 1 from TABLE where status='DEFAULT'))
then
signal sqlstate = '4500'
set message_text = 'can not update, default value already present';
end if;
end $$
delimiter ;
I'm assuming, that you mean the value of the column is a string "default", not the default value you defined when creating the table.