Uknown Column new in FIELD LIST Mysql Trigger - mysql

I have the following trigger which is supposed to insert on a trigger table after insert. I keep on getting the following error : Unknown Column Field in List
Below is my trigger :
DROP TRIGGER IF EXISTS `trgr_client_statement_report`;CREATE DEFINER=`uniquelo`#`localhost` TRIGGER `trgr_client_statement_report` AFTER INSERT ON `statement` FOR EACH ROW INSERT INTO trgr_statement
( `amnt_cr`, `amnt_dr`, `approved`, `clnt_id`,`date_added`,`days`, `description`,
`discount`,
`id_2`,
`invoiced`,
`invoice_no`,
`job_card_id`,
`payment_date`,
`payment_status`,
`pymnt_code`,
`pymnt_mthd`,
`qty`,
`rcpt_no`,
`status`)
VALUES
( NEW.`amnt_cr`,NEW.`amnt_dr`,NEW.`approved`,NEW.`clnt_id`,NEW.`date_added`,NEW.`days`,NEW.`description`,NEW.`discount`,NEW.`id`,NEW.`invoiced`,NEW.`invoice_no`,NEW.`job_card_id`,NEW,`payment_date`,NEW.`payment_status`,NEW.`pymnt_code`,NEW.`pymnt_mthd`,NEW.`qty`,NEW.`rcpt_no`,NEW.`status`)

There are some Syntax error in fieldlist. You must use NEW.field not NEW,field : NEW,payment_date
DROP TRIGGER IF EXISTS `trgr_client_statement_report`;
CREATE DEFINER=`uniquelo`#`localhost` TRIGGER `trgr_client_statement_report` AFTER INSERT ON `STATEMENT` FOR EACH ROW INSERT INTO trgr_statement
( `amnt_cr`, `amnt_dr`, `approved`, `clnt_id`,`date_added`,`days`, `description`,
`discount`,
`id_2`,
`invoiced`,
`invoice_no`,
`job_card_id`,
`payment_date`,
`payment_status`,
`pymnt_code`,
`pymnt_mthd`,
`qty`,
`rcpt_no`,
`status`)
VALUES
( NEW.`amnt_cr`,NEW.`amnt_dr`,NEW.`approved`,NEW.`clnt_id`,NEW.`date_added`,NEW.`days`,NEW.`description`,NEW.`discount`,NEW.`id`,NEW.`invoiced`,NEW.`invoice_no`,NEW.`job_card_id`,NEW.`payment_date`,NEW.`payment_status`,NEW.`pymnt_code`,NEW.`pymnt_mthd`,NEW.`qty`,NEW.`rcpt_no`,NEW.`status`)

Related

Is it possible to insert in mysql a combo of words and variable?

Trigger definition:
BEGIN
INSERT INTO `user` (`ID`, `Username`, `Password`, `Online`) VALUES
(NEW.ID, "store_"NEW.ID , 'blablabla', 0);
END
In this trigger I have to insert as username something like store_(id of the new store).
Is it possible to do this and if yes how?
There are 2 ways to concat/append string in SQL
a. Use + operator to join the string
b. Use Concat function.
By using + operator the query would be like below.
BEGIN
INSERT INTO `user` (`ID`, `Username`, `Password`, `Online`) VALUES
(NEW.ID, "store_" + NEW.ID , 'blablabla', 0);
END
By using Concat() function the query would be like below.
BEGIN
INSERT INTO `user` (`ID`, `Username`, `Password`, `Online`) VALUES
(NEW.ID, Concat("store_",NEW.ID) , 'blablabla', 0);
END
Recommended option is use 'Concat' function as syntax is easier to follow, and the code looks cleaner
Use concat function :
BEGIN
INSERT INTO user (ID, Username, Password, Online)
VALUES
(NEW.ID, CONCAT('store_',NEW.ID), 'blablabla', 0);
END

Increment a number field via a trigger INSERT in MySQL

