SQL SERVER – Add Column With Default Column Constraint to Table

Just a day ago while working with database Jr. A developer asked me question how to add a column along with a column constraint. He also wanted to specify the name of the constraint. The newly added column should not allow NULL value. He requested my help as he thought he might have to write many lines to achieve what was requested.

It is very easy to add columns and specify default constraints. I have seen many examples where the constraint name is not specified, if constraint name is not specified SQL Server will generate a unique name for itself. I prefer to specify my constraint name as per my coding standards. You can read my coding standard here: SQL SERVER Database Coding Standards and Guidelines Complete List Download

ALTER TABLE TestTable
ADD NewCol VARCHAR(50)
CONSTRAINT DF_TestTable_NewCol DEFAULT '' NOT NULL
GO

SQL SERVER - Add Column With Default Column Constraint to Table constraint-800x144

Here are few of the related blog posts on this subject.

Please leave a comment about what you think about this blog post about column constraint.

Reference : Pinal Dave (https://blog.sqlauthority.com)

SQL Coding Standards, SQL Constraint and Keys, SQL Scripts, SQL Server
Previous Post
SQL SERVER – 2005 – Analysis Services Query Performance Top 10 Best Practices
Next Post
SQLAuthority News – Book Review – Joe Celkos SQL Puzzles and Answers, Second Edition, Second Edition

Related Posts

Leave a Reply