How to remove not null constraint in sql server using query
Asked Answered
N

4

154

I am trying to remove not null constraint in sql server 2008 without losing data.

Nash answered 30/7, 2010 at 9:20 Comment(0)
R
263
 ALTER TABLE YourTable ALTER COLUMN YourColumn columnType NULL
Raman answered 30/7, 2010 at 9:22 Comment(3)
I found I had to include the type in YourColumn eg. ALTER TABLE YourTable ALTER COLUMN YourColumn int NULLGoal
or you can do : alter table table_name modify column_name type(30) NULL. 30 being the size of your column type, example: varchar(30)Insecurity
In postgres: ALTER TABLE YourTable ALTER COLUMN YourColumn DROP NOT NULLBewilderment
S
3

Remove constraint not null to null

ALTER TABLE 'test' CHANGE COLUMN 'testColumn' 'testColumn' datatype NULL;
Shalandashale answered 19/8, 2016 at 18:59 Comment(3)
Why does this work and not the above accepted answer for mysql ?Jemy
@Jemy the question was about Microsoft SQL Server and not MySql.Victorious
I'm finding that our copy of MS SQL Server 2019 isn't familiar with the change keyword.Prognosticate
F
3

Remove column constraint: not null to null

ALTER TABLE test ALTER COLUMN column_01 DROP NOT NULL;
Francoisefrancolin answered 18/4, 2019 at 12:17 Comment(1)
That doesn't look like valid T-SQL. Although the question was marked with SQL, note that the question explicitly refers to SQL Server which only accepts T-SQL.Brenneman
I
-2

Reference: https://www.tutorialspoint.com/How-can-we-remove-NOT-NULL-constraint-from-a-column-of-an-existing-MySQL-table

ALTER TABLE tableName MODIFY columnName columnType NULL;
Improvement answered 17/11, 2019 at 7:1 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.