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表的完整性约束的更多相关文章

  1. mysql表的完整性约束

    概览 为了防止不符合规范的数据进入数据库,在用户对数据进行插入.修改.删除等操作时,DBMS自动按照一定的约束条件对数据进行监测, 使不符合规范的数据不能进入数据库,以确保数据库中存储的数据正确.有效 ...

  2. day--38 mysql表的完整性约束总结

    表的简单查询参考:https://www.cnblogs.com/clschao/articles/9995531.html 一:单表的查询: 查询数据的本质:mysql会去本地的硬盘上面找到对应的文 ...

  3. SQL学习笔记三之MySQL表操作

    阅读目录 一 存储引擎介绍 二 表介绍 三 创建表 四 查看表结构 五 数据类型 六 表完整性约束 七 修改表ALTER TABLE 八 复制表 九 删除表 一 存储引擎介绍 存储引擎即表类型,mys ...

  4. MySQL表完整性约束

    =======MySQL表完整性约束====== 目录: 一.介绍 二.not null 与 default 三.unique 四.primary key 五.auto_increment 六.for ...

  5. Mysql数据库(三)Mysql表结构管理

    一.MySQL数据类型 1.数字类型 (1)整数数据类型包括TINYINT/BIT/BOOL/SMALLINT/MEDIUMINT/INT/BIGINT (2)浮点数据类型包括FLOAT/DOUBLE ...

  6. Mysql(三):表操作

    一 存储引擎介绍 存储引擎即表类型,mysql根据不同的表类型会有不同的处理机制 详见:http://www.cnblogs.com/6324TV/p/8481061.html 二 表介绍 表相当于文 ...

  7. MySQL(三) 数据库表的查询操作【重要】

    序言 1.MySQL表操作(创建表,查询表结构,更改表字段等), 2.MySQL的数据类型(CHAR.VARCHAR.BLOB,等), 本节比较重要,对数据表数据进行查询操作,其中可能大家不熟悉的就对 ...

  8. mysql(三) 数据表的基本操作操作

    mysql(三) 数据表的基本操作操作 创建表,曾删改查,主键,外键,基本数据类型. 1. 创建表 create table 表名( 列名 类型 是否可以为空, 列名 类型 是否可以为空 )ENGIN ...

  9. {MySQL的逻辑查询语句的执行顺序}一 SELECT语句关键字的定义顺序 二 SELECT语句关键字的执行顺序 三 准备表和数据 四 准备SQL逻辑查询测试语句 五 执行顺序分析

    MySQL的逻辑查询语句的执行顺序 阅读目录 一 SELECT语句关键字的定义顺序 二 SELECT语句关键字的执行顺序 三 准备表和数据 四 准备SQL逻辑查询测试语句 五 执行顺序分析 一 SEL ...

随机推荐

  1. gradle-4.1-all.zip离线包下载 极速 android studio2.3 3.0编译必备

    http://download.csdn.net/download/yongheng289/10039982 gradle-4.1-all.zip离线包下载 极速 android studio2.3 ...

  2. Oracle表中的主键被当成哪些表的外键

    SELECT B.TABLE_NAME FROM USER_CONSTRAINTS A INNER JOIN USER_CONS_COLUMNS B ON A.CONSTRAINT_NAME = B. ...

  3. 阿里云视频直播PHP-SDK

    阿里云 视频直播 配置 及 PHP-SDK 接入教程准备工作域名管理配置鉴权地址生成器及DEMO演示-熟悉鉴权接入SDK推流回调的配置阿里云 视频直播 配置 及 PHP-SDK 接入教程 个人感觉,阿 ...

  4. This iPhone 6s is running iOS 11.3.1 (15E302), which may not be supported by this version of Xcode.

    This iPhone 6s is running iOS 11.3.1 (15E302), which may not be supported by this version of Xcode.

  5. C# 数值的隐式转换

    Debug2.Log(5/8.0f, 5.0f/8, 5/8);//output:0.625, 0.625, 0 隐式数值转换表

  6. spring的ioc与aop原理

    ioc(反向控制) 原理:    在编码阶段,既没有实例化对象,也没有设置依赖关系,而把它交给Spring,由Spring在运行阶段实例化.组装对象.这种做法颠覆了传统的写代码实例化.组装对象.然后一 ...

  7. URL和URI

    (一)URL和URI是什么 1.URL(Universal Resource Locator) 是统一资源定位符,对可以从互联网上得到的资源的位置和访问方法的一种简洁的表示,是互联网上标准资源的地址. ...

  8. extJs学习的资源

    http://www.qeefee.com/zt-extjs   Ext JS 6 入门学习资料大全(2016-12-14)   cddnExtJS学习:http://blog.csdn.net/co ...

  9. Chrome浏览器 调试工具 vue-devtools 的安装和使用

    https://www.cnblogs.com/yuqing6/p/7440549.html

  10. Linux firewalld使用教程+rhce课程实验

    --timeout= 设置规则生效300秒 调试阶段使用,防止规则设置错误导致无法远程连接 实验:在server0机器上部署httpd服务,通过添加富规则,只允许172.25.0.10/32访问,并且 ...