Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL - Table Design - DateCreated and DateUpdated columns

For my application there are several entity classes, User, Customer, Post, and so on

I'm about to design the database and I want to store the date when the entities were created and updated. This is where it gets tricky. Sure one option is to add created_timestamp and update_timestamp columns for each of the entity tables but that isn't that redudant?

Another possibility could be to create a log table that stores this information, and it could be made to contain keep track of updates for any entity.

Any thoughts? I'm leaning on implementing the latter.

like image 694
Adam Asham Avatar asked Oct 26 '08 00:10

Adam Asham


People also ask

How do I add a last modified and created column in a SQL Server table?

You need to join the Inserted pseudo table which contains all rows that were updated with your base table on your primary key for that table. And you'll have to create this AFTER UPDATE trigger for each table that you want to have a modified column in.

How do I add a date to a column in SQL?

ADD DateOfBirth date; Notice that the new column, "DateOfBirth", is of type date and is going to hold a date. The data type specifies what type of data the column can hold. For a complete reference of all the data types available in MS Access, MySQL, and SQL Server, go to our complete Data Types reference.

How do I find the last updated column in SQL?

To get the last updated record in SQL Server: We can write trigger (which automatically fires) i.e. whenever there is a change (update) that occurs on a row, the “lastupdatedby” column value should get updated by the current timestamp.

How do I find the modified column in SQL Server?

Using a SQL Server trigger to check if a column is updated, there are two ways this can be done; one is to use the function update(<col name>) and the other is to use columns_updated().


1 Answers

The single-log-table-for-all-tables approach has two main problems that I can think of:

  1. The design of the log table will (probably) constrain the design of all the other tables. Most likely the log table would have one column named TableName and then another column named PKValue (which would store the primary key value for the record you're logging). If some of your tables have compound primary keys (i.e. more than one column), then the design of your log table would have to account for this (probably by having columns like PKValue1, PKValue2 etc.).
  2. If this is a web application of some sort, then the user identity that would be available from a trigger would be the application's account, instead of the ID of the web app user (which is most likely what you really want to store in your CreatedBy field). This would only help you distinguish between records created by your web app code and records created otherwise.

CreatedDate and ModifiedDate columns aren't redundant just because they're defined in each table. I would stick with that approach and put insert and update triggers on each table to populate those columns. If I also needed to record the end-user who made the change, I would skip the triggers and populate the timestamp and user fields from my application code.

like image 147
MusiGenesis Avatar answered Sep 20 '22 03:09

MusiGenesis