523

I want to alter a table column to be nullable. I have used:

ALTER TABLE Merchant_Pending_Functions Modify NumberOfLocations NULL

This gives an error at Modify. What is the correct syntax?

3
  • 10
    Please post the error message in future Commented Aug 24, 2010 at 13:58
  • 4
    I've removed the SQL Server tag as it looks like this has become a general free for all. Commented Mar 6, 2016 at 14:13
  • Please edit your question to say more about what isn't working. Do you see an error message? If so, what is it? It is not producing the expected output? If not, what output do you see and what output do you expect? Commented Feb 6, 2024 at 20:13

14 Answers 14

799

Assuming SQL Server (based on your previous questions):

ALTER TABLE Merchant_Pending_Functions ALTER COLUMN NumberOfLocations INT NULL

Replace INT with your actual datatype.

Sign up to request clarification or add additional context in comments.

3 Comments

Tried to use the table "Design" utility in SSMS to change a bit column from NOT NULL to NULL. I got an error that indicated that the table needed to be dropped and re-created. Whoa! This ALTER TABLE syntax worked and was much easier than dropping and re-creating a table to make a single column nullable.
@JohnH Most attempts to modify a table with the Designer result in that warning. You can remedy that by changing the options in SSMS: stackoverflow.com/questions/1969096/…
@JLRishe It's worth noting that it's not advisable to remove that warning unless someone really knows the consequences of the designer dropping/recreating a table behind-the-scenes. It has the potential to bite.
105

In PostgreSQL it is:

ALTER TABLE tableName ALTER COLUMN columnName DROP NOT NULL;

Comments

77

If this was MySQL syntax, the type would have been missing, as some other responses point out. Correct MySQL syntax would have been:

ALTER TABLE Merchant_Pending_Functions MODIFY NumberOfLocations INT NULL

Posting here for clarity to MySQL users.

Comments

49

for Oracle Database 10g users:

alter table mytable modify(mycolumn null);

You get "ORA-01735: invalid ALTER TABLE option" when you try otherwise

ALTER TABLE mytable ALTER COLUMN mycolumn DROP NOT NULL;

1 Comment

Cool, you don't have to specify the data-type, just "null" to make it nullable.
15

For SQL Server or TSQL

ALTER TABLE Complaint.HelplineReturn ALTER COLUMN IsDisposed BIT NULL 

Comments

8

Although I don't know what RDBMS you are using, you probably need to give the whole column specification, not just say that you now want it to be nullable. For example, if it's currently INT NOT NULL, you should issue ALTER TABLE Merchant_Pending_Functions Modify NumberOfLocations INT.

1 Comment

This is a correct and descriptive answer, so just clarifying if Null | NOT NULL is not specified, the column will be nullable.
4

As others have observed, the precise syntax for the command varies across different flavours of DBMS. The syntax you use works in Oracle:

SQL> desc MACAddresses
 Name                                      Null?    Type
 ----------------------------------------- -------- ----------------------------
 COMPUTER                                           NUMBER
 MACADDRESS                                         VARCHAR2(12)
 CORRECTED_MACADDRESS                      NOT NULL VARCHAR2(17)

SQL> alter table MACAddresses
  2       modify corrected_MACAddress null
  3  /

Table altered.

SQL> desc MACAddresses
 Name                                      Null?    Type
 ----------------------------------------- -------- ----------------------------
 COMPUTER                                           NUMBER
 MACADDRESS                                         VARCHAR2(12)
 CORRECTED_MACADDRESS                               VARCHAR2(17)

SQL>

Comments

2

This depends on what SQL Engine you are using, in Sybase your command works fine:

ALTER TABLE Merchant_Pending_Functions 
Modify NumberOfLocations NULL;

Comments

2

Oracle

ALTER TABLE Merchant_Pending_Functions MODIFY([column] NOT NULL);

1 Comment

Does this answer bring anything new with respect to @IgorS's answer? And what does SQL_SCRIPT stand for?
1

For HSQLDB:

ALTER TABLE tableName ALTER COLUMN columnName SET NULL;

Comments

1
ALTER TABLE Merchant_Pending_Functions MODIFY COLUMN `NumberOfLocations` INT null;

This will work for you.

If you want to change a not null column to allow null, no need to include not null clause. Because default columns get not null.

ALTER TABLE Merchant_Pending_Functions MODIFY COLUMN `NumberOfLocations` INT;

Comments

1

SQLite

The ALTER TABLE command is a bit special. There is no possibility to modify a column. You have to create a new column, migrate the data, and then drop the column:

-- 1. First rename
ALTER TABLE
    Merchant_Pending_Functions
RENAME COLUMN
    NumberOfLocations
TO
   NumberOfLocations_old

-- 2. Create new column
ALTER TABLE
    Merchant_Pending_Functions
ADD COLUMN
    NumberOfLocations INT NULL

-- 3. Migrate data - you need to write code for that
-- 4. Drop the old column
ALTER TABLE
    Merchant_Pending_Functions
DROP COLUMN
    NumberOfLocations_old

3 Comments

Thanks Dr Hipp. SQLite is unusual as the schema is stored character for character as the user entered it, including all the white space. This is claimed as an advantage as it takes less storage. But makes DDL tasks difficult. Also a real pain for DBAs.
What do you mean with "Dr Hipp"?
Dr Richard Hipp wrote SQLite. And made the world a better place.
1
ALTER TABLE Merchant_Pending_Functions ALTER COLUMN NumberOfLocations Varchar (32) NULL

For varchar type column also add size

1 Comment

Thank you for your interest in contributing to the Stack Overflow community. This question already has quite a few answers—including one that has been extensively validated by the community. Are you certain your approach hasn’t been given previously? If so, it would be useful to explain how your approach is different, under what circumstances your approach might be preferred, and/or why you think the previous answers aren’t sufficient. Can you kindly edit your answer to offer an explanation?
0

Make sure you add the data_type of the column to modify.

ALTER TABLE TABLE_NAME MODIFY COLUMN_NAME DATA_TYPE NULL;

1 Comment

Please add code type notation for your answer and some describe about this

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.