Is there any version control in mysql? - 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.

Related

MySQL select statement result in after delete trigger always null

I'm having a little issue with a trigger in a MySQL database. I have a DB with two tables: "tasks" and "files". The "tasks" table have a field which is a foreign key of the primary key from the "files" table. It also sometimes may be null.
What I'm trying to acomplish is to delete in the first place a row in the "tasks" table, and after that delete the corresponding row in the "files" table using a trigger.
This is the trigger I'm using right now:
DELIMITER //
CREATE TRIGGER after_delete_file AFTER DELETE ON tasks
FOR EACH ROW
BEGIN
DECLARE fileId int;
SELECT file INTO fileId FROM tasks WHERE id=old.id;
DELETE FROM files WHERE id=fileId;
END;//
DELIMITER ;
The field "file" in the "tasks" table is the one containing the foreign key. In the examples I've been running, that field has never been null.
The problem is that the select statement always returns null. The delete statement that triggers this trigger goes fine, but the row in the "files" table is never deleted. I've tried to insert the "fieldId" variable on a testing table, and it's always saving a null value.
Is there any problem on that trigger? Maybe I'm trying to do something merely impossible?
All the help is much appreciated :)
Since it should be looping over each deleted row, why would this not work?
DELIMITER //
CREATE TRIGGER after_delete_file AFTER DELETE ON tasks
FOR EACH ROW
BEGIN
DELETE FROM files WHERE id=old.file;
END;//
DELIMITER ;
If that doesn't work, could try this:
DELIMITER //
CREATE TRIGGER after_delete_file AFTER DELETE ON tasks
FOR EACH ROW
BEGIN
DELETE FROM files INNER JOIN tasks ON files.id=tasks.file WHERE tasks.id=old.id;
END;//
DELIMITER ;
but I don't think that should be necessary.
AFTER delete means that the data is deleted, of course you can't find it. Try creating the trigger for BEFORE delete.
You could also more carefully use all of the old values rather than selecting from the table that was deleted from.

MySQL delete similar records after insert with Trigger

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?

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).

MySQL trigger: turn UPDATE into UPDATE and INSERT?

I have several tables that I want to enforce versioning, and have an effective from and effective to date. Whenever an application or user writes an UPDATE to this table I want it redirected into two entirely new commands: UPDATE the targeted record so the EFFECTIVE_TO date is populated with current date and time, and INSERT an entirely new record with the updated attributes.
Is this possible to do with a trigger or do I have to keep controlling this externally with a Java application?
Something like this probably
delimiter |
CREATE TRIGGER versioningcontrol AFTER UPDATE ON yourtable
FOR EACH ROW
BEGIN
INSERT INTO yourtable(col1,col2,...) values(...);
END;
|
delimiter ;

Create a trigger that removes a name from a table if it is present in another

To futher clarify i was trying to create a trigger that checks a table for a number in sql.
If it finds said number then it erases that entire row.
It uses a separate table of names to check.
I thought it be could done using a join but have had no luck.
So it would look like this I suppose if(tb1.name = tb2.name) then DELETE row.
I'm sorry if the formatting is off.
EDIT; I am using phpmyadmin so some of the the code may be missing but here is the code from my latest "attempt"
It uses on INSERT and time is set to AFTER
SELECT * FROM flights WHERE NOT EXISTS (SELECT * FROM no fly list WHERE PassengerId.Id = Passenger.Id)
have not added the DELETE as of now but the work is somewhat ongoing
Assuming that flight and dnf both have passenger_id column that uniquely identifies a passenger of interest then your trigger might look like this
DELIMITER $$
CREATE TRIGGER dnf_insert AFTER INSERT
ON dnf
FOR EACH ROW BEGIN
DELETE FROM flights WHERE passenger_id = NEW.passenger_id;
END$$
DELIMITER ;
If you post DDL (create table statements) for all relevant tables we can refine the query