MySQL if condition inside a trigger - mysql

I have 2 tables, lets name it table A and table B, every time something is inserted in the table A I want some of the data (id, name, comment) automatically inserts also in the table B but only if there is no raw in table B with the same name.
Example:
If I insert OBJECT1 in A with values:
id=1, colour=2, name=example, price=5€, comment="blablabla"
it should be inserted in the table B a row with this values:
id=1, name=example, comment="blablabla"
but if then I insert OBJECT2 in A with values:
id=2, colour=5, name=example, price=10€, comment="aaa";
nothing should happen in the table B, because there is already a row in table B with name=example.
This is my code:
CREATE TRIGGER mytrigger
BEFORE INSERT ON tablea
IF NOT EXISTS (SELECT * FROM tableb WHERE tableb.name = NEW.name)
THEN
INSERT INTO tableb (id, name, comment)
VALUES (NEW.id, NEW.name, NEW.comment)
END IF
And this is the error I receive:
You have an error in your SQL Syntax...
I also tried adding BEGIN and END at the starting and end of the definition, also tried with END instead of END IF, also tried BEGIN instead of THEN
Does someone know what am I doing wrong?

In PL/SQL, you can't put an EXISTS() in an IF statement.
So please try this:
CREATE OR REPLACE TRIGGER mytrigger
BEFORE INSERT ON tablea REFERENCING NEW AS NEW
FOR EACH ROW
declare cnt integer;
BEGIN
SELECT count(*) into cnt FROM tableb WHERE tableb.name = :NEW.name;
IF cnt >0
THEN
INSERT INTO tableb (id, name, comment)
VALUES (:NEW.id, :NEW.name, :NEW.comment)
END IF
END;

Related

Trigger to insert values from Table C into tableB after insert On Table A

I am trying to create a trigger in mysql that updates Table B with values from Table C, after a new record is inserted into Table A. I've tried a few variations of the code below, but can't get it to work. Can anyone point me in the right direction?
delimiter //
CREATE TRIGGER updateC AFTER Insert ON Table A
FOR EACH ROW in TableB
BEGIN
IF TableB.Group = 1 THEN
Insert into TableC(inserthere) values (TableB.AddThis);
END IF;
END;//
delimiter ;
Table A includes the fields ProjectID | TaskGroup.
Table B includes the fields ID | DefaultTask | TaskGroup.
Table C includes the fields ID | ProjectID | DefaultTask.
When a new ProjectID & TaskGroup are added to table A, I would like to find all DefaultTasks in TableB that have the same TaskGroup, and then insert those DefaultTasks & the ProjectID as new records in Table C
After You e explanation in the comment.
This shows you how to do it, you Must test The INSERT Query, it assumes that id is an autoincrement.
Every NEW.columname is the value of the inserted row.
With INSERT INTO ... SELCT You can male a bulk insert from all Values from Taböleb that fit the bill
DELIMITER //
CREATE TRIGGER updateC AFTER Insert ON TableA
FOR EACH ROW
BEGIN
INSERT INTO TableC (ProjectID,DefaultTask) SELECT NEW.ProjectID,DefaultTask
FROM TABLEB
WHERE TaskGroup = NEW.TaskGroup;
END;//
DELIMITER ;
FYI: Write sql Comands in UPPER case, it smales the reading much more simple
Can anyone point me in the right direction?
The record inserted into A must define the record(s) in B which must be updated, and the records in C which values must be taken for this update.
For example, it may be
CREATE TRIGGER updateC
AFTER INSERT
ON Table A
FOR EACH ROW
UPDATE B, C
SET B.field1 = C.field2
WHERE B.field3 = NEW.field4
AND C.field5 = NEW.field6;

Mysql trigger multiply copies to another table

