I know there must be a simple way to do this but I couldn't find one. I want to create a trigger that basically, when a row is created in table1, creates a new row in table2 with a foreign key of the id of table1. what is the general syntax for this? thanks!!
CREATE TRIGGER Syntax
For example -
DELIMITER $$
CREATE TRIGGER trigger1
AFTER INSERT
ON table1
FOR EACH ROW
BEGIN
INSERT INTO table2(id) VALUES(NEW.id);
END$$
DELIMITER ;
something like this:
CREATE TRIGGER `create_t1` AFTER INSERT ON `table1` FOR EACH ROW BEGIN
INSERT INTO table2
SET t1ID = NEW.ID,
when = Now();
END;
Related
I have two table in two separate databases. However, they are supposed to have the same data. I would like to make sure that whenever I make changes to data in table_a from database_a, they get reflected in table_b from database_b.
Is there any MySQL command that I can run to achieve this?
I read this question: Copy Data from a table in one Database to another separate database but it seems to insert data instead of updating it.
Thanks.
The best way to accomplish this would be with triggers. I haven't tested this, but it gives you the idea.
DELIMITER $$
CREATE
TRIGGER table_a_after_insert AFTER INSERT
ON database_a.table_a
FOR EACH ROW BEGIN
-- update to match columns in your table
INSERT INTO database_b.table_b (
id,
name
)
VALUES (
NEW.id,
NEW.name
);
END$$
CREATE
TRIGGER table_a_after_update AFTER UPDATE
ON database_a.table_a
FOR EACH ROW BEGIN
DECLARE updated_rows INT;
-- again update the column list to match your table
UPDATE database_b.table_b
SET
name = NEW.name
WHERE id = NEW.id;
-- in case the row didn't already exist in table_b, insert it
SET #updated_rows = ROW_COUNT();
IF updated_rows = 0
THEN
INSERT INTO database_b.table_b (
id,
name
)
VALUES (
NEW.id,
NEW.name
);
END IF;
END$$
CREATE
TRIGGER table_a_after_delete AFTER DELETE
ON database_a.table_a
FOR EACH ROW BEGIN
-- obviously update the column list to match the columns in your table
DELETE FROM database_b.table_b
WHERE id = OLD.id;
END$$
You'll have to make sure the user has the right privileges to database_b.table_b
You can use Database triggers (https://dev.mysql.com/doc/refman/8.0/en/trigger-syntax.html) for something like this.
CREATE TRIGGER triggername AFTER INSERT ON database_a.table_a
FOR EACH ROW
BEGIN
INSERT INTO database_b.table_b (id, ...) VALUES (NEW.id, ...);
END;
You will however have to create triggers for each event. When inserting into table_a you need to insert into table_b, when deleting from table_a you will have to delete from table_b and so on.
€dit: Update for instance could look like this:
CREATE TRIGGER triggername AFTER UPDATE ON database_a.table_a FOR EACH ROW
UPDATE TABLE database_b.table_b
SET table_b.id = NEW.id,
...(SET each column here)...
WHERE table_b.id = NEW.id;
I have two tables for example table1 and table2. If something is deleted in table1 i want to update a column in table2. Is this even possible with a trigger in phpmyadmin? if yes what do i have to add or which syntax i have to use for it to work?
I tried this so far:
DELIMITER //
CREATE TRIGGER `update` AFTER DELETE ON table1
FOR EACH ROW BEGIN
-- this is the part i dont know what to do and i couldnt find any related to my task
END//
DELIMITER ;
Well, in your TRIGGER you can access the value you just deleted with OLD.your_column_name.
So just do :
DELIMITER //
CREATE TRIGGER `update` AFTER DELETE ON table1
FOR EACH ROW BEGIN
UPDATE table2 SET column_name = your_new_value WHERE column_name = OLD.old_value;
END//
DELIMITER ;
I am learning to create a trigger that can insert the row I update in one table into another table.
the query below is my creating trigger:
DELIMITER $$
CREATE TRIGGER update_edit_deliverycompany BEFORE UPDATE ON `deliverycompany`
FOR EACH ROW
BEGIN
INSERT INTO `editor_log` (`edit_table`, `edit_field`, `editor`, `edit_time`)
SELECT 'deliverycompany',`deliverycompany`.`CompanyID`,USER(),CURDATE() FROM `deliverycompany` ;
END$$
DELIMITER ;
when the trigger is fired, All rows in 'deliverycompany' are insert into the 'editor_log' rather than just the row I updated.
How to just select the row I update
if using where how can I locate the row I updated (any value in the row)
Try something like this
DELIMITER $$
CREATE TRIGGER update_edit_deliverycompany BEFORE UPDATE ON `deliverycompany`
FOR EACH ROW
BEGIN
INSERT INTO `editor_log` (`edit_table`, `edit_field`, `editor`, `edit_time`) VALUES (OLD.deliverycompany, OLD.deliverycompany, USER(),CURDATE()) ;
END$$
DELIMITER ;
I want to be able to do something like this:
IF EXISTS (SELECT * FROM table WHERE col=val)
INSERT ......
ELSE
UPDATE ......
I know that you are able to do this:
IF EXISTS (SELECT * FROM table WHERE col=val)
INSERT ......
But I'm not sure if you can do an else along with that.
What would fastest way to do the first query?
Yes it is possible. One way to do this is by using a procedure.
DELIMITER $$
DROP PROCEDURE IF EXISTS `YOUR-PROCEDURE`$$
CREATE DEFINER=`root`#`localhost` PROCEDURE `YOUR-PROCEDURE`()
BEGIN
IF EXISTS (SELECT * FROM table WHERE col=val)
THEN
BEGIN
UPDATE table SET ..;
END;
ELSE
BEGIN
INSERT INTO table ..;
END;
END IF;
END$$
DELIMITER ;
You should rather use INSERT ... ON DUPLICATE KEY UPDATE
insert into table1 (col1,col2) values(1,2)
on duplicate key update col2=3, col1=4
See Here for more information.
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 ;