zoukankan      html  css  js  c++  java
  • 30 约束(Constraint)

    30 约束(Constraint)
     
        什么是约束?常见的约束有哪些呢?
            在创建表的时候,可以给表的字段添加相应的约束,添加约束的目的是为了保证表中数据的合法性、有效性、完整性。
            表格
            id            username(唯一性约束)            password(非空约束)
            
            常见的约束有哪些呢?
                非空约束(not null):约束的字段不能为null
                唯一约束(unique):约束数据的字段不能重复
                主键约束(primary key):约束的字段既不能为null,也不能重复(简称PK)
                外键约束(foreign key):...(简称FK)
                检查约束(check):注意Orcale数据库有check约束,但是mysql没有,目前mysql不支持该约束。
                
            非空约束 not null
            
            ----------------------------------------
            drop table if exists t_user;
            create table t_user(
                id int,
                username varchar(255) not null,
                password varchar(255)
            );
            
            // ERROR 1364 (HY000): Field 'username' doesn't have a default value
            insert into t_user(id,password) values(1,'123');
            
            insert into t_user(id,username,password) values(1,'lisi','123');
            
        唯一性约束(unique)
            
            * 唯一约束修饰的字段具有唯一性,不能重复,但可以为null;
            * 案例:
                drop table if exists t_user;
                create table t_user(
                    id int,
                    username varchar(255) unique // 列级约束
                );
                
                // 1062 - Duplicate entry 'zhangsan' for key 'username'
                insert into t_user (id,username) values(1,'zhangsan'),(2,'zhangsan');
                
                insert into t_user(id) values(1),(2),(3);
                
            * 案例,给两个列或者多个列添加unique
                drop table if exists t_user;
                create table t_user(
                    id int,
                    usercode varchar(255),
                    username varchar(255),
                    unique(usercode,username) // 多个字段联合添加1个约束 unique(表级约束)
                );
                
                insert into t_user values(1,'111','zs');
                insert into t_user values(2,'111','ls');
                insert into t_user values(3,'222','zs');
                select * from t_user;
                
                //1062 - Duplicate entry '111-zs' for key 'usercode'
                insert into t_user values(4,'111','zs');
                
                
                drop table if exists t_user;
                create table t_user(
                    id int,
                    usercode varchar(255) unique,
                    username varchar(255) unique
                );
                
                // 1062 - Duplicate entry '111' for key 'usercode'
                insert into t_user values(1,'111','zs'),(2,'111','ls');
                
            * 注意:not null约束只有列级约束,没有表级约束。
            
        主键约束:
            
            * 怎么给一张表添加主键约束?
                drop table if exists t_user;
                create table t_user(
                    id int primary key,// 列级约束
                    username varchar(255),
                    email varchar(255)
                );
                
                insert into t_user values(1,'zs','zs@qq.com'),(2,'ls','ls@qq.com'),(3,'ww','ww@qq.com');
                
                select * from t_user;
                +----+----------+-----------+
                | id | username | email     |
                +----+----------+-----------+
                |  1 | zs       | zs@qq.com |
                |  2 | ls       | ls@qq.com |
                |  3 | ww       | ww@qq.com |
                +----+----------+-----------+
                
                // 1062 - Duplicate entry '1' for key 'PRIMARY'
                insert into t_user(id,username,email) values(1,'ll','ll@qq.com');
                
                // 1364 - Field 'id' doesn't have a default value
                insert into t_user(username,email) values('ll','ll@qq.com');
                
                根据以上的测试得出:id是主键,因为添加了主键约束,主键字段中的数据不能为null,也不能重复。
                主键的特点:不能为null,也不能重复。
                
            * 主键相关的术语?
                主键约束:primary key
                主键字段:id字段添加了primary key之后,id叫做主键字段
                主键值:id字段中的每一个值都是主键值。
                
            * 主键有什么作用?
                
                - 表的设计三范式中有要求:第一范式就要求任何一张表都应该有主键。
                - 主键的作用:主键值是这行记录在这张表当中的唯一标识。(就像一个人的身份证号码一样。)
                
            * 主键的分类:
                根据主键字段的字段数量来划分:
                    单一主键(推荐的,常用你的)
                    复合主键(多个字段联合起来添加一个主键约束)(复核主键不建议使用,因为复合主键违背三范式。)
                        
                根据主键性质来划分:
                    自然主键:主键值最好是一个和业务没有任何关系的自然数。(这种方式是推荐的。)
                    业务主键:主键值和系统的业务挂钩,例如:拿着银行卡的卡号做主键,拿着身份证号码作为主键。(不推荐用)
                    最好不要拿着和业务挂钩的字段作为主键。因为以后的业务一旦发生改变的时候,主键值可能也需要随着发生
                    变化,但有的时候没有办法变化,因为变化可能会导致主键重复。
                    
            * 一张表的主键约束只能有1个。(必须记住)
            
            * 使用表级约束方式定义主键:
                drop table if exists t_user;
                create table t_user(
                    id int,
                    username varchar(255),
                    primary key(id)
                );
            
                insert into t_user(id,username) values(1,'zs');
                insert into t_user(id,username) values(2,'ls');
                insert into t_user(id,username) values(3,'ww');
                insert into t_user(id,username) values(4,'ll');
                
                select * from t_user;
                +----+----------+
                | id | username |
                +----+----------+
                |  1 | zs       |
                |  2 | ls       |
                |  3 | ww       |
                |  4 | ll       |
                +----+----------+
                
                // 1062 - Duplicate entry '4' for key 'PRIMARY'
                insert into t_user(id,username) values(4,'faker');
                
                以下内容是演示一下复合主键,不需要掌握:
                    drop table if exists t_user;
                    create table t_user(
                        id int,
                        username varchar(255),
                        password varchar(255),
                        primary key(id,username)
                    );
                    
            
            * mysql提供的主键值自增:(非常重要)
                drop table if exists t_user;
                create table t_user(
                    id int primary key auto_increment, // id字段自动维护一个自增的数字,从1开始,以1递增。
                    username varchar(255)
                );
                
                insert into t_user(username) values('a');
                insert into t_user(username) values('b');
                insert into t_user(username) values('c');
                insert into t_user(username) values('d');
            
                select * from t_user;
                +----+----------+
                | id | username |
                +----+----------+
                |  1 | a        |
                |  2 | b        |
                |  3 | c        |
                |  4 | d        |
                +----+----------+
            
                提示:orcale当中也提供了一个自增机制,叫做:序列(sequence)对象。
  • 相关阅读:
    分页
    ARC127C Binary Strings 思维 二进制 树
    ARC127A Leading 1s 数位DP
    【算法复习】背包问题 经典动态规划
    iOS15真机调试包下载
    Linux基础03 绝对路径/相对路径, 切换目录cd, 创建目录mkdir, 创建文件touch, 树状显示tree, 复制cp
    Jedis支持哨兵模式下认证
    19C ORA-00600: internal error code, arguments: [kkmmctbf:bad intcoln], [49]
    useMemo 和 useCallback 简单理解
    前端大屏页面布局经验
  • 原文地址:https://www.cnblogs.com/xlwu/p/13639805.html
Copyright © 2011-2022 走看看