• 『MySQL快速上手』-⑥-表的约束


    在这里插入图片描述
    真正约束字段的是数据类型,但是数据类型约束很单一,需要有一些额外的约束,更好的保证数据的合法性,从业务逻辑角度保证数据的正确性。

    1.空属性

    • 数据库默认字段基本都是字段为空,但是实际开发时,尽可能保证字段不为空,因为数据为空没办法参与运算

    案例1

    select null;
    
    • 1

    在这里插入图片描述

    select 1+null;
    
    • 1

    在这里插入图片描述

    案例2

    创建一个班级表,包含班级名和班级所在的教室。
    站在正常的业务逻辑中:

    • 如果班级没有名字,你不知道你在哪个班级;
    • 如果教室名字可以为空,就不知道在哪上课;

    所以我们在设计数据库表的时候,一定要在表中进行限制,满足上面条件的数据就不能插入到表中。这就是“约束”

    create table class(
        -> class_name varchar(20) not null,
        -> class_room varchar(10) not null
        -> );
    
    • 1
    • 2
    • 3
    • 4
    desc class;
    
    • 1

    在这里插入图片描述

    • 插入数据时,如果没有给教室数据则会插入失败;
    insert into class(class_name) values ('class1'); -- 插入失败
    
    • 1

    在这里插入图片描述

    2.默认值

    • 默认值:某一种数据会经常性的出现某个具体的值,可以在一开始就指定好,在需要真实数据的时候,用户可以选择性的使用默认值。

    案例

    create table t1(
        -> name varchar(20) not null,
        -> age tinyint unsigned default 0,
        -> sex char(2) default '男'
        -> );
    
    • 1
    • 2
    • 3
    • 4
    • 5
    desc t1;
    
    • 1

    在这里插入图片描述

    • 默认值的生效:数据在插入的时候不给该字段赋值,就使用默认值
    insert into t1(name) values ("张三");
    
    • 1
    select * from t1;
    
    • 1

    在这里插入图片描述

    • 注意:只有设置了default的列,才可以在插入值的时候,对列进行省略

    3.列描述

    • 列描述(comment):没有实际含义,专门用来描述字段,会根据表创建语句保存,用来给程序员或DBA来进行了解。

    案例

    create table t2(
        -> name varchar(20) not null comment '姓名',
        -> age tinyint unsigned default 0 comment '年龄',
        -> sex char(2) default '男' comment '性别'
        -> );
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 通过desc查看不到注释信息:
    desc t2;
    
    • 1
    • 通过show可以看到:
    show create table t2\G
    
    • 1

    在这里插入图片描述

    4.zerofill

    刚开始学习数据库时,很多人对数字类型后面的长度很迷茫。例如我们在创建下表时:

    create table t2(
        -> a int(10) unsigned default null,
        -> b int(10) unsigned default null
        -> );
    
    • 1
    • 2
    • 3
    • 4
    show create table t3\G
    
    • 1

    在这里插入图片描述

    可以看到int(10),这个代表什么意思呢?整型不是4字节码?这个10又代表什么呢?其实没有zerofill这个属性,括号内的数字是毫无意义的

    • 插入数据;
    insert into t3 values (1,2);
    select * from t3;
    
    • 1
    • 2
    • 当我们对列添加了zerofill属性后,显示的结果就有所不同了;
    • 修改t3表的属性:
    alter table t3 change a a int(5) unsigned zerofill;
    
    • 1
    show create table t3\G
    
    • 1

    在这里插入图片描述

    • a列添加了zerofill属性,再进行查找,返回如下结果:
    select * from t3;
    
    • 1

    在这里插入图片描述

    • 这次可以看到a的值由原来的1变成00001,这就是zerofill属性的作用,如果宽度小于设定的宽度(这里设置的是5),自动填充0。要注意的是,这只是最后显示的结果,在MySQL中实际存储的还是1。

    • 我们可以用hex函数来证明;

    select a, hex(a) from t3;
    
    • 1

    在这里插入图片描述

    可以看出数据库内部存储的还是1,00001只是设置了zerofill属性后的一种格式化输出而已。

    5.主键

    • 主键(primary ley):用来唯一的约束该字段里面的数据,不能重复,不能为空,一张表中最多只能有一个主键;主键所在的列通常是整数类型。

    案例

    • 创建表的时候直接在字段上指定主键;
    create table t4(
        -> id int unsigned primary key comment '学号不能为空',
        -> name varchar(20) not null
        -> );
    
    • 1
    • 2
    • 3
    • 4
    desc t4;
    
    • 1

    在这里插入图片描述

    • 主键约束:主键对应的字段中不能重复,一旦重复,操作失败
    insert into t4 values (1,'Make');
    insert into t4 values (1,'John'); -- 主键冲突
    
    • 1
    • 2

    在这里插入图片描述

    • 当表创建好以后但是没有主键的时候,可以再次追加主键;
    alter table 表名 add primary key(字段列表)
    
    • 1
    • 删除主键;
    alter table 表名 drop primary key;
    
    • 1
    alter table t4 drop primary key;
    desc t4;
    
    • 1
    • 2

    在这里插入图片描述

    • 复合主键:在创建表的时候,在所有字段之后,使用primary key(主键字段列表)来创建主键,如果有多个字段作为主键,可以使用复合主键
    create table t5(
        -> id int unsigned,
        -> course char(10) comment '课程代码',
        -> score tinyint unsigned default 60 comment '成绩',
        -> primary key(id, course)
        -> );
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    desc t5;
    
    • 1

    在这里插入图片描述

    insert into t5 (id, course) values (1, '123');
    insert into t5 (id, course) values (1, '123'); -- 报错
    
    • 1
    • 2

    在这里插入图片描述

    6.自增长

    • auto_increment:当对应的字段,不给值,会自动的被系统触发,系统会从当前字段中已经有的最大值+1操作,得到一个新的不同的值。通常和主键搭配使用,作为逻辑主键;

    自增长的特点:

    • 任何一个字段要做自增长,前提是本身是一个索引(key一栏有值)
    • 自增长字段必须是整数
    • 一张表最多只能有一个自增长

    案例

    create table t6(
        -> id int unsigned primary key auto_increment,
        -> name varchar(10) not null default ''
        -> );
    
    • 1
    • 2
    • 3
    • 4
    insert into t5(name) values ('Jimmy');
    insert into t6(name) values ('Stevn');
    select * from t6;
    
    • 1
    • 2
    • 3

    在这里插入图片描述

    • 在插入后获取上次插入的 AUTO_INCREMENT 的值(批量插入获取的是第一个值);
    select last_insert_id();
    
    • 1

    在这里插入图片描述

    • 索引概念:在关系数据库中,索引是一种单独的、物理的对数据库表中一列或多列的值进行排序的一种存储结构,它是某个表中一列或若干列值的集合和相应的指向表中物理标识这些值的数据页的逻辑指针清单。索引的作用相当于图书的目录,可以根据目录中的页码快速找到所需的内容。
      索引提供指向存储在表的指定列中的数据值的指针,然后根据您指定的排序顺序对这些指针排序。数据库使用索引以找到特定值,然后顺指针找到包含该值的行。这样可以使对应于表的SQL语句执行得更快,可快速访问数据库表中的特定信息

    7.唯一键

    • 一张表中往往有很多字段需要唯一性,数据不能重复,但是一张表中只能有一个主键。

    • 唯一键就可以解决表中有多个字段需要唯一性约束的问题。唯一键的本质和主键差不多,唯一键允许为空,而且可以多个为空,空字段不做唯一性比较

    关于唯一键和主键的区别:

    • 我们可以简单理解成,主键更多的是标识唯一性的。而唯一键更多的是保证在业务上,不要和别的信息出现重复。乍一听好像没啥区别,我们举一个例子:

    假设一个场景(当然,具体可能并不是这样,仅仅为了帮助大家理解)
    比如在公司,我们需要一个员工管理系统,系统中有一个员工表,员工表中有两列信息,一个身份证号码,一个是员工工号,我们可以选择身份号码作为主键。

    而我们设计员工工号的时候,需要一种约束:而所有的员工工号都不能重复。
    具体指的是在公司的业务上不能重复,我们设计表的时候,需要这个约束,那么就可以将员工工号设计成为唯一键。
    一般而言,我们建议将主键设计成为和当前业务无关的字段,这样,当业务调整的时候,我们可以尽量不会对主键做过大的调整。

    案例

    create table student(
        -> id char(10) unique comment '学号,不能重复,但可以为空',
        -> name varchar(10)
        -> );
    
    • 1
    • 2
    • 3
    • 4
    insert into student (id, name) values ('01', '张三');
    insert into student (id, name) values ('01', '李四'); -- 唯一约束不能重复
    insert into student (id, name) values ('null', '李四'); -- 但可以为空
    
    • 1
    • 2
    • 3
    select * from student;
    
    • 1

    在这里插入图片描述

    8.外键

    • 外键用于定义主表和从表之间的关系:外键约束主要定义在从表上,主表则必须是有主键约束或unique约束。当定义外键后,要求外键列数据必须在主表的主键列存在或为null

    语法:

    foreign key (字段名) references 主表()
    
    • 1

    案例

    在这里插入图片描述

    • 先创建主表;
    create table myclass(
        -> id int primary key,
        -> name varchar(30) not null comment '班级名'
        -> );
    
    • 1
    • 2
    • 3
    • 4
    • 再创建从表;
    create table student(
        -> id int primary key,
        -> name varchar(30) not null comment '学生名',
        -> class_id int,
        -> foreign key (class_id) references myclass (id)
        -> );
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 正常插入数据;
    insert into myclass values (10, 'C++菜鸟班'), (20, 'Java菜鸟班');
    insert into student values (100, '张三', 10), (101, '李四', 20);
    
    • 1
    • 2
    • 插入一个班级号为30的学生,因为没有这个班级,所以插入不成功;
    insert into student values (102, '王五', 30);
    
    ERROR 1452 (23000): Cannot add or update a child row: a foreign 
    key constraint fails (`db1`.`student`, CONSTRAINT `student_ibfk_1` 
    FOREIGN KEY (`class_id`) REFERENCES `myclass` (`id`))
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 插入班级id为null,比如来了一个学生,目前还没有分配班级;
    insert into student values (102, '王五', null);
    
    • 1
    select * from student;
    
    • 1

    在这里插入图片描述

    9.综合案例

    有一个商店的数据,记录客户及购物情况,有以下三个表组成:

    • 商品goods(商品编号goods_id,商品名goods_name, 单价unitprice, 商品类别category, 供应商provider);
    • 客户customer(客户号customer_id,姓名name,住址address,邮箱email,性别sex,身份证card_id);
    • 购买purchase(购买订单号order_id,客户号customer_id,商品号goods_id,购买数量nums);

    要求:

    • 每个表的主外键;
    • 客户的姓名不能为空值;
    • 邮箱不能重复;
    • 客户的性别(男,女);

    正式开始:

    • 创建数据库;
    create database if not exists shop_data default character set utf8;
    
    • 1
    • 选择数据库;
    use shop_data;
    
    • 1
    • 创建数据库表;
    • 商品:
    mysql> create table if not exists goods(
        -> goods_id int primary key auto_increment comment '商品编号',
        -> goods_name varchar(32) not null comment '商品名称',
        -> unitprice int not null default 0 comment '单价/分',
        -> category varchar(12) comment '商品分类',
        -> provider varchar(64) not null comment '供应商名称'
        -> );
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 客户:
    mysql> create table if not exists customer(
        -> customer_id int primary key auto_increment comment '客户编号',
        -> name varchar(32) not null comment '客户名称',
        -> address varchar(256) comment '客户地址',
        -> email varchar(64) unique key comment '电子邮箱',
        -> sex enum ('男', '女') not null comment '性别',
        -> card_id char(18) unique key comment '身份证'
        -> );
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 购买:
    mysql> create table if not exists purchase
        -> (
        -> order_id int primary key auto_increment comment '订单号',
        -> customer_id int comment '客户编号',
        -> goods_id int comment '商品编号',
        -> nums int default 0 comment '购买数量',
        -> foreign key (customer_id) references customer (customer_id),
        -> foreign key (goods_id) references goods (goods_id)
        -> );
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    在这里插入图片描述
    在这里插入图片描述

  • 相关阅读:
    T — SQl 高阶语法之索引
    Java笔记--反射机制一
    37. 干货系列从零用Rust编写负载均衡及代理,负载均衡中try_files实现
    常见面试问题及回答——JAVA基础
    deepvariant 基因变异识别算法docker版使用
    云海再获中国第一 OpenStack社区Xena版本新特性快来划重点
    日期相关整理
    tomcat敏感数据加密实现方案
    [附源码]计算机毕业设计JAVAjsp社区医院电子病历系统
    极市打榜|70G+已标注数据集出炉,油品泄露识别等全新算法上线!
  • 原文地址:https://blog.csdn.net/gllll_yu/article/details/134287920