• Mysql知识进阶


    查询表格详细内容

    基础版

    desc 表格名;

    1. mysql> desc lanqiao;
    2. +------------+------------+------+-----+---------+-------+
    3. | Field | Type | Null | Key | Default | Extra |
    4. +------------+------------+------+-----+---------+-------+
    5. | candidate | varchar(3) | YES | | NULL | |
    6. | age | int | YES | | NULL | |
    7. | grade | int | YES | | NULL | |
    8. | is_promote | tinyint(1) | YES | | NULL | |
    9. +------------+------------+------+-----+---------+-------+
    10. 4 rows in set (0.00 sec)

    进阶版

    show full columns from 表名;

    通过此查询方法,可以查询到与权限相关的更为详细的内容

    1. mysql> show full columns from lanqiao;
    2. +------------+------------+--------------------+------+-----+---------+-------+---------------------------------+---------+
    3. | Field | Type | Collation | Null | Key | Default | Extra | Privileges | Comment |
    4. +------------+------------+--------------------+------+-----+---------+-------+---------------------------------+---------+
    5. | candidate | varchar(3) | utf8mb4_0900_ai_ci | YES | | NULL | | select,insert,update,references | |
    6. | age | int | NULL | YES | | NULL | | select,insert,update,references | |
    7. | grade | int | NULL | YES | | NULL | | select,insert,update,references | |
    8. | is_promote | tinyint(1) | NULL | YES | | NULL | | select,insert,update,references | |
    9. +------------+------------+--------------------+------+-----+---------+-------+---------------------------------+---------+
    10. 4 rows in set (0.00 sec)

    变更key属性

     给某属性增加特征(例如unique)

    alter table 表名 add unique;

    举个例子

    1. alter table lanqiao add unique(candidate);
    2. Query OK, 0 rows affected (0.02 sec)
    3. Records: 0 Duplicates: 0 Warnings: 0

    可见,表格属性那里增加了unique限制 

    1. mysql> show full columns from lanqiao;
    2. +------------+------------+--------------------+------+-----+---------+-------+---------------------------------+---------+
    3. | Field | Type | Collation | Null | Key | Default | Extra | Privileges | Comment |
    4. +------------+------------+--------------------+------+-----+---------+-------+---------------------------------+---------+
    5. | candidate | varchar(3) | utf8mb4_0900_ai_ci | YES | UNI | NULL | | select,insert,update,references | |
    6. | age | int | NULL | YES | | NULL | | select,insert,update,references | |
    7. | grade | int | NULL | YES | | NULL | | select,insert,update,references | |
    8. | is_promote | tinyint(1) | NULL | YES | | NULL | | select,insert,update,references | |
    9. +------------+------------+--------------------+------+-----+---------+-------+---------------------------------+---------+
    10. 4 rows in set (0.00 sec)

    查看各种约束(主外键引用关系)

    desc information_schema.key_column_usage

    通过information_schema.key_column_usage表可以来查看外键引用关系

    1. desc information_schema.key_column_usage;
    2. +-------------------------------+--------------+------+-----+---------+-------+
    3. | Field | Type | Null | Key | Default | Extra |
    4. +-------------------------------+--------------+------+-----+---------+-------+
    5. | CONSTRAINT_CATALOG | varchar(64) | YES | | NULL | |
    6. | CONSTRAINT_SCHEMA | varchar(64) | YES | | NULL | |
    7. | CONSTRAINT_NAME | varchar(64) | YES | | NULL | |
    8. | TABLE_CATALOG | varchar(64) | YES | | NULL | |
    9. | TABLE_SCHEMA | varchar(64) | YES | | NULL | |
    10. | TABLE_NAME | varchar(64) | YES | | NULL | |
    11. | COLUMN_NAME | varchar(64) | YES | | NULL | |
    12. | ORDINAL_POSITION | int unsigned | NO | | 0 | |
    13. | POSITION_IN_UNIQUE_CONSTRAINT | int unsigned | YES | | NULL | |
    14. | REFERENCED_TABLE_SCHEMA | varchar(64) | YES | | NULL | |
    15. | REFERENCED_TABLE_NAME | varchar(64) | YES | | NULL | |
    16. | REFERENCED_COLUMN_NAME | varchar(64) | YES | | NULL | |
    17. +-------------------------------+--------------+------+-----+---------+-------+
    18. 12 rows in set (0.00 sec)

    更改终止标识符

    原本默认的语句结束标识为分号“;”

    使用delimiter语句,可以更改该默认属性。

    delimiter 更正后的标识符

    1. mysql> show databases;
    2. +--------------------+
    3. | Database |
    4. +--------------------+
    5. | aid |
    6. | heroes_heaven |
    7. | information_schema |
    8. | mysql |
    9. | performance_schema |
    10. | sakila |
    11. | student_grades |
    12. | test |
    13. +--------------------+
    14. 9 rows in set (0.00 sec)
    15. mysql> delimiter //
    16. mysql> show databases //
    17. +--------------------+
    18. | Database |
    19. +--------------------+
    20. | aid |
    21. | heroes_heaven |
    22. | information_schema |
    23. | mysql |
    24. | performance_schema |
    25. | sakila |
    26. | student_grades |
    27. | test |
    28. +--------------------+
    29. 9 rows in set (0.00 sec)

    主码变更

    增加主码

    alter table 表名 add primary key(需要添加的属性名);

    1. alter table lanqiao add primary key(candidate);
    2. Query OK, 0 rows affected (0.03 sec)
    3. Records: 0 Duplicates: 0 Warnings: 0

    为选手candidate增加主码后,显示key属性有了pri 

    1. show full columns from lanqiao;
    2. +------------+------------+--------------------+------+-----+---------+-------+---------------------------------+---------+
    3. | Field | Type | Collation | Null | Key | Default | Extra | Privileges | Comment |
    4. +------------+------------+--------------------+------+-----+---------+-------+---------------------------------+---------+
    5. | candidate | varchar(3) | utf8mb4_0900_ai_ci | NO | PRI | NULL | | select,insert,update,references | |
    6. | age | int | NULL | YES | | NULL | | select,insert,update,references | |
    7. | grade | int | NULL | YES | | NULL | | select,insert,update,references | |
    8. | is_promote | tinyint(1) | NULL | YES | | NULL | | select,insert,update,references | |
    9. +------------+------------+--------------------+------+-----+---------+-------+---------------------------------+---------+
    10. 4 rows in set (0.00 sec)

    删除主码pri

    alter table 表名 drop primary key;

    1. mysql> alter table lanqiao drop primary key;
    2. Query OK, 0 rows affected (0.03 sec)
    3. Records: 0 Duplicates: 0 Warnings: 0
    1. show full columns from lanqiao;
    2. +------------+------------+--------------------+------+-----+---------+-------+---------------------------------+---------+
    3. | Field | Type | Collation | Null | Key | Default | Extra | Privileges | Comment |
    4. +------------+------------+--------------------+------+-----+---------+-------+---------------------------------+---------+
    5. | candidate | varchar(3) | utf8mb4_0900_ai_ci | NO | PRI | NULL | | select,insert,update,references | |
    6. | age | int | NULL | YES | | NULL | | select,insert,update,references | |
    7. | grade | int | NULL | YES | | NULL | | select,insert,update,references | |
    8. | is_promote | tinyint(1) | NULL | YES | | NULL | | select,insert,update,references | |
    9. +------------+------------+--------------------+------+-----+---------+-------+---------------------------------+---------+
    10. 4 rows in set (0.00 sec)

     对插入的数据进行分析,插入对应分组的预定结果(good,not good)

     

  • 相关阅读:
    处理器 Handler 详解
    集简云&银行系统:API连接广告推广与客服系统,降低企业研发成本
    [Bug]Ubuntu下使用TexStudio存在的一些问题
    Exam diagnosis Privacy Policy
    自学 TypeScript 第五天,手把手项目搭建 TS 篇
    kafka在windows下单机版搭建
    Flash的学习
    一、数据库的基础简介
    4.迭代最近点ICP及非线性优化求解
    【云原生监控系列第三篇】Prometheus普罗米修斯进阶——PromQL的指标类型
  • 原文地址:https://blog.csdn.net/weixin_60535956/article/details/127719627