I have a trigger which copies the inserted data from my table 1 to table 2. I tried inserting 2 data into table 1 and it copies it to my table 2 but the the problem is it copies 2 times the data. Whenever I insert A, B, C into table 1 the value being copied in my table 2 is A, A, A, B, B, B, C, C, C. Please help.
delimiter //
CREATE TRIGGER `copy_table` AFTER INSERT ON table_2
insert into table_1 (id, code, name)
select id, code, name
from table_2;
END;
//
delimiter ;
Your problem is that you are inserting all the data from table_2 into table_1 each time your trigger executes. You need to only insert the new values, which you can do by referring to the NEW pseudo-table:
delimiter //
CREATE TRIGGER `copy_table` AFTER INSERT ON table_2
FOR EACH ROW
BEGIN
INSERT INTO table_1 (id, code, name)
VALUES (NEW.id, NEW.code, NEW.name);
END;
//
delimiter ;
Demo on dbfiddle
Update
If you want the trigger to only insert values if they don't already exist in table_1, you can either
add a UNIQUE index on (id, code, name) to table_1 and use INSERT IGNORE in the trigger; or
Update the trigger to check that the data doesn't exist in the table already:
CREATE TRIGGER `copy_table` AFTER INSERT ON table_2
FOR EACH ROW
BEGIN
IF NOT EXISTS (SELECT *
FROM table_1
WHERE id = NEW.id AND code = NEW.code AND name = NEW.name) THEN
INSERT INTO table_1 (id, code, name) VALUES (NEW.id, NEW.code, NEW.name);
END IF;
END;
Demo on dbfiddle

Insert a record and also update existing records

hi there i was trying to right a Proc for a DataLoad when you want to insert records on a Table1 based on Table2
This is what i came of with
it has 2 condition
1) if the record does not exist create a new row of record
2) if the record already exist update the record based on keys
this is my proc need some help thanks
DECLARE #TableKey INT --(it is passed by the user proc param),
DECLARE #TableCount INT,
DECLARE #CLassKey INT,
SELECT #TableCount= COUNT(*) FROM Table1 WHERE Tablekey= #TableKey
INSERT INTO #CLassKey
SELECT Distinct c.PK_ClassKey FROM CLASS as c
INNER JOIN BOOK as B ON B.FK_ClassKey=C.PK_ClassKEy
IF ((SELECT COUNT(*) FROM #ClassKey) > 0 AND #TableCount= 0)--- this will check
BEGIN
Insert into NOTE
n.note
Select
c.note
FROM Class where c.FK_Note = n.PK_Note.
END
---- this will just insert for the first time..
How do i update it any idea as the records are only inserted for the first time put does not update using the same format thanks a lot
try this one
INSERT INTO table_name (id,col2,col3)
VALUES (value_id,value2,value3)
ON DUPLICATE KEY UPDATE
col2=value2,
col3=value3;

IF condition on MySQL trigger

I have two tables (Table A, Table B) with the same fields (user_id, domi, ipiresia).
Users can add values in Table A. I want these values to be inserted automatically in Table B BUT only when the user_id is not included in Table B.
on.
CREATE TRIGGER new_value_added
AFTER INSERT ON table_a
FOR EACH ROW
IF NEW.user_id != user_id
INSERT INTO table_b (user_id, domi, ipiresia)
VALUES (NEW.user_id, NEW.domi, NEW.ipiresia);
I know that the IF statement is wrong because the trigger doesn't work as expected.
Any help?
You don't need an IF at all. Just do an insert select on your trigger like this:
INSERT INTO table_b (user_id, domi, ipiresia)
SELECT NEW.user_id, NEW.domi, NEW.ipiresia
FROM table_a
WHERE NOT EXISTS (select 1 from table_b where user_id = NEW.user_id);

BEFORE INSERT TRIGGER on one table but insert should be done on another table

I have two tables say table1 and table2.
fields of table1 - id and count
fields of table2 - id and count
both tables have same fields. Now I want to create BEFORE INSERT TRIGGER on table1 which will check if count = 2 then insert into table2 otherwise in table1.
I created the trigger but when I fire this query ..example-
insert into table1 (id, count) values (1323, 2);
then one row is inserted into table2 (this I want) and same row is inserted into table1 also.
I want row should be inserted in table1 or table not in both tables.
what I do to achieve this ?
Try with this:
delimiter $$
CREATE TRIGGER myTrigger
BEFORE INSERT ON table1
FOR EACH ROW
BEGIN
IF NEW.count = 2 THEN
insert into table2 (id, count) values (New.id,New.Count);
ELSE
<<no action>>
END IF;
END$$
I'm not sure why you want this, so I suspect I might be giving advice on some sort of solution that should be fixed differently, but here we go.
As you can see in this question here it is not possible to neatly stop an insert. You could try and make an error so you'll not actually insert, but I think that is really painful.
What you can do is make a fake table dummytable and put your insert trigger there. Your row will always insert in the dummy table, but you either insert it in table1 or table2 according to your wishes. The meat of the trigger would look like
IF NEW.count = 2 THEN
INSERT INTO table2 (id, count) VALUES (New.id,New.Count);
ELSE
INSERT INTO table2 (id, count) VALUES (New.id,New.Count);
END IF;
You'll have to clean the dummytable now and then.