当前位置:网站首页>SQL学习笔记(03)——数据约束关系
SQL学习笔记(03)——数据约束关系
2022-07-01 09:21:00 【雨轩GodLike】
一、实体完整性
1.主键约束(primary key)
- 唯一,不重复,不能为空 ,一个表中有且只有一个主键约束
- 主键中的字段可以设置
自动增长(auto_increment)
1-1.创建表的同时创建主键约束
格式一:
create table 表名(
列名1 数据类型 primary key,
列名2 数据类型
);
create table stu(sno int(10) primary key auto_increment);
格式二:主键约束名字的写法:PK_列名
create table 表名(
列名1 数据类型,
列名2 数据类型,
constraint 主键约束的名字
);
create table stu(
sno int(10),
sname varchar(10),
constraint PK_name primary key(sname)
);
格式三:
create table 表名(
列名1 数据类型,
列名2 数据类型,
primary key (列名1)
);
create table stu(sno int(10),primary key(sno));
1-2.针对已经存在的表,添加主键约束
格式一:
alter table 表名 modity 列名 数据类型 primary key;
格式二:
alter table 表名 add primary key(列名);
格式三:
alter table 表名 add constraint 主键约束的名字 primary key(列名);
2.唯一约束(unique)
- 1.不允许有
重复的值,保证数据的唯一性。 - 2.可以有
空值 - 3.在一个表中,可以有
多个唯一约束 - 4.默认情况下,唯一约束的名字和列名保持—致
- 5.添加唯一约束的列,系统也会默认给这个列添加一个
唯一索引 索引:等同于书本的目录,将来能够加快数据的查询速度。
2-1.创建表的同时创建唯—约束(UN_列名)
create table表名(
列名1 数据类型 unique,
列名2 数据类型 unique,
列名3 数据类型
);
create table stu(sno int(10) unique,sname varchar(20) unique,ssex varchar(10));
2-2.针对已经存在的表,添加唯一约束
alter table 表名 add unique(列名1[,列名2]);
2-3.删除唯一约束
alter table 表名 drop index 唯一约束的名字;
二、域完整性
1.默认约束(default)
- 保证在表中不会输入
无效的数据 - 当默认约束来修饰某个列的时候,修饰的列即使不写数据也会默认一个值的
1-1.创建表的同时创建默认约束
create table 表名(
列名1 数据类型 default '字符串类型或者日期类型的默认值',
列名2 数据类型 default 数值,
列名3 数据类型
);
create table student(ssex varchar(10) default '男',record int(10) default '70');
1-2.针对已经存在的约束,添加默认约束
alter table 表名 modify 列名 数据类型 default '数值';
alter table student modify record int(10) default '80';
1-3.删除默认约束
alter table 表名 drop 列名 数据类型;
alter table student drop ssex varchar(10);
2.非空约束(not null) 当前列必须有值
2-1.创建表的同时创建非空约束
create table 表名(
列名1 数据类型 not null,
列名2 数据类型
);
create table student(sno int(10),sname varchar(20) not null);
2-2.针对已经存在的约束,添加非空约束
alter table 表名 modify 列名 数据类型 not null;
alter table student modify ssex varchar(10) not null;
2-3.删除非空约束
alter table 表名 drop 列名 数据类型;
alter table student drop ssex varchar(10);
三、参照完整性
外键约束(foreign key)
- 外键约束是建立在
从表; - 当从表的列参照主表的列,列名可以不一样,但是列里面的数据类型和内容要保持一致;
- 从表引用主表的列,要求主表的列必须有主键约束或者唯一约束;
- 当主表的数据,被从表引用,主表的数据是不能进行删除;
- 当从表中的数据想做操作的时候,先询问主表的意见,主表有的,允许从表操作,主表没有,拒绝从表操作。
创建表的同时创建外键约束
create table 表名(
列名1 数据类型,
列名2 数据类型,
constraint 外键约束的名字 foreign key(从表的列名1) references 主表表名(主表的列名1)
);
针对已经存在的表,添加外键约束
alter table 从表表名 add constraint 外键约束的名字 foreign key(从表列名1) references 主表表名(主表列名);
删除外键约束
alter table 表名 drop foreign key 外键约束的名字;
边栏推荐
- tensorrt yolov5_ trt. Py comments
- js函数arguments对象
- PR training notes
- Exception handling of classes in C #
- Principles of Microcomputer - internal and external structure of microprocessor
- nacos简易实现负载均衡
- Niuke monthly race 22- collect pieces of paper
- Daily office consumables management solution
- Log4j 日志框架
- Class loading
猜你喜欢

小鸟识别APP

【pytorch】softmax函数

2.4 activation function

【pytorch】nn.CrossEntropyLoss() 与 nn.NLLLoss()

Redis -- lattice connects to redis cluster
![[pytorch] 2.4 convolution function nn conv2d](/img/eb/382a00af5f88d5954f10ea76343d6e.png)
[pytorch] 2.4 convolution function nn conv2d

Pain points and solutions of equipment management in large factories

ESP8266 FreeRTOS开发环境搭建

How to solve the problem of fixed assets management and inventory?

钓鱼识别app
随机推荐
Solution of EPS image blur by latex insertion
Simple load balancing with Nacos
LeetCode 344. Reverse string
How to manage fixed assets well? Easy to point and move to provide intelligent solutions
JS scope chain and closure
【ESP 保姆级教程】疯狂毕设篇 —— 案例:基于阿里云、小程序、Arduino的WS2812灯控系统
js 使用toString 区分Object、Array
【pytorch】nn.CrossEntropyLoss() 与 nn.NLLLoss()
【pytorch】nn. Crossentropyloss() and nn NLLLoss()
An overview of the design of royalties and service fees of mainstream NFT market platforms
Yidian Yidong helps enterprises to efficiently manage equipment and improve equipment utilization
nacos简易实现负载均衡
【电赛训练】红外光通信装置 2013年电赛真题
Day06 branch structure and cycle (III)
类加载
Bird recognition app
[pytorch] 2.4 convolution function nn conv2d
Log4j log framework
樹結構---二叉樹2非遞歸遍曆
美团2022年机试