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 ...
随机推荐
- 【laravel5.4】PHP5.6+ 调用命名空间下类方法、属性和对象
1.调用命名空间的类方法,对象.属性 类对象:\App\User(); 类方法:\App\User::find($this->user_id) //查询构造器方法,将$this->user ...
- PHP中的一些安全配置
PHP中的配置至关重要,包含php.ini的配置,还有系统权限的配置,一下是我总结的一些配置 一.PHP的模块 ./configure \ --with-libdir=lib64 \ --prefix ...
- write()和prinln()的区别?
输出数字不同: write()输出数字转换为字符,println原样输出. 输出null不同: write()输出引用类型的时候调用的toString转换为String数据,因此如果对象为null那么 ...
- Servlet线程安全性
问题:使用以下的代码演示servlet的线程安全问题? public class MultiThreadQuestion extends HttpServlet { public int count ...
- PAT 1087 All Roads Lead to Rome
PAT 1087 All Roads Lead to Rome 题目: Indeed there are many different tourist routes from our city to ...
- 百度定位SDK实现获取当前经纬度及位置
使用Android自带的LocationManager和Location获取位置的时候,经常会有获取的location为null的情况,并且操作起来也不是很方便,在这个Demo里我使用了百度地图API ...
- iOS - App 间的通信方式
1.前言 iOS 系统是相对封闭的系统,App 各自在各自的沙盒(sandbox)中运行,每个 App 都只能读取 iPhone 上 iOS 系统为该应用程序程序创建的文件夹 AppData 下的内容 ...
- 使用Xcode、Android Studio将项目链接到Git
一.使用Android Studio创建本地git仓库: 1.检查本地git环境:在Android Studio中setting-->Version Control 点击Test按钮,提示suc ...
- oc字符串的用法
#import <UIKit/UIKit.h> #import "AppDelegate.h" int main(int argc, char * argv[]) { ...
- Linux中断 - 驱动申请中断API
一.前言 本文主要的议题是作为一个普通的驱动工程师,在撰写自己负责的驱动的时候,如何向Linux Kernel中的中断子系统注册中断处理函数?为了理解注册中断的接口,必须了解一些中断线程化(threa ...