Mysql problems with Triggers - mysql

I have created two tables customerbkp (customer backup) and another table called customer.
my intention is to copy all the modifications made in the customer table..
but the code does not compile when I use
delimiter //
create trigger custtrig
before insert or update or delete on customer
for each row
//statements
I tried few variants of that frustrated with that I am creating individual triggers , the below code seems syntatically correct but it enters only null into the back up table.
Could any one explain me why I am getting this result (i.e null ),Is there any way that I could record all the changes in my backup table
create table customerbkp(cid int, cname varchar(25), cemail varchar(25), phone int, operation varchar(10));
create table customer(cid int, cname varchar(25), cemail varchar(25), phone int);
delimiter //
create trigger custtrig
before insert on customer
for each row
begin
insert into customerbkp(cid) values (cid);
end;
//
delimiter;
insert into customer values(101,'cust1','cust1#gmail.com',1111);

MySQL does not support insert or update or delete. You need to create 3 separate
triggers.
Use AFTER xxx instead of BEFORE xxx triggers, because otherwise you won't have the values for the auto-increment fields (and maybe some others).
You are inserting a row with a value for cid only. You should insert it with all the values:
...
begin
INSERT INTO customerbkp(cid,cname,cemail,phone,operation)
VALUES (NEW.cid, NEW.cname, NEW.cemail, NEW.phone, 'insert');
end;
...
Change 'insert' to an appropriate value for each trigger. Also you will need to use OLD instead of NEW in the AFTER DELETE trigger (or maybe have NULL's...).

Related

MYSQL Trigger with Insert from another table

FOUND SOLUTION !!!!
CREATE TRIGGER `triggerau_user` AFTER INSERT ON `customers`
FOR EACH ROW INSERT INTO au_user (nome, datahora) values (new.name,new.created)
Found pretty strange that i doesnt use BEGIN nor `END, but it worked, is doing fine, every time i insert into the table customer it makes the trigger and inserts on the au_user
After about 1.5 year, i come back and need to work with MySQL again, so i am a bit rusty on the subject, i can do the basics right, but something about phpadmin and the mysql interface on it doesnt get to me, i cant do simple triggers, and i want to know what is the mistake in those.
So i have these 2 tables
CREATE TABLE au_user (nome varchar(100), datahora datetime);
CREATE TABLE customer (name varchar(100), created datetime);
the customer table, picks data from my crud/website, with no problem at all(the table is bigger), both of these are fine, but now for the question how do i pick name and created and save on au_user nome and datahora.
CREATE TRIGGER triggerau_user AFTER INSERT ON customers
FOR EACH ROW
BEGIN
INSERT INTO au_user (nome, datahora)
SELECT
name, created
FROM customers
END;
it always says that i have a error near line 8 END; but how can i fix this, and sorry for the incovenience.
This should work:
CREATE TRIGGER triggerau_user AFTER INSERT ON customer
FOR EACH ROW
BEGIN
INSERT INTO au_user(nome, datahora) values(new.name,new.created); END;
I forgot to add ; after Insert query.
Hope it helps!

Making triggers in mySQL

I'm fairly new to triggers and mySQL, and there was a question asked as a part of my class which was to make a trigger that will support inserting a new "invoice" which is apart of the database, and that will automatically update the customer in the 'customer' table by adding this new invoice onto the customer balance attribute.
Sorry if my question isn't clear but I can answer any details that need clarifying.
Example of customer, and invoice table as following.
CREATE TABLE customer (
id INT,
...
transaction_count INT DEFAULT 0
);
CREATE TABLE invoice (
id INT,
customer_id INT,
...
);
And your trigger will be something like this.
DELIMITER
$$
CREATE TRIGGER invoice_counter
AFTER INSERT ON invoice
FOR EACH ROW
BEGIN
INSERT INTO customer
SET transaction_count = transaction_count + 1
WHERE id = NEW.customer_id;
END
$$
DELIMITER ;
Following link will be more useful.
Create Trigger in MySQL
When you need to deeper on trigger
MySQL Trigger Manual

mysql: trigger to create n number of rows after insert in another row

I am using mysql and phpmyadmin and I have two tables in mysql database:
job (ID int, milestones int, description VARCHAR(50))
which will store a job and its number of milestones and the description of that job
the second table is:
milestones (ID int, jobID - reference the job ID as a foreign key , description VARCHAR(50) )
what I want is to create a trigger of every insertion of a job, so if I inserted a job with 2 milestones, I want 2 rows created in the milestones table.
for example:
INSERT INTO `job`(`ID`, `name`, `mileStones`)
VALUES (1,'its a good project',2);
I want to automatically create 2 rows in the milestones table as follow:
1,1,'milestone'
2,1,'milestone'
what I did is I followed some questions like:
this and I came up with this code
DELIMITER //
CREATE TRIGGER job_after_insert AFTER INSERT ON job
FOR EACH ROW
BEGIN
DECLARE i INT DEFAULT 1;
WHILE (i<=NEW.milestones) DO
INSERT INTO milestones(ID, jobID, description) VALUES ( i ,NEW.ID,'milestone')
END WHILE;
END; //
DELIMITER ;
when I insert a row into job, I get the following error:
#1442 - Can't update table 'job' in stored function/trigger because it is already used by statement which invoked this stored function/trigger.
can you explain to me what is wrong with this approach ?

Create Trigger in MySQL and Update

I need help creating a particular trigger using MySQL. The details for my trigger is given below along with my current statement. However, my statement keeps giving me an error message so I'm doing something wrong. Any assistance would be great! Thanks!
Problem a) Create a trigger called membership_balance_updates that will capture any updates made to the mem_balance column in membership. The trigger should only capture those transactions in which the member’s balance actually changes. The mem_num, old mem_balance, new mem_balance, user, transaction date should be placed into a membership_balance_audit table.
My Statement:
CREATE TRIGGER membership_balance_updates
AFTER UPDATE OF mem_balance ON membership
FOR EACH ROW
INSERT INTO membership_balance_audit
VALUES (mem_mum, old_mem_balance, new_mem_balance, transaction_date,
transaction_user);
Table "Membership"
Here's the following script to create the membership_balance_audit table.
CREATE TABLE IF NOT EXISTS membership_balance_audit
(mem_num INTEGER,
old_mem_balance DECIMAL(10,2),
new_mem_balance DECIMAL(10,2),
transaction_date TIMESTAMP,
transaction_user VARCHAR(50));
Part c
Try using this:
CREATE TRIGGER membership_balance_updates
AFTER UPDATE ON membership
FOR EACH ROW
BEGIN
IF NEW.mem_balance <> OLD.mem_balance THEN
INSERT INTO membership_balance_audit
(mem_mum, old_mem_balance, new_mem_balance, transaction_date,
transaction_user)
VALUES (OLD.membership_id, OLD.mem_balance, NEW.mem_balance, NOW(),
CONCAT(OLD.first_name, ' ', OLD.last_name));
END IF;
END;

create trigger on update to insert old values into other table in MYSQL

I have two tables. The first is my person table which has id, name, creation_date as values, I have a old_person table (id, name, modified_date) which I want to populate the value of person before it actually changes. How would I go about that? I have tried triggers but failed.
I tried as follows
create trigger Person_Trigger Update on person
before update
as
insert into old_person(id, name, modified)
select id, new.name, getdate()
from person
It's giving me syntax errors...Not many Trigger references out there either, a little push would be greatly appreciated!
Have a look at the following example
SQL Fiddle DEMO
IO hade a bit of trouble myself, but from How to Create Triggers in MySQL there was a line that made me think
The first MySQL command we’ll issue is a little unusual:
DELIMITER $$
Our trigger body requires a number of SQL commands separated by a
semi-colon (;). To create the full trigger code we must change
delimiter to something else — such as $$.
Finally, we set the delimiter back to a semi-colon:
DELIMITER ;
So in the SQL Fiddle I changed the query terminator to GO and that seemed top work.
CREATE TABLE person
(
id INT,
name varchar(20)
)
GO
CREATE TABLE old_person
(
id INT,
name varchar(20),
modified DATETIME
)
GO
CREATE TRIGGER Person_Trigger before update on person
FOR EACH ROW BEGIN
INSERT INTO old_person(id, name, modified)
VALUES (OLD.id, OLD.name, NOW());
END
GO
INSERT INTO person VALUES (1,'TADA')
GO
UPDATE person SET name = 'FOO'
GO
Try this:
DELIMITER \\
CREATE TRIGGER `Person_Trigger`
BEFORE UPDATE ON `Person`
FOR EACH ROW
BEGIN
DECLARE date_modified datetime;
SET date_modified = NOW();
INSERT INTO old_person(id, name, modified)
VALUES (OLD.id, OLD.name, date_modified);
END\\
This syntax works for me on my own projects. You may also need to declare delimiters before you begin the trigger. Also if you want to use the NEW keyword it should be AFTER update. Switch to the OLD keyword if you are going to keep using BEFORE update on your trigger.