三.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 ...
随机推荐
- 关于javabean
[javabean定义]定义:JavaBeans是Java语言中可以重复使用的软件组件,实质上是一种特殊的Java类.特点是可序列化,提供无参构造器,提供getter方法和setter方法访问对象的属 ...
- SecureCR 改变背景色和文字颜色
1.打开SecureCR链接Linux服务器,Options->Session Options->Emulation->Terminal 选择Linux (相应的服务器系统)ANSI ...
- 文件上传:swfupload.js、blueimp-file-upload
一.swfupload 1.下载swfupload http://code.google.com/p/swfupload/ 2. 3.API http://www.cnblogs.com/henw/ ...
- 图释SQL的Join
对于SQL的Join,在学习起来可能是比较乱的.我们知道,SQL的Join语法有很多inner的,有outer的,有left的,有时候,对于Select出来的结果集是什么样子有点不是很清楚.Codin ...
- 20175314 《Java程序设计》第六周学习总结
20175314 <Java程序设计>第六周学习总结 教材学习内容总结 第七章:内部类与异常类 内部类:内部类就是在一个类中再定义一个类,这个在类中定义的类就叫做内部类,而包含内部类的类叫 ...
- Mac 安装、卸载JDK 1.6
卸载 输入 sudo rm -fr /Library/Internet\ Plug-Ins/JavaAppletPlugin.plugin sudo rm -fr /Library/Preferenc ...
- java基础 ----- 选择结构
--------- 流程控制 ------ 流程图 ------ 基本的 if 选择结构 import java.util.Scanner; public class GetPr ...
- 199. Binary Tree Right Side View 从右侧看的节点数
[抄题]: Given a binary tree, imagine yourself standing on the right side of it, return the values of t ...
- 791. Custom Sort String字符串保持字母一样,位置可以变
[抄题]: S and T are strings composed of lowercase letters. In S, no letter occurs more than once. S wa ...
- mybatis pagehelper分页插件使用
使用过mybatis的人都知道,mybatis本身就很小且简单,sql写在xml里,统一管理和优化.缺点当然也有,比如我们使用过程中,要使用到分页,如果用最原始的方式的话,1.查询分页数据,2.获取分 ...