【问题标题】:How to remove not null constraint in sql server using query如何使用查询删除sql server中的非空约束
【发布时间】:2011-03-23 03:50:19
【问题描述】:

我正在尝试在不丢失数据的情况下删除 sql server 2008 中的非空约束。

【问题讨论】:

    标签: sql-server sql


    【解决方案1】:
     ALTER TABLE YourTable ALTER COLUMN YourColumn columnType NULL
    

    【讨论】:

    • 我发现我必须在 YourColumn 中包含类型,例如。 ALTER TABLE YourTable ALTER COLUMN YourColumn int NULL
    • 或者你可以这样做:alter table table_name modify column_name type(30) NULL。 30 是列类型的大小,例如:varchar(30)
    • 在 postgres 中:ALTER TABLE YourTable ALTER COLUMN YourColumn DROP NOT NULL
    【解决方案2】:

    删除约束not nullnull

    ALTER TABLE 'test' CHANGE COLUMN 'testColumn' 'testColumn' datatype NULL;
    

    【讨论】:

    • 为什么这行得通,而不是上面接受的 mysql 答案?
    • @HopeKing 问题是关于 Microsoft SQL Server 而不是 MySql。
    【解决方案3】:

    删除列约束:not nullnull

    ALTER TABLE test ALTER COLUMN column_01 DROP NOT NULL;
    

    【讨论】:

    • 这看起来不像是有效的 T-SQL。虽然问题用 SQL 进行了标记,但请注意,该问题明确指的是仅接受 T-SQL 的 SQL Server。
    【解决方案4】:

    【讨论】:

      猜你喜欢
      • 2016-07-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-12-08
      • 2020-12-20
      • 2012-03-20
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多