zoukankan      html  css  js  c++  java
  • Day40-数据库入门学习-完整性约束、关联关系

    一、完整性约束

    1.什么是约束

      为了保证数据的合法性与完整性,对字段进行了除了数据类型以外添加额外的约束。

    2.not null

      2.1意义

        not null是非空约束,数据不能为空

      2.2语法

    create table student (id int,name char(10) not null);#名字不能为空

    3.default

      3.1意义

        default  默认值约束,可以指定字段的默认值

      3.2语法

    create table user (id int,name char(10) not null,sex char(5) default "woman");

    #当在一个大多数的情况都为一个值的时候,就可以用默认约束


    4.unique

      4.1意义

        unique   唯一性约束,该字段的值不能重复。比如,身份证,手机号,学号

        unique其实是一种索引,索引是一种数据结构,用于提高查询效率。

        unique可以为空,一张表中可以有多个唯一约束

      4.2语法

        单列唯一约束

    create table t5(idcard char(18) unique);

        多列联合唯一约束

    create table t6(idcard char(18),phonenumber char(11),unique(idcard,phonenumber));
    #意思: 身份证相同 并且 手机号相同 那就叫相同


    5.primary key 

      5.1意义

        primary key称之为主键约束,用于唯一标识表中一条记录。

        如何能做到唯一标识?

          该字段,只要是唯一的,并且不为空即可。也就是说,从约束的角度来看主键约束和非空加唯一约束没有区别

        那它们之间的区别是什么?

          唯一约束,是一种索引,必然存在硬盘上的某个文件中,是物理层面(实实在在存在的数据)
          primary key,是一种逻辑意义上的数据(实际上不存在)
          换句话说,主键就是由唯一约束和非空约束 组成的约束

        有主键和没有主键的区别?
          1.无法区分两个相同记录,比如班级里有两个人名字相同
          2.有主键则意味有这索引,效率更高
          3.可以建立关联关系

      5.2语法

    create table stu (stuid int primary key,name char(3));
    #create table stu(stuid int unique not null,name char(3));

      多列联合主键:

    create table t8(idcard char(18),phonenumber char(11),primary key(idcard,phonenumber));
    #注意复合主键必须同时建立或者同时删除


    6.auto_increment 

      6.1意义

        auto_increment ,自动增长,通常搭配主键字段使用,可以自动为你的数据分配逐渐
        如何分配的?
          添加一条就自动加1 计数从1开始

      6.2语法

    create table t9(id int primary key auto_increment,name char(3));

      如果主键是自动增长的,当你执行insert操作时要注意,插入的时候可以跳过这个字段或者插入的时候为null

      修改自动增长的起始位置

    alter table t9 auto_increment = 7;

    7.foreign key

      7.1意义

           foreign key 专门用于为表和表之间建立物理关联

        现在有两张表,员工表和部门表

          1. 从员工出发 员工对于部门来说 时 多个员工对应一个部门
          2. 从部门出发 一个部门对应多个员工

        虽然有了关系,但是两个表之间还是没有任何物理联系,插一个不存在的部门也没问题。

        所以就需要外键来是的员工表和部门表产生关联。

        添加外键约束时: 产生的限制
          被关联的表需要先被创建
          部门数据(主表)应该先插入 员工数据(从表)后插入
          在删除部门数据前(主表)前 要保证该部门的员工数据都删除了
          在更新部门编号前 要先保证没有员工关联到这个部门

        简单的说 外键指的是 另一张的主键

          外键加上以后 主表中的数据 删除 和更新时 都受到限制

      7.2语法

        先创建部门表(主表)

    create table dept(id int primary key auto_increment,name char(10),manager char(10));

        在创建员工表(从表)

    create table emp(id int primary key auto_increment,name char(10),dept_id int,foreign key(dept_id) references dept(id));

    8.级联操作

    8.1意义

       常用于外键,指的是就是主表与从表同步更新和删除

    8.2语法

    create table class(id int primary key auto_increment,namechar(10));
    create table student(
    id int primary key auto_increment,
    name char(10),
    c_id int,
    foreign key(c_id) references class(id)
    on update cascade#同步更新
    on delete cascade#同步删除
    );
    
    #对主表的id进行更新
    #以及删除某条主表记录 来验证效果

    二、关联关系

    1.一对多或多对一

    表:学校表和课程表
    关系:一个学校有多个课程
    1 创建学校表:有学校id,学校名称,地址
    create table school(id int primary key auto_increment,
    name char(10),
    address char(20)
    );
    
    2 创建课程表:有课程id,课程名称,课程价格,课程周期,所属学校
    create table course(id int primary key auto_increment,
    name char(10),
    price int,
    period char(5),
    school_id int,
    foreign key(school_id)
    references school(id)
    on update cascade
    on delete cascade
    );
    
    3.创建学校
    insert into school(name,address) values
    ('oldboyBeijing','北京昌平'),
    ('oldboyShanghai','上海浦东')
    ;
    
    
    
    4.创建课程
    insert into course(name,price,period,school_id) values
    ('Python全栈开发一期',20000,'5个月',2),
    ('Linux运维一期',200,'2个月',2),
    ('Python全栈开发20期',20000,'5个月',1)
    ;

    2.一对一

    表:学生表和客户表
    关系:一个学生对应一个客户
    #一定是student来foreign key表customer,这样就保证了:
    #1 学生一定是一个客户,
    #2 客户不一定是学生,但有可能成为一个学生
    
    1.创建客户表
    create table customer(
    id int primary key auto_increment,
    name varchar(20) not null,
    qq varchar(10) not null,
    phone char(16) not null
    );
    
    2.创建学生表
    create table student(
    id int primary key auto_increment,
    class_name varchar(20) not null,
    customer_id int unique, #该字段一定要是唯一的
    foreign key(customer_id) references customer(id) #外键的字段一定要保证unique
    on delete cascade
    on update cascade
    );
    
    
    3.增加客户
    insert into customer(name,qq,phone) values
    ('李飞机','31811231',13811341220),
    ('王大炮','123123123',15213146809),
    ('守榴弹','283818181',1867141331),
    ('吴坦克','283818181',1851143312),
    ('赢火箭','888818181',1861243314),
    ('战地雷','112312312',18811431230)
    ;
    
    
    4.增加学生
    insert into student(class_name,customer_id) values
    ('脱产3班',3),
    ('周末19期',4),
    ('周末19期',5)
    ;

    3.多对多

    表:学生表和老师表
    关系:一个学生上过多个老师的课,一个老师教过多个学生
    
    1.创建老师表
    create table teacher (id int primary key auto_increment,name char(10));
    
    2.创建学生表
    create table student (id int primary key auto_increment,name char(10));
    
    #为了避免重复无用的关系数据  关系表加上关联的主键约束
    3.创建关联表
    create table t_s (t_id int,
    s_id int,
    foreign key(t_id) references teacher(id),
    foreign key(s_id) references student(id),
    primary key(t_id,s_id)
    );
    
    4插入数据
    insert into student value(null,"lxx");
    insert into teacher value(null,"exx");
    insert into t_s value(1,1);
  • 相关阅读:
    textarea宽度、高度自动适应处理方法
    Table嵌套去掉子table的外边框
    发现原来自己挺能给自己找理由开脱的
    Life is not the amount of breath you take.
    在遍历ResultSet的循环中再执行SQL会发生什么(前提:同一个Statement)
    按月查询数据
    Oracle SQL 判断某表是否存在
    在Python程序中执行linux命令
    在Oracle中十分钟内创建一张千万级别的表
    Redis Sentinel结构 及相关文档
  • 原文地址:https://www.cnblogs.com/xvchengqi/p/9641314.html
Copyright © 2011-2022 走看看