Why I have this error in my trigger? MySQL - 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;

Related

MySQL - Insert Into 2 Tables on 1 Procedure

I need to create a procedure for inserting records into 2 tables, but on the second table, I want to insert the last ID that was inserted on the first table. Could anyone help me with this?
This is my query
DELIMITER //
DROP PROCEDURE IF EXISTS ROOM_FEATURE_INSERT;
CREATE PROCEDURE ROOM_FEATURE_INSERT (propID INT, featID INT, featNme VARCHAR(50))
BEGIN
-- BEGIN CHECK
IF NOT EXISTS
(
SELECT rFeatureName FROM COMPANY_T3s71.PROPERTY_RFEATURE PRFE
INNER JOIN COMPANY_T3s71.ROOM_FEATURE RFEA ON PRFE.rFeatureID=RFEA.rFeatureID
WHERE BINARY rFeatureName = featNme AND propertyID = propID
)
AND
(
SELECT rFeatureName FROM COMPANY_T3s71.ROOM_VIEW
WHERE BINARY rFeatureName = featNme
)
THEN
-- IF NOT EXISTS INSERT INTO 1st TABLE
INSERT INTO COMPANY_T3s71.ROOM_FEATURE (rFeatureName) VALUES (featNme);
END IF;
-- END CHECK
-- BEGIN CHECK 2nd TABLE
IF NOT EXISTS
(
SELECT propertyID, rFeatureID FROM COMPANY_T3s71.PROPERTY_RFEATURE
WHERE rFeatureID = featID AND propertyID = propID
)
THEN
-- IF NOT EXISTS INSERT INTO 2nd TABLE
INSERT INTO COMPANY_T3s71.PROPERTY_RFEATURE (propertyID, rFeatureID) VALUES (propID, featID);
END IF;
-- END CHECK 2nd TABLE
END
DELIMITER ;
How do we pass the featID param, when we just inserted it on the first INSERT query?
Thank you before hand.
Use SET featID = LAST_INSERT_ID(); after the first query and then use the variable
INSERT INTO COMPANY_T3s71.ROOM_FEATURE (rFeatureName) VALUES (featNme);
SET featID = LAST_INSERT_ID();
However, if the data is not insert at anytime then you have to make query in the if block to set the value for featID.

Mysql said: #1422 - Explicit or implicit commit is not allowed in stored function or trigger

In the code below I'm trying to create a trigger which inserts 'incidentimg' table id along with an image path if the user of the web application did not upload a image with the form belonging to 'incidentreport' table, however, I'm only getting the error "MySQL said: #1422 - Explicit or implicit commit is not allowed in stored function or trigger"
DROP TRIGGER IF EXISTS `insertImage`;
CREATE TRIGGER `insertImage` AFTER INSERT ON `incidentreport`
FOR EACH ROW
BEGIN
IF(SELECT incidentimg.incidentImgId FROM incidentimg
LEFT JOIN incidentreport ON incidentimg.imgs_incidentReportId = incidentreport.incidentReportId
WHERE incidentimg.incidentImgId IS NULL)
THEN INSERT INTO incidentimg(incidentImgId, imgUrl)values(NEW.incidentReportId, 'images/no-image.png');
END IF;
END
;
Here are the tables being used
Can someone assist me on how to correct this problem
I finally got the code to work. Now the code will check whether 'incidentimg' table has all the 'incidentReportId' that the 'incidentreport' table has, and if it doesn't then the last inserted id from the 'incidentreport' table along with a default image path will be inserted into 'incidentimg' table AFTER INSERT.
CREATE TRIGGER insertImage AFTER INSERT ON incidentreport
FOR EACH ROW
BEGIN
IF(SELECT incidentreport.incidentReportId
FROM incidentreport
LEFT JOIN incidentimg ON incidentreport.incidentReportId = incidentimg.imgs_incidentReportId
WHERE incidentimg.imgs_incidentReportId IS NULL)
THEN INSERT INTO incidentimg(imgs_incidentReportId, imgUrl) VALUES(NEW.incidentReportId, 'images/no-image.png');
END IF;
END
I am not much sure but I am guessing that probably it's because of the IF .. THEN conditional INSERT. Try modifying your INSERT statement with an EXISTS like
DROP TRIGGER IF EXISTS `insertImage`;
CREATE TRIGGER `insertImage` AFTER INSERT ON `incidentreport`
FOR EACH ROW
BEGIN
INSERT INTO incidentimg(incidentImgId, imgUrl)
SELECT NEW.incidentReportId, 'images/no-image.png'
FROM DUAL WHERE EXISTS (
SELECT 1 FROM incidentimg
LEFT JOIN incidentreport
ON incidentimg.imgs_incidentReportId = incidentreport.incidentReportId
WHERE incidentimg.incidentImgId IS NULL
)
END;

Mysql trigger to insert on two connected tables

I have 3 mysql tables:
user_followers has columns id,user_id
notification has columns id,user_id
notification_object has columns notification_id , type
On insert on user_followers i want to insert
user_id=New.user_id in notification
and then using the id of notification just inserted
notification_object.type = "2" and notification_object.notification_id = just inserted id in notifcication table
now the problem is how can i find the id of notifcation table:
DELIMITER //
CREATE TRIGGER follow_notification AFTER INSERT ON user_friends
FOR EACH ROW
Begin
insert into notification(user_id) values(New.user_id);
insert into notification_object(notification_id,type) values(????,"2");
End
//
if notifications.id is auto-incremented, mysql includes a function last_insert_id() that will return (with caveats) the lastly inserted id.
DELIMITER //
CREATE TRIGGER follow_notification AFTER INSERT ON user_friends
FOR EACH ROW
Begin
declare v_notification_id INT;
insert into notification(user_id) values(New.user_id);
/* set a variable to the last id inserted to the db */
set v_notification_id=(select last_insert_id());
/* use the variable in the next insert */
insert into notification_object(notification_id,type) values(v_notification_id,"2");
End//

MYSQL trigger issue 33999

I am trying to create a live update in a history table. Whenever record is inserted/updated in o_training approval table , a corresponding record should be INSERTED in o_training_appr_history table. For this i created below triggers.
appr_after_insert
**DELIMITER $$
create trigger olattest.appr_after_insert after insert on o_training_approval
for each row
BEGIN
INSERT INTO o_training_appr_history
select
NEW.request_id,
NEW.user_id,
m.band,
m.grade,
NEW.approver_id,
NEW.approver_role,
NEW.project_id,
NEW.course_id,
NEW.comments,
NEW.submitted_date,
NEW.status_id,
m.vertical,
m.sub_vertical,
m.city,
m.location_type as location
from o_training_approval t join o_master_employees m
on(t.user_id = m.emp_id)
where m.emp_id=NEW.user_id;
END;
$$**
appr_after_update
**DELIMITER $$
create trigger olattest.appr_after_update after update on o_training_approval
for each row
BEGIN
INSERT INTO o_training_appr_history
select
NEW.request_id,
NEW.user_id,
m.band,
m.grade,
NEW.approver_id,
NEW.approver_role,
NEW.project_id,
NEW.course_id,
NEW.comments,
NEW.submitted_date,
NEW.status_id,
m.vertical,
m.sub_vertical,
m.city,
m.location_type as location
from o_training_approval t join o_master_employees m
on(t.user_id = m.emp_id)
where m.emp_id=NEW.user_id;
END;
$$**
Both the triggers got created without any error. However, on insert/update on the base table , no record is getting inserted in the history table.
I am clueless as for the reason and solution. Could anyone please help in this regard?

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.