MySQL语句之describe,explain,help,use

本文介绍了MySQL中的一些关键查询语句,包括DESCRIBE(DESC)用于查看表的列信息,EXPLAIN用于分析查询执行计划,HELP用于获取命令帮助,以及USE语句用于切换数据库。通过示例和语法解析,帮助读者理解和应用这些语句。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1 创建测试表及数据

create table utility_table(  
    c1 int not null auto_increment,   
    c2 varchar(10),   
    c3 int(10),   
    primary key(c1)  
)engine=innodb;  
insert into utility_table values('', 'name1', 001);  
insert into utility_table values('', 'name2', 002);  
insert into utility_table values('', 'name3', 003);  
insert into utility_table values('', 'name4', 004);  
insert into utility_table values('', 'name5', 005);

2 DESCRIBE,DESC,EXPLAIN语句

    DESCRIBE,DESC与EXPLAIN是同义词,它门是SHOW COLUMNS FROM的快捷方式,DESCRIBE语句被设立出来,用于与Oracle相兼容。语句给出了有关表的列信息。

    基本语法:

{EXPLAIN | DESCRIBE | DESC}
    tbl_name [col_name | wild]

{EXPLAIN | DESCRIBE | DESC}
    [explain_type]
    explainable_stmt

explain_type: {
    EXTENDED
  | PARTITIONS
}
explainable_stmt: {
    SELECT statement
  | DELETE statement
  | INSERT statement
  | REPLACE statement
  | UPDATE statement
}

    实例:

mysql> describe utility_table;
mysql> desc utility_table;
mysql> explain utility_table;
mysql> show columns from  utility_table;
+-------+-------------+------+-----+---------+----------------+
| Field | Type        | Null | Key | Default | Extra          |
+-------+-------------+------+-----+---------+----------------+
| c1    | int(11)     | NO   | PRI | NULL    | auto_increment |
| c2    | varchar(10) | YES  |     | NULL    |                |
| c3    | int(10)     | YES  |     | NULL    |                |
+-------+-------------+------+-----+---------+----------------+
    注:上面四个语句打印结果都一样。

    NULL字段指示是否NULL可以被存储在列中。
    Key字段指示是否该列已编制索引。PRI的值指示该列是表的主键的一部分。UNI指示,该列是UNIQUE索引的一部分。MUL值指示,在列中某个给定值多次出现是允许的。
    Extra字段包含可以获取的与给定列有关的附加信息。在我们的例子中,Extra字段指示,Id列使用AUTO_INCREMENT关键词创建。

mysql> describe utility_table 'c1%';
+-------+---------+------+-----+---------+----------------+
| Field | Type    | Null | Key | Default | Extra          |
+-------+---------+------+-----+---------+----------------+
| c1    | int(11) | NO   | PRI | NULL    | auto_increment |
+-------+---------+------+-----+---------+----------------+

mysql> describe select c1,c2,c3 from utility_table where c1 < 3;
+----+-------------+---------------+-------+---------------+---------+---------+------+------+-------------+
| id | select_type | table         | type  | possible_keys | key     | key_len | ref  | rows | Extra       |
+----+-------------+---------------+-------+---------------+---------+---------+------+------+-------------+
|  1 | SIMPLE      | utility_table | range | PRIMARY       | PRIMARY | 4       | NULL |    2 | Using where |
+----+-------------+---------------+-------+---------------+---------+---------+------+------+-------------+

mysql> describe extended select c1,c2,c3 from utility_table where c1 < 3 \G;
+----+-------------+---------------+-------+---------------+---------+---------+------+------+----------+-------------+
| id | select_type | table         | type  | possible_keys | key     | key_len | ref  | rows | filtered | Extra       |
+----+-------------+---------------+-------+---------------+---------+---------+------+------+----------+-------------+
|  1 | SIMPLE      | utility_table | range | PRIMARY       | PRIMARY | 4       | NULL |    2 |   100.00 | Using where |
+----+-------------+---------------+-------+---------------+---------+---------+------+------+----------+-------------+

mysql> describe partitions select c1,c2,c3 from utility_table where c1 < 3;
+----+-------------+---------------+------------+-------+---------------+---------+---------+------+------+-------------+
| id | select_type | table         | partitions | type  | possible_keys | key     | key_len | ref  | rows | Extra       |
+----+-------------+---------------+------------+-------+---------------+---------+---------+------+------+-------------+
|  1 | SIMPLE      | utility_table | NULL       | range | PRIMARY       | PRIMARY | 4       | NULL |    2 | Using where |
+----+-------------+---------------+------------+-------+---------------+---------+---------+------+------+-------------+
    注:mysql5.6之前explain只可以用在select上,5.6及其以后可以使用SELECT, DELETE, INSERT, REPLACE, 和UPDATE。

            EXPLAIN EXTENDED:可获取额外的执行计划信息。
            EXPLAIN PARTITIONS:对分区表语句的检查非常有用。

3 HELP语句

    显示关于可能用到的命令的信息,它是mysql里面最有用的一个命令,有了它就不用担心记不清语法了。

    基本语法:

HELP 'search_string'

    实例:

mysql> HELP 'contents'
You asked for help about help category: "Contents"
For more information, type 'help <item>', where <item> is one of the following
categories:
   Account Management
   Administration
   Compound Statements
   Data Definition
   Data Manipulation
   Data Types
   Functions
   Functions and Modifiers for Use with GROUP BY
   Geographic Features
   Help Metadata
   Language Structure
   Plugins
   Procedures
   Table Maintenance
   Transactions
   User-Defined Functions
   Utility
    这个语句列出了所有MySQL使用的类别。

    当然,我们还可以查看很多,例如:

HELP 'data types'
HELP 'ascii'
HELP 'create table'
HELP 'status'
HELP 'functions'
HELP 'change master'

4 USE语句
    用来选择数据库。

    基本语法:

USE db_name

    实例:

use test;
    选择test数据库。


****************************************************************************************
    原文地址:http://blog.csdn.net/jesseyoung/article/details/38257727

    博客主页:http://blog.csdn.net/jesseyoung
****************************************************************************************


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值