Trigger Insert with a SELECT/JOIN - mysql

I wrote a trigger INSERT AFTER: with I inside SELECT AND JOIN.
DROP TRIGGER IF EXISTS `InsertArticle`;
DELIMITER //
CREATE TRIGGER `InsertArticle` AFTER INSERT ON `article`
FOR EACH ROW
insert into log (LogTime, LogIdNote, LogName, LogType, LogIdUser, logTypeCategory, LogTypeUser, LogUrl)
select NEW.ArticleTime, NEW.idArticle, NEW.ArticleName, 1, NEW.ArticleToUserID, NEW.ArticleCategory, u.UsersTypeAccount, ct.URLCategorysubscribetotype
from users u where u.idUsers = NEW.ArticleToUserID LEFT JOIN categorysubscribetotype ct ON ct.CategoryTypeCategorysubscribetotype = 1;
END
//
DELIMITER ;
When I tried to create trigger, I get error 1422.
May be I have a wrong trigger sintax?

Try This:
DROP TRIGGER IF EXISTS `InsertArticle`;
DELIMITER //
CREATE TRIGGER `InsertArticle` AFTER INSERT ON `article`
FOR EACH ROW
insert into log (LogTime, LogIdNote, LogName, LogType, LogIdUser, logTypeCategory, LogTypeUser, LogUrl)
select NEW.ArticleTime, NEW.idArticle, NEW.ArticleName, 1 as LogType, NEW.ArticleToUserID, NEW.ArticleCategory, u.UsersTypeAccount, ct.URLCategorysubscribetotype
from users u LEFT JOIN categorysubscribetotype ct ON ct.CategoryTypeCategorysubscribetotype = 1 where u.idUsers = NEW.ArticleToUserID;
END
//
DELIMITER ;

Related

TRIGGER with conditional IF statement

Drop TRIGGER if exists triggername;
DELIMITER $$
CREATE TRIGGER triggername
AFTER UPDATE ON table1 FOR EACH ROW
BEGIN
IF (
Select person from table1 e
JOIN table1Type et ON e.table1TypeID = et.table1TypeID
where et.Description = 'University Degree' and e.Active = 1)
THEN update table2
set field = 1 ;
ELSE update table2 set field = 0;
END IF;
END$$
DELIMITER ;
I am trying to use a conditional statement to update another table if that condition is met.
So in other words, if the person in table 1 has a type of university degree and is active then update another field in table 2.
CREATE TRIGGER triggername
AFTER UPDATE
ON table1
FOR EACH ROW
UPDATE table2
SET field = EXISTS (SELECT NULL
FROM table1 e
JOIN table1Type et USING (table1TypeID)
WHERE et.Description = 'University Degree'
AND e.Active = 1);
Maybe you must use NOT EXISTS (the logic is unclear).
PS. DELIMITER not needed for this trigger code.

Why I have this error in my trigger? MySQL

I am creating a MySQL database, I want to create a trigger to change the amount of inventory available after a sale, but I get an error
This is the code of the trigger
DROP TRIGGER IF EXISTS tr_inventario;
CREATE TRIGGER tr_inventario AFTER INSERT ON factura FOR EACH ROW
UPDATE producto INNER JOIN factura ON producto.IdProducto=factura.IdProducto
SET producto.Invenatario=producto.Invenatario-factura.Cantidad
WHERE producto.IdProducto=factura.IdProducto;
This is the INSERT that i want to do
INSERT INTO factura(FechaVenta, NombreCliente, IdProducto, IdEmpleado, Cantidad) VALUES (now(), 'MarĂ­a Guadalupe',3, 2, 2);
And this is the error that I get
Error Code: 1048. Column 'Invenatario' cannot be null
relational table
You don't need a JOIN:
DELIMITER $$
CREATE TRIGGER tr_inventario AFTER INSERT ON factura FOR EACH ROW
BEGIN
UPDATE producto p
SET p.Invenatario = p.Invenatario - new.Cantidad
WHERE p.IdProducto = new.IdProducto;
END;

MySql trigger with where clause

This is my first trigger in MySql and I am having a few problems. I tried both of these pieces of code but both would not compile. I got it to work without the where clause.
CREATE TRIGGER ins_meal_details
AFTER INSERT ON meal_details
FOR EACH ROW
INSERT INTO sql_changes
SET
sc_table='book_room',
sc_reason='DINNER1',
sc_key='bh_no=NEW.bh_no,date=NEW.md_date',
sc_value='1',
sc_done =0
WHERE not exists (select 1 from booking where bh_no = NEW.bh_no and bo_date = NEW.md_date and bo_meals < 1)
CREATE TRIGGER ins_meal_details AFTER INSERT meal_details FOR EACH ROW
BEGIN
IF NOT EXISTS (select 1 from booking where bh_no = NEW.bh_no and bo_date = NEW.md_date and bo_meals < 1) THEN
INSERT INTO sql_changes (sc_table, sc_reason, sc_key, sc_value, sc_done )
VALUES ('book_room','DINNER1', 'bh_no=NEW.bh_no,date=NEW.md_date','1', 0);
END IF
END
CREATE TRIGGER ins_meal_details
AFTER INSERT
ON meal_details
FOR EACH ROW
INSERT INTO sql_changes (sc_table,
sc_reason,
sc_key,
sc_value,
sc_done)
SELECT 'book_room',
'DINNER1',
CONCAT('bh_no=',NEW.bh_no,',date=',NEW.md_date),
1,
0
WHERE NOT EXISTS (SELECT 1
FROM booking
WHERE bh_no = NEW.bh_no
AND bo_date = NEW.md_date
AND bo_meals < 1);
MySql did not like the select/where exists in my code when there is no table specified. This was due to using version 5.6 of MySql server.
This will not work: select 'works' where exists (select 1 from my-table)
The fix would be thanks to #akina to add from DUAL. The best solution.
I got round it by using a count(*) instead :-
DROP TRIGGER IF EXISTS ins_meal_details;
DELIMITER //
CREATE TRIGGER ins_meal_details
AFTER INSERT ON meal_details FOR EACH ROW
BEGIN
IF (select count(*) from booking where bh_no = NEW.bh_no and bo_date = NEW.md_date and bo_meals < 1) > 0 THEN
INSERT INTO sql_changes (sc_table,
sc_reason,
sc_key,
sc_value,
sc_done)
VALUES ('book_room','DINNER1', CONCAT('bh_no=',NEW.bh_no,',date=',NEW.md_date),'New Value', 0);
END IF;
END//
DELIMITER ;

