Trigger before insert on not concatenating columns - mysql

I have create a trigger where the first column and second column would be concatenated into 3rd column.
USE `customer_1`;
DELIMITER $$
CREATE DEFINER=`root`#`%` TRIGGER `Metric1_anpr_vega_BINS` BEFORE INSERT ON `Metric1_anpr_vega` FOR EACH ROW
BEGIN
SET NEW.image_id = CONCAT(NEW.camera_id,'-', NEW.id);
END
Id camera_id image_id
4567 236 236-0
( here i can't get id of the row when triggered it is '0' instead of 4567)
what trigger would get the ID value in image_Id after insert statment exceuted.
If i use after insert I get this error - Updating of NEW row is not allowed in after trigger

USE `customer_1`;
DELIMITER $$
CREATE DEFINER=`root`#`%` TRIGGER `Metric1_anpr_vega_BINS` AFTER INSERT ON `Metric1_anpr_vega` FOR EACH ROW
BEGIN
SET NEW.image_id = CONCAT(NEW.camera_id,'-', NEW.id);
END

Related

Create a trigger that finds string value and copies ID to insert to new table

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.

Database Trigger Insert from other table

I have two tables as described below. I made a trigger which must be executed on the sgr_customer_group table after a new insert into sgr_customer table:
After a new insert into sgr_customer, a new record with id_customer (Primary key of sgr_customer) and the constant 4 must be inserted into sgr_customer_group table.
DELIMITER $$
CREATE TRIGGER sgr_customer_trig
AFTER INSERT ON `sgr_customer` FOR EACH ROW
begin
DECLARE id_exists Boolean;
SELECT 1
INTO #id_exists
FROM sgr_customer
WHERE sgr_customer.id_customer= NEW.id_customer;
IF #id_exists = 1
THEN
Insert into sgr_customer_group (id_customer, id_group)VALUES(id_customer,4);
END IF;
END
$$
DELIMITER ;
Table definitions:
sgr_customer_group(id_group,id_customer#,id_group );
id_group:Primary Key
sgr_customer(id_customer,......);
id_customer:Primary key
Great Thanks.
Everything #Uueerdo wrote in their comment to your question is correct, so the trigger can be simplified to:
DELIMITER $$
CREATE TRIGGER sgr_customer_trig
AFTER INSERT ON `sgr_customer` FOR EACH ROW
BEGIN
INSERT INTO sgr_customer_group (id_customer, id_group) VALUES(NEW.id_customer, 4);
END
$$
DELIMITER ;

sum trigger on insert Mysql

I have a table called sum_test with id,a,b,c fields
I need a trigger that calculates the sum of a+b on c but Im new to triggers.
How can I do that on Mysql?
CREATE TRIGGER `example` AFTER INSERT ON `sum_test`
FOR EACH ROW BEGIN
UPDATE sum_test
SET c= (a+b)
WHERE id = id
END
If we are wanting to set column c on the row we are inserting, then we can do like this, in a BEFORE insert trigger:
DELIMITER $$
CREATE TRIGGER `sum_test_bi` BEFORE INSERT ON `sum_test`
FOR EACH ROW
BEGIN
SET NEW.c = NEW.a + NEW.b ;
END$$
DELIMITER ;
I don't understand the reference to table suma. What does that table have to do with anything? We note that the condition id = id is going to evaluate to TRUE for every row in the table where id IS NOT NULL.

I have error "#1363 - There is no NEW row in on DELETE trigger "

I have table "book" and "store_order" have relation
I want to make trigger(but it contain error):
DELIMITER $$
CREATE TRIGGER t1
before delete ON store_order
FOR EACH ROW
BEGIN
update book set number = number + NEW.quantity where ISBN = NEW.ISBN;
END
$$
DELIMITER ;
DELIMITER $$
CREATE
TRIGGER t2 AFTER delete
ON library.store_order
FOR EACH ROW BEGIN
update library.book
set library.book.number = (library.book.number + OLD.quantity)
where library.book.ISBN = OLD.ISBN;
END$$
DELIMITER ;
Use OLD instead of NEW when you want to get the deleted object.
for an example of my case. I'm getting the id of the newly added role by calling
NEW.id
and getting the same field's value while deleting by calling
OLD.id
Example:
DELIMITER $$
CREATE TRIGGER after_insert_role
AFTER INSERT ON role FOR EACH ROW
BEGIN
INSERT INTO `sync_mapping`
(`operation_type`, `table_name`, `oid`, `end_point`)
VALUES
('insert', 'role', NEW.id, 'new/role');
END $$
DELIMITER $$
CREATE TRIGGER after_delete_role
AFTER DELETE ON role FOR EACH ROW
BEGIN
INSERT INTO `sync_mapping` (`operation_type`, `table_name`, `oid`, `end_point`) VALUES
('delete', 'role', OLD.id, 'delete/role');
END $$
Whenever we are deleting the data
USE
OLD. instead of NEW.
CREATE TRIGGER user_history_delete AFTER DELETE ON user FOR EACH ROW
INSERT INTO user_history(action , name )
VALUES ("Delete" , OLD.name );

#1442 - Can't update table 'sale_price' in stored function/trigger because it is already used by statement

I am trying to update the same table before inserting new row in the table.
I want to set the status of all previous rows with the same product_id to 0 and after that insert new row with the status 1....Please help. here is my code written mysql..
DROP TRIGGER IF EXISTS `priceStatusUpdate`//
CREATE TRIGGER `priceStatusUpdate` BEFORE INSERT ON `sale_price`
FOR EACH ROW BEGIN
UPDATE sale_price
SET status=0
WHERE product_id=new.product_id;
END
//
Use a stored procedure instead:
DELIMITER $$
CREATE PROCEDURE procedureName (IN p_product_id)
BEGIN
UPDATE sale_price SET status = 0 WHERE product_id = p_product_id;
INSERT INTO sale_price (product_id) VALUES (p_product_id);
END $$
DELIMITER ;
Then call it with
CALL procedureName(1);
DROP TRIGGER IF EXISTS `priceStatusUpdate`//
CREATE TRIGGER `priceStatusUpdate` BEFORE INSERT ON `sale_price`
FOR EACH ROW BEGIN
SET NEW.status= 0;
END