I am trying to create a trigger that will multiply the sales_price field by -1, the sales table is like this transaction type, it has varchar R for refund and S for Sales.
This is the trigger I am trying to create:
delimiter $$
CREATE trigger refund_negative before insert on sale
for each row
begin
set new.SALE_PRICE = new.SALE_PRICE *(-1)
where TRA_TYPE = 'R';
end;
delimiter $$
Try this one:
CREATE TRIGGER `refund_negative` BEFORE INSERT ON `sales` FOR EACH ROW BEGIN
IF NEW.TRA_TYPE='R' THEN
SET NEW.price = NEW.price * -1;
END IF;
END
so basically it just test whether TRA_TYPE is equal to 'R' and if so, NEW.price is multiplied by negative one.
Related
I'm trying to do a room capacity validation for a specific date using a before trigger.
This is the current trigger I'm using:
delimiter $$
create trigger my_insert_trigger before insert on my_table
for each row
begin
if (select count(*) from my_table where room_type = new.room_type) > 3 then
signal sqlstate '45000';
end if;
end;
$$
delimiter ;
I currently have two columns in the same table which are date and room, this is my desired output
However this would not be the case because the trigger will still limit 'Single' three times regardless of date.
do a room capacity validation for a specific date
Just add the date to the control query:
delimiter $$
create trigger my_insert_trigger before insert on my_table
for each row
begin
if (
select count(*)
from my_table t
where t.room_type = new.room_type and t.date = new.date
) >= 3 then
signal sqlstate '45000';
end if;
end;
$$
delimiter ;
Note: because it has condition > 3, your existing code would allow 4 records per room. I changed that to >= 3 so only 3 records are allowed per room and date, which seems to be what you are looking for.
I have three tables and I have one trigger from table D_ISI in table 2 to trigger MAX_ISI value to write to the other table. Can you help me solve the trigger problem?
DELIMITER $$
CREATE TRIGGER `ISI_YUKSEK_ISE` BEFORE INSERT ON `TB1` FOR EACH ROW BEGIN
DECLARE
D_ISI INT ;
SET
D_ISI = NEW.D_ISI ; IF
(SELECT D_ISI = NEW.D_ISI FROM TB1,TB2 WHERE TB2.CIHAZ_ID=TB1.CIHAZ_KODU AND TB1.D_ISI >= TB2.MAX_ISI OR TB1.D_ISI <= TB2.MIN_ISI) THEN
INSERT
INTO
TB3(CIHAZ_KODU,D_ISI)
VALUES(NEW.CIHAZ_KODU,NEW.D_ISI) ;
END IF ; END
$$
DELIMITER ;
I have two tables stock and order and i am trying to get the trigger to work so that after the an order has been put in the order quantity is then taken away from the stock quantity field.
DELIMITER $$
CREATE TRIGGER stock_update
AFTER INSERT ON order
FOR EACH ROW
BEGIN
SET #quantity = NEW.quantity
UPDATE stock s;
SET s.quantity = OLD.quantity - NEW.quantity
FROM stock s, order o
WHERE s.ID_stock = o.ID_stock;
END;
$$
DELIMITER;
but i am just getting the error number 1064
There are several issues:
order is a reserved word, therefore you need to use back ticks around it
you don't need to use a variable #quantity
update statement was wrong
DELIMITER and ; should be separated with a space
Try this
DELIMITER $$
CREATE TRIGGER stock_update
AFTER INSERT ON `order`
FOR EACH ROW
BEGIN
UPDATE stock s
SET s.quantity = s.quantity - NEW.quantity
WHERE s.ID_stock = NEW.ID_stock;
END $$
DELIMITER ;
I'm not quite sure it will work, since I would need the table definitions in order to test it.
In each case you should remove the FROM clause and also the last line DELIMITER;. You
should also end the line SET #quantity = NEW.quantity whith a semicolon.
DELIMITER $$
CREATE TRIGGER stock_update
AFTER INSERT ON order FOR EACH ROW
BEGIN
UPDATE stock s;
SET s.quantity = OLD.quantity - NEW.quantity
WHERE s.ID_stock = NEW.ID_stock;
END;
$$
I want to create a trigger so that:
if the AMOUNT field < 100 the AMOUNT field automatically inserts 100.
If AMOUNT > 100 the entered value should be inserted in the AMOUNT field.
Table:
CREATE TABLE DB_triggers.acc(
C_ID INT(3) UNIQUE NOT NULL AUTO_INCREMENT ,
customer_name CHAR(30),amount FLOAT(10,2) UNSIGNED,
date_created DATE,PRIMARY KEY(C_ID,customer_name)
) AUTO_INCREMENT=100;
Trigger:
DELIMITER $$
CREATE TRIGGER trig_acc
BEFORE INSERT ON acc
FOR EACH ROW
BEGIN
IF amount <100 THEN
INSERT INTO acc VALUES(new.c_id,new.customer_name,100,new.now());
ELSE
INSERT INTO acc VALUES(new.c_id,new.customer_name,new.amount,new.now());
END IF;
END $$
DELIMITER ;
BUT, when I insert a value :
INSERT INTO
acc(c_id,customer_name,amount,date_created)
VALUES
(DEFAULT,'ABC',1000,NOW());
gives error 1054
Change IF amount <100 THEN to IF new.amount <100 THEN then it will work.
Try this:
DELIMITER $$
CREATE TRIGGER trig_acc
BEFORE INSERT ON acc
FOR EACH ROW
BEGIN
IF new.amount <100 THEN
INSERT INTO acc VALUES(new.c_id,new.customer_name,100,new.now());
ELSE
INSERT INTO acc VALUES(new.c_id,new.customer_name,new.amount,new.now());
END IF;
END $$
DELIMITER ;
I have a problem with a MySQL Trigger.
I have 3 tables Customers, Products and Sales.
In Sales I reference customer and product and I want to update the some counts on Products and Customers after a new sale is inserted.
The following trigger fails to update both tables... I cannot figure out what I am doing wrong.
DELIMITER $
CREATE TRIGGER OnSalesInsert AFTER INSERT ON Sales
FOR EACH ROW BEGIN
UPDATE Products SET Products.sold=Products.sold+NEW.amount WHERE Products.id=NEW.product_id;
UPDATE Customers SET Customers.amount=Customers.amount+NEW.amount WHERE Customers.id=NEW.customer_id;
END $
DELIMITER ;
Try this:
DELIMITER $$
CREATE
/*!50017 DEFINER = 'root'#'%' */
TRIGGER `OnSalesInsert` BEFORE INSERT ON `Sales`
FOR EACH ROW BEGIN
UPDATE Products
SET sold = sold + new.amount
WHERE id = new.product_id;
UPDATE Customers
SET amount = amount + new.amount
WHERE id = new.customer_id;
END;
$$
DELIMITER ;
DELIMITER $$
create trigger UpdateAvail after insert on product_details
for each row
Begin
Declare a1 INT;
Declare d1 VARCHAR(1);
Declare d2 VARCHAR(100);
Select count(0) INTO a1 from prod_available where P_Id=new.P_Id;
Select P_Name,P_Brand INTO d1,d2 from product where P_Id=new.P_Id;
IF a1>0 THEN
Update prod_available set P_quantity=P_quantity+new.quantity where P_Id=new.P_Id;
ELSE
insert into prod_available (P_Id,P_Name,P_Brand,P_quantity) values (new.P_Id,d1,d2,new.quantity);
END IF;
END;
$$
DELIMITER ;