三.mysql表的完整性约束
mysql表的完整性约束
什么是约束
not null 不能为空的
unique 唯一 = 不能重复
primary key 主键 = 不能为空 且 不能重复
foreign key 外键约束
为什么要约束
是因为一个表中的数据要想完整规范,就必须对一些字段有基础的约束
一般情况下,我们都会根据程序的需求个特点对数据库进行约束
约束一 not null
1. create table t (id int not null,
sex enum('male','female') not null default 'male');
约束二 unique 唯一,允许多个字段为null
设置某一个字段的内容必须是唯一的
1.create table t3 (id int unique); # 你设置了唯一,就不能插入两个相同的内容 测试:除了null之外
2. create table t3 (id int not null unique); # 你设置了唯一+非空,就不能插入两个相同的内容,也不能插入NULL,就相当于设置了一个主键primary key
3.create table t3 (id int, name char(12), unique(id),unique(name));
两个或者多个字段的内容 = 联合唯一
4. create table t4 (pid int, pname char(12),ip char(15),port int,unique(ip,port)); #设置联合唯一 (ip+端口的联合唯一)

5.create table t4 (pid int, pname char(12), #设置非空且联合唯一
ip char(15) not null,
port int not null ,
unique(ip,port));
约束三 主键约束
主键 : 每一张表只能有一个主键 primary key = not null + unique 主键=非空+唯一
1. create table t5 (id int primary key); #设置主键
2.能设置多个主键么 ? 不能
create table t5 (id int primary key,name char(12) primary key);
3. 能不能设置多个非空 + 唯一 ? 能
create table t6 (id int not null unique,name char(12) not null unique);
4. create table t7 (pid int, pname char(12), #联合主键
ip char(15),
port int,
primary key(ip,port));
auto_increment 自增
步长:auto_increment_increment, 起始偏移量:auto_increment_offset
自增字段必须针对于int数据类型的且必须针对一个unique约束
1.create table t8 (id int unique auto_increment,name char(12));
列

2.create table t9 (id int primary key auto_increment,name char(12)); #也可以设置主键也可以自增
3.对于自增id来说,删除数据并不会影响自增,设置为自增,用户最好不要自己插入这个字段
例

约束四 外键
部门id 部门名称 部门办公室号
alter table department modify id int unique;
员工id name 年龄 工资 部门id(外键)
create table employee (id int,name char(12),age int,salary int,dep_id int,
foreign key(dep_id) references department(id));
例

