I need to know the best way for keeping the change of a table rows.
So I have article table (ArticleId,Designation,Price), the price change every year and I need to keep the previous value(in 2015 = 500$ and in 2016 = 600$), I propose to create another table ArticleHistory with the some structure and insert the copy of row article with the new value or save the new value in a new row , especially knowing that article table count more than 5000 rows.
Using a history table is a good option and you can use an after update update trigger on your Article table to insert the rows in the history table..
Another option is to use Change Data Capture.
Related
I have a table A with attributes studentID(PK),name,address and allotment_status(value can be zero or one) and Table B with roomid(PK),studentID(FK) and roomno now I want that whenever the value of allotment_status is updated to one a new row is inserted in table B and whenever it is set to zero if row exists in table B it gets deleted.
One of way to create trigger on tableA, with update/insert/delete event. This is pure database solution. Whether it's good design or bad depends on your business requirements. So weight it thoroughly before making a decision. The other solution could be to code in your PHP application layer, but I have less experience on that, so would like to avoid answer code level details.
CREATE TRIGGER on_tablea_updateb after/before INSERT/update ON tableA FOR EACH ROW // your business logic goes here condition....; END IF;//
My situation is this:
I have a table, call it x.
Every time a row is updated or deleted, a copy of the old row should be inserted into x_history.
Additionally x_history will have its' own auto-incrementing id column, call that histid.
It is very important to have its' own id column as this will give us the flexibility to build version restore functionality.
I have 100+ tables to apply this to so I'm looking for a generic trigger that can be used for any table to backup one row into a history table. Only the 2 table names should vary from trigger to trigger. Specifying all column names is really not what I'm looking for.
I need to do this in MySQL but have added MSSQL too - I know both so can convert between one and the other easy enough.
Usually, Triggers are not the optimal solution for such purposes.
If possible, you might want to consider changing your database design.
Normally, a better way to handle such things are keeping the hole history in the source table, And have a status column that tells you for each row if it's deleted, updated, or current.
I have little to no experience with MySql, but I have been working with Sql server for the past 7 or 8 years, so what I'm about to say is true for sql server, but may be different for MySql.
If you choose to go with the triggers approach, keep in mind that after update triggers will execute even if the update does not change the row data (e.g update tableName set col1 = 1 where idCol = 4, the update trigger will be executed even if the col1 value before the update was 1, so no data was changed.)
For SqlServer, you might want to consider a common history table, that has only 6 columns:
1. Identity column
2. Table name column
3. Row Id column (original id from the original table)
4. Row Status column (e.g updated, deleted)
5. Action date (the date the row was copied to the history table)
6. Row content column (this should be an XML datatype (not sure if MySql has such dataType))
and then all you have to do is to use "SELECT * FROM deleted/inserted FOR XML AUTO" to create the content for the 6th. column.
Assuming I have the following table named "contacts":
id|name|age
1|John|5
2|Amy|2
3|Eric|6
Is there some easy way to check whether or not this table changes much like how a sha/md5 hash works when getting the checksum for a file on your computer?
So for example, if a new row was added to this table, or if a value was changed within the table, the "hash" or some generated value shows that the table has changed.
If there is no direct mechanism, what is the best way to do this (could be some arbirary hash mechanism, as long as the method puts emphasis on performance and minimizing latency)? Could it be applied to multiple tables?
There is no direct mechanism to get that information through SQL.
You could consider adding an additional LastModified column to each row. To know the last time the table was modified, select the maximum value for that column.
You could achieve a similar outcome by using a trigger on the table for INSERT, UPDATE and DELETE, which updates a separate table with the last modified timestamp.
If you want to know if something has changed, you need something to compare. For example a date. You can add a table with two columns, the tablename and the timestamp, and program a trigger for the events on the table you are interested to control, so this trigger will update the timestamp column of this control table.
If the table isn't too big, you could take a copy of the entire table. When you want to check for changes, you can then query the old vs. new data.
drop table backup_table_name;
CREATE TABLE backup_table_name LIKE table_name;
INSERT INTO backup_table_name SELECT * FROM `table_name`;
I'm quite new to sql. I am using a mysql db with opensource cms. I want to insert a row into the zone table which has all of the locale names stored inside.
I want to insert a row at position 3561, and increment the value of zone id for all of the following rows. Can you help?
Also, if you know of any good tutorial resources that you could recommend and perhaps a decent online reference (both free please - I'm skint) then I'd be grateful.
Cheers
You don't want to do this. The zone_id should be an id in the zone table that serves no purpose other than identifying the row in the table. Generally, these are auto-incremented ids that simply add 1 to the previous largest id.
You can insert, delete, and modify the rows in the zone table. The id will always refer to the same row. This helps ensure relational integrity. So, you can refer to a row in the table using the id, rather than some other feature that might get updated.
If, for some reason, you need to output the rows in the table with a sequential id, there are ways to do this in most databases, including MySQL.
Thanks for your help. I received an answer from the opencart forum and it seems there is a php function to achieve this built into the admin.
I have a table that one of this record has been changed.
Is there any MySQL function to get the date of this updating?
No. There is no way to get that modification date unless you explicitly stored it somewhere else (through a trigger or application side technique).
Well it depends on how you are using the data value.
but mostly it is a good practice to have both created_at and updated_at as attributes for your table.
OR if you want to keep each update date info for a record then store them in different table. before each update in main table insert existing row in tracking table from main table.