mysql trigger error with 2 conditions

I want to add an after insert trigger which will do the following.
The first IF condition works normally, but when it comes to the second everything stops.
Any ideas?
USE `Syslog`;
DELIMITER $$
CREATE TRIGGER `SystemEventsR_AINS` AFTER INSERT ON SystemEventsR FOR EACH ROW
IF
(exists
(select syslogtag from SystemEventsRcounter where syslogtag=
new.syslogtag)
AND
(select simpledate from SystemEventsRcounter
where syslogtag=new.syslogtag)=new.simpledate)
THEN
UPDATE SystemEventsRcounter
SET records=records+1
WHERE SystemEventsRcounter.syslogtag=new.syslogtag;
ELSE INSERT SystemEventsRcounter (simpledate, syslogtag, records) values (new.simpledate,new.syslogtag,1);
END IF
UPDATED:
What you need is INSERT INTO ... ON DUPLICATE KEY.
CREATE TRIGGER `SystemEventsR_AINS`
AFTER INSERT ON SystemEventsR
FOR EACH ROW
INSERT INTO SystemEventsRcounter (simpledate, syslogtag, records)
VALUES (NEW.simpledate, NEW.syslogtag, 1)
ON DUPLICATE KEY UPDATE records = records + 1;
In order for it to work you need to create a unique composite index on (simpledate, syslogtag)
CREATE UNIQUE INDEX idx_u_simpledate_syslogtag
ON SystemEventsRcounter (simpledate, syslogtag);
Here is SQLFiddle demo.
If you wanted it your way then it might look like
DELIMITER $$
CREATE TRIGGER `SystemEventsR_AINS`
AFTER INSERT ON SystemEventsR
FOR EACH ROW
BEGIN
IF (
SELECT COUNT(*) simpledate
FROM SystemEventsRcounter
WHERE syslogtag = NEW.syslogtag
AND simpledate = NEW.simpledate
) > 0 THEN
UPDATE SystemEventsRcounter
SET records = records + 1
WHERE SystemEventsRcounter.syslogtag = NEW.syslogtag;
ELSE
INSERT INTO SystemEventsRcounter (simpledate, syslogtag, records)
VALUES (NEW.simpledate, NEW.syslogtag, 1);
END IF;
END$$
DELIMITER ;
Here is SQLFiddle demo.

MySQL Trigger to insert data into different DB

It sounds so simple... I spent a few hours just getting the first part working which was a mysql trigger to a different database. Now I want to get smart and JOIN a couple tables.
I have two master tables PROJ and COMP. Both share id. When PROJ gets inserted I want to insert some of the NEW.PROJ info and some of the COMP info into a single row in the db.table axis.axis_data
Would someone please help me do a SELECT...INSERT with a TRIGGER. I might be in over my head on this one
My WORKING Trigger.
DELIMITER $$
DROP TRIGGER IF EXISTS `rate_data_trigger` $$
CREATE TRIGGER rate_data_trigger
BEFORE INSERT on PROJ FOR EACH ROW
BEGIN
INSERT INTO axis.axis_data
(projinfo_table_id, rate_user, name,
property_owner, property_address, property_city,
property_state, property_zip, property_phone,
rating_date, rating_type, rating_reason, rating_number
)
VALUES
(NEW.id, user(), NEW.BLGNAME,
NEW.POWNER, NEW.STREET, NEW.CITY,
NEW.STATE, NEW.ZIP, NEW.PHONE,
NEW.RATDATE, NEW.RATTYPE, NEW.RATREAS, NEW.RATNGNO
);
END$$
DELIMITER ;
Simply use the following syntax in your select statement:
INSERT INTO axis.axis_data
(projinfo_table_id, rate_user, name,
property_owner, property_address, property_city,
property_state, property_zip, property_phone,
rating_date, rating_type, rating_reason, rating_number,
field1, field2
)
SELECT NEW.id, user(), NEW.BLGNAME,
NEW.POWNER, NEW.STREET, NEW.CITY,
NEW.STATE, NEW.ZIP, NEW.PHONE,
NEW.RATDATE, NEW.RATTYPE, NEW.RATREAS, NEW.RATNGNO,
c.field1, c.field2
FROM COMP c WHERE c.id = NEW.id
If COMP doesn't always have a corresponding record in PROJ, you can do use SELECT ... FROM DUAL LEFT JOIN COMP c ON c.id = NEW.id