I haven’t worked much with databases before but I understand a bit of the basics. Right now, I’ve got two tables, we’ll call Table 1 and Table 2. I’m trying to create a trigger so that when I insert data into Table 1, it fires the trigger to get all distinct values for col1 in Table 1, which I want to use to insert into Table 2. As more data gets added into Table 1, duplicates will no doubt occur. If a duplicate occurs, I don’t want the duplicate inserted into Table 2. So, I thought that I might use a Union to pull the unique values and insert them into Table 2, but after the Union I’m not entirely sure what to do. Am I crazy for thinking this way and there’s a way to do this better?
DELIMITER //
CREATE TRIGGER testdb
AFTER INSERT ON testdb.table1
FOR EACH ROW
BEGIN
SELECT col1 FROM table1
UNION
SELECT col1 FROM table2
(I imagine the insert statement would go here)
END //
DELIMITER ;
In short, data goes into Table 1, trigger launches on insert to get unique values from Table 1 Col 1 and Table 2 Col 1 in the attempt to get all unique values, and then I want to insert the unique values into Table 2 Col 1, but some of those unique values may already exist.
Assuming that
table2is defined asYou could do this: