138

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

Shantanu Gupta
  • 20,130
  • 53
  • 177
  • 277

4 Answers4

239
 ALTER TABLE YourTable ALTER COLUMN YourColumn columnType NULL
Omu
  • 67,351
  • 88
  • 268
  • 400
Michael Pakhantsov
  • 24,077
  • 5
  • 58
  • 59
  • 5
    I found I had to include the type in YourColumn eg. ALTER TABLE YourTable ALTER COLUMN YourColumn int NULL – Adam Butler Jun 28 '11 at 01:16
  • 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) – nr5 Sep 19 '12 at 18:11
  • 60
    In postgres: `ALTER TABLE YourTable ALTER COLUMN YourColumn DROP NOT NULL` – Shane Mar 20 '13 at 16:41
4

Remove constraint not null to null

ALTER TABLE 'test' CHANGE COLUMN 'testColumn' 'testColumn' datatype NULL;
Pang
  • 9,073
  • 146
  • 84
  • 117
Nan Yu
  • 57
  • 1
0

Remove column constraint: not null to null

ALTER TABLE test ALTER COLUMN column_01 DROP NOT NULL;
PasQualE
  • 17
  • 4
  • 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. – TT. Apr 18 '19 at 14:06
-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;
Rujoota Shah
  • 1,051
  • 13
  • 17