MySQL delete similar records after insert with Trigger - mysql

I have MySQL database running with InnoDB engine. I have table which has columns id, nickname, score (so it's a scoreboard).
I need to check AFTER INSERT all rows and find similar record, if there is one delete old one.
USE 'database';
DELIMITER $$
CREATE TRIGGER tests_AINS AFTER INSERT ON tests FOR EACH ROW
-- Edit trigger body code below this line. Do not edit lines above this one
So how do I proceed?

Related

MySQL REPLACE Table Values using Identical Staging table Trigger Without Defining Columns

Within a MySQL 8 database I have two identical tables in different schemas with one being used as a staging table for its equivalent production table. The tables contain 200+ columns with more to be added in the future, making explicitly defining values for column names difficult.
I need to INSERT or UPDATE values in the production table with those in the staging table using an AFTER INSERT trigger, where all values of a row within the production table are replaced upon the existence of a duplicate PRIMARY KEY. Note that the production table has its PRIMARY KEY defined.
After this, I should delete all rows from the staging table.
Here is the code I have tried:
CREATE TRIGGER stage.my_table_after_insert AFTER INSERT ON my_table
FOR EACH ROW
BEGIN
REPLACE INTO prod.my_table
SELECT * FROM new;
DELETE FROM stage.my_table
END
I don't get why are You doing so, but why arent you doing something like:
REPLACE INTO prod.my_table
SELECT * FROM stage.my_table;
DELETE FROM stage.my_table;
You cant alter a table within its own contained trigger. Additionally, using a trigger will execute all SQL statements once per row (inefficient in this case). Therefore, a procedure is instead used to perform the required functions, using the following:
DELIMITER $$
CREATE PROCEDURE stage.load_data()
BEGIN
REPLACE INTO prod.my_table
SELECT * FROM stage.my_table;
DELETE FROM stage.my_table;
END$$
DELIMITER ;

How can I trigger this SQL query the right way?

This is my problem:
Create a trigger which will add the id and age columns of rows deleted
from the table "metadata" to the "metadata2" table.
The query that will be run to check if the trigger works is:
DELETE FROM metadata WHERE ID=40124197; SELECT * FROM metadata2.
So, what did I do? I did this:
CREATE TRIGGER somerandomtrigger
AFTER INSERT ON metadata2
FOR EACH ROW
BEGIN
INSERT INTO metadata2 (id, age)
END
Yet it keeps saying: Query failed: near "END": syntax error, but I don't get why.
It seems you used the wrong event trigger on the worng table.
When you delete a record from metadata table, you want to insert some data from this record into metadata2.
On delete trigger, there is a a records managed by MySQL named OLD containing the record that has been deleted.
CREATE TRIGGER somerandomtrigger
AFTER DELETE ON metadata
FOR EACH ROW
BEGIN
INSERT INTO metadata2(id, age) VALUES (OLD.id, OLD.age);
END;
Syntax and examples of MySQL trigger

how to use auto-incremented column of one table in another table-mysql workbench 6.2

I tried it with the help of this query "insert into class(faculty_id) select faculty_id from faculty;" but it is inserting already existing values only. Here one column of the table is under auto-increment so I wanted to take this incremented values to another table's column in mysql workbench 6.2. I even tried setting a foreign key between the two columns but it dint work. Please can anyone help out with this issue??
This is what you want i think. an after insert trigger will fire every time something is inserted into faculty, and after the data has been inserted. You can access the values that have just been inserted with the NEW. prefix.
DELIMITER //
create trigger classInsert after insert on faculty
for each row
begin
insert into class(faculty_id) values (NEW.faculty_id);
end//
DELIMITER ;
And it will automatically add an entry into the class table every time you add a new value to faculty.
Fiddle example

MySQL trigger for change detection

I have several tables in the database: Users, Profiles, Articles
I also have one table called Changes, which is used for administrative purposes. This table consists of id, table_name, and date_created.
What I need to do is whenever something is added, deleted or updated in a regular table (Users, Profiles, Articles), create a new row in the Changes with the name of the updated table and the current timestamps.
I've been browsing for a while and tried many different methods, but nothing really worked. I know the solution should be very simple, may be someone can help me. Thank you for your time.
So in this case you need 9 trigger 3 for each of the regular table after insert, after update, after delete
Here is for one table you can write for the others
When you insert on Users
delimiter //
create trigger log_user_insert after insert on Users
for each row
begin
insert into Changes (table_name,date_created) values ('Users',now());
end; //
delimiter ;
When update happens on Users
delimiter //
create trigger log_user_update after update on Users
for each row
begin
insert into Changes (table_name,date_created) values ('Users',now());
end; //
delimiter ;
When delete happens on Users
delimiter //
create trigger log_user_update after delete on Users
for each row
begin
insert into Changes (table_name,date_created) values ('Users',now());
end; //
delimiter ;
I would suggest to add a column called action in the table Changes and to insert each action name as well i.e. insert,update and delete.
You need to create an update, insert and delete trigger on each of the data tables:
CREATE TRIGGER upd_changes_users BEFORE UPDATE ON Users
FOR EACH ROW
BEGIN
INSERT INTO changes (table_name, date_created) VALUES ('users', NOW());
END;
This code assumes, that the id column in changes is auto_generated. You might also want to consider including a type column in the changes table (to differentiate between insert, update and delete).

Is there any version control in mysql?

It is possible to use a version control system with mysql databases?
Or, is there a version control system already implemented?
I want to say e.g.: SELECT foo FROM bar WHERE version = X
Whereeby version is a mysql internal colum with last update date.
Very late response... similar to Ruben's suggestion, I have setup triggers to update a version_control table to increment version number every time there is an INSERT, UPDATE & DELETE.
I laid out the steps on my site mradamfrancis.tumblr.com
** update **
I’ve decided to use triggers to assist with version control. Here’s how…
I have a table containing players, if there are changes (INSERT, DELETE, or UPDATE) I want to increment the version number in my version_control table.
This is how the version_control table looks:
version_id (key), table_name (varchar), version (integer)
I then create 3 triggers on the players table, one for INSERT, DELETE & UPDATE.
INSERT:
delimiter //
CREATE TRIGGER `player_table_INSERT` AFTER INSERT ON `players`
FOR EACH ROW BEGIN
UPDATE version_control SET version=version+1 WHERE table_name=’players’;
END;//
delimiter ;
DELETE:
delimiter //
CREATE TRIGGER `player_table_DELETE` AFTER DELETE ON `players`
FOR EACH ROW BEGIN
UPDATE version_control SET version=version+1 WHERE table_name=’players’;
END;//
delimiter ;
UPDATE:
delimiter //
CREATE TRIGGER `player_table_UPDATE` AFTER UPDATE ON `players`
FOR EACH ROW BEGIN
UPDATE version_control SET version=version+1 WHERE table_name=’players’;
END;//
delimiter ;
** I have additional SQL statements in the FOR EACH section of the trigger, hence I’ve used delimiter (1st line and last line) along with BEGIN & END.
You could also define extra tables for logging. For instance if you already have a table news, you can duplicate it as news_log. then add columns for logging data such as: modified date, action (update, delete, add) and so on.
next you define triggers on the original tables that will insert the data into your logging table. for instance when you update a record in you news table the news_log_trigger that you define is executed and a new record is inserted into new_log with the action value "UPDATE" and the current date as modified date.
for more info on mysql triggers:
http://dev.mysql.com/doc/refman/5.0/en/triggers.html
In case you want to do this for every project you can problably write a generic stored procedure to do the actual logging. that way you can reuse it and you only have to define the triggers and logging tables.