Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add column to existing SQL Server table - Implications

I have an existing table in SQL Server with existing entries (over 1 million in fact).

This table gets updated, inserted and selected from on a regular basis by a front-end application. I want/need to add a datetime column e.g. M_DateModified that can be updated like so:

UPDATE Table SET M_DateModified = GETDATE()

whenever a button gets pressed on the front-end and a stored procedure gets called. This column will be added to an existing report as requested.

My problem, and answer is this. Being one of the core tables of our app, will ALTERING the table and adding an additional column break other existing queries? Obviously, you can't insert into a table without specifying all values for all columns so any existing INSERT queries will break (WHICH is a massive problem).

Any help would be much appreciated on the best solution regarding this problem.

like image 374
frans Avatar asked Aug 12 '13 13:08

frans


People also ask

Can we add column to existing table in SQL?

The ALTER TABLE statement is used to add, delete, or modify columns in an existing table. The ALTER TABLE statement is also used to add and drop various constraints on an existing table.

Can we add column to the existing table with data?

We can use the ALTER TABLE statement to alter our existing table and add in this new column. The basic syntax for adding a new column is as follows: ALTER TABLE table_name ADD column_name data_type constraints; The SQL ALTER TABLE add column statement we have written above takes four arguments.

How do I add a column to an existing table without losing data?

If you use ALTER TABLE statement, it will automatically add the new column to the end of the table. The ALTER privilege is required to perform this: ALTER TABLE table_name ADD col_name data_type NULL | NOT NULL; It will not drop any existing data.


1 Answers

First, as marc_s says, It should only affect SELECT * queries, and not even all of them would necessarily be affected.

Secondly, you only need to specify all non-Null fields on an INSERT, so if you make it NULL-able, you don't have to worry about that. Further, for a Created_Date-type column, it is typical to add a DEFAULT setting of =GetDate(), which will fill it in for you if it is not specified.

Thirdly, if you are still worried about impacting your existing code-base, then do the following:

  1. Rename your table to something like "physicalTable".
  2. Create a View with the same name that your table formely had, that does a SELECT .. FROM physicalTable, listing the columns explicitly and in the same order, but do not include the M_DateModified field in it.
  3. Leave your code unmodified, now referencing the View, instead of directly accessing the table.

Now your code can safely interact with the table without any changes (SQL DML code cannot tell the difference between a Table and a writeable View like this).

Finally, this kind of "ModifiedDate" column is a common need and is most often handled, first by making it NULL-able, then by adding an Insert & Update trigger that sets it automatically:

UPDATE t
SET    M_DateModified = GetDate()
FROM   (SELECT * FROM physicalTable y JOIN inserted i ON y.PkId = i.PkId) As t

This way the application does not have to maintain the field itself. As an added bonus, neither can the application set it incorrectly or falsely (this is a common and acceptable use of triggers in SQL).

like image 73
RBarryYoung Avatar answered Sep 20 '22 01:09

RBarryYoung