MySQL ERROR 1054:UNKNOWN COLUMN 'amount' in field list - mysql

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 ;

Related

How can I determine why my trigger does not run?

create database triggers;
use triggers;
create table if not exists Customers(
custID INT unsigned not null auto_increment,
age int,
name varchar(30),
primary key(custID)
);
delimiter //
create trigger age_verify
before insert on customers
for each row
if new.age < 0 then set new.age = 0;
end if; //
insert into Customers
values (101, 27, 'James'),
(102, -40, 'Ammy'),
(103, 32, 'Ben'),
(104, -39, 'Angela');
select * from Customers;
For some reason my trigger in MySQL workbench does not run and when I run select * from customers it prints the negatives still and does not update the value. How can I debug this?
Your trigger is syntactically incorrect. Multiple-statement trigger code must be enclosed with BEGIN-END block:
delimiter //
create trigger age_verify
before insert on customers
for each row
BEGIN
if new.age < 0 then
set new.age = 0;
end if;
END //
DELIMITER ;
But in your particular case you do not need in BEGIN-END and DELIMITER, use simple
CREATE TRIGGER age_verify
BEFORE INSERT ON customers
FOR EACH ROW
SET NEW.age = GREATEST(NEW.age, 0);
delimiter //
create trigger age_verify before
insert on Customers
for each row
begin
if new.age < 0 then set new.age = 0;
end if;
end
//
delimiter ;

How do I simultaneous record the old value and new value with a trigger in an update table in MYSQL?

I'm trying to create a trigger the simultaneously records the new and old value of MSRP on an update. I thought I had my code right but I get an 1136 error column count doesn't match.
code I have now:
DELIMITER $$
create trigger prod_upd_old
after update on products
for each row
BEGIN
insert into audit_table(productCode, productName, productline, MSRP, last_update, rowValue)
Values(old.productCode, old.productName, old.productLine, old,MSRP, now(), 'before update');
END $$
DELIMITER ;
DELIMITER $$
create trigger prod_upd_new
after update on products
for each row
follows prod_upd_old
BEGIN
insert into audit_table(productCode, productName, productline, MSRP, last_update, rowValue)
values(new.productCode, new.productName, new.productLine, new.MSRP, now(), 'after update');
END $$
DELIMITER ;
update products set MSRP = '147.50' where productCode = 'S10_4962';

trigger two according to data

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 ;

MySQL trigger to multiply value

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.

Trigger before insert on not concatenating columns

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