SQL Server 数据库添加主键,唯一键,外键约束脚本
-- 声明使用数据库
use 数据库;
go
-- 添加主键(primary key)约束
-- 基本语法
-- 判断主键约束是否存在,如果存在则删除,不存在则添加
if exists(select * from sysobjects where name=主键名)
alter table 表明 drop constraint 主键名;
go
alter table 表明 add constraint 主键名 primary key(列名)
use SapDBT;
go
-- 实际例子
if exists(select * from sysobjects where name='PK_student_id')
alter table [dbo].[Student] drop constraint PK_student_id;
go
alter table [dbo].[Student] add constraint PK_student_id primary key([ID])
-- 添加唯一键(UNIQUE)约束
-- 基本语法
-- 判断唯一键约束是否存在,如果存在则删除,不存在则添加
if exists(select * from sysobjects where name=约束名)
alter table 表明 drop constraint 约束名;
go
alter table 表明 add constraint 约束名 primary key(列名)
-- 实际例子
if exists(select * from sysobjects where name='UQ_student_name')
alter table [dbo].[Student] drop constraint UQ_student_name;
go
alter table [dbo].[Student] add constraint UQ_student_name unique([Name])
-- 添加外键(foreign key)约束
-- 基本语法
-- 判断外键约束是否存在,如果存在则删除,不存在则添加
if exists(select * from sysobjects where name=约束名)
alter table 表明 drop constraint 约束名;
go
alter table 外键表 add constraint 约束名 foreign key(外键列) references 主键表
-- 实际例子
if exists(select * from sysobjects where name='FK_Score_Student')
alter table [dbo].[Score] drop constraint FK_Score_Student;
go
-- [dbo].[Score]中的外键[StudentId]是[dbo].[Student]的主键
alter table [dbo].[Score] add constraint FK_Score_Student foreign key([StudentId]) references [dbo].[Student]
SQL Server 数据库添加主键,唯一键,外键约束脚本的更多相关文章
- sql server数据库备份单个表的结构和数据生成脚本【转】
1.使用场景:sql server数据库备份单个表的结构和数据,在我们要修改正式系统的数据的一天或者多条某些数据时候,要执行update语句操作,安全稳健考虑,最好先做好所修改的表的结构和数据备份! ...
- 何查询SQL Server数据库没有主键的表并增加主键
SQL Server数据库中,如果一个表没有主键,我们该如何查询呢?本文我们主要就介绍了如何查询数据库中没有主键的表名并为其增加主键的方法,希望能够对您有所帮助. 该功能的实现代码如下: declar ...
- SQL Server 数据库基于备份文件的【一键还原】
1. 备份与还原的基础说明 我们知道在DBA的日常工作中,SQL Server 数据库的恢复请求偶有发生,可能是用作数据的追踪,可也可能能是数据库的灾难恢复. 数据库常用的备份命令如下: ----完整 ...
- sql server数据库备份单个表的结构和数据生成脚本
1.使用场景:sql server数据库备份单个表的结构和数据,在我们要修改正式系统的数据的一天或者多条某些数据时候,要执行update语句操作,安全稳健考虑,最好先做好所修改的表的结构和数据备份! ...
- sql server drop talbe 自动删除关联的外键 ,权限体系(一)
if object_id('Proc_DropTableWithFK') is not null begin drop proc dbo.Proc_DropTableWithFK end GO ) a ...
- [转载]C#中使用ADO.NET连接SQL Server数据库,自动增长字段用作主键,处理事务时的基本方法
问题描述: 假设在数据库中存在以下两张数据表: User表,存放用户的基本信息,基本结构如下所示: 类型 说明 ID_User int 自动增长字段,用作该表的主键 UserName varcha ...
- sql server drop talbe 自动删除关联的外键 ,权限体系(二)
alter table dbo.Sys_PowerTeamForUser add constraint FK_Sys_User_Sys_PowerTeamForUser foreign key (Sy ...
- sql server数据库添加记录
转自:http://jingyan.baidu.com/article/f25ef254449a9a482c1b8293.html
- SQL Server数据库---》基础
SQL Server:只是操作数据库的一个工具(这种工具,只是提供一个界面化的方式让用户方便操作数据库) 开启服务:点击:我的电脑(计算机)--管理--服务和应用程序--服务--开启SQL Serve ...
随机推荐
- c学习 - 第四章:顺序程序设计
4.4 字符数据的输入输出 putchar:函数的作用是想终端输出一个字符 putchar(c) getchar:函数的作用是从输入设备获取一个字符 getchar(c) 4.5 格式输入与输出 pr ...
- 视频框架 Vitamio使用
转自http://blog.csdn.net/u010181592/article/category/5893483 1.在https://github.com/yixia/VitamioBundle ...
- Gradle入门及SpringBoot项目构建
https://blog.csdn.net/qq_27520051/article/details/90384483 一.介绍 Gradle 是一种构建工具,它抛弃了基于XML的构建脚本,取而代之的是 ...
- Java-如何合理的设置线程池大小
想要合理配置线程池线程数的大小,需要分析任务的类型,任务类型不同,线程池大小配置也不同. 配置线程池的大小可根据以下几个维度进行分析来配置合理的线程数: 任务性质可分为:CPU密集型任务,IO密集型任 ...
- Nginx配置访问黑名单
目录 一.简介 二.脚本 一.简介 有的时候需要将某些大访问量的ip加入到黑名单中 二.脚本 1.脚本内容为,检测本地并发访问超过15并且是ip地址,则加入nginx黑名单中.其中的53a是deny行 ...
- yum的epel源
目录 一.centos7的源 一.centos7的源 以下一个不行,可以试试另一个 rpm -ivh https://mirrors.aliyun.com/epel/epel-release-late ...
- 第46篇-signature_handler与result_handler
在之前介绍为native方法设置解释执行的入口时介绍过,当Method::native_function为空时会调用InterpreterRuntime::prepare_native_call()函 ...
- 听听文档(视频)-Power Pivot
打开微信扫描二维码
- 解决android studio no debuggable process
这个问题可能是由多种因素造成的. 一.可能是buildtypes配置或选择错误,在对应module的build.gradle中确认如下配置 debug { debuggable true//一定要配置 ...
- VMware 打开虚拟机出现另一个程序已锁定文件的一部分,进程无法访问
打开虚拟机出现 另一个程序已锁定文件的一部分,进程无法访问 打不开磁盘"D:\Virtual Machines\CentOS 7 64 位\CentOS 7 64 位.vmdk"或 ...