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!
Related
I'm running a wateranalyzer and want to save the sensor-data in a MariaDB.
I split the data into 2 tables: one for the automated part and one table which stores data I enter manually:
Tables:
I'm having a hard time (with just basic knowledge about databases) to figure out how I can "bind" ID and DateTime from one table to the other one, so if manual data is added, ID is incremented by 1 and the actual Date and Time is set in DateTime.
I bet I can do this somehow in PHPmyadmin?
thanks for your time!
using triger. this example for you.
DELIMITER //
CREATE TRIGGER contacts_after_insert
AFTER INSERT
ON contacts FOR EACH ROW
BEGIN
DECLARE vUser varchar(50);
-- Find username of person performing the INSERT into table
SELECT USER() INTO vUser;
-- Insert record into audit table
INSERT INTO contacts_audit
( contact_id,
deleted_date,
deleted_by)
VALUES
( NEW.contact_id,
SYSDATE(),
vUser );
END; //
DELIMITER ;
Is it anything more complex than having an ID in Wasser that matches the other ID? That is, first insert into Luft, then get the id and only then, INSERT INTO Wasser....
(A Trigger seems unnecessarily complicated.)
As Rick Suggested, you need to have an ID column in the second table that references ID in first table. Trigger is a better option if the process of getting the ID and inserting it along with other columns (pH, Redox...) into the second table is complicated.
Make ID in the second table as a foreign key to the ID in first table.
Hello and thanks in advance for any help given!
I have created a very simple database for an electronics online shop.
Among the other tables I have a tbl_orders for orders being placed manually (user inserts the product_id, number of units, payment_details_id and so on manually) and I want to make an AFTER INSERT trigger on tbl_orders that would send the order_id (which is tbl_order's PK) to a table called purchase_history.
The table purchase_history also has a purchase_date column. It's PK is purchase_id, with order_id as a FK.
I am unfamiliar with triggers but I've tried all sorts of simple queries inside a trigger with no luck, though the errors I was getting were getting "better" if one could call it that.
The last try I had was this:
CREATE DEFINER = CURRENT_USER TRIGGER `onlineshop`.`tbl_orders_AFTER_INSERT` AFTER INSERT ON `tbl_orders` FOR EACH ROW
BEGIN
INSERT INTO purchase_history (order_id)
VALUES (NEW.order_id);
INSERT INTO purchase_history ( purchase_date )
VALUES (purchase_date = NOW());
END
I am using MYSQL Workbench, which is great but also, sometimes, slightly buggy or confusing.
CREATE TRIGGER `tbl_orders_AFTER_INSERT` AFTER INSERT ON `tbl_orders`
FOR EACH ROW BEGIN
INSERT INTO purchase_history (order_id, purchase_date) VALUES (NEW.order_id, NOW());
END;
This way your insert would make more sense, as you would want both of the data in the same row in the history, I'd guess.
If the DEFINER-line is needed:
CREATE DEFINER = CURRENT_USER
TRIGGER stuff.`tbl_orders_AFTER_INSERT` AFTER INSERT ON `tbl_orders`
FOR EACH ROW
INSERT INTO purchase_history (order_id, purchase_date) VALUES (NEW.order_id, NOW());
I tried some things, mysql seems not to like begin, end and DEFINER = ... at the same time when creating triggers. Though BEGIN and END are not needed with triggers so there is no problem in removing them from your statement.
You can use the below syntax to execute it. First we need to change delimiter if there is multiple statament.
DELIMITER #
create trigger SportsTriggerInsert after insert on sports
for each row begin
insert into settings(sports_id, auto_add_on_matches)
values (new.id, 0);
end;#
DELIMITER ;
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;
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...).
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.