📜  mysql 显示架构 - SQL (1)

📅  最后修改于: 2023-12-03 15:03:06.677000             🧑  作者: Mango

MySQL 显示架构 - SQL

在MySQL中,有很多方式可以显示数据库中的架构信息。本文将介绍一些主要的方法,并给出相应的SQL语句。

1. 查看数据库

使用SHOW DATABASES;可以列出所有的数据库名。

SHOW DATABASES;

结果如下:

+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| test_db            |
+--------------------+
2. 查看数据表

首先需要切换到相应的数据库。使用USE dbname;可以切换数据库。

USE test_db;

使用SHOW TABLES;可以列出数据库中所有的数据表。

SHOW TABLES;

结果如下:

+-----------------+
| Tables_in_test_db |
+-----------------+
| customers       |
| orders          |
+-----------------+
3. 查看数据表结构

使用DESC tablename;可以查看数据表的结构信息。

DESC customers;

结果如下:

+-------+--------------+------+-----+---------+-------+
| Field | Type         | Null | Key | Default | Extra |
+-------+--------------+------+-----+---------+-------+
| id    | int(11)      | NO   | PRI | NULL    |       |
| name  | varchar(255) | NO   |     | NULL    |       |
| email | varchar(255) | NO   |     | NULL    |       |
+-------+--------------+------+-----+---------+-------+
4. 查看索引

使用SHOW INDEXES FROM tablename;可以查看数据表中的索引信息。

SHOW INDEXES FROM orders;

结果如下:

+--------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| Table  | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment |
+--------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| orders |          0 | PRIMARY  |            1 | id          | A         |           6 |     NULL | NULL   |      | BTREE      |         |               |
+--------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
5. 查看外键

使用SHOW CREATE TABLE tablename;可以查看数据表的创建语句,包含外键信息。

SHOW CREATE TABLE orders;

结果如下:

| orders | CREATE TABLE `orders` (
  `id` int(11) NOT NULL,
  `customer_id` int(11) NOT NULL,
  `order_date` date NOT NULL,
  PRIMARY KEY (`id`),
  KEY `customer_id` (`customer_id`),
  CONSTRAINT `orders_ibfk_1` FOREIGN KEY (`customer_id`) REFERENCES `customers` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci |
6. 查看触发器

使用SHOW TRIGGERS;可以查看数据库中所有的触发器。

SHOW TRIGGERS;

结果如下:

+---------------+--------+-----------+----------------+----------------------+----------------------+----------------+----------------------+--------+------+----------------+-------------+-----------------+---------------------+---------------------+------------------+---------------+---------+------------+-------------------+----------+----------------+--------------------------------------------------------------------------------+
| Trigger       | Event  | Table     | Timing         | Created              | sql_mode             | Definer        | character_set_client | collation_connection | Database | Collation | Action         | Condition   | `Timing`_charset | `Event`_charset     | `Action`_charset    | `Definer`_charset | `character_set_client`_charset | `collation_connection`_charset | `Database`_charset | `Collation`_charset | `Action`_length`charset | `Type` | `Definer`_charset | `SQL Original Statement`                                                        |
+---------------+--------+-----------+----------------+----------------------+----------------------+----------------+----------------------+--------+------+----------------+-------------+-----------------+---------------------+---------------------+------------------+---------------+---------+------------+-------------------+----------+----------------+--------------------------------------------------------------------------------+
| order_count   | INSERT | customers | AFTER          | 2021-10-17 20:50:48 | STRICT_TRANS_TABLES | root@localhost | utf8mb4              | utf8mb4_general_ci   | test_db | utf8mb4 | UPDATE         | NULL        | utf8mb4         | utf8mb4_general_ci | utf8mb4_general_ci | utf8mb4_general_ci | utf8mb4                    | utf8mb4_general_ci            | utf8mb4            | utf8mb4_general_ci | utf8mb4_general_ci     | TRIGGER  | utf8mb4_general_ci | CREATE TRIGGER `order_count`                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               |
| orders_amount | INSERT | orders    | BEFORE         | 2021-10-17 20:52:42 | STRICT_TRANS_TABLES | root@localhost | utf8mb4              | utf8mb4_general_ci   | test_db | utf8mb4 | INSERT         | NULL        | utf8mb4         | utf8mb4_general_ci | utf8mb4_general_ci | utf8mb4_general_ci | utf8mb4                    | utf8mb4_general_ci            | utf8mb4            | utf8mb4_general_ci | utf8mb4_general_ci     | TRIGGER  | utf8mb4_general_ci | CREATE TRIGGER `orders_amount`                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             |
+---------------+--------+-----------+----------------+----------------------+----------------------+----------------+----------------------+--------+------+----------------+-------------+-----------------+---------------------+---------------------+------------------+---------------+---------+------------+-------------------+----------+----------------+--------------------------------------------------------------------------------+
总结

通过以上SQL语句,可以查看MySQL数据库的架构信息,包括数据库、数据表、索引、外键、触发器等。这些信息对于开发和维护MySQL应用程序非常重要。