Modify table: How to change 'Allow Nulls' attribute from not null to allow null

SqlSql ServerSql Server-2008

Sql Problem Overview


How to change one attribute in a table using T-SQL to allow nulls (not null --> null)? Alter table maybe?

Sql Solutions


Solution 1 - Sql

-- replace NVARCHAR(42) with the actual type of your column
ALTER TABLE your_table
ALTER COLUMN your_column NVARCHAR(42) NULL

Solution 2 - Sql

Yes you can use ALTER TABLE as follows:

ALTER TABLE [table name] ALTER COLUMN [column name] [data type] NULL

Quoting from the ALTER TABLE documentation:

> NULL can be specified in ALTER COLUMN to force a NOT NULL column to allow null values, except for columns in PRIMARY KEY constraints.

Solution 3 - Sql

ALTER TABLE is right:

ALTER TABLE MyCustomers ALTER COLUMN CompanyName VARCHAR(20) NULL

Solution 4 - Sql

For MySQL, MariaDB

ALTER TABLE [table name] MODIFY COLUMN [column name] [data type] NULL

Use MODIFY COLUMN instead of ALTER COLUMN.

Solution 5 - Sql

ALTER TABLE public.contract_termination_requests
ALTER COLUMN management_company_id DROP NOT NULL;

Solution 6 - Sql

I wrote this so I could edit all tables and columns to null at once:

select 
case
when sc.max_length = '-1' and st.name in ('char','decimal','nvarchar','varchar')
then
'alter table  [' + so.name + '] alter column [' + sc.name + '] ' + st.name + '(MAX) NULL'
when st.name in ('char','decimal','nvarchar','varchar')
then
'alter table  [' + so.name + '] alter column [' + sc.name + '] ' + st.name + '(' + cast(sc.max_length as varchar(4)) + ') NULL'
else
'alter table  [' + so.name + '] alter column [' + sc.name + '] ' + st.name + ' NULL'
end as query
from sys.columns sc
inner join sys.types st on st.system_type_id = sc.system_type_id
inner join sys.objects so on so.object_id = sc.object_id
where so.type = 'U'
and st.name <> 'timestamp'
order by st.name

Solution 7 - Sql

This is the approach to do this: -

  1. Check whether the table or column exists or not.

  2. If yes, then alter the column. e.g:-

IF EXISTS (SELECT 1 FROM INFORMATION_SCHEMA.COLUMNS WHERE 
        	TABLE_CATALOG = 'DBName' AND 
        	TABLE_SCHEMA = 'SchemaName' AND
        	TABLE_NAME = 'TableName' AND
        	COLUMN_NAME = 'ColumnName')
BEGIN
    ALTER TABLE DBName.SchemaName.TableName ALTER COLUMN ColumnName [data type] NULL
END  

If you don't have any schema then delete the schema line because you don't need to give the default schema.

Solution 8 - Sql

So the simplest way is,

alter table table_name change column_name column_name int(11) NULL;

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionCrazyMouseView Question on Stackoverflow
Solution 1 - SqlLukeHView Answer on Stackoverflow
Solution 2 - SqlDaniel VassalloView Answer on Stackoverflow
Solution 3 - SqlOdedView Answer on Stackoverflow
Solution 4 - SqlVijay NandwanaView Answer on Stackoverflow
Solution 5 - Sqllaxmi kalakeView Answer on Stackoverflow
Solution 6 - SqlJeffrey PallattView Answer on Stackoverflow
Solution 7 - SqlTilak DewanganView Answer on Stackoverflow
Solution 8 - SqlMohsin YounasView Answer on Stackoverflow