I'm building a data versioning system, and I need to increment a version number each time a new row is added to the version table, but it increments once and then stops:
DELIMITER |
CREATE TRIGGER trigger2 AFTER UPDATE ON something
FOR EACH ROW
BEGIN
IF NEW.updated_at <> OLD.updated_at THEN
INSERT INTO versions_something (
`id`,
`some_id`,
`version`,
`title`,
`description`,
`created_at`,
`updated_at`
) VALUES (
null,
NEW.id,
1,
NEW.title,
NEW.description,
NOW(),
NOW()
);
END IF;
UPDATE
versions_something
SET
version = (SELECT MAX(version)) + 1
WHERE versions_something.id = LAST_INSERT_ID();
END;
|
DELIMITER ;
I've tried putting the UPDATE into a separate trigger (AFTER INSERT ON versions_something ...), but MySQL complains that it's clashing with the trigger before it.
I've tried the UPDATE on its own, using the last ID in the table and it works each time, so I have no idea what's happening.

Create a trigger to insert the old data to a new table

Here is the table I created.
USE my_guitar_shop;
DROP TABLE IF EXISTS Products_Audit;
CREATE TABLE Products_Audit (
audit_id INT PRIMARY KEY,
category_id INT REFERENCES categories(category_id),
product_code VARCHAR ( 10 ) NOT NULL UNIQUE ,
product_name VARCHAR ( 255 ) NOT NULL,
list_price INT NOT NULL,
discount_percent INT NOT NULL DEFAULT 0.00 ,
date_updated DATETIME NULL);
Create a trigger named products_after_update. This trigger should insert the old data about the product into the Products_Audit table after the row is updated. Then, test this trigger with an appropriate UPDATE statement.
Here is the trigger I created but the data is not showing up in the Products_Audit table it is showing all null.
USE my_guitar_shop;
DROP TRIGGER IF EXISTS products_after_update;
DELIMITER $$
CREATE TRIGGER products_after_update
BEFORE UPDATE ON products
FOR EACH ROW
BEGIN
INSERT INTO products_audit (audit_id, product_id, category_id, product_code,
product_name, list_price, discount_percent, date_updated)
SELECT audit_id, products.product_id, products.category_id, products.product_code,
products.product_name,products.list_price, products.discount_percent, date_updated
FROM products JOIN products_audit
ON products_audit.audit_id = (SELECT audit_id FROM inserted);
END $$
DELIMITER ;
EDIT with the INSERT INTO
USE my_guitar_shop;
DROP TRIGGER IF EXISTS products_after_update;
DELIMITER $$
CREATE TRIGGER products_after_update
BEFORE UPDATE ON products
FOR EACH ROW
BEGIN
INSERT INTO products_audit (audit_id, product_id, category_id,product_code,
product_name, list_price, discount_percent, date_updated)
VALUES (OLD.audit_id, OLD.product_id, OLD.category_id, OLD.product_code,
OLD.product_name, OLD.list_price, OLD.discount_percent, OLD.date_updated)
DELIMITER ;
You are overcomplicating the insert. As mysql documentation on triggers says:
In an UPDATE trigger, you can use OLD.col_name to refer to the columns
of a row before it is updated and NEW.col_name to refer to the columns
of the row after it is updated.
Therfore, use the OLD.column_name format in the insert. Also, I would set the audit_id field to auto increment and leave it out of the insert:
INSERT INTO products_audit (product_id, category_id, product_code,
product_name, list_price, discount_percent, date_updated)
VALUES (OLD.product_id, OLD.category_id, OLD.product_code,
OLD.product_name, OLD.list_price, OLD.discount_percent, OLD.date_updated)
Here's an example of how I do it:
CREATE OR REPLACE EDITIONABLE TRIGGER E_TABLE_TRG
before insert or update or delete on e_table
for each row
declare
l_seq number;
begin
-- Get a unique sequence value to use as the primary key
select s_seq.nextval
into l_seq
from dual;
if inserting then
:new.date_opened := sysdate;
:new.last_txn_date := null;
:new.status := 'A';
end if;
if inserting then
insert into e_table_history
(
t_seq,
user_id,
date_opened,
last_txn_date,
status,
insert_update_delete,
insert_update_delete_date
)
values
(
l_seq,
:new.user_id,
:new.date_opened,
:new.last_txn_date,
:new.status,
'I',
sysdate
);
elsif updating then
insert into e_table_history
(
t_seq,
date_opened,
last_txn_date,
status,
insert_update_delete,
insert_update_delete_date
)
values
(
l_seq,
:new.date_opened,
:new.last_txn_date,
:new.status,
'U',
sysdate
);
else
insert into e_table_history
(
t_seq,
date_opened,
last_txn_date,
status,
insert_update_delete,
insert_update_delete_date
)
values
(
l_seq,
:old.date_opened,
:old.last_txn_date,
:old.status,
'D',
sysdate
);
end if;
end;
/
ALTER TRIGGER E_TABLE_TRG ENABLE;
/

