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$$
Related
I am trying to make a stored procedure in MySQL that will take the highest number from a column, add one and use it to make the next entry.
DROP PROCEDURE IF EXISTS ops_software.create_invoice;
DELIMITER //
CREATE PROCEDURE ops_software.create_invoice(IN company VARCHAR(50))
BEGIN
SELECT #old_invoice_number := MAX(invoice_number)
FROM invoices
WHERE invoices.company = company;
SET #new_invoice_number := #old_invoice_number + 1
INSERT INTO invoices (company, invoice_number)
VALUES (company, #new_invoice_number)
END//
DELIMITER ;
CALL ops_software.create_invoice('Super Company')
I don't want to use the auto-increment feature because there are several different company names and each has their own invoice numbers
Getting the value works, but I can't add one to it or insert it to make a new entry
Thanks
CREATE PROCEDURE ops_software.create_invoice(IN in_company VARCHAR(50))
INSERT INTO invoices (company, invoice_number)
SELECT in_company, MAX(invoices.invoice_number) + 1
FROM invoices
WHERE invoices.company = in_company;
DELIMITER and BEGIN-END not needed.
PS. May produce duplicates in concurrent environment.
Can we perform count operation inside a trigger?
I have a table TB1 with fields id,title,author,date. I want to create a trigger that when ever I will insert a row in tb1. A trigger should run and the total no of authors in the tb1 should show in a new table total.
I have written the code
DELIMITER $$
CREATE TRIGGER insertintonew2
AFTER
INSERT ON tb1
FOR EACH ROW
BEGIN
UPDATE total
SET total_author = (select count(*) from tb1);
END;
$$
The output I am getting a is populated with total_author column name but the count value is not populated.
Can anyone tell me why is it happening?
Tables
I have the two tables above. In tblUniformAndMaterials the field AllocatedMaterials is populated from a drop down list which is fed by from tblMaterials. Once selected the field MaterialID in tblUniformAndMaterials is Auto Populated from tblMaterials.
What I want to accomplish by using a Trigger is after the record in tblUniformAndMaterials is inserted I want to update the NiveauDeStock field in tblMaterials to (NiveauDeStock-1). In other words after each material allocation I reduce the stock level by one.
Reference for CREATE TRIGGER https://dev.mysql.com/doc/refman/5.7/en/create-trigger.html
Try this:
DELIMITER $$
CREATE TRIGGER tblUniformAndMaterials_ai
AFTER INSERT
ON tblUniformAndMaterials
FOR EACH ROW
BEGIN
UPDATE tblMaterials
set NiveauDeStock = NiveauDeStock -1
WHERE CodeDeMaterial = NEW.MaterialID;
END;
$$
DELIMITER ;
Note AFTER INSERT and how NEW.MaterialID is the ID inserted into tblUniformAndMaterials.
Good Luck!
I have a table for holding student datat in a school, this DB holds information about who the student is, what sujects they study, who their tutors are, and what modules etc.Tables are as follows:
Student(**StudentId**,name,address,dob,contactNum,classId)
classes(**classId**,className,totalStudents)
modules(**moduleId**,name,lecturer)
lecturers(**staffId**,name,moduleId)
registeredClass(moduleId,staffId)
moduleRegistartions(studentId,moduleId)
If i was to add data to the lecturers table, how could it automactically / garantee that the inserts would be completed in one action Rather than having to individually insert data into the registeredClass table.
Thanks
DELIMITER $$
CREATE DEFINER=`root`#`localhost` TRIGGER `trgInsertLecturers` AFTER INSERT ON `lecturers` FOR EACH ROW
BEGIN
INSERT INTO registeredClass(moduleId,staffId) values(NEW.moduleId, NEW.staffId);
END
$$
DELIMITER ;
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.