首页 > 数据库 >Sqlserver中所有约束的类型,创建、修改与删除

Sqlserver中所有约束的类型,创建、修改与删除

时间:2024-03-14 11:46:31浏览次数:20  
标签:dbo 删除 创建 Sqlserver 约束 Key table null StuID

     1、 https://blog.51cto.com/u_15738244/5535432 

      2、 https://blog.csdn.net/realoser/article/details/121496126

数据库所有的约束:

​ ​一、主键约束(primary key)​​
​ ​二、外键约束(foreign key)​​
​ ​三、检查约束(check)​​
​ ​四、非空约束(not null)​​
​ ​五、唯一性约束(unique)​​
​ ​六、默认值约束(default)

创建、修改与删除:

1. 非空约束 Not Null

create table dbo.not_null
(
ID int not null,
Name varchar(10) not null
)

2. 默认约束 default

① 创建表 default 约束

create table dbo.default_table -- 创建班级信息表
(
ClassId int not null, -- 班级号不为空
Dept varchar(10) default('软工'), -- 所在系部默认为“软工”
ClassRenshu int default(0) -- 班级人数默认为 0
)


② 修改表 default 约束

alter table dbo.default_table
add constraint df_ClassId default(1001) for ClassId -- constraint 约束关键字 给班级号增加默认值 1001 

 

3. 唯一约束 unique
字段值取值唯一,允许 NULL 值。主键不允许 NULL 值。
① 创建表 unique 约束

create table dbo.unique_table
(
StuID int not null,
sex varchar(10),
StuCard int unique
)

  

② 修改表 unique 约束

alter table dbo.unique_table
add constraint uq_StuID unique(StuID) -- 给 StuID 定义唯一约束

  

4. 检查约束 check
check 约束检查输入的值是否在规定的范围内
① 创建表 check 约束

create table dbo.check_table
(
StuID int not null,
Score numeric check(Score>=0 and Score<=100), -- numeric 数字型,int 整数型 |check(Score between 0 and 100)
Setdate date
)

 

② 修改表 check 约束

alter table dbo.check_table
add constraint ch_Setdate check(Setdate between '2001-1-1' and '2002-1-1')
-- add constraint ch_Setdate check(Setdate>='2001-1-1' and Setdate<='2002-1-1')

  

5. 主键约束 Primary Key
① 创建表 Primary Key 约束

create table dbo.Primary_Key
(
StuID int primary key not null,
StuName varchar(10)
)

 

复合主键

create table dbo.Primary_Key2
(
StuID int not null,
CourseID int not null,
Score numeric not null,
constraint pk_StuID_CourseID primary key(StuID,CourseID)
)

 

② 修改表 Primary Key 约束

alter table dbo.Primary_Key
add constraint pk_StuID primary key(StuID)

 

6. 外键约束 Foreign Key
① 创建表 Foreign Key 约束

create table Foreign_Key
(
StuID int primary key not null,
StuName varchar(10),
Stu_No varchar(12) foreign key references my.Tb_Stu_Info(Stu_No) -- references 连接到某个表的某个主键
)

  

② 修改表 Foreign Key 约束

alter table Foreign_Key
add constraint fk_Stuno foreign key(Stu_No) references my.Tb_Stu_Info(Stu_No)

  

7. 删除约束

alter table Foreign_Key
drop constraint fk_Stuno

  

标签:dbo,删除,创建,Sqlserver,约束,Key,table,null,StuID
From: https://www.cnblogs.com/Dongmy/p/18072507

相关文章

  • 创建LVM挂载到指定目录
    一、创建目录mkdir/data 二、环境信息将sdb做成lvm挂载到/data目录 三、磁盘分区并做成lvm格式磁盘分区[root@CMSgeL~]#fdisk/dev/sdbWelcometofdisk(util-linux2.23.2).Changeswillremaininmemoryonly,untilyoudecidetowritethem.Becareful......
  • Redis高级-删除策略、主从复制、哨兵模式
    1.删除策略1.过期数据redis中的数据特征Redis是一种内存级数据库,所有数据均存放在内存中,内存中的数据可以通过TTL指令获取其状态TTL返回的值有三种情况:正数,-1,-2正数:代表该数据在内存中还能存活的时间-1:永久有效的数据-2 :已经过期的数据或被删除的数据或未定义的数据......
  • 如何在Windows 10中恢复已删除/丢失的驱动程序?这里有几种方法
    如果你意外删除了Windows10中的驱动程序,可以尝试3种解决方案来恢复删除的驱动程序。回滚驱动程序1、按Windows+X,然后选择设备管理器以在Windows10中打开设备管理器。2、展开设备类别,并选择没有驱动程序的目标设备。3、双击目标设备以打开其“属性”窗口。4、单击驱动程......
  • 备战蓝桥杯Day25 - 二叉搜索树查询和删除操作
    一、查询递归查询寻找的值比根节点大,遍历右子树;寻找的值比根节点小,遍历左子树。defqurey(self,node,val):ifnotnode:#没有节点,返回空returnNoneifnode.data<val:returnself.qurey(node.rchild,val)......
  • Linux线程池的创建(超详细解析)
    线程池:若干个线程组合在一起形成线程池;为什么需要线程池:多线程版本服务器一个客户端就需要创建一个线程,如果客户端太多,明显不太合适;创建思路:我们需要一个线程池结构体,然后这个结构体里面包含任务池,这个线程池结构体是全局变量,需要使用互斥锁,当子线程执行回调函数时,把该线程......
  • 解决表格文件上传无法删除临时文件的问题Failed to perform cleanup of multipart ite
    java.io.UncheckedIOException:CannotdeleteC:\Users\hasee\AppData\Local\Temp\tomcat如图所示,刚开始以为是apifox没删除的问题,换了之后依旧这样 尝试方案1-失败 方法二-失败 方法三-成功 原文链接报错:StandardServletMultipartResolver:Failedtoperform......
  • vim没有clipboard,没法复制到系统剪切板,通过xclip将复制、删除的内容放到系统剪切板
    解决方法:在/etc/vim/vimrc或者~/.vimrc中添加下面的命令auTextYankPost*exesystem("xclip-selectionclipboard",@")一开始遇到这个问题,网上的解决方案都是安装一个超大的vim,这很不优雅,我喜欢小而高效的解决方案。后来看到一个使用xclip的方案,这个方案用的是按键映射......
  • MySQL 清空数据表、添加新数据、删除数据表
    1.清空数据表里面的数据,同步重置主键truncatetableuser;2.添加数据的两种常用方法replaceINTOuser(name,phone,sex,...)VALUESINSERTINTOuser(name,phone,sex,...)VALUESreplaceINTO和INSERTINTO,区别在于INSERTINTO是在添加数据的时候,如果遇到重复的数据,则......
  • C# ContextMenuStrip创建主菜单与子菜单
    ContextMenuStrip创建主菜单与子菜单usingSystem;usingSystem.Windows.Forms;usingSystem.ComponentModel;usingSystem.Data;namespaceMenuStripExample{publicpartialclassForm1:Form{publicForm1(){InitializeCompo......
  • 380. O(1) 时间插入、删除和获取随机元素.18071112
    380.O(1)时间插入、删除和获取随机元素实现RandomizedSet类:RandomizedSet()初始化RandomizedSet对象boolinsert(intval)当元素val不存在时,向集合中插入该项,并返回true;否则,返回false。boolremove(intval)当元素val存在时,从集合中移除该项,并返回true;否则......