最近公司项目要升级新版本,涉及到数据库升级中各种约束。亦是整理出如下脚本方便以后查询。

--删除全文索引
DECLARE c0 cursor for
SELECT
'DROP FULLTEXT INDEX ON '+tab.name+';'
FROM
sys.fulltext_indexes idx
JOIN sys.tables tab
ON (idx.object_id = tab.object_id);
open c0
declare @c0 varchar(8000)
fetch next from c0 into @c0
while(@@fetch_status=0)
begin
exec(@c0)
fetch next from c0 into @c0
end
close c0
deallocate c0

Go

--删除约束和唯一约束
DECLARE c1 cursor for
select 'alter table ['+ object_name(parent_obj) + '] drop constraint ['+name+']; '
from sysobjects
where xtype = 'D' OR xtype = 'UQ'
open c1
declare @c1 varchar(8000)
fetch next from c1 into @c1
while(@@fetch_status=0)
begin
exec(@c1)
fetch next from c1 into @c1
end
close c1
deallocate c1

Go

--删除外键
DECLARE c2 cursor for
select 'alter table ['+ object_name(parent_obj) + '] drop constraint ['+name+']; '
from sysobjects
where xtype = 'F'
open c2
declare @c2 varchar(8000)
fetch next from c2 into @c2
while(@@fetch_status=0)
begin
exec(@c2)
fetch next from c2 into @c2
end
close c2
deallocate c2

Go

--删除主键
DECLARE c3 cursor for
select 'alter table ['+ object_name(parent_obj) + '] drop constraint ['+name+']; '
from sysobjects
where xtype = 'PK'
open c3
declare @c3 varchar(8000)
fetch next from c3 into @c3
while(@@fetch_status=0)
begin
exec(@c3)
fetch next from c3 into @c3
end
close c3
deallocate c3

Go

--删除索引
DECLARE c4 cursor for
SELECT
'DROP INDEX '+ idx.name +' ON '+tab.name+';'
FROM
sys.indexes idx
JOIN sys.index_columns idxCol
ON (idx.object_id = idxCol.object_id
AND idx.index_id = idxCol.index_id
AND idx.is_primary_key <> 1)
JOIN sys.tables tab
ON (idx.object_id = tab.object_id)
JOIN sys.columns col
ON (idx.object_id = col.object_id
AND idxCol.column_id = col.column_id);
open c4
declare @c4 varchar(8000)
fetch next from c4 into @c4
while(@@fetch_status=0)
begin
exec(@c4)
fetch next from c4 into @c4
end
close c4
deallocate c4

Go

sql server 删除主键、外键、索引、约束的脚本的更多相关文章

  1. SQL Server修改主、外键和约束

    0.创建表 create table Users ( Id int, Name ), Phone ), Email ), Role_Id uniqueidentifier ) go create ta ...

  2. sql server删除主键约束所想到的

    从网上找到了下面一段代码: declare @Pk varchar(100);select @Pk=Name from sysobjects where Parent_Obj=OBJECT_ID('表 ...

  3. SQL语句删除和添加外键、主键的方法

    --删除外键 语法:alter table 表名 drop constraint 外键约束名 如: alter table Stu_PkFk_Sc drop constraint FK_s alter ...

  4. 【转】SQL语句删除和添加外键、主键

    --删除外键 语法:alter table 表名 drop constraint 外键约束名 如: alter table Stu_PkFk_Sc drop constraint FK_s alter ...

  5. sql数据库删除表的外键约束(INSERT 语句与 FOREIGN KEY 约束"XXX"冲突。该冲突发生于数据库"XXX",表"XXX", column 'XXX)

    使用如下SQL语句查询出表中外键约束名称: 1 select name 2 from sys.foreign_key_columns f join sys.objects o on f.constra ...

  6. SQL Server数据库学习笔记-外键

    关于主键的话大家很好理解,主键的主要作用就是用来标识一条数据是唯一的,也就是保证数据的实体完整性.防止数据重复.但是外键的话就有许多的疑问了,那外键是咋回事儿呢? 1. 外键的定义: 外键(FK)是用 ...

  7. sql server 删除表字段和字段的约束

    删除数据库表中的字段时,使用了 :alter table 表名 drop column 列名 服务器返回的错误为:Server: Msg 5074, Level 16, State 1, Line 1 ...

  8. Sql Server 主键 外键约束

    主键约束 表通常具有包含唯一标识表中每一行的值的一列或一组列. 这样的一列或多列称为表的主键 (PK),用于强制表的实体完整性. 由于主键约束可保证数据的唯一性,因此经常对标识列定义这种约束. 如果为 ...

  9. 【SQL Server DBA】维护语句:删除并创建外键约束、获取建表语句

    原文:[SQL Server DBA]维护语句:删除并创建外键约束.获取建表语句 1.删除外键约束,建立外键约束 先建立3个表: /* drop table tb drop table tb_b dr ...

随机推荐

  1. PHP-Open Flash Chart报表生成

    下载: http://www.cnblogs.com/huangcong/archive/2013/01/27/2878650.html 安装: 解压ZIP包, 将open-flash-chart.s ...

  2. redis安装配置文件配置

    环境: 虚拟机redhat5.5安装redis4.0.2 虚拟机IP:192.168.60.130 reids端口:6379 安装步骤不详述了(可参考: 亲密接触Redis-第一天), 大致步骤如下: ...

  3. 给maven仓库添加镜像

    用maven下载好慢啊,快试试阿里云镜像吧!修改$MAVEN_HOME/conf/settings.xml <!-- 阿里云仓库 --> <mirror> <id> ...

  4. "Only the original thread that created a view hierarchy can touch its views.” 解决方法

    这个主要总是,开启的线程和 UI 线程(主线程)不是同一个线程.可以Runnable方式避免,如下例所示就可以解决这个问题了. public static void updateText(Activi ...

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

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

  6. python添加tab键自动补全功能

    默认python是没有tab键补全功能的: >>> import tab Traceback (most recent call last): File "<stdi ...

  7. 关于centos7中使用rpm方式安装mysql5.7版本后无法使用root登录的问题

    最近在centos7中通过rpm方式安装了最新版本的mysql-server 5.7 (mysql57-community-release-el7-7.noarch.rpm) ,发现安装成功后无法使用 ...

  8. dataframe 合并(append, merge, concat)

    https://www.cnblogs.com/guxh/p/9451532.html https://blog.csdn.net/zutsoft/article/details/51498026 写 ...

  9. scrapy 的一个例子

    1.目标: scrapy 是一个爬虫构架,现用一个简单的例子来讲解,scrapy 的使用步骤 2.创建一个scrapy的项目: 创建一个叫firstSpider的项目,命令如下: scrapy sta ...

  10. 微服务架构的进程间通信(IPC)

    先抛出几个问题: 微服务架构的交互模式有哪些? 微服务常用的进程间通信技术有哪些? 如何处理部分请求失败? API的定义需要注意的事项有哪些 微服务的通信机制与SOA的通信机制之间的关系与区别 微服务 ...