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.
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'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
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 two tables which I use to store call details in. One table (Call_Detail) stores the header details against each call that gets entered, the second (Call_History) stores every comment against the call. So a single call will only appear ONCE in the Call_Detail table, but may appear multiple times in the Call_History table.
I currently run a Query to return the latest comment against a group of calls. So, I return the header details out of Call_Detail and then cross reference against the Call_History to find the 'newest' comment (thanks to some outside help). However, this Query can be quite time consuming when running against a large number of calls.
Therefore, I'm thinking to optimize my Query, I want to setup a trigger that records these details.
I am wanting to catch any INSERT command into the Call_History table and record the comment and date/time into the Call_Detail table against the relevant call ID.
So far I have the following but it doesn't like my syntax for some reason:
DELIMITER $$
CREATE TRIGGER Last_Call_Update
AFTER INSERT ON call_history
FOR EACH ROW
BEGIN
UPDATE call_detail
SET last_updated = NEW.updated_at, last_commment = NEW.body
WHERE id = NEW.ticket_id
END $$
DELIMITER ;
Add semicolon after UPDATE statement
DELIMITER $$
CREATE TRIGGER Last_Call_Update
AFTER INSERT ON call_history
FOR EACH ROW
BEGIN
UPDATE call_detail
SET last_updated = NEW.updated_at, last_commment = NEW.body
WHERE id = NEW.ticket_id;
END $$
DELIMITER ;
I Have a database called 'lms' with two tables loan and value, table loan has: loan_amount, yearly_intrest, loan type; table value has value_id,value_name, value_amount.
What i want is for my trigger to calculate the yearly interest in the loan table using the interest rate(value_amount) from the other table value where the loan_type(from loan table) is equal to the value (from Value table)
I tried this, it needs some help
-- Trigger DDL Statements
DELIMITER $$
USE `lms`$$
CREATE
DEFINER=`root`#`localhost`
TRIGGER `lms`.`updateloan`
BEFORE INSERT ON `lms`.`loan` INNER JOIN 'lms'.'value'
FOR EACH ROW
BEGIN
l.loan_type ="Computer Loan"
SET l.yearly_intrest = (l.loan_amount *(v.value_amount/100))
WHERE l.loan_type=v.value_name;
END$$
Table value contains two value_names Computer and Motor vehicle with value amounts of 2, 5
i hope my explanation is clear enough
I have not tried this but it should work -
DELIMITER $$
USE `lms`$$
CREATE
DEFINER=`root`#`localhost`
TRIGGER `lms`.`updateloan`
BEFORE INSERT ON `lms`.`loan`
FOR EACH ROW BEGIN
SET NEW.yearly_interest = (SELECT NEW.loan_amount * value_amount/100 FROM `lms`.`value` WHERE value_name = NEW.loan_type);
END$$