76

How do you alter a column to remove the default value?

The column was created with:

 ALTER table sometable Add somecolumn nchar(1) NOT NULL DEFAULT 'N'

And then altered with:

 alter table sometable alter column somecolumn nchar(1) null

That allows nulls, but the default value remains. How can you remove it?

Yishai
  • 90,445
  • 31
  • 189
  • 263
  • possible duplicate of [How do you drop a default value or similar constraint in T-SQL?](http://stackoverflow.com/questions/1123060/how-do-you-drop-a-default-value-or-similar-constraint-in-t-sql) – Ruben Bartelink Nov 01 '13 at 10:40
  • any chance of a VTC from you? – Ruben Bartelink Nov 01 '13 at 10:51
  • @RubenBartelink, the answers there are half the solution. If a Mods wants to merge, I don't mind, but my answer below actually goes to the end of how to drop the constraint, instead of just discovering the name. – Yishai Nov 01 '13 at 13:15
  • we can agree to disagree but http://stackoverflow.com/a/10758357/11635 answers it perfectly for me and 4 copies of the question is jsut a timewasting PITA for me – Ruben Bartelink Nov 01 '13 at 13:47
  • @RubenBartelink, it may, it doesn't address the default constraint specifically, I think. I would have to test it out. – Yishai Nov 01 '13 at 13:50
  • The thing I linked to specifically (only) addresses default (vs other) constraints. Hopefully as VTCs from the other 3 dups percolate, people will contribute the unique bits of their answers to the the oldest question (This one isnt too bad, at least its 2009 - there are dups from 2010, 2011 and 2012 too) – Ruben Bartelink Nov 01 '13 at 14:56
  • @RubenBartelink, Alright, I'll take your word for it. I don't have time to check. – Yishai Nov 01 '13 at 15:04

7 Answers7

77

Its a default constraint, you need to perform a:

ALTER TABLE {TableName} 
DROP CONSTRAINT ConstraintName

If you didn't specify a name when you created the constraint, then SQL Server created one for you. You can use SQL Server Management Studio to find the constraint name by browsing to the table, opening its tree node, then opening the Constraints node.

If I remember correctly, the constraint will be named something along the lines of DF_SomeStuff_ColumnName.

EDIT: Josh W.'s answer contains a link to a SO question that shows you how to find the auto generated constraint name using SQL instead of using the Management Studio interface.

Mason G. Zhwiti
  • 6,444
  • 11
  • 61
  • 97
Dan Rigby
  • 17,133
  • 6
  • 43
  • 60
21

If you not know of constraints name

DECLARE @ConstraintName nvarchar(200)
SELECT @ConstraintName = Name FROM SYS.DEFAULT_CONSTRAINTS
WHERE PARENT_OBJECT_ID = OBJECT_ID('__TableName__')
AND PARENT_COLUMN_ID = (SELECT column_id FROM sys.columns
                        WHERE NAME = N'__ColumnName__'
                        AND object_id = OBJECT_ID(N'__TableName__'))
IF @ConstraintName IS NOT NULL
EXEC('ALTER TABLE __TableName__ DROP CONSTRAINT ' + @ConstraintName)

https://stackoverflow.com/a/13715343/2547164

Mise
  • 3,267
  • 1
  • 22
  • 22
15

This is what I came up with (before seeing Josh W. answer, well actually I saw it but skimmed it so fast I misunderstood it):

declare @name nvarchar(100)
select @name = [name] from sys.objects where type = 'D' and parent_object_id = object_id('sometable')

if (@name is not null)
  begin
     exec ('alter table [sometable] drop constraint [' + @name +']')
  end

The advantage I have here is that I know that there is only one such constraint on the whole table. If there had been two, well I guess that is why you are supposed to name them ;).

(The issues is that that this is a modification made to 10 different customer databases, so there isn't one consistent name to put in a script)

JKennedy
  • 18,150
  • 17
  • 114
  • 198
Yishai
  • 90,445
  • 31
  • 189
  • 263
  • 1
    Yeah, that becomes an issue for any large scale database roll out. What you end up having to do is force yourself to always name your constraints so that they're consistent across environments. Ex.: ALTER table sometable Add somecolumn nchar(1) NOT NULL CONSTRAINT DF_TableName_SomeColumn DEFAULT 'N' – Dan Rigby Sep 01 '09 at 21:43
  • 1
    I believe your solution will not work on a table with multiple default values, it will remove the last one – AaA Apr 22 '14 at 01:44
  • 1
    If the table has some constrains to different columns it is necessary to filter by column name: `declare @name nvarchar(100) select * from sys.objects o join sys.columns col on o.object_id = col.default_object_id where type = 'D' and parent_object_id = object_id('tablename') and col.name = 'columnname'` – MirrorBoy Nov 08 '17 at 15:28
6
select name from sys.default_constraints where name like '%first_3_chars_of_field_name%'

Find your constraint and use DROP CONSTRAINT to drop it. Or run a cursor/while loop to drop all similar defaults in the database.

user247702
  • 23,641
  • 15
  • 110
  • 157
Roman
  • 61
  • 1
  • 1
1

If you are using SQL Server Management Studio this is pretty easy.

If there are several constraints associated with the table and you don't want to delete them all, right-click on the constraint and select "Script Constraint as -> CREATE to -> New Query Editor Window". This will show you the code that created the constraint including the column name and default value.

Then just right-click on the constraint you want to delete and select Delete.

Mike Godin
  • 3,727
  • 3
  • 27
  • 29
0

I also had the same problem, and altered the table and added Default value the same way as you did. But non of the above solutions solved my problem because despite several refreshes the default value was not listed in the constraint list however it was functioning while inserting into table.

I finally solved it by right clicking the table name in SQL Management Studio and selecting Design. Then I deleted the default values there in the column properties.

Tekin
  • 554
  • 7
  • 17
-2
alter table <tablename> drop constraint <constraintname>

if you don't know the name of the constraint you can use sql server manangement studio to check the constraint name .... hope this helps.

Daniyal Awan
  • 107
  • 5