当前位置:网站首页>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 外键约束的名字;
边栏推荐
- js变量提升(hoisting)
- Log4j log framework
- Analysis and solution of JS this loss
- Redis source code learning (29), compressed list learning, ziplist C (II)
- Can diffusion models be regarded as an autoencoder?
- Exception handling of classes in C #
- phpexcel 里 获取某一列的列表 获取某一列的字母
- 【ESP 保姆级教程 预告】疯狂Node.js服务器篇 ——案例:ESP8266 + DS18B20温度传感器 +NodeJs本地服务+ MySQL数据库
- How to solve the problem of fixed assets management and inventory?
- 樹結構---二叉樹2非遞歸遍曆
猜你喜欢

Understanding and implementation of AVL tree

I use flask to write the website "one"

nacos簡易實現負載均衡

Simple load balancing with Nacos

Imitation of Baidu search results top navigation bar effect

MySQL optimization

Redis -- lattice connects to redis cluster

树结构---二叉树2非递归遍历

How to manage fixed assets efficiently in one stop?

Pain points and solutions of equipment management in large factories
随机推荐
树结构---二叉树1
Principle and application of single chip microcomputer timer, serial communication and interrupt system
[interview brush 101] linked list
2.2 【pytorch】torchvision.transforms
Structure de l'arbre - - - arbre binaire 2 traversée non récursive
Phishing identification app
JS prototype inheritance can only inherit instances, not constructors
序列化、监听、自定义注解
2022.02.15_ Daily question leetcode six hundred and ninety
MySQL optimization
[ESP nanny level tutorial] crazy completion chapter - Case: temperature and humidity monitoring system based on Alibaba cloud, applet and Arduino
手指点击屏幕就模拟进入F11进入全屏
Ranking list of domestic databases in February, 2022: oceanbase regained the "three consecutive increases", and gaussdb is expected to achieve the largest increase this month
记一次redis超时
laravel postman 提交表单出现419错误。2020年7月6日记。
Serialization, listening, custom annotation
Mise en œuvre simple de l'équilibrage de la charge par nacos
Flink interview questions
How to launch circle of friends marketing and wechat group activities
Nacos service configuration and persistence configuration