drop有default constraint的column
有时候我们在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的更多相关文章
- SQL DEFAULT 约束
DEFAULT 约束用于向列中插入默认值. 如果没有规定其他的值,那么会将默认值添加到所有的新记录. 下面的 SQL 在 "Persons" 表创建时为 "City&qu ...
- 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 ...
- 数据定义: CREATE、DROP、ALTER
CREATE DATABASE 句法 CREATE DATABASE [IF NOT EXISTS] db_name 数据库.表.索引.列和别名 中被给出. 如果数据库已经存在,并且你没有指定 IF ...
- SQL-W3School-高级:SQL DEFAULT 约束
ylbtech-SQL-W3School-高级:SQL DEFAULT 约束 1.返回顶部 1. SQL DEFAULT 约束 DEFAULT 约束用于向列中插入默认值. 如果没有规定其他的值,那么会 ...
- Constraint2:constraint
一,Constraint 是表定义的一部分,用于实现数据完整性. Data Integrity 由三种类型的constraint实现: Entity Integrity:数据是唯一的.约束: prim ...
- 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 ...
- SQL UNIQUE Constraint
SQL UNIQUE Constraint The UNIQUE constraint uniquely identifies each record in a database table. The ...
- 【Flask】Column常用参数
### Column常用参数:1. primary_key:设置某个字段为主键.2. autoincrement:设置这个字段为自动增长的.3. default:设置某个字段的默认值.在发表时间这些字 ...
- SQL 约束
先用设计器创建约束.再用代码创建约束.数据库约束是为了保证数据的完整性(正确性)而实现的一套机制见文件Employee.sql非空约束(选择复选框)主键约束(PK) primary key const ...
随机推荐
- 基于Arch的live系统
今天在linuxsir的archlinux分区闲逛,看到了carbonjiao的帖子 http://bbs.linuxeye.cn/thread-652-1-1.html 同时还关注他的帖子:1.Ar ...
- java获取某个范围内的一个随机数
一.取模操作 public static void main(String[] args){ for (int i = 1; i <= 20; i++){ int j = i % 11; Sys ...
- PHP5.3新特性
1.首先对之前滥用的语法进行了规范 众所周知PHP在语言开发过程中有一个很好的容错性,导致在数组或全局变量中包含字符串不使用引号是可以不报错的,很多业余的开发者因为懒惰而产生的安全问题十分严重,之所以 ...
- Oracle截取字符串函数和查找字符串函数,连接运算符||
参考资料:Oracle截取字符串和查找字符串 oracle自定义函数学习和连接运算符(||) oracle 截取字符(substr),检索字符位置(instr) case when then else ...
- WordPress网站搬家全过程 亲身体验WordPress搬家,总结几点
需要移动的文件主要是网站文件和数据库文件,如果是简单的wordpress 操作就是:备份网站文件,导出数据库文件,上传网站文件,导入数据库文件,移动网站文件,修改wordpress的wp-conf ...
- Python练习笔记——字符串反转
请输入一段字符串,不利用反转函数,编写一段代码,将其反转. def list_reverse(a): list_long = len(a) list_long_half = list_long // ...
- Python学习笔记020——数据库基本操作
本数据库的操作是Linux虚拟机平台下进行的 1 启动和链接MySQL服务 1.1 服务端 (1)查看服务状态 sudo /etc/init.d/mysql stauts (2)启动服务端 sudo ...
- Shlwapi.h头文件的使用
// TestShlwAPI.cpp : 定义控制台应用程序的入口点.// #include "stdafx.h"#include <Shlwapi.h>#pragma ...
- Intelligence System (hdu 3072 强联通缩点+贪心)
Intelligence System Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Othe ...
- django web 笔记
安装 django pip install django 创建虚拟环境 python -m venv testenvironment 进入虚拟环境: testenvironment\Script ...