80

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?

7
  • possible duplicate of How do you drop a default value or similar constraint in T-SQL? Commented Nov 1, 2013 at 10:40
  • any chance of a VTC from you? Commented Nov 1, 2013 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
    Commented Nov 1, 2013 at 13:15
  • we can agree to disagree but stackoverflow.com/a/10758357/11635 answers it perfectly for me and 4 copies of the question is jsut a timewasting PITA for me Commented Nov 1, 2013 at 13:47
  • @RubenBartelink, it may, it doesn't address the default constraint specifically, I think. I would have to test it out.
    – Yishai
    Commented Nov 1, 2013 at 13:50

7 Answers 7

80

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.

1
  • you can obtain the name of the constraint using sp_help 'table_name' in sql server
    – Warios
    Commented Sep 30, 2020 at 8:15
22

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

17

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

UPDATE: If the table has default constraints on different columns,the second line should be changed as suggested in MirrorBoy comment below

select @name = o.name 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('sometable') and col.name = 'someColumn' 

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)

3
  • 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
    Commented Sep 1, 2009 at 21:43
  • 2
    I believe your solution will not work on a table with multiple default values, it will remove the last one
    – AaA
    Commented Apr 22, 2014 at 1:44
  • 2
    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
    Commented Nov 8, 2017 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.

1
  • 1
    It doesn't work, because my constrains have name like 'DF_tableName_someLetters'
    – MirrorBoy
    Commented Nov 8, 2017 at 15:26
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.

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.

-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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.