<p>Is there any way to use inheritance in database (Specifically in SQL Server 2005)?</p>

<p>Suppose I have few field like <strong>CreatedOn</strong>, <strong>CreatedBy</strong> which I want to add on all of my entities. I looking for an alternative way instead of adding these fields to every table.</p>

[{'answer_id': 5803, 'author': 'GateKiller', 'author_id': 383, 'author_profile': 'https://Stackoverflow.com/users/383', 'pm_score': 0, 'selected': False, 'text': '<p>You could create a template in the template pane in Management Studio. And then use that template every time you want to create a new table.</p>\n\n<p>Failing that, you could store the CreatedOn and CreatedBy fields in an Audit trail table referencing the original table and id.</p>\n\n<p>Failing that, do it manually.</p>\n'}, {'answer_id': 5807, 'author': 'Neall', 'author_id': 619, 'author_profile': 'https://Stackoverflow.com/users/619', 'pm_score': 2, 'selected': False, 'text': "<p>PostgreSQL has this feature. Just add this to the end of your table definition:</p>\n\n<pre><code>INHERITS FROM (tablename[, othertable...])\n</code></pre>\n\n<p>The child table will have all the columns of its parent, and changes to the parent table will change the child. Also, everything in the child table will come up in queries to the parent table (by default). Unfortunately indices don't cross the parent/child border, which also means you can't make sure that certain columns are unique across both the parent and child.</p>\n\n<p>As far as I know, it's not a feature used very often.</p>\n"}, {'answer_id': 5812, 'author': 'Lasse V. Karlsen', 'author_id': 267, 'author_profile': 'https://Stackoverflow.com/users/267', 'pm_score': 3, 'selected': True, 'text': "<p>There is no such thing as inheritance between tables in SQL Server 2005, and as noted by the others, you can get as far as getting help adding the necessary columns to the tables when you create them, but it won't be inheritance as you know it.</p>\n\n<p>Think of it more like a template for your source code files.</p>\n\n<p>As GateKiller mentions, you can create a table containing the shared data and reference it with a foreign key, but you'll either have to have audit hooks, triggers, or do the update manually.</p>\n\n<p>Bottom line: Manual work.</p>\n"}, {'answer_id': 5826, 'author': 'Jorriss', 'author_id': 621, 'author_profile': 'https://Stackoverflow.com/users/621', 'pm_score': 0, 'selected': False, 'text': "<p>You could use a data modeling tool such as ER/Studio or ERWin. Both tools have domain columns where you can define a column template that you can apply to any table. When the domain changes so do the associated columns. ER/Studio also has trigger templates that you can build and apply to any table. This is how we update our LastUpdatedBy and LastUpdatedDate columns without having to build and maintain hundreds of trigger scripts. </p>\n\n<p>If you do create an audit table you would have one row for every row in every table that uses the audit table. That could get messy. In my opinion, you're better off putting the audit columns in every table. You also may want to put a timestamp column in all of your tables. You never know when concurrency becomes a problem. Our DB audit columns that we put in every table are: CreatedDt, LastUpdatedBy, LastUpdatedDt and Timestamp.</p>\n\n<p>Hope this helps.</p>\n"}, {'answer_id': 6037, 'author': 'ZombieSheep', 'author_id': 377, 'author_profile': 'https://Stackoverflow.com/users/377', 'pm_score': 0, 'selected': False, 'text': "<p>We have a SProc that adds audit columns to a given table, and (optionally) creates a history table and associated triggers to track changes to a value.  Unfortunately, company policy means I can't share, but it really isn't difficult to achieve.</p>\n"}, {'answer_id': 17826, 'author': 'Rowan', 'author_id': 2087, 'author_profile': 'https://Stackoverflow.com/users/2087', 'pm_score': 0, 'selected': False, 'text': '<p>If you are using GUIDs you could create a CreateHistory table with columns GUID, CreatedOn, CreatedBy. For populating the table you would still have to create a trigger for every table or handle it in the application logic.</p>\n'}, {'answer_id': 69658, 'author': 'decibel', 'author_id': 11116, 'author_profile': 'https://Stackoverflow.com/users/11116', 'pm_score': 0, 'selected': False, 'text': '<p>You do NOT want to use inheritance to do this! When table B, C and D inherits from table A, that means that querying table A will give you records from B, C and D. Now consider...</p>\n\n<p>DELETE FROM a;</p>\n\n<p>Instead of inheritance, use LIKE instead...</p>\n\n<pre><code>CREATE TABLE blah (\n    blah_id     serial       PRIMARY KEY\n    , something text         NOT NULL\n    , LIKE template_table    INCLUDING DEFALUTS\n);\n</code></pre>\n'}, {'answer_id': 86661, 'author': 'Mike McAllister', 'author_id': 16247, 'author_profile': 'https://Stackoverflow.com/users/16247', 'pm_score': 0, 'selected': False, 'text': '<p>Ramesh - I would implement this using supertype and subtype relationships in my E-R model. There are a few different physical options you have of implementing the relationships as well.</p>\n'}, {'answer_id': 86900, 'author': 'Steven A. Lowe', 'author_id': 9345, 'author_profile': 'https://Stackoverflow.com/users/9345', 'pm_score': 0, 'selected': False, 'text': '<p>in O-R mapping, inheritance maps to a parent table where the parent and child tables use the same identifier</p>\n\n<p>for example</p>\n\n<pre><code>create table Object (\n    Id int NOT NULL --primary key, auto-increment\n    Name varchar(32)\n)\ncreate table SubObject (\n    Id int NOT NULL  --primary key and also foreign key to Object\n    Description varchar(32)\n)\n</code></pre>\n\n<p>SubObject has a foreign-key relationship to Object. when you create a SubObject row, you must first create an Object row and use the Id in both rows</p>\n'}]
['https://Stackoverflow.com/questions/5802', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/191/']