MySQL trigger: turn UPDATE into UPDATE and INSERT? - mysql

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 ;

Related

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

Identifying the edited columns in the database

At the beginning the application have an admins and employees and records.
The records table has many columns than can be changed by any employee. However, this change can not be submitted. When the admin approve for an edit the record will show up again in the system.
I was trying to identify the column name and the value, and send it to another table using triggers on UPDATE.
So when the employee edits any record, the record will be disabled in the system. Also the admin will be able to know which values has been changed.
Is this possible in databases ?
Records Table
-------------------------------------------------------------------
record_id record_name record_serial record_active
-------------------------------------------------------------------
1 something 5151 YES
When an update happens to the record_serial, such as from 5151 to 9844 I need to do this.
Records_changes
-------------------------------------------------------------------
change_id record_col record_old_val record_new_val
-------------------------------------------------------------------
1 record_serial 5151 9844
At the same time
-------------------------------------------------------------------
record_id record_name record_serial record_active
-------------------------------------------------------------------
1 something 9844 NO
I can do it using my application, but if there is anyway to do it using the database it would be much nicer.
I will use this to track the changes, and also create a history for the records old values.
I am using MySQL
You can do something like this
DELIMITER $$
CREATE TRIGGER tg_bu_records
BEFORE UPDATE ON records
FOR EACH ROW
BEGIN
IF NOT (OLD.record_serial <=> NEW.record_serial AND
OLD.record_name <=> NEW.record_name) THEN
SET NEW.record_active = 0;
END IF;
END$$
CREATE TRIGGER tg_au_records
AFTER UPDATE ON records
FOR EACH ROW
BEGIN
IF NOT (OLD.record_serial <=> NEW.record_serial) THEN
INSERT INTO records_changes (record_col, record_old_val, record_new_val)
VALUES ('record_serial', OLD.record_serial, NEW.record_serial);
END IF;
IF NOT (OLD.record_name <=> NEW.record_name) THEN
INSERT INTO records_changes (record_col, record_old_val, record_new_val)
VALUES ('record_name', OLD.record_name, NEW.record_name);
END IF;
END$$
DELIMITER ;
Note: The trick is to change record_active flag in BEFORE trigger because it's the only event when you can change values of a row being updated/inserted in a table on which you defined that trigger. Now in AFTER trigger we record the changes that have been made.
Here is SQLFiddle demo
A good database pattern for this, if I understand your problem correctly, is to use versioned rows. You add columns (or additional referenced tables) with meta data about when/who/what the edit was about. When a record is edited, a new row is inserted. Previous versions are never modified or deleted. Then it's up to your application logic to decide what to do with all of this.

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.

Multiple MySQL queries (no PHP)

I am wondering if it is possible to perform a SQL query then update another table with the generated ID and continue through all of the rows?
I have this SQL query that works but what I need to do is after each row is added to cards to then update merged.cars_id with the last generated ID so they are linked. normally I would do this with PHP but ideally I would like to just do it with MySQL if possible.
MAIN QUERY
INSERT INTO cards (first_contact_date, card_type, property_id, user_id)
SELECT first_contact_date, 'P', property_id, user_id FROM merged
THEN I NEED WITH MATCHING ROWS (Roughly)
UPDATE merged SET merged.card_id = LAST_INSERT_ID (FROM ABOVE) into the matching record..
Is something like this possible and how do I do it?
I would recommend using MySQL triggers to do this
http://dev.mysql.com/doc/refman/5.0/en/create-trigger.html
A trigger is a function that will be executed AFTER or BEFORE the INSERT or DELETE or UPDATE is done over any record of your table.
In your case you need to do a AFTER INSERT on cards that just updates the merged table. Make sure its AFTER insert as you wont be able to access the new row's ID otherwise.
The code would look something like this, assuming the id field from the cards table its named "id"
delimiter |
CREATE TRIGGER updating_merged AFTER INSERT ON cards
FOR EACH ROW BEGIN
UPDATE merged SET card_id = NEW.id;
END;
|
delimiter ;
May I suggest Stored Procedures?
http://dev.mysql.com/doc/refman/5.0/en/create-procedure.html
--EDIT--
Ah yes, triggers. For this particular situation, Jimmy has the answer. I will leave this post for the sake of the link.
I would set up a trigger to do this. For mysql, read http://dev.mysql.com/doc/refman/5.0/en/triggers.html. This is what triggers are designed to handle.

MYSQL auto increase column entity by 1 on update?

I have a table: ID,name,count,varchar(255)
Now, what i'd like is to increase the "count" each time that row in the table is updated.
Of course, the easy way is to read first, get the value, increase by 1 in php, then update with the new value. BUT!
is there any quicker way to do it? is there a system in mysql that can do the ++ automatically? like autoincrement, but for a single entity on itself?
I see two options:
1.
Just add this logic to every update query
UPDATE `table` SET
`data` = 'new_data',
`update_counter` = `update_counter` + 1
WHERE `id` = 123
2.
Create a trigger that will do the work automatically:
CREATE TRIGGER trigger_name
AFTER UPDATE
ON `table`
FOR EACH ROW
BEGIN
UPDATE `table`
SET `update_counter` = `update_counter` + 1
WHERE `id` = NEW.id
END
Create a trigger:
http://dev.mysql.com/doc/refman/5.1/en/create-trigger.html
Triggers are pieces of code that are "triggered" by the database on certain events. In your case, the event would be an update. Many RDBMS support triggers, so does MySQL. The advantage of using a trigger is that every piece of your PHP logic that updates this entity, will implicitly invoke the trigger logic, you don't have to remember that anymore, when you want to update your entity from a different piece of PHP logic.
you can look up at the trigger
or can do with the extra mysql query
update table set count=count+1 ;
UPDATE table SET name='new value', count=count+1 WHERE id=...
An SQL update can use fields in the record being updated as a source of data for the update itself.