Sakila -> insert into Film ( mysql WorkBench )

I'm trying put a new row in table sakila. film, but I can't see it in the table, I think that not exist, but when execute this statement Works correctly, I don't know what happened
INSERT INTO `sakila`.`film`
(
`title`,
`description`,
`release_year`,
`language_id`,
`original_language_id`,
`rental_duration`,
`rental_rate`,
`length`,
`replacement_cost`,
`rating`,
`special_features`,
`last_update`)
VALUES( 'GUSANO' ,'GUSANO' ,2006,1,1,3,4.99,4 ,19.99, 'G','Trailers','2006-02-15 04:34:33.0');
Result :
21:52:44 INSERT INTO `sakila`.`film` ( `film_id`, `title`, `description`, `release_year`, `language_id`, `original_language_id`, `rental_duration`, `rental_rate`, `length`, `replacement_cost`, `rating`, `special_features`, `last_update`) VALUES( 10010, 'GUSANO' ,'GUSANO' ,2006,1,1,3,4.99,4 ,19.99, 'G','Trailers','2006-02-15 04:34:33.0') 1 row(s) affected 0.015 sec
Image insert :
Image select :

Trigger to insert values from another table - Mysql

I did this to trigger when the temperature is higher or lower than the parameter set off an alarm. Ie automatically populates the fields of the table alarm.
Now I created a new row in the table alarms, the idRegisto which is foreign key, now I want that id be completed in accordance with the idRegisto other corresponding table.
Does anyone can help me please?
If you are not understanding the question I will try to clarify further.
Thank you.
------------TRIGGER-------------------
DELIMITER $$
create TRIGGER alerta
BEFORE INSERT ON registos
FOR EACH ROW
begin
Set #tempmax=0;
Set #tempmin=0;
Set #hummax=0;
select lim_inf_temp, lim_sup_temp into #tempmin, #tempmax from sensores where idSensor=NEW.idSensor;
Set #maxidAlarme=0;
if (CAST(NEW.Temperatura AS UNSIGNED)<#tempmin) then
SELECT MAX(idAlarme) into #maxidAlarme FROM alarmes;
SET #maxidAlarme=#maxidAlarme+1;
INSERT INTO alarmes(idAlarme,descricao_alarme) VALUES (#maxidAlarme,"high-temperature");
INSERT INTO sensores_tem_alarmes(idSensor,idAlarme,dataAlarme) VALUES (NEW.idSensor,#maxidAlarme,NOW());
end if;
if (CAST(NEW.Temperatura AS UNSIGNED)>#tempmax) then
SELECT MAX(idAlarme) into #maxidAlarme FROM alarmes;
SET #maxidAlarme=#maxidAlarme+1;
INSERT INTO alarmes(idAlarme,descricao_alarme) VALUES (#maxidAlarme,"lower temperature");
INSERT INTO sensores_tem_alarmes(idSensor,idAlarme,dataAlarme) VALUES (NEW.idSensor,#maxidAlarme,NOW());
end if;
DELIMITER ;
------------ER------------------------
You can use an "after insert trigger"
AFTER INSERT ON registos
And take the "New.IdRegistro" to use as foreign key when populating the alarmes table.
//EDIT:
using your code:
AFTER INSERT ON registos
FOR EACH ROW
begin
Set #tempmax=0;
Set #tempmin=0;
Set #hummax=0;
...
INSERT INTO alarmes(idAlarme,descricao_alarme,idRegistro) VALUES (#maxidAlarme,"lower temperature",New.IdRegistro);
INSERT INTO sensores_tem_alarmes(idSensor,idAlarme,dataAlarme) VALUES (NEW.idSensor,#maxidAlarme,NOW());
...
I assume here that IdRegistro is a primary key (that will be autogenarated by your application or via autoincrement) of the registro table.
Register->if value higher/lower threshold -> trigger alarm -> insert sensores_tem_alarmes