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

后端 未结 8 843
星月不相逢
星月不相逢 2020-12-12 13:48

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

相关标签:
8条回答
  • 2020-12-12 14:49
    ALTER TABLE public.contract_termination_requests
    ALTER COLUMN management_company_id DROP NOT NULL;
    
    0 讨论(0)
  • 2020-12-12 14:49

    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.

    0 讨论(0)
提交回复
热议问题