Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 540083
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T10:12:12+00:00 2026-05-13T10:12:12+00:00

For entities in my application, I am planning to log common meta-data like DateCreated,

  • 0

For entities in my application, I am planning to log common meta-data like DateCreated, DateModified, IPAddress, etc. Does it make sense to add these columns in entity tables or is it better to have a single table for logging meta-data where each row has link back to the item that it corresponds to? Later for purpose of reporting and analysis, I can create desired views.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T10:12:12+00:00Added an answer on May 13, 2026 at 10:12 am

    I use a special query that adds all these common columns (I call them audit columns) to all tables (using a cursor going over the information schema), which makes it easy to apply them to new databases.

    My columns are (SQL Server specific):

    RowId UNIQUEIDENTIFIER NOT NULL DEFAULT (NEWID()),
    Created DATETIME NOT NULL DEFAULT (GETDATE()),
    Creator NVARCHAR(256) NOT NULL DEFAULT(SUSER_SNAME())
    RowStamp TIMESTAMP NOT NULL
    

    Now, in a fully normalised schema, I’d only need RowId, which would link to an Audit table containing the other rows. In fact, on reflection, I almost wish I had gone down this route – mainly because it makes the tables ugly (in fact I leave these columns out of database schema diagrams).

    However, when dealing with very large data sets, you do get a performance boost from having this data within the table, and I haven’t experienced any problems with this system to date.

    Edit: Might as well post the code to add the audit columns:

    DECLARE AuditCursor CURSOR FOR
      SELECT TABLE_SCHEMA, TABLE_NAME from INFORMATION_SCHEMA.TABLES
      WHERE TABLE_TYPE = 'BASE TABLE'
      AND OBJECTPROPERTY(OBJECT_ID(QUOTENAME(TABLE_SCHEMA) + '.' + QUOTENAME(TABLE_NAME)), 'IsMSShipped') = 0
      AND TABLE_NAME NOT IN ('sysdiagrams')
      AND TABLE_NAME NOT LIKE 'dt_%'
    
      -- NB: you could change the above to only do it for certain tables
    
    OPEN AuditCursor
      DECLARE @schema varchar(128), @table varchar(128)
    
      FETCH NEXT FROM AuditCursor INTO @schema, @table
    
      WHILE @@FETCH_STATUS  -1
      BEGIN
    
        PRINT '* Adding audit columns to [' + @schema + '].[' + @table + ']...'
    
        IF NOT EXISTS  (SELECT NULL
            FROM information_schema.columns
            WHERE table_schema = @schema
            AND table_name = @table
            AND column_name = 'Created')
        BEGIN
          DECLARE @sql_created varchar(max)
          SELECT  @sql_created = 'ALTER TABLE [' + @schema + '].[' + @table + '] ADD [Created] DATETIME NOT NULL CONSTRAINT [DF_' + @table + '_Created] DEFAULT (GETDATE())'
    
          EXEC  ( @sql_created )
          PRINT ' - Added Created'
        END
        ELSE
          PRINT ' - Created already exists, skipping'
    
        IF NOT EXISTS  (SELECT NULL
            FROM information_schema.columns
            WHERE table_schema = @schema
            AND table_name = @table
            AND column_name = 'Creator')
        BEGIN
          DECLARE @sql_creator varchar(max)
          SELECT  @sql_creator = 'ALTER TABLE [' + @schema + '].[' + @table + '] ADD [Creator] VARCHAR(256) NOT NULL CONSTRAINT [DF_' + @table + '_Creator] DEFAULT (SUSER_SNAME())'
    
          EXEC  ( @sql_creator )
          PRINT ' - Added Creator'
        END
        ELSE
          PRINT ' - Creator already exists, skipping'
    
        IF NOT EXISTS  (SELECT NULL
            FROM information_schema.columns
            WHERE table_schema = @schema
            AND table_name = @table
            AND column_name = 'RowId')
        BEGIN
          DECLARE @sql_rowid varchar(max)
          SELECT  @sql_rowid = 'ALTER TABLE [' + @schema + '].[' + @table + '] ADD [RowId] UNIQUEIDENTIFIER NOT NULL CONSTRAINT [DF_' + @table + '_RowId] DEFAULT (NEWID())'
    
          EXEC  ( @sql_rowid )
          PRINT ' - Added RowId'
        END
        ELSE
          PRINT ' - RowId already exists, skipping'
    
        IF NOT EXISTS  (SELECT NULL
            FROM information_schema.columns
            WHERE table_schema = @schema
            AND table_name = @table
            AND (column_name = 'RowStamp' OR data_type = 'timestamp'))
        BEGIN
          DECLARE @sql_rowstamp varchar(max)
          SELECT  @sql_rowstamp = 'ALTER TABLE [' + @schema + '].[' + @table + '] ADD [RowStamp] ROWVERSION NOT NULL'
          EXEC  ( @sql_rowstamp )
          PRINT ' - Added RowStamp'
        END
        ELSE
          PRINT ' - RowStamp or another timestamp already exists, skipping'
    
        -- basic tamper protection against non-SA users
        PRINT ' - setting DENY permission on audit columns'
        DECLARE @sql_deny VARCHAR(1000)
        SELECT  @sql_deny = 'DENY UPDATE ON [' + @schema + '].[' + @table + '] ([Created]) TO [public]'
          + 'DENY UPDATE ON [' + @schema + '].[' + @table + '] ([RowId]) TO [public]'
          + 'DENY UPDATE ON [' + @schema + '].[' + @table + '] ([Creator]) TO [public]'
    
        EXEC (@sql_deny)
    
        PRINT '* Completed processing [' + @schema + '].[' + @table + ']'
        FETCH NEXT FROM AuditCursor INTO @schema, @table
    
      END
    
    CLOSE AuditCursor
    DEALLOCATE AuditCursor
    GO
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In my application, I would like to fetch a set of entities from the
I am using ASP.NET Dynamic Data Entities project to generate a web application for
Users in our application participate in forums and create various entities. In the schema
I'm trying build my application using REST and Spring MVC. For some entities I
I'm using JPA to load and persist entities in my Java EE-based web application.
Pretty simple, in my AppEngine application, I have over 1 million entities of one
I have the necessity for a client application to lock (aka check-out certain business
I am new to C# and .NET and I just started to study LINQ
I managed (as you can see in older posts from me) to insert a
I try to map a relation for the first time. I have found a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.