有时候我们在drop column的时候,会遇到一些default constraints而不能drop,如果我们已经知道constraint name,则可以用下面的语句先把constraint remove掉,然后再drop column。

declare @sql nvarchar()
set @sql = N'alter table [system] drop constraint DF_system_LastGraceDate'
exec sp_executesql @sql

如果我们不知道constraint name,我们可以先把他们找出来,然后再remove掉。

-- first define variables
declare @default sysname, @sql nvarchar(max) -- get name of default constraint
select @default = name
from sys.default_constraints
where parent_object_id = object_id('TABLE_NAME')
AND type = 'D'
AND parent_column_id = (
select column_id
from sys.columns
where object_id = object_id('TABLE_NAME')
and name = 'COLUMN_NAME'
) -- create alter table command as string and run it
set @sql = N'alter table TABLE_NAME drop constraint ' + @default
exec sp_executesql @sql

drop有default constraint的column的更多相关文章

  1. SQL DEFAULT 约束

    DEFAULT 约束用于向列中插入默认值. 如果没有规定其他的值,那么会将默认值添加到所有的新记录. 下面的 SQL 在 "Persons" 表创建时为 "City&qu ...

  2. Part 3 talking about constraint in sql

    What is Foreign key and how to create a Foreign key constraint? Note:Foreign Keys are used to enforc ...

  3. 数据定义: CREATE、DROP、ALTER

    CREATE DATABASE 句法 CREATE DATABASE [IF NOT EXISTS] db_name 数据库.表.索引.列和别名 中被给出. 如果数据库已经存在,并且你没有指定 IF ...

  4. SQL-W3School-高级:SQL DEFAULT 约束

    ylbtech-SQL-W3School-高级:SQL DEFAULT 约束 1.返回顶部 1. SQL DEFAULT 约束 DEFAULT 约束用于向列中插入默认值. 如果没有规定其他的值,那么会 ...

  5. Constraint2:constraint

    一,Constraint 是表定义的一部分,用于实现数据完整性. Data Integrity 由三种类型的constraint实现: Entity Integrity:数据是唯一的.约束: prim ...

  6. Have You Ever Wondered About the Difference Between NOT NULL and DEFAULT?

    https://blog.jooq.org/2014/11/11/have-you-ever-wondered-about-the-difference-between-not-null-and-de ...

  7. SQL UNIQUE Constraint

    SQL UNIQUE Constraint The UNIQUE constraint uniquely identifies each record in a database table. The ...

  8. 【Flask】Column常用参数

    ### Column常用参数:1. primary_key:设置某个字段为主键.2. autoincrement:设置这个字段为自动增长的.3. default:设置某个字段的默认值.在发表时间这些字 ...

  9. SQL 约束

    先用设计器创建约束.再用代码创建约束.数据库约束是为了保证数据的完整性(正确性)而实现的一套机制见文件Employee.sql非空约束(选择复选框)主键约束(PK) primary key const ...

随机推荐

  1. kubernetes中使用NFS创建pv_pvc

    Persistent Volume(持久化卷)简称PV, 是一个K8S资源对象,我们可以单独创建一个PV, 它不和Pod直接发生关系, 而是通过 Persistent Volume Claim, 简称 ...

  2. 查看Windows端口及端口关闭方法(转)

    摘自:http://www.hackbase.com/tech/2011-05-17/63766.html 查看Windows端口及端口关闭方法 一.查看已开放的端口: 1.借助系统自带MS-DOS命 ...

  3. PHP-九个非常有用的功能[转]

    1. 函数的任意数目的参数你可能知道PHP允许你定义一个默认参数的函数.但你可能并不知道PHP还允许你定义一个完全任意的参数的函数下面是一个示例向你展示了默认参数的函数:// 两个默认参数的函数fun ...

  4. struts2 中请求转发与请求重定向方法

    本文转自:http://blog.csdn.net/a327736051/article/details/50240491 一.Chain Result:这个result调用另外的一个action,连 ...

  5. 网站跳转到cgi-sys/defaultwebpage.cgi的原因和解决方式

    cpanel遇到这种问题,看了这篇文章老鹰主机域名解析A记录教程–关于cgi-sys/defaultwebpage.cgi后,尝试后     首先ping 域名,结果如下     看到没有ping结果 ...

  6. RabbitMQ概念及环境搭建(三)RabbitMQ cluster

    测试环境:VMS00781 VMS00782 VMS00386 (centos5.8) 1.先在三台机器上分别安装RabbitMQ Server 2.读取其中一个节点的cookie,并复制到其他节点( ...

  7. RHEL7 -- 识别文件系统和设备

    逻辑卷依赖于设备映射程序(DM)内核驱动程序. 比如有个逻辑卷组rhel中有一个逻辑卷root,对应的设备为/dev/rhel/root.符号链接/dev/rhel/root指向/dev/dm-< ...

  8. Android studio 如何让包有层次显示

    Android studio中我新建的包在原来包名后面显示,而我想让包名能层次展示: 方法: 点击如图部分,在弹出框中 去掉 ”compact empty middle package“前面勾

  9. view变化监听器ViewTreeObserver介绍

      A view tree observer is used to register listeners that canbe notified of global changes in the vi ...

  10. if else和switch的效率

    switch和if-else相比,由于使用了Binary Tree算法,绝大部分情况下switch会快一点,除非是if-else的第一个条件就为true. 说实话  我也没有深入研究过这个问题的根源  ...