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;
Related
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!
I want the table cart to be populated when I populate the table cart_utilizador and the table cart will be populated with CURDATE(); on the field datas. How can I achieve this using triggers?
DATABASE diagram:
I tried this but the ON gives me an error saying "Unexpected ON":
CREATE TRIGGER cart_trigger
ON cart_utilizador AFTER UPDATE
AS
INSERT INTO cart (id, datas)
SELECT id_cart, CURDATE()
FROM cart_utilizador;
You can use a trigger. The trigger should fire up when you insert data into cart_utilizador, you can try this :
DELIMITER |
CREATE TRIGGER before_insert_cart BEFORE INSERT
ON cart FOR EACH ROW
BEGIN
INSERT INTO cart_utilizador(datas) values(now());
END |
DELIMITER ;
Your user may need the super privilege to create trigger. Permissions for creating a trigger in mysql
Hope it helps.
Just move the After Update clause before ON clause. Something like this -
CREATE TRIGGER cart_trigger AFTER UPDATE
ON cart_utilizador FOR EACH ROW
BEGIN
INSERT INTO cart (id, datas)
SELECT id_cart, CURDATE()
FROM cart_utilizador;
END
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 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.
I have a small question. I have 2 tables:
CREATE TABLE park(parkcode CHAR(5), name CHAR(15));
CREATE TABLE ticket(date_purchase_ticket TIMESTAMP, parkcode CHAR(5));
What I am trying to do is to create a trigger that when I want to delete a row from table park, the trigger first looks to see if there were any purchases after a certain date. And only if there were no purchases after a certain date then you can delete that row from the table park.
You need to think about making a procedure that you can call when you want to delete a row from park.
Something along the lines of:
DELIMITER //
CREATE PROCEDURE DeleteIfNoPurchases (IN code CHAR(5),
IN date TIMESTAMP)
BEGIN
IF (SELECT MAX(date_purchase_ticket)
FROM ticket
WHERE parkcode = code) < date THEN
DELETE FROM park WHERE parkcode = code;
END IF;
END//
DELIMITER ;
Then you can call it with the code you want to delete and the timestamp you want to test against.
You can not prevent deleting a row with trigger.