Hey all so I have created a table named products that looks like so:
`CREATE TABLE products
(
Prod_ID int(10),
Prod_name varchar(20),
Prod_qty varchar(20),
Prod_price int(20)
);`
Product_log table is nearly identical to another table called products:
`CREATE TABLE product_log
(
Prod_ID int(10),
Prod_name varchar(20),
Action_Date date,
Updated_by varchar(30),
Action varchar(30)
);`
Next I have created a trigger called products_after_insert which should insert data into the product_log table after a row in products is inserted.
The requirement for after insert trigger is that action date should be inserted in the product_log table and the user name should be inserted like who inserted the data ex: data operator,
and on last the action should be inserted in the product_log table automatically like action here is insertion.
here is my trigger:
`DELIMITER //
CREATE TRIGGER products_after_insert
AFTER INSERT
ON products
FOR EACH ROW
BEGIN
DECLARE data_operator int(10);
DECLARE action_per varchar(200);
SET data_operator = 1;
SET action_per = 'INSERTION';
IF data_operator=1 THEN
INSERT INTO product_log(prod_id,prod_name,Action_date,Updated_by,Action)
VALUES(NEW.prod_id,NEW.prod_name,SYSDATE(),'data_operator','action_per');
END IF;
END;
//DELIMITER;`
Now, I assume I am constructing my trigger incorrectly because it appears to not be working.
Does anyone know what I am doing wrong? Any help would be greatly appreciated! Thanks!
There's at least this you are doing wrong :
VALUES(NEW.prod_id,NEW.prod_name,SYSDATE(),'data_operator','action_per');
You have properly defined the variables data_operator and action_per previously:
SET data_operator = 1;
SET action_per = 'INSERTION';
But in your INSERT instruction you surrounded them with quotes, which turned them into strings. Your instruction should be :
VALUES(NEW.prod_id,NEW.prod_name,SYSDATE(),data_operator,action_per);
Related
I made AFTER INSERT Trigger on joomla database (table _users) which after registration copies user_id and name to another table.
Everything works fine until someone during registration enters a name that already exists Otherwise the column name is not the primary one or unique. I get an error:
Save failed with the following error: Result consisted of more than one row
My trigger looks like this:
BEGIN
DECLARE `new_user` integer;
DECLARE `new_name` VARCHAR(400);
SELECT `id`
INTO `new_user`
FROM `eio3k_users`
WHERE `id` = NEW.id;
SELECT `name`
INTO `new_name`
FROM `eio3k_users`
WHERE `name` = NEW.name;
INSERT INTO `eio3k_point_system`(`user_id`, `name` ) VALUES (NEW.id, NEW.name);
END
What should I do to avoid this error?
I have to create a trigger a which captures old and new value while updation & deletion taken place
Audit table :
create table if not EXISTS tbl_audits
(
TA_Auditid int AUTO_INCREMENT PRIMARY key,
TA_Name varchar(100),
TA_Oldvalue varchar(1000),
TA_Newvalue varchar(1000),
TA_Actiontaken varchar(100),
TA_createddt datetime,
TA_createdby varchar(100)
)
DELIMITER //
create TRIGGER TRfiledetails_update
BEFORE update on tbl_file_details
for EACH ROW
BEGIN
insert into tbl_audits
set TA_createddt =CURRENT_TIMESTAMP,
TA_Actiontaken ='update',
TA_Name ='File',
TA_Oldvalue =old.FL_dtstartdt ,
TA_Newvalue =new.FL_dtstartdt
end //
DELIMITER ;
In my main table i have to create trigger an event on two columns startdt and enddate.
In this case I would like to know whether I need to create two triggers separately for each column.
Is it possible to create two columns action on same trigger or need to create separate columns in audit table.
I am trying to delete a single row using a stored procedure in MySQL, however the stored procedure is deleting all rows. The stored procedure is:
CREATE DEFINER=`sherattd`#`%` PROCEDURE `deleteFilm`(in ID int)
BEGIN
delete from films where id=ID;
END
The table I am trying to delete from has headings:
create table films (
id int(8),
title varchar(100),
year int(8),
director varchar(100),
stars varchar(100),
review text);
Thanks
Because you use as parameter the same name as the column name. It is case insensitive.
where id=id
is always true. Choose another parameter name!
Parameter name needs to be different as per answer by #juergen d
e.g.
CREATE DEFINER=`sherattd`#`%` PROCEDURE `deleteFilm`(in FID int)
BEGIN
delete from films where id=FID;
END
I created a table
Databases Name - mytrigger;
Table name - employee_audit
use mytrigger;
create table employee_audit(
id int auto_increment primary key,
employeeNumber int not null,
lastName varchar(50) not null,
changee datetime default null,
action varchar(50) default null
);
After that, I created one update trigger
My trigger name is
before_employees_update
DELIMITER $$
create trigger before_employees_update
before update on employee_audit
for each row
begin
insert into employee_audit
set action ='update',
employeeNumber = OLD.employeeNumber,
lastName = OLD.lastName,
changee = now();
end$$
DELIMITER ;
After that, I inserted values in table using this command ->
insert into employee_audit values(1,112,'prakash','2015-11-12 15:36:20' ,' ');
After that, I want to update my table row where id =1
update employee_audit set lastName = 'Sharma' where employeeNumber =112;
But it is not executed give an error
ERROR 1442 (HY000): Can't update table 'employee_audit' in stored
function/trigger because it is already used by statement which invoked
this stored function/trigger.
When I searched on Google I found a lot of Question with the same error. But not able to fix my problem. what is the reason I'm not able to update my row?
What i suggest,you can create one log table like employee_audit_LOG .
And on every insert or update in main table you can make new entry in this table or update existing record.
Also you can add updated_timestamp column to that LOG table which maintain when did specific record get updated.
The error itself tells you the answer. This is because, you can't use the same table on which trigger is being executed. You need to store your audit logs into some different table.
Whats wrong with this code?
CREATE TRIGGER User_trigger AFTER DELETE ON users
FOR EACH ROW
BEGIN
INSERT INTO del_users ('fullname') VALUES ('fullname');
END;
Please help.
There are several issues:
If you use BEGIN ... END block you have to change DELIMITER. On the other hand if your trigger contains only one statement just don't use BEGIN ... END. Take a closer look at Defining Stored Programs
In MySQL to be able to refer to columns of a row being deleted you have to use OLD keyword. Take a closer look at Trigger Syntax and Examples
That being said and assuming that your simplified table schema look something like this
CREATE TABLE users
(
id int not null auto_increment primary key,
fullname varchar(8)
);
CREATE TABLE del_users
(
id int not null auto_increment primary key,
user_id int, fullname varchar(32),
deleted datetime
);
Your trigger would look
CREATE TRIGGER tg_ad_users
AFTER DELETE ON users
FOR EACH ROW
INSERT INTO del_users (user_id, fullname, deleted)
VALUES (OLD.id, OLD.fullname, NOW());
Here is SQLFiddle demo
or with BEGIN ... END block
DELIMITER //
CREATE TRIGGER tg_ad_users
AFTER DELETE ON users
FOR EACH ROW
BEGIN
INSERT INTO del_users (user_id, fullname, deleted)
VALUES (OLD.id, OLD.fullname, NOW());
END//
DELIMITER ;
Here is SQLFiddle demo
Try the following:
CREATE TRIGGER User_trigger AFTER DELETE
ON users
AS
INSERT INTO del_users (fullname)
SELECT d.fullname from Deleted d
Here is information on using the DELETED and INSERTED Tables:
http://technet.microsoft.com/en-us/library/ms191300.aspx