• 主键约束!


    目录

    主键约束(primary key,简称PK)非常重要五颗星*****

    1. 主键约束的相关术语?

    2. 什么是主键?有啥用?

    3. 主键的特征

    4. 怎么给一张表添加主键约束呢?

    4.1 单一主键:1个字段做主键

    4.2 使用表级约束添加主键

    4.3 复合主键:多个字段联合起来做主键

    5. 一张表中主键约束只能添加1个(主键只能有1个。)

    6. 主键值 

    7. 主键除了单一主键和复合主键之外,还可以这样进行分类?

    7.1 在实际开发中使用业务主键多,还是使用自然主键多一些?

    7.2 在mysql当中,有一种机制,可以帮助我们自动维护一个主键值


    主键约束(primary key,简称PK)非常重要五颗星*****

    1. 主键约束的相关术语?

    • 主键约束:就是一种约束。
    • 主键字段:该字段上添加了主键约束,这样的字段叫做主键字段
    • 主键值:主键字段中的每一个值都叫做主键值。

    2. 什么是主键?有啥用?

    • 主键值是每一行记录的唯一标识。
    • 主键值是每一行记录的身份证号!!!

    记住:任何一张表都应该有主键,没有主键,表无效!!

    3. 主键的特征

    not null + unique(主键值不能是NULL,同时也不能重复!)

    4. 怎么给一张表添加主键约束呢?

    4.1 单一主键:1个字段做主键

    drop table if exists t_vip;
    create table t_vip(
            id int primary key,  // 列级约束
            name varchar(255)
    );

    insert into t_vip(id,name) values(1,'zhangsan');
    insert into t_vip(id,name) values(2,'lisi');

     select * from t_vip;

    insert into t_vip(id,name) values(2,'wangwu');

    错误:id 不能重复

    insert into t_vip(name) values('zhaoliu');

    错误:id 不能为NULL

    4.2 使用表级约束添加主键

    drop table if exists t_vip;
    create table t_vip(
               id int,
               name varchar(255),
               primary key(id)  // 表级约束
    );

    insert into t_vip(id,name) values(1,'zhangsan'); 

    insert into t_vip(id,name) values(1,'lisi'); 

    错误:id 不能重复

    4.3 复合主键:多个字段联合起来做主键

    表级约束主要是给多个字段联合起来添加约束

    drop table if exists t_vip;
    create table t_vip(
              id int,
              name varchar(255),
              email varchar(255),
              primary key(id,name)
    );

    insert into t_vip(id,name,email) values(1,'zhangsan','zhangsan@123.com');
    insert into t_vip(id,name,email) values(1,'lisi','lisi@123.com'); 

    select * from t_vip;

    insert into t_vip(id,name,email) values(1,'lisi','lisi@123456.com'); 

    错误:id和name不能同时重复

    在实际开发中不建议使用复合主键,建议使用单一主键!因为主键值存在的意义就是这行记录的身份证号,只要意义达到即可,单一主键可以做到。复合主键比较复杂,不建议使用!!!

    5. 一张表中主键约束只能添加1个(主键只能有1个。)

    drop table if exists t_vip;
    create table t_vip(
             id int primary key,
             name varchar(255) primary key
    );

    6. 主键值 

    • 建议使用:int、bigint、char 等类型。
    • 不建议使用:varchar来做主键。主键值一般都是数字,一般都是定长的!

    7. 主键除了单一主键和复合主键之外,还可以这样进行分类?

    • 自然主键:主键值是一个自然数,和业务没关系。
    • 业务主键:主键值和业务紧密关联,例如拿银行卡账号做主键值,这就是业务主键!

    7.1 在实际开发中使用业务主键多,还是使用自然主键多一些?

    • 自然主键使用比较多,因为主键只要做到不重复就行,不需要有意义。
    • 业务主键不好,因为主键一旦和业务挂钩,那么当业务发生变动的时候,可能会影响到主键值,所以业务主键不建议使用,尽量使用自然主键。

    7.2 在mysql当中,有一种机制,可以帮助我们自动维护一个主键值

    auto_increment:表示自增,从1开始,以1递增!

    drop table if exists t_vip;
            create table t_vip(
                id int primary key auto_increment,
                name varchar(255)
    );


    insert into t_vip(name) values('zhangsan');
    insert into t_vip(name) values('zhangsan');
    insert into t_vip(name) values('zhangsan');
    insert into t_vip(name) values('zhangsan');
    insert into t_vip(name) values('zhangsan');
    insert into t_vip(name) values('zhangsan');
    insert into t_vip(name) values('zhangsan');
    insert into t_vip(name) values('zhangsan');

    select * from t_vip;

  • 相关阅读:
    springboot
    Git学习笔记4
    pytest数据驱动
    Gitlab部署
    linux 下载 安装 php详细步骤与nginx配置
    GhMYB7促进棉纤维中次生壁纤维素的积累
    特斯拉全自动驾驶(FSD)系统发展与解析
    C高级 脚本相关练习
    Java.lang.Character类中codePointAt(CharSequence seq, int index)方法具有什么功能呢?
    C++ 多线程
  • 原文地址:https://blog.csdn.net/weixin_52385232/article/details/126049536