• unique key index区别


    关系大致是这样:

    mysql中的unique约束是通过索引实现的;

    key的含义是概念级别的,意味着唯一性,key的概念等价于unique;

    所以说只要加了unique约束或者key,就会建立一个索引。

    在mysql中,使用index或者unique(以及key)都会简历索引,区别在于是否允许重复,这个可以在show index命令中看到。

    1. CREATE TABLE user1(
    2. id INT PRIMARY KEY AUTO_INCREMENT COMMENT '主键',
    3. name VARCHAR(200) COMMENT '姓名',
    4. age int COMMENT '年龄',
    5. unique aaa (`name`, `age`)
    6. )
    7. CREATE TABLE user1(
    8. id INT PRIMARY KEY AUTO_INCREMENT COMMENT '主键',
    9. name VARCHAR(200) COMMENT '姓名',
    10. age int COMMENT '年龄',
    11. constraint aaa unique(`name`, `age`)
    12. )

    这两种建表语句都会建立一个联合索引:

    mysql> show index from user1;
    +-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
    | Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment |
    +-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
    | user1 |          0 | PRIMARY  |            1 | id          | A         |           0 |     NULL | NULL   |      | BTREE      |         |               |
    | user1 |          0 | aaa      |            1 | name        | A         |           0 |     NULL | NULL   | YES  | BTREE      |         |               |
    | user1 |          0 | aaa      |            2 | age         | A         |           0 |     NULL | NULL   | YES  | BTREE      |         |               |
    +-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
    3 rows in set (0.00 sec)

    都有一个index,第二列表明这个index是否允许重复。0代表不允许重复。

    那么把这个aaa的unique删掉,建立一个普通的index:

    mysql> drop index aaa on user1;
    Query OK, 0 rows affected (0.02 sec)
    Records: 0  Duplicates: 0  Warnings: 0

    mysql> create index aaa on user1(`name`, `age`);
    Query OK, 0 rows affected (0.02 sec)
    Records: 0  Duplicates: 0  Warnings: 0

    mysql> show index from user1;
    +-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
    | Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment |
    +-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
    | user1 |          0 | PRIMARY  |            1 | id          | A         |           0 |     NULL | NULL   |      | BTREE      |         |               |
    | user1 |          1 | aaa      |            1 | name        | A         |           0 |     NULL | NULL   | YES  | BTREE      |         |               |
    | user1 |          1 | aaa      |            2 | age         | A         |           0 |     NULL | NULL   | YES  | BTREE      |         |               |
    +-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
    3 rows in set (0.00 sec)

    可以看到仍然有索引,但是第二列为1,表示该index可以允许重复。

    原文地址:https://blog.csdn.net/u010900754/article/details/94314066

  • 相关阅读:
    输出1-100 , 奇数偶数分别添加标识(for循环语句嵌套if-else语句)
    输出 1-100 内的奇数和偶数,并对其分别求和(while嵌套if-else循环)
    1至100累加求和问题
    淡旺季机票的价格问题(switch语句与if-else语句嵌套)
    (折扣计算)需求说明:普通顾客购物满100元打9折;会员购物打8折;会员购物满200元打7.5折(判断语句if-else和switch语句的嵌套结
    随机产生1-12的整数 , 根据产生整数输出一下该月份的季节信息(Math.random()和if语句的应用)
    在Hibernate单向一对多关联关系中的org.hibernate.StaleStateException 异常。
    关于Hibernate的报错org.hibernate.MappingException: Unknown entity
    Hibernate多对一关联关系
    关于映射异常org.hibernate.MappingException: An association from the table DUTY_INFO refers to an unmapped class: com.pms.entities.other.Department的原因。
  • 原文地址:https://www.cnblogs.com/jpfss/p/12190945.html
Copyright © 2020-2023  润新知