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
Related
Essentially I am trying to create a 'Retired' SQL table which will get its values from an already existing 'Staff' SQL table.
If the staff has more than 5 YearsOfService, then he/she will be added to the retired table.
I am sharing the code that I have tried.
CREATE TABLE Retired(
StaffID INT
);
DELIMITER #
CREATE TRIGGER RetiredTrigger after insert on Staff
FOR EACH ROW
BEGIN
INSERT INTO ALUMNI(StaffID) VALUES(new.StaffID);
IF Staff.YearsOfService > 5
END #
The result should ideally be a table(i.e. Retired Table) that should have StaffID's of only those teachers who have YearsOfService greater than 5.
I hope I was able to articulate my doubts clearly.
Thanks in advance.
CREATE TABLE Retired(
StaffID INT
);
DELIMITER #
CREATE TRIGGER RetiredTrigger after insert on Staff
FOR EACH ROW
BEGIN
IF (NEW.YearsOfService > 5) THEN
INSERT INTO Retired(StaffID) VALUES(new.StaffID);
END IF;
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 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.
I need to do trigger after Inserting on a table called jos_jquarks_quizzes, I need to create a course name which will have the same name as the quizz name , but its own id,
Tables
jos_jquarks_quizzes
id
title
description
course_id
jos_jquarks_users_training
id
quiz_id
user_id
agree
current approach
BEGIN
INSERT INTO jos_users_trainings
(jos_users_trainings.quiz_id) VALUES
SELECT jos_jquarks_quizzes.id FROM jos_jquarks_quizzes
END
Can you please help. Thanks in Advance
DELIMITER $$
CREATE TRIGGER ai_jos_jquarks_quizzes_each AFTER INSERT ON jos_jquarks_quizzes
FOR EACH ROW
BEGIN
INSERT INTO jos_users_trainings
(quiz_id) VALUES (new.id);
END $$
DELIMITER ;
In a trigger the virtual table new holds the newly inserted values.
In an update or delete trigger the virtual table old holds the values before the change.