Default Constraint Names

By Bill Graziano on 24 January 2001 | 2 Comments | Tags: Table Design

A DBA I know named Lance sent me a view and a write-up on it and asked if I'd like to publish it. He posts in the forum under JohnDeere. Lance was fighting with server named default constraints and was using the view to help manage and rename the constraints.

When you create a table in SQL Server you can add various constraints to the table. One of the constraints you can add is a default contstraint. Your CREATE TABLE might look like this:
CREATE TABLE Test1 (Val1 int DEFAULT -1, 
  Val2 int DEFAULT -2, 
  Val3 int DEFAULT -3 )

SQL Server will assign each default constraint default a default name. In my case it named the first default constraint DF__Test1__Val1__32E0915F. You can see the default values for each column in the Enterprise Manager in the Design Table dialog box. You can't see the constraint names however.

This doesn't cause you any problems until you try to change one of the defaults. In order to alter a constraint, you need to know the name of the constraint. The code to change the default value from -1 to -9 looks like this:

  DROP CONSTRAINT DF__Test1__Val1__32E0915F

  ADD CONSTRAINT DF__Test1__Val1__32E0915F DEFAULT (-9) FOR Val1

This becomes important in rolling out new databases or keeping multiple copies synchronized. The problems that Lance faced were compounded by the fact that he had four databases on two remote servers. Lance created a view to help him identify the default constraints and rename them. Here's his view:

select	db_name()				as CONSTRAINT_CATALOG
	, 				as TABLE_NAME
	,user_name(c_obj.uid)			as CONSTRAINT_SCHEMA
	,				as COLUMN_NAME
	,col.colid				as ORDINAL_POSITION
	,com.text				as DEFAULT_CLAUSE

from	sysobjects	c_obj
join 	syscomments	com on =
join 	sysobjects	t_obj on c_obj.parent_obj =  
join    sysconstraints con on	= con.constid
join 	syscolumns	col on =
			and con.colid = col.colid
	c_obj.uid	= user_id()
	and c_obj.xtype	= 'D'


After that, all Lance needed to do was drop the constraints and recreate them using a naming scheme he defined. All that took was a little dynamic SQL and SQL Server do the work.

Lance's advice was to always name your constraints. Especially if you are using scripts to rollout or modify databases. This works much better if the initial CREATE TABLE looked like this:

  Val1 int CONSTRAINT Test1_Val1_DF DEFAULT -1, 
  Val2 int CONSTRAINT Test1_Val2_DF DEFAULT -2, 
  Val3 int CONSTRAINT Test1_Val3_DF DEFAULT -3 )

It's a little more work up front but worth it if you're going to be working in a multiple server environment.

Discuss this article: 2 Comments so far. Print this Article.

If you like this article you can sign up for our weekly newsletter. There's an opt-out link at the bottom of each newsletter so it's easy to unsubscribe at any time.

Email Address:

Related Articles

Implementing Table Interfaces (19 May 2008)

Implementing Table Inheritance in SQL Server (20 February 2008)

Custom Auto-Generated Sequences with SQL Server (24 April 2007)

The Case for the Surrogate Key (9 August 2002)

Using TABLE Variables (7 June 2002)

More Trees & Hierarchies in SQL (1 May 2002)

Temporary Tables (17 January 2001)

Denormalize for Performance (10 January 2001)

Other Recent Forum Posts

Getting current quarter & 4 previous quarters data (1 Reply)

Report model security (1 Reply)

More data extract - expert opinion (1 Reply)

SQL Server Mirroring - Two Principal databases (1 Reply)

Ask a query to make reports (0 Replies)

SQL CLR, HttpWebRequest, and Connection Throttling (1 Reply)

Search 2 Dates between Date Range (0 Replies)

SQL group by clause issue (0 Replies)

Subscribe to

Weekly SQL Server newsletter with articles, forum posts, and blog posts via email. Subscribers receive our white paper with performance tips for developers. Articles via RSS Weblog via RSS

- Advertisement -