dpt_id外键,关联父表(department主键id),同步更新,同步删除
create table employee2 (id int,name char(12),age int,salary int,dep_id int,
foreign key(dep_id) references department(id) on delete cascade on update cascade );
小结
约束 4个
1. not null # 不允许为空
# default # 设置默认值
2. unique # 唯一,不能约束null
# 联合唯一
# auto_increment # 自增
3. primary key # 主键 = not null + unique (同一张表不能有两个主键)
# 联合主键
4. foreign key # 本表中的字段关联另一张表中的"唯一"字段 ,本表中的字段是 外键,外表中的字段必须唯一/主键
唯一约束:
create table 表名 (
字段名1 字段类型(宽度) not null default 默认值,
字段名2 字段类型(宽度) not null unique, #即非空也有一个唯一
字段名3 字段类型(宽度) primary key, #设置一个主键
字段名4 int(宽度) unique auto_increment, #自增
) create table 表名 (
字段名1 字段类型(宽度) not null,
字段名2 字段类型(宽度) not null,
字段名3 字段类型(宽度),
字段名4 int(宽度),
unique(字段名2),
primary key(字段名3),
unique(字段名4) auto_increment,
)
联合主键:
create table 表名 (
字段名1 字段类型(宽度) not null,
字段名2 字段类型(宽度) not null,
字段名3 字段类型(宽度),
字段名4 int(宽度) auto increment,
unique(字段名1,字段名2),
primary key(字段名3,字段名4),
);
外键:
create table 表名(
字段名1 字段类型(宽度) not null,
字段名2 字段类型(宽度) not null,
外键名 字段类型(宽度),
foreign key (外建) references 外表(外表中的字段)
on delete cascade
on update cascade,
primary key(字段名1)
) 添加主键
alter table 表名 modify 字段名 类型(宽度) primary key;
三.mysql表的完整性约束的更多相关文章
- mysql表的完整性约束
概览 为了防止不符合规范的数据进入数据库,在用户对数据进行插入.修改.删除等操作时,DBMS自动按照一定的约束条件对数据进行监测, 使不符合规范的数据不能进入数据库,以确保数据库中存储的数据正确.有效 ...
- day--38 mysql表的完整性约束总结
表的简单查询参考:https://www.cnblogs.com/clschao/articles/9995531.html 一:单表的查询: 查询数据的本质:mysql会去本地的硬盘上面找到对应的文 ...
- SQL学习笔记三之MySQL表操作
阅读目录 一 存储引擎介绍 二 表介绍 三 创建表 四 查看表结构 五 数据类型 六 表完整性约束 七 修改表ALTER TABLE 八 复制表 九 删除表 一 存储引擎介绍 存储引擎即表类型,mys ...
- MySQL表完整性约束
=======MySQL表完整性约束====== 目录: 一.介绍 二.not null 与 default 三.unique 四.primary key 五.auto_increment 六.for ...
- Mysql数据库(三)Mysql表结构管理
一.MySQL数据类型 1.数字类型 (1)整数数据类型包括TINYINT/BIT/BOOL/SMALLINT/MEDIUMINT/INT/BIGINT (2)浮点数据类型包括FLOAT/DOUBLE ...
- Mysql(三):表操作
一 存储引擎介绍 存储引擎即表类型,mysql根据不同的表类型会有不同的处理机制 详见:http://www.cnblogs.com/6324TV/p/8481061.html 二 表介绍 表相当于文 ...
- MySQL(三) 数据库表的查询操作【重要】
序言 1.MySQL表操作(创建表,查询表结构,更改表字段等), 2.MySQL的数据类型(CHAR.VARCHAR.BLOB,等), 本节比较重要,对数据表数据进行查询操作,其中可能大家不熟悉的就对 ...
- mysql(三) 数据表的基本操作操作
mysql(三) 数据表的基本操作操作 创建表,曾删改查,主键,外键,基本数据类型. 1. 创建表 create table 表名( 列名 类型 是否可以为空, 列名 类型 是否可以为空 )ENGIN ...
- {MySQL的逻辑查询语句的执行顺序}一 SELECT语句关键字的定义顺序 二 SELECT语句关键字的执行顺序 三 准备表和数据 四 准备SQL逻辑查询测试语句 五 执行顺序分析
MySQL的逻辑查询语句的执行顺序 阅读目录 一 SELECT语句关键字的定义顺序 二 SELECT语句关键字的执行顺序 三 准备表和数据 四 准备SQL逻辑查询测试语句 五 执行顺序分析 一 SEL ...
随机推荐
- 性能测试day03_前端分析调优思路
刚刚看到有人支持我写的博客,表示还是比较感动的,发现热心的用户在我的博客留言说“一个系统每天有200万在线用户,问我怎么设计性能场景?”,其实这个问题呢就属于业务没理清,这个问题就像我问你,一个城市一 ...
- ArcGIS自定义工具箱-显示地图文档结构
ArcGIS自定义工具箱-显示地图文档结构 联系方式:谢老师,135-4855-4328,xiexiaokui#qq.com 目的:显示地图文档mxd的数据组织结构,数据框,图层,表 使用方法: 地图 ...
- 计算机系统的通信PPT版本
通信方式 并行传送:一次8位,需要保持各路信号同时到达,距离和速度受限制,成本较高 串行传送:一次1位,距离较远.速率更高,成本较低. {在单片机的通信中,使用的是串行传送,但是数据寄存器里面的是8位 ...
- layer弹窗和日期
这个插件用的最多的是,弹窗和日期
- BM递推
从别的大佬处看到的模板 #include<bits/stdc++.h> #define fi first #define se second #define INF 0x3f3f3f3f ...
- VueJs学习参考的例子
his is a vue+mint's demo ,for loler(PAD LOL) https://github.com/yuanman0109/vue2.0-Mint-lolbox An ...
- HDU 5988 Coding Contest(最小费用最大流变形)
Problem DescriptionA coding contest will be held in this university, in a huge playground. The whole ...
- linux后台运行命令:&和nohup
常用后台运行命令包含:&和nohup 一.& 使用 当在前台运行某个作业时,终端被该作业占据:可以在命令后面加上& 实现后台运行.例如:sh test.sh & 适合在 ...
- [leetcode]70. Climbing Stairs爬楼梯
You are climbing a stair case. It takes n steps to reach to the top. Each time you can either climb ...
- [leetcode]94. Binary Tree Inorder Traversal二叉树中序遍历
Given a binary tree, return the inorder traversal of its nodes' values. Example: Input: [1,null,2,3] ...