• mysql三-3:完整性约束


    一 介绍

    约束条件与数据类型的宽度一样,都是可选参数

    作用:用于保证数据的完整性和一致性
    主要分为:

    复制代码
    PRIMARY KEY (PK)    标识该字段为该表的主键,可以唯一的标识记录
    FOREIGN KEY (FK)    标识该字段为该表的外键
    NOT NULL    标识该字段不能为空
    UNIQUE KEY (UK)    标识该字段的值是唯一的
    AUTO_INCREMENT    标识该字段的值自动增长(整数类型,而且为主键)
    DEFAULT    为该字段设置默认值
    
    UNSIGNED 无符号
    ZEROFILL 使用0填充
    复制代码

     

    说明:

    复制代码
    1. 是否允许为空,默认NULL,可设置NOT NULL,字段不允许为空,必须赋值
    2. 字段是否有默认值,缺省的默认值是NULL,如果插入记录时不给字段赋值,此字段使用默认值
    sex enum('male','female') not null default 'male'
    age int unsigned NOT NULL default 20 必须为正值(无符号) 不允许为空 默认是20
    3. 是否是key
    主键 primary key
    外键 foreign key
    索引 (index,unique...)
    复制代码

    二 not null与default

    是否可空,null表示空,非字符串
    not null - 不可空
    null - 可空


    默认值,创建列时可以指定默认值,当插入数据时如果未主动设置,则自动添加默认值
    create table tb1(
    nid int not null defalut 2,
    num int not null
    )

    验证

    三 unique

    View Code
    not null+unique的化学反应
    联合唯一

    四 primary key

    primary key字段的值不为空且唯一

    一个表中可以:

    单列做主键
    多列做主键(复合主键)

    但一个表内只能有一个主键primary key

    单列主键
    多列主键

    五 auto_increment

    约束字段为自动增长,被约束的字段必须同时被key约束

    View Code
    步长increment与起始偏移量offset:auto_increment_increment,auto_increment_offset

    六 foreign key

    一 快速理解foreign key

    员工信息表有三个字段:工号  姓名  部门

    公司有3个部门,但是有1个亿的员工,那意味着部门这个字段需要重复存储,部门名字越长,越浪费

    解决方法:

    我们完全可以定义一个部门表

    然后让员工信息表关联该表,如何关联,即foreign key

    示范 

    二 如何找出两张表之间的关系 

    复制代码
    分析步骤:
    #1、先站在左表的角度去找
    是否左表的多条记录可以对应右表的一条记录,如果是,则证明左表的一个字段foreign key 右表一个字段(通常是id)
    
    #2、再站在右表的角度去找
    是否右表的多条记录可以对应左表的一条记录,如果是,则证明右表的一个字段foreign key 左表一个字段(通常是id)
    
    #3、总结:
    #多对一:
    如果只有步骤1成立,则是左表多对一右表
    如果只有步骤2成立,则是右表多对一左表
    
    #多对多
    如果步骤1和2同时成立,则证明这两张表时一个双向的多对一,即多对多,需要定义一个这两张表的关系表来专门存放二者的关系
    
    #一对一:
    如果1和2都不成立,而是左表的一条记录唯一对应右表的一条记录,反之亦然。这种情况很简单,就是在左表foreign key右表的基础上,将左表的外键字段设置成unique即可
    复制代码

    三 建立表之间的关系

    #一对多或称为多对一
    三张表:出版社,作者信息,书
    
    一对多(或多对一):一个出版社可以出版多本书
    
    关联方式:foreign key
    View Code
    其他例子

      

    #多对多
    三张表:出版社,作者信息,书
    
    多对多:一个作者可以写多本书,一本书也可以有多个作者,双向的一对多,即多对多
      
    关联方式:foreign key+一张新的表
    View Code
    其他例子

     

    #一对一
    两张表:学生表和客户表
    
    一对一:一个学生是一个客户,一个客户有可能变成一个学校,即一对一的关系
    
    关联方式:foreign key+unique
    View Code
    其他例子

    七 作业

    练习:账号信息表,用户组,主机表,主机组

    #用户表
    create table user(
    id int not null unique auto_increment,
    username varchar(20) not null,
    password varchar(50) not null,
    primary key(username,password)
    );
    
    insert into user(username,password) values
    ('root','123'),
    ('egon','456'),
    ('alex','alex3714')
    ;
    
    
    #用户组表
    create table usergroup(
    id int primary key auto_increment,
    groupname varchar(20) not null unique
    );
    
    insert into usergroup(groupname) values
    ('IT'),
    ('Sale'),
    ('Finance'),
    ('boss')
    ;
    
    
    #主机表
    create table host(
    id int primary key auto_increment,
    ip char(15) not null unique default '127.0.0.1'
    );
    
    insert into host(ip) values
    ('172.16.45.2'),
    ('172.16.31.10'),
    ('172.16.45.3'),
    ('172.16.31.11'),
    ('172.10.45.3'),
    ('172.10.45.4'),
    ('172.10.45.5'),
    ('192.168.1.20'),
    ('192.168.1.21'),
    ('192.168.1.22'),
    ('192.168.2.23'),
    ('192.168.2.223'),
    ('192.168.2.24'),
    ('192.168.3.22'),
    ('192.168.3.23'),
    ('192.168.3.24')
    ;
    
    
    #业务线表
    create table business(
    id int primary key auto_increment,
    business varchar(20) not null unique
    );
    insert into business(business) values
    ('轻松贷'),
    ('随便花'),
    ('大富翁'),
    ('穷一生')
    ;
    
    
    #建关系:user与usergroup
    
    create table user2usergroup(
    id int not null unique auto_increment,
    user_id int not null,
    group_id int not null,
    primary key(user_id,group_id),
    foreign key(user_id) references user(id),
    foreign key(group_id) references usergroup(id)
    );
    
    insert into user2usergroup(user_id,group_id) values
    (1,1),
    (1,2),
    (1,3),
    (1,4),
    (2,3),
    (2,4),
    (3,4)
    ;
    
    
    
    #建关系:host与business
    
    create table host2business(
    id int not null unique auto_increment,
    host_id int not null,
    business_id int not null,
    primary key(host_id,business_id),
    foreign key(host_id) references host(id),
    foreign key(business_id) references business(id)
    );
    
    insert into host2business(host_id,business_id) values
    (1,1),
    (1,2),
    (1,3),
    (2,2),
    (2,3),
    (3,4)
    ;
    
    #建关系:user与host
    
    create table user2host(
    id int not null unique auto_increment,
    user_id int not null,
    host_id int not null,
    primary key(user_id,host_id),
    foreign key(user_id) references user(id),
    foreign key(host_id) references host(id)
    );
    
    insert into user2host(user_id,host_id) values
    (1,1),
    (1,2),
    (1,3),
    (1,4),
    (1,5),
    (1,6),
    (1,7),
    (1,8),
    (1,9),
    (1,10),
    (1,11),
    (1,12),
    (1,13),
    (1,14),
    (1,15),
    (1,16),
    (2,2),
    (2,3),
    (2,4),
    (2,5),
    (3,10),
    (3,11),
    (3,12)
    ;
    View Code

    作业:

     
  • 相关阅读:
    Future接口和Callable接口以及FeatureTask详解
    puppet的使用:ERB模板介绍
    puppet的使用:依赖关系整理
    数字证书常见格式整理
    c3p0配置文件
    dockerfile简述
    Grape简介
    keytool和openssl生成的证书转换
    Grape教程-params
    耿丹CS16-2班助教总结
  • 原文地址:https://www.cnblogs.com/llhtjwq/p/8306721.html
Copyright © 2020-2023  润新知