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.
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 am having a problem with a creation of a trigger.
So I have two tables: product(name,...) and detect(id,name_prod,quantity), and I wrote a code that whenever you insert a value in product, you would create a new row in detect for each id existent and initiate the value of quantity to zero:
DELIMITER $$
CREATE TRIGGER detect_default
AFTER INSERT ON product
FOR EACH ROW
BEGIN
IF NOT EXISTS (SELECT name_prod FROM detect WHERE new.name=name_prod) THEN
INSERT INTO detect(name_prod,quantity) VALUES(new.name,0);
END IF;
END$$
DELIMITER ;
The problem of this code is that only creates one row in detect with the name insert on product.name and quantity 0, but the id will be null.
How can I create a row for each existent ID?
I need to update prefixed_sku column by prefixing "A|" for every insert or update on column sku
SKU------------prefixed_sku
Hp3001-------- A|HP3001
HS1001-------- A|HP3001
I tried reading many articles but I'm not a programmer so could not figure it out on my own. This is for my orders database. Thanks
table name: orders
column name: sku
Add an after insert and after update trigger to go back and update the prefixed_sku column in the orders table:
DELIMITER $$
CREATE TRIGGER after_order_insert
AFTER INSERT ON orders
FOR EACH ROW
BEGIN
UPDATE orders
SET prefix_sku=CONCAT('A|',sku)
WHERE sku=NEW.sku
END$$
DELIMITER ;
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'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 ;