zoukankan      html  css  js  c++  java
  • MySQL SQL键区别分析

    在Mysql 5.7中,
    • 如果键是PRI,则列是主键或多列主键中的列之一。
    • 如果键是UNI,则该列是唯一索引的第一列。(唯一索引允许多个空值,但可以通过检查Null字段来判断该列是否允许空。)
    • 如果键为MUL,则该列是非唯一索引的第一列,其中允许在列中多次出现给定值。

    示例分析

    没有PRI、MUL、UNI

    mysql> create table penguins (foo INT);
    Query OK, 0 rows affected (0.01 sec)
    
    mysql> desc penguins;
    +-------+---------+------+-----+---------+-------+
    | Field | Type    | Null | Key | Default | Extra |
    +-------+---------+------+-----+---------+-------+
    | foo   | int(11) | YES  |     | NULL    |       |
    +-------+---------+------+-----+---------+-------+
    1 row in set (0.00 sec)

    MUL的示例

    mysql> create table penguins (foo INT, index(foo));
    Query OK, 0 rows affected (0.01 sec)
    
    mysql> desc penguins;
    +-------+---------+------+-----+---------+-------+
    | Field | Type    | Null | Key | Default | Extra |
    +-------+---------+------+-----+---------+-------+
    | foo   | int(11) | YES  | MUL | NULL    |       |
    +-------+---------+------+-----+---------+-------+
    1 row in set (0.00 sec)

    具有主键的列

    mysql> create table penguins (foo INT primary key);
    Query OK, 0 rows affected (0.02 sec)
    
    mysql> desc penguins;
    +-------+---------+------+-----+---------+-------+
    | Field | Type    | Null | Key | Default | Extra |
    +-------+---------+------+-----+---------+-------+
    | foo   | int(11) | NO   | PRI | NULL    |       |
    +-------+---------+------+-----+---------+-------+
    1 row in set (0.00 sec)

    具有唯一键的列Uni

    mysql> create table penguins (foo INT unique);
    Query OK, 0 rows affected (0.01 sec)
    
    mysql> desc penguins;
    +-------+---------+------+-----+---------+-------+
    | Field | Type    | Null | Key | Default | Extra |
    +-------+---------+------+-----+---------+-------+
    | foo   | int(11) | YES  | UNI | NULL    |       |
    +-------+---------+------+-----+---------+-------+
    1 row in set (0.00 sec) 

    具有MUL

    mysql> create table penguins (foo INT, bar INT, index(foo, bar));
    Query OK, 0 rows affected (0.01 sec)
    
    mysql> desc penguins;
    +-------+---------+------+-----+---------+-------+
    | Field | Type    | Null | Key | Default | Extra |
    +-------+---------+------+-----+---------+-------+
    | foo   | int(11) | YES  | MUL | NULL    |       |
    | bar   | int(11) | YES  |     | NULL    |       |
    +-------+---------+------+-----+---------+-------+
    2 rows in set (0.00 sec) 

    每个列都有MUL

    mysql> create table penguins (foo INT, bar int, index(foo), index(bar));
    Query OK, 0 rows affected (0.01 sec)
    
    mysql> desc penguins;
    +-------+---------+------+-----+---------+-------+
    | Field | Type    | Null | Key | Default | Extra |
    +-------+---------+------+-----+---------+-------+
    | foo   | int(11) | YES  | MUL | NULL    |       |
    | bar   | int(11) | YES  | MUL | NULL    |       |
    +-------+---------+------+-----+---------+-------+
    2 rows in set (0.00 sec) 

    第一列中具有MUL

    mysql> create table penguins (foo INT, 
           bar INT, 
           baz INT, 
           INDEX name (foo, bar, baz));
    Query OK, 0 rows affected (0.01 sec)
    
    mysql> desc penguins;
    +-------+---------+------+-----+---------+-------+
    | Field | Type    | Null | Key | Default | Extra |
    +-------+---------+------+-----+---------+-------+
    | foo   | int(11) | YES  | MUL | NULL    |       |
    | bar   | int(11) | YES  |     | NULL    |       |
    | baz   | int(11) | YES  |     | NULL    |       |
    +-------+---------+------+-----+---------+-------+
    3 rows in set (0.00 sec) 

    带有外键引用但主键的表是MUL

    mysql> create table penguins(id int primary key);
    Query OK, 0 rows affected (0.01 sec)
    
    mysql> create table skipper(id int, foreign key(id) references penguins(id));
    Query OK, 0 rows affected (0.01 sec)
    
    mysql> desc skipper;
    +-------+---------+------+-----+---------+-------+
    | Field | Type    | Null | Key | Default | Extra |
    +-------+---------+------+-----+---------+-------+
    | id    | int(11) | YES  | MUL | NULL    |       |
    +-------+---------+------+-----+---------+-------+
    1 row in set (0.00 sec)
    
    mysql> desc penguins;
    +-------+---------+------+-----+---------+-------+
    | Field | Type    | Null | Key | Default | Extra |
    +-------+---------+------+-----+---------+-------+
    | id    | int(11) | NO   | PRI | NULL    |       |
    +-------+---------+------+-----+---------+-------+
    1 row in set (0.00 sec) 
  • 相关阅读:
    关于Java 如何采用 metadata-extractor 读取数码相机 Exif 数据信息的代码
    Maven 项目引用文件地址管理配置与Eclipse的使用设置
    MySql如何将一个表字段更新到另一个表字段
    关于jquery.fileupload结合PHP上传图片的开发用法流程
    Windows下 Composer 安装 Thinkphp5 的记录.
    NASM汇编学习系列(6)——示例打印0到10
    NASM汇编学习系列(0)——说明、目录和环境搭建
    NASM汇编学习系列(5)——使用bss段和获取用户输入
    NASM汇编学习系列(4)——获取命令行参数
    NASM汇编学习系列(3)——多汇编文件间函数调用
  • 原文地址:https://www.cnblogs.com/johnvwan/p/15637952.html
Copyright © 2011-2022 走看看