148

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

Shantanu Gupta
  • 20,688
  • 54
  • 182
  • 286

4 Answers4

259
 ALTER TABLE YourTable ALTER COLUMN YourColumn columnType NULL
Omu
  • 69,856
  • 92
  • 277
  • 407
Michael Pakhantsov
  • 24,855
  • 6
  • 60
  • 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
  • 69
    In postgres: `ALTER TABLE YourTable ALTER COLUMN YourColumn DROP NOT NULL` – Shane Mar 20 '13 at 16:41
3

Remove constraint not null to null

ALTER TABLE 'test' CHANGE COLUMN 'testColumn' 'testColumn' datatype NULL;
Pang
  • 9,564
  • 146
  • 81
  • 122
Nan Yu
  • 55
  • 1
2

Remove column constraint: not null to null

ALTER TABLE test ALTER COLUMN column_01 DROP NOT NULL;
PasQualE
  • 37
  • 4
  • 3
    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,251
  • 16
  • 18