I'm a MySql newbie so sorry if this has been asked before.
I am going round in circles trying to get a solution to this question.
I have a MySql database with two tables.
TERRITORIES
This table lists all nations of the world and has a column for listing the currency of each country.
CURRENCIES
This is essentially a "lookup" table including a country ID and a list of world currencies.
When inputting data into the TERRITORIES.currency cell I would like a drop-down list to appear from the CURRENCIES.country column.
I've spent hours googling to see if an ENUM field can call that data from the other table or whether one should use SET etc.
If anyone can assist me with reference to a good CLEAR tutorial on this issue I'd appreciate it.
There appear to be 2 things you want to happen 1) Populate 2) validate
For both triggers may be good for you.
delimiter $$
Create trigger t before insert on territories
for each row
begin
set new.currency = (Select currency from currencies where country = new.country);
end $$
delimiter ;
It doesn't then matter if an insert tries to insert to currency as well as country.
You also need an update trigger
delimiter $$
Create trigger t before update on territories
for each row
begin
set new.currency = (Select currency from currencies where country = new.country);
end $$
delimiter ;
To stop an update changing the currency to an invalid currency.
Related
I'm attempting to create a trigger that increases the value of a column INCOME in the Salary database by 500 each time the value of WorkYear in the Employee table is increased by one year. For example, if the workYear is 4 and the salary is 1000, the salary should be 1500 if the workYear is increased by one year, 2000 if the workYear is increased by two years, and so on.
I tried to create such trigger and here is my code :
DELIMITER $$
create trigger increment AFTER UPDATE on employee
for each row
BEGIN
IF OLD.workYear <> new.workYear THEN
update salary
set income = (income + (new.workYear-old.workYear)*500);
END IF;
END$$
The idea behind this code is that after we update the workYear, the trigger should increase the salary by the difference of years * 500, (new.workYear-old.workYear)*500, but it increases all the rows by the same number, (5500 if we add one year, 27500 if we add two years, etc.) which not what we are looking for .
I am new to MySQL and would appreciate it if someone could assist me with this.
Thanks in advance
FaissalHamdi
In MySQL an AFTER trigger can affect the entire table, so you must declare the update scope in the form of criteria or a join.
Create Trigger in MySQL
To distinguish between the value of the columns BEFORE and AFTER the DML has fired, you use the NEW and OLD modifiers.
The concept is similar but each RDBMS has a slightly different syntax for this, be careful to search for help specifically on your RDBMS.
In the original query these special table references were used to evaluate the change condition however the scope of the update was not defined.
Assuming that there is a primary key field called Id on this salary table.
Also note that if you can, the query should be expressed in the form of a set-based operation, instead of static procedural script, this will be more conformant to other database engines.
So lets try this:
DELIMITER $$
create trigger increment AFTER UPDATE on employee
for each row
BEGIN
UPDATE salary s
SET income = (income + (new.workYear-old.workYear)*500)
WHERE s.Id = OLD.Id
END$$
I am trying to complete this question
**
Produce an audit trail (in a separate table) that records the current username, system date & grade change when someone attempts to update a Students grade
**
Here are the tables and columns
Module (code,credits,cost,name)
session (code,date,room)
Exam (no,code,grade)
course (code,credits,name)
Student (no,name,cell)
Here is the code I have so far
DELIMITER $$
USE `HarlemHS`$$
CREATE
DEFINER=`HarlemHS`#`%`
TRIGGER `HarlemHS`.`ExamChange`
AFTER UPDATE ON `HarlemHS`.`Exam`
FOR EACH ROW
BEGIN
INSERT INTO NEW.GradeUpdateLog Date_Of_Change,old_grade)
VALUES (CURDATE(), grade);
END$$
I know I have to use DECLARE somewhere but not too sure where to use it and what to put in the DECLARE statement.
If i have missed anything let me know,
Thank you for the help :)
To futher clarify i was trying to create a trigger that checks a table for a number in sql.
If it finds said number then it erases that entire row.
It uses a separate table of names to check.
I thought it be could done using a join but have had no luck.
So it would look like this I suppose if(tb1.name = tb2.name) then DELETE row.
I'm sorry if the formatting is off.
EDIT; I am using phpmyadmin so some of the the code may be missing but here is the code from my latest "attempt"
It uses on INSERT and time is set to AFTER
SELECT * FROM flights WHERE NOT EXISTS (SELECT * FROM no fly list WHERE PassengerId.Id = Passenger.Id)
have not added the DELETE as of now but the work is somewhat ongoing
Assuming that flight and dnf both have passenger_id column that uniquely identifies a passenger of interest then your trigger might look like this
DELIMITER $$
CREATE TRIGGER dnf_insert AFTER INSERT
ON dnf
FOR EACH ROW BEGIN
DELETE FROM flights WHERE passenger_id = NEW.passenger_id;
END$$
DELIMITER ;
If you post DDL (create table statements) for all relevant tables we can refine the query
I am working on a car rental system. The table 'Hire' has fields like hireDate, returnDate, etc. I want to be able to calculate the rent and insert that value automatically into the database (same table).
I am wondering if I can subtract these two date fields, and the value which comes out of this can be multiplied by a certain number (say 300) and automatically added to another attribute called 'Rent_Due'. The idea is to calculate the total rent due by getting the number of days and then multiplying that by a certain number. So for every Hire that is made, I just enter the hiring date and the return date, and the Rent Due attribute is automatically filled. Is this possible by any chance?
I dont know what more information I need to provide to seek help, kindly let me know if you might require an image of my ERD.
Thanks!
Assuming the difference you want to compute in in days, this should work.
INSERT INTO yourTable (hireDate, returnDate, Rent_Due) VALUES
(StartDate. EndDate, DATEDIFF(EndDate, StartDate)*300)
Personally I would steer clear of having calculated fields in the database, and try to keep it normalized.
You could, instead, use a view for the duration of hire and hence the price.
Example as follows:
create view hire_duration as (
select hireId, hireDate, datediff(returnDate,hireDate) as duration
from Hire
);
(This assumes you have a primary key called hireId in your Hire table)
You could easily modify the view to have cost of hire as well, by multiplying duration by some constant, and that would be OK if the cost never changes, but if it you want to have the flexibility of different hire rates you wound't want to hard-code a rate in your SQL.
You don't required to create another field you can get your result be the following query.
select (datediff(hireDate, returnDate)*300) as rent,hireDate,returnDate from Hire;
Are you looking for maybe something like this?
Before-Insert-Event:
DELIMITER $$
CREATE TRIGGER name_of_before_insert_trigger BEFORE INSERT ON name_of_table
FOR EACH ROW BEGIN
SET NEW.Rent_Due = DATEDIFF(NEW.returnDate, NEW.hireDate) * 300;
END;
$$
DELIMITER ;
Before-Update-Event:
DELIMITER $$
CREATE TRIGGER name_of_before_update_trigger BEFORE UPDATE ON name_of_table
FOR EACH ROW BEGIN
SET NEW.Rent_Due = DATEDIFF(NEW.returnDate, NEW.hireDate) * 300;
END;
$$
DELIMITER ;
The daily rate you can also outsource in a separate table:
DELIMITER $$
CREATE TRIGGER name_of_before_insert_event_trigger BEFORE INSERT ON name_of_table
FOR EACH ROW BEGIN
SET NEW.Rent_Due = DATEDIFF(NEW.returnDate, NEW.hireDate) * (SELECT value FROM name_of_price_table LIMIT 1);
END;
$$
DELIMITER ;
I'm new to working with triggers and am having a hard time understanding how to write a trigger to update a field in one table, when a record is inserted in another.
To elaborate, I have 2 tables: servTickets and servTicketNotes.
servTickets has several text fields for customer, contact, phone, email, problem description, status, etc...the PK in this table is an INT field called callID.
servTicketNotes has only 2 fields - again, the PK is an INT field 'callID' and there is a BLOB field called image which stores an image of a service report.
What I'm struggling to do is have a trigger update the status field in servTickets with a value of Closed when a new record is inserted into servTicketNotes.
I'm confused if this is an INSERT AFTER or BEFORE or BOTH scenario, but basically if a report is sent in (thereby creating a record in servTicketNotes, I want the trigger to seek out the record with the same callID in the servTickets table and change the value of status to 'Closed'.
This seems like it should be so simple, but I can't seem to grasp how to get started...
Thanks in advance for your help/guidance!
is it probably a POST trigger - which means:
AFTER you have committed the incoming record, you want to take further action - i.e. inserting into the other table.
if you do it PRE commit, then you would worry about some error happening on the Notes and you might end up with an incorrect update to the status.
You can do this with an AFTER INSERT trigger. Try something like this:
DELIMITER $$
DROP TRIGGER IF EXISTS tr_a_ins_servTicketNotes $$
CREATE TRIGGER pabeta.tr_a_ins_servTicketNotes AFTER INSERT ON servTicketNotes FOR EACH ROW BEGIN
update servTickets
set status = 'Closed'
where callID = NEW.callID;
END $$
DELIMITER ;