【MySQL】常用 SQL 语句(Common SQL)- Display

Posted by 西维蜀黍 on 2022-01-27, Last Modified on 2023-09-20

显示DBs and Tables

mysql> show databases;
+----------------------------------+
| Database                         |
+----------------------------------+
| entry_task2_db                   |
| information_schema               |
| mysql                            |
| performance_schema               |
| sw_benchmark_test                |
| sw_benchmark_test_oltp_read_only |
| sw_test                          |
| sys                              |
+----------------------------------+

mysql> use sw_test;

mysql> SHOW TABLES;
+--------------------------------------------------+
| Tables_in_sw_test                                |
+--------------------------------------------------+
| logistic_promotion_seller_promo_budget_tab       |
| logistic_promotion_seller_promo_budget_usage_tab |
| logistic_promotion_seller_promo_channel_map_tab  |
| logistic_promotion_seller_promo_rule_map_tab     |
| logistic_promotion_seller_promo_tab              |
| seller_grouped_rule_budget_tab_00000004          |
| seller_grouped_rule_budget_usage_tab_00000005    |
| tb_person                                        |
| user                                             |
+--------------------------------------------------+
9 rows in set (0.00 sec)

显示 Table Schema

Ref

show create table <table_name>

mysql> show create table [db_name.]table_name;

e.g.,

mysql> show create table task2.auth_permission;
+-----------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Table           | Create Table                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     |
+-----------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| auth_permission | CREATE TABLE `auth_permission` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(255) NOT NULL,
  `content_type_id` int(11) NOT NULL,
  `codename` varchar(100) NOT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `auth_permission_content_type_id_codename_01ab375a_uniq` (`content_type_id`,`codename`),
  CONSTRAINT `auth_permission_content_type_id_2f476e4b_fk_django_co` FOREIGN KEY (`content_type_id`) REFERENCES `django_content_type` (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=40 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci |
+-----------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)

describe <table_name>

mysql> describe [db_name.]table_name;	

e.g.,

mysql> describe task2.auth_permission;
+-----------------+--------------+------+-----+---------+----------------+
| Field           | Type         | Null | Key | Default | Extra          |
+-----------------+--------------+------+-----+---------+----------------+
| id              | int(11)      | NO   | PRI | NULL    | auto_increment |
| name            | varchar(255) | NO   |     | NULL    |                |
| content_type_id | int(11)      | NO   | MUL | NULL    |                |
| codename        | varchar(100) | NO   |     | NULL    |                |
+-----------------+--------------+------+-----+---------+----------------+
4 rows in set (0.01 sec)

显示 Index

Ref

mysql> show index from seller_xxx_usage_tab_00000005;
+-----------------------------------------------+------------+--------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+---------+------------+
| Table                                         | Non_unique | Key_name     | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment | Visible | Expression |
+-----------------------------------------------+------------+--------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+---------+------------+
| seller_xxx_usage_tab_00000005 |          0 | PRIMARY      |            1 | id          | A         |           0 |     NULL |   NULL |      | BTREE      |         |               | YES     | NULL       |
| seller_xxx_usage_tab_00000005 |          1 | idx_order_id |            1 | order_id    | A         |           0 |     NULL |   NULL |      | BTREE      |         |               | YES     | NULL       |
+-----------------------------------------------+------------+--------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+---------+------------+
2 rows in set (0.00 sec)

Reference