SQl Insert Into Tables Autoamtically? - mysql

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 ;

Related

How to insert values into an SQL table using triggers and if conditions?

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 ;

My SQL: How to make a trigger that inserts data to another table

I'm learning JDBC and I'm trying to make a bank program. I'm using mySQL to make the database of the program which includes users and balance tables. I want to make a trigger that will automatically create data when data is inputted in the users table
So far i've made this code which doesn't work:
create trigger onInputUsers before insert on users
for each row
begin
insert into finances (id,balance,debt) values (null,0,0);
end;
NOTE: The table structure of your tables is not provided, hence there is an assumption that there are columns as like userid, balance and debt in your tables.
There can be two triggers that can fulfil the requirement of yours (one that creates a record whenever there is a new user created and the other when a transaction is made by the user)!
This handles the creation of the audit entries for your record, which is not very much required (if you ask for my opinion)
DELIMITER $$
CREATE TRIGGER USER_FINANCE_TRIGGER BEFORE INSERT ON users
FOR EACH ROW
BEGIN
INSERT INTO finances (id, userid, balance, debt) VALUES (null, OLD.userid, 0, 0);
END$$
DELIMITER;
This handles updating the finances table with the user activity:
DELIMITER $$
CREATE TRIGGER USER_FINANCE_UPDATE_TRIGGER BEFORE UPDATE ON balance
FOR EACH ROW
BEGIN
INSERT INTO finances (id, userid, balance, debt) VALUES (null, OLD.userid, OLD.balance, OLD.debt);
END$$
DELIMITER;
Hope this answers your question well!

MySQL Trigger Creation. What's wrong with my trigger?

I'm trying to create a trigger, however I keep getting back a syntax error.
Here's the statement:
DELIMITER $$
CREATE TRIGGER `swtalentbank`.`after_candidate_insert`
AFTER INSERT ON `Candidates` FOR EACH ROW
BEGIN
INSERT INTO useradmin (username, talent)
VALUES (NEW.email, 1);
UPDATE `Candidates` SET UserID = useradmin.userid where useradmin.username = NEW.email;
END
DELIMITER ;
I have a registration form on my site. When a person registers it populates the Candidates table with their profile information.
In the Candidates table, there are various fields, two of them being 'email' and 'UserID'.
UserID is also the PK in 'useradmin', so I'm linking the two up.
So when a user registers, I need to insert a record into 'useradmin' with the email address that's just been used to register, and then update the 'Candidates' table, with UserID that's just been created in 'useradmin'.
I hope this makes sense?
NB. I am changing the delimiter before running the statement.
Besides properly using DELIMITER when creating a trigger you have at least two fundamental issues with your current code:
In MySQL you can't use issue a DML statement (in your case UPDATE) against a table (candidates) on which you defined a trigger (also candidates). Your only option is to use BEFORE trigger and set a value of userid column of a row being inserted to a proper value.
You can't arbitrarily reference a column (useradmin.userid) of a table out of the context like you did in your UPDATE. You didn't joined useradmin table or used it in a subquery.
That being said and assuming that userid in useradmin table is an auto_increment column your trigger might look like this
DELIMITER $$
CREATE TRIGGER after_candidate_insert
BEFORE INSERT ON candidates
FOR EACH ROW
BEGIN
INSERT INTO useradmin (`username`, `talent`) VALUES (NEW.email, 1);
SET NEW.userid = LAST_INSERT_ID();
END$$
DELIMITER ;
Here is SQLFiddle demo
You should use semicolon after end your insert query.
You can use INSERT ... ON DUPLICATE KEY UPDATE syntax for your purpose
try out this...
DELIMITER $$
CREATE TRIGGER `swtalentbank`.`after_candidate_insert`
AFTER INSERT ON `Candidates` FOR EACH ROW
BEGIN
INSERT INTO useradmin (username, talent)
VALUES (NEW.email, 1);
UPDATE `Candidates` SET UserID = useradmin.userid where useradmin.username = NEW.email;
END $$
DELIMITER ;

mysql trigger using two tables

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$$

Help on Mysql 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.