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
Related
I am trying to write an MYSQL statement that will copy the patient_id where they have a value of "Dose: Second" and paste it to my table vaccinatedpatients.
Also, I wanted to implement this within a trigger to automatically update itself after each new record on the table.
Any help with this will be great!
CREATE DEFINER=`root`#`localhost` TRIGGER `patient_AFTER_INSERT` AFTER INSERT ON `patient` FOR EACH ROW BEGIN
SELECT #variable = patient_id from patient where vaccine_Dose = 'Dose: Second';
INSERT INTO vaccinatedpatients VALUES(variable);
END
You can use an IF cLause, to determines if the patient gets the second dose
DELIMITER &&
CREATE DEFINER=`root`#`localhost` TRIGGER `patient_AFTER_INSERT` AFTER INSERT ON `patient` FOR EACH ROW
BEGIN
IF NEW.vaccine_Dose = 'Dose: Second' THEN
INSERT INTO vaccinatedpatients VALUES (NEW.patinet_id);
END IF;
END&&
DELIMITER ;
The DELIMITER may not be needed.
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)
I can enforce data restriction at the time of data insertion in a table using a trigger like following:
create table com(id int);
DELIMITER ;;
CREATE TRIGGER checkage_bi BEFORE INSERT ON com FOR EACH ROW
BEGIN
DECLARE dummy,baddata INT;
SET baddata = 0;
IF NEW.id > 20 THEN
SET baddata = 1;
END IF;
IF NEW.id < 1 THEN
SET baddata = 1;
END IF;
IF baddata = 1 THEN
SELECT CONCAT('Cannot Insert This Because id ',NEW.id,' is Invalid')
INTO dummy FROM information_schema.tables;
END IF;
END;;
this will restrict the values which will not meet the restriction condition.
But the problem is that if I insert multiple values along then for some values which do not meet the condition the other values will also be restricted from entry to table.
What I want is that when I insert multiple values along then the values that do not meet the condition should be skipped and the rest get inserted into the table
i.e. no any error is given.
Only the the bad values will be skipped and the other values will be inserted that's it.
Please give me some idea to do so.Thanks in advance...
You should better use stored Procedures rather than triggers for this purpose because you are not permitted within a stored function or trigger to modify a table that is already being used (for reading or writing) by the statement that invoked the function or trigger itself.
DELIMITER ;;
create table com(id int,var char(40));;
create procedure Validate(IN id int,IN var char(40))
BEGIN
declare entry_id int;
SET entry_id=id;
IF (id > 20) OR (id < 1) THEN
SET entry_id=NULL;
END IF;
insert into com values(entry_id,var);
END;;
Here is the output screen
This is the database vehicle table's trigger
DROP TRIGGER IF EXISTS InsertVehTrig;
DELIMITER $$
CREATE TRIGGER InsertVehTrig AFTER INSERT
ON Vehicle FOR EACH ROW
SWL_return:
BEGIN
DECLARE Cph CHAR(50);
DECLARE DevID CHAR(12);
DECLARE VehID BIGINT;
DECLARE TmpID BIGINT;
DECLARE DevCount INT;
SET Cph = rtrim(ltrim(NEW.cph));
SET VehID = NEW.ID;
SET DevID = NEW.DevID;
if(VehID is null) then
select count(id) into #DevCount from vehicle where (cph=#Cph) or (DevID=#DevID);
-- 条件:当前的车牌号 或 设备ID
end if;
if (DevCount > 1) then -- 如果记录数,超过1,则认为有重复
-- Rollback not supported in trigger
SET #SWV_Null_Var = 0;
Leave SWL_return;
else
if (DevCount = 1) then
select ID INTO #TmpID from Vehicle where (Vehicle.cph = #Cph) or (Vehicle.DevID = #DevID);
if (TmpID != VehID) then -- --如果增加的车牌号码与数据库中的在相同的,则不允许增加
-- Rollback not supported in trigger
Leave SWL_return;
SET #SWV_Null_Var = 0;
end if;
end if;
end if;
update vehicle set cph = #Cph where ID = #VehID;
END;
Right now i m trying to insert new data row in the vehicle table, but error with this
ERROR 1442: Can't update table 'vehicle' in stored function/trigger because it is already used by statement which invoked this stored function/trigger.
SQL Statement:
INSERT INTO `gis_server`.`vehicle` (`TrackerNum`, `cph`, `DevID`, `DevType`) VALUES ('1', 'NR09B00555', 'NR09B00555', '2')
those database are designed by 3 party company,
How do i insert data to vehicle table?
You can achieve this by making two changes to your approach:
Firstly, use a BEFORE trigger rather than an AFTER
Secondly, rather than updating the same table, update the NEW table, setting the column value to the new value before the NEW table hits the table targeted by the INSERT.
For example, replace your UPDATE line with the following:
UPDATE NEW SET cph = Cph;
MySQL doesn't allow you to edit the data in the table on which a trigger is created in that trigger, but you can edit the NEW table to modify the values going into that table.
I'm using mysqlimport to do mass table inserts (replacing duplicates primary keys). Several tables have date time columns with records containing values '0000-00-00'. What I would like is a trigger which detects these '0000-00-00' values and replaces with '1950-01-01', otherwise keep the datetime value in the record (i.e. when it's not '0000-00-00').
I have tried the following:
CREATE TRIGGER set_datetime BEFORE INSERT ON tbl
FOR EACH ROW
CASE
WHEN date_col='0000-00-00' THEN SET date_col='1950-01-01';
END CASE
Thank you.
EDIT
Update attempt:
CREATE TRIGGER set_datetime BEFORE INSERT ON tbl
FOR EACH ROW
IF (NEW.date_col='0000-00-00') THEN
SET NEW.date_col='1950-01-01';
END IF;
Still getting an error in the SET portion.
EDIT 2
WORKING:
DELIMITER $$
CREATE TRIGGER insert_check BEFORE INSERT ON ar
FOR EACH ROW
BEGIN
IF NEW.delivery_date='0000-00-00' THEN
SET NEW.delivery_date='1950-01-01';
END IF;
END; $$
DELIMITER ;
Use IF THEN :
IF (NEW.date_col = '0000-00-00') THEN
SET NEW.date_col='1950-01-01';
END IF;
CREATE TRIGGER set_datetime BEFORE INSERT ON tbl
FOR EACH ROW
IF YEAR(NEW.date_col)=0 THEN
SET NEW.date_col='1950-01-01 00:00:00';
END IF;
Give it a Try !!!
If you want, or need, to use 'case' then this should work:
SET NEW.date_col=CASE WHEN NEW.date_col='0000-00-00' THEN ='1950-01-01'
WHEN NEW.date_col='something else' THEN 'whatever'
ELSE 'default'
END CASE;