I am a beginner with MySQL. I want to create a trigger with a more specific condition (specific statement) than just INSERT, UPDATE, DROP, etc. For example, I would like something like the following:
CREATE TRIGGER name AFTER UPDATE table1 SET column1 = 'P' WHERE column2 = 'Q' FOR EACH ROW UPDATE table2 SET column1 = 42 WHERE column2 = 66;
Where AFTER UPDATE table1 SET column1 = 'P' WHERE column2 = 'Q' would be the trigger event and UPDATE table2 SET column1 = 42 WHERE column2 = 66 would be the trigger action.
You can check the old and new values for an UPDATE via the old and new pseudo rows. Of course you can use them in your logic.
CREATE TRIGGER name
AFTER UPDATE
ON table1
FOR EACH ROW
BEGIN
IF new.column1 = 'P'
AND old.column2 = 'Q' THEN
UPDATE table2
SET column1 = 42
WHERE column2 = 66;
END IF;
END;
Related
Drop TRIGGER if exists triggername;
DELIMITER $$
CREATE TRIGGER triggername
AFTER UPDATE ON table1 FOR EACH ROW
BEGIN
IF (
Select person from table1 e
JOIN table1Type et ON e.table1TypeID = et.table1TypeID
where et.Description = 'University Degree' and e.Active = 1)
THEN update table2
set field = 1 ;
ELSE update table2 set field = 0;
END IF;
END$$
DELIMITER ;
I am trying to use a conditional statement to update another table if that condition is met.
So in other words, if the person in table 1 has a type of university degree and is active then update another field in table 2.
CREATE TRIGGER triggername
AFTER UPDATE
ON table1
FOR EACH ROW
UPDATE table2
SET field = EXISTS (SELECT NULL
FROM table1 e
JOIN table1Type et USING (table1TypeID)
WHERE et.Description = 'University Degree'
AND e.Active = 1);
Maybe you must use NOT EXISTS (the logic is unclear).
PS. DELIMITER not needed for this trigger code.
How do I do this?
CREATE DEFINER=`root`#`localhost` PROCEDURE `MyProcedure`(
IN param VARCHAR(50))
BEGIN
SELECT id, column2, column3
FROM table1
WHERE column5 = param;
LIMIT 1;
IF (FOUND_ROWS() > 0) THEN
UPDATE table1 SET column4 = 1 WHERE id = theIdFromRowAbove;
END IF;
END
I want my stored procedure to select a row from table1, and if a row is found, update table1 where the id = the id from the found row. I have been searching the internet but still can't seem to figure it out. I'm not exactly sure what to search for.
You can take a variable and set its value false after find row set true
CREATE DEFINER=`root`#`localhost` PROCEDURE `MyProcedure`()
BEGIN
DECLARE find_row INT DEFAULT 0;
DECLARE theIdFromRowAbove INT DEFAULT 0;
SELECT id, column2, column3, 1 INTO theIdFromRowAbove, #column2, #column3, find_row
FROM table1
WHERE id = 5
LIMIT 1;
IF (find_row) THEN
UPDATE table1 SET column4 = 1 WHERE id = theIdFromRowAbove;
END IF;
END
for more info about SELECT....INTO Official Link
I have a very simple procedure which performs an update on multiple rows. Is their a way I can loop through the insert id of each updated row?
BEGIN
UPDATE testTable set testValue = 2
where username = inputUser and flag = 1;
//for each update performed, insert LAST_INSERT_ID() into tableB
END
is this possible? If so, can you lead me in the right direction?
LAST_INSERT_ID only gives you one last id. You can do multiple inserts without a loop. You can use the output of SELECT query and insert to the desired table. You can do something like that:
BEGIN
UPDATE testTable set testValue = 2
where username = inputUser and flag = 1;
INSERT into tableB (column1, column2)
SELECT column1, column2 FROM testTable
WHERE username = inputUser and flag = 1;
END
For example:
User (username, password)
UserLoginLog (username, last_log_in_date)
INSERT INTO UserLogInLog (username, last_log_in_date)
SELECT username, NOW()
FROM User
WHERE username = "JohnDoe"
I would like to make a SQL Trigger that will evaluate a query like this:
IF NOT (SELECT * FROM table1 WHERE table1.id=1 AND table1.finished=0)
DO
UPDATE table2 SET finished=1 WHERE table2.id=table1.id
That would tell me that all rows from table1 with an id of 1 are finished (if there are no unfinished(0) rows) and, if so, it should update table2 and set the value finished to 1.
Can anyone help me with the Trigger structure? I'm new when it comes to that.
You can do it this way
DELIMITER //
CREATE TRIGGER tg_au_table1
AFTER UPDATE ON table1
FOR EACH ROW
BEGIN
IF NOT OLD.finished <=> NEW.finished THEN
UPDATE table2 t
SET finished = (EXISTS(SELECT *
FROM table1
WHERE id = t.id
AND finished = 0))
WHERE id = NEW.id;
END IF;
END//
DELIMITER ;
Here is SQLFiddle demo
Extended answer is provided in your other question https://stackoverflow.com/a/21270582/1920232
Question is create a trigger that will only update a record if the old record is different from the new. Can i get some help please i don't even know how to start the syntax apart from
CREATE TRIGGER update_marketingliste
ON marketing_list FOR UPDATE
AS
BEGIN
SELECT * FROM INSERTED
END
I am not sure what the schema is but the sample code below should help you to understand how this can be done.
SET NOCOUNT ON
GO
CREATE TABLE test(col1 int primary key , col2 int )
go
CREATE TRIGGER trg_test_upd on test
INSTEAD OF UPDATE
AS
BEGIN
IF EXISTS (
SELECT *
FROM Deleted del -- old value
INNER JOIN Inserted ins -- new value
on del.col1 = ins.col1
and del.col2 = ins.col2)
BEGIN
PRINT 'No Update'
RETURN --if old value is same as new value dont update
END
ELSE
BEGIN
UPDATE t1
set col2 = i.col2
from dbo.test t1
inner join inserted i
on t1.col1 = i.col1
END
END
go
Insert into test (col1, col2)
select 10, 10
go
update test
set col2 = 200
where col1 = 10
SELECT *
FROM TEST
--This would not do the update
update test
set col2 = 200
GO
Drop table test
go