challengeAccepted
challengeAccepted

Reputation: 7590

Altering a column to be nullable

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?

Upvotes: 514

Views: 778031

Answers (14)

Pritesh singh
Pritesh singh

Reputation: 11

ALTER TABLE Merchant_Pending_Functions ALTER COLUMN NumberOfLocations Varchar (32) NULL

For varchar type column also add size

Upvotes: 0

Paul LeBeau
Paul LeBeau

Reputation: 101800

In PostgreSQL it is:

ALTER TABLE tableName ALTER COLUMN columnName DROP NOT NULL;

Upvotes: 98

Luqman Cheema
Luqman Cheema

Reputation: 477

For SQL Server or TSQL

ALTER TABLE Complaint.HelplineReturn ALTER COLUMN IsDisposed BIT NULL 

Upvotes: 15

abraham
abraham

Reputation: 37

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

ALTER TABLE TABLE_NAME MODIFY COLUMN_NAME DATA_TYPE NULL;

Upvotes: 0

Martin Thoma
Martin Thoma

Reputation: 136177

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

Upvotes: 1

Rodrigo Itursarry
Rodrigo Itursarry

Reputation: 31

Oracle

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

Upvotes: 2

ZORRO_BLANCO
ZORRO_BLANCO

Reputation: 909

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

ALTER TABLE Merchant_Pending_Functions 
Modify NumberOfLocations NULL;

Upvotes: 2

chamzz.dot
chamzz.dot

Reputation: 775

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;

Upvotes: 1

Libor B.
Libor B.

Reputation: 509

For HSQLDB:

ALTER TABLE tableName ALTER COLUMN columnName SET NULL;

Upvotes: 1

djjeck
djjeck

Reputation: 1840

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.

Upvotes: 77

Igor S.
Igor S.

Reputation: 3350

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;

Upvotes: 49

APC
APC

Reputation: 146179

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>

Upvotes: 4

Hammerite
Hammerite

Reputation: 22340

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.

Upvotes: 8

Quassnoi
Quassnoi

Reputation: 425241

Assuming SQL Server (based on your previous questions):

ALTER TABLE Merchant_Pending_Functions ALTER COLUMN NumberOfLocations INT NULL

Replace INT with your actual datatype.

Upvotes: 786

Related Questions