MySQL 是 RDBMS(Relational Database Management System:关系数据库管理系统)应用软件之一,由瑞典 MySQL AB 公司开发,总部位于瑞典。目前属于 Oracle 公司。
MySQL 有两个版本:企业版(收费)、社区版。
MySQL 的优点
net start 服务名
net stop 服务名
DOS命令行
mysql -h localhost -P 3306 -u root -p root
-h 是连接主机名,localhost是本机连接(可省略)
-P 大写P是端口号Port(可省略)
-u 是连接用户user
-p 小写p是密码password(注意:-p和密码之间不能有空格)
注意:
如果找不到mysql的命令,看一下环境变量有没有配置
C:\Users\****>mysql --version
mysql Ver 14.14 Distrib 5.7.17, for Win64 (x86_64)
C:\Users\****>mysql -V
mysql Ver 14.14 Distrib 5.7.17, for Win64 (x86_64)
mysql> select version();
+-----------+
| version() |
+-----------+
| 5.7.17 |
+-----------+
1 row in set (0.01 sec)
查看 MySQL 数据库管理系统的数据库列表。
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
4 rows in set (0.00 sec)
infoemation_schema:保存元数据信息
mysql:保存用户信息
performance_schema:收集性能参数信息
选择/使用要操作的 MySQL 数据库
mysql> use test; # test 是数据库的名称
Database changed
查看当前所使用的数据库
mysql> select database();
+------------+
| database() |
+------------+
| test |
+------------+
1 row in set (0.00 sec)
查看数据库中的表
查看当前数据库中的表,使用该命令前需要使用 use 命令来选择要操作的数据库。
mysql> show tables;
+-----------------+
| Tables_in_girls |
+-----------------+
| admin |
| beauty |
| boys |
| stuinfo |
+-----------------+
4 rows in set (0.00 sec)
查看指定数据库中的表
mysql> show tables from girls; # girls是指定数据表的名称
+-----------------+
| Tables_in_girls |
+-----------------+
| admin |
| beauty |
| boys |
| stuinfo |
+-----------------+
4 rows in set (0.00 sec)
查看指定数据表的结构
desc 数据表;
show columns from 数据表;
mysql> desc boys; # boys是数据表的名称
+---------+-------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+---------+-------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| boyName | varchar(20) | YES | | NULL | |
| userCP | int(11) | YES | | NULL | |
+---------+-------------+------+-----+---------+----------------+
3 rows in set (0.00 sec)
mysql> show columns from boys; # boys是数据表的名称
+---------+-------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+---------+-------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| boyName | varchar(20) | YES | | NULL | |
| userCP | int(11) | YES | | NULL | |
+---------+-------------+------+-----+---------+----------------+
3 rows in set (0.00 sec)
查看数据表的详细索引信息,包括 PRIMARY KEY(主键)
mysql> show index from boys; # boys是数据表名
+-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment |
+-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| boys | 0 | PRIMARY | 1 | id | A | 4 | NULL | NULL | | BTREE | | |
+-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
1 row in set (0.00 sec)
查看 MySQL 数据库管理系统的性能及统计信息
mysql> show table status from girls; # 显示数据库 girls 中所有表的信息
+---------+--------+---------+------------+------+----------------+-------------+-----------------+--------------+-----------+----------------+---------------------+-------------+------------+-----------------+----------+----------------+---------+
| Name | Engine | Version | Row_format | Rows | Avg_row_length | Data_length | Max_data_length | Index_length | Data_free | Auto_increment | Create_time | Update_time | Check_time | Collation | Checksum | Create_options | Comment |
+---------+--------+---------+------------+------+----------------+-------------+-----------------+--------------+-----------+----------------+---------------------+-------------+------------+-----------------+----------+----------------+---------+
| admin | InnoDB | 10 | Dynamic | 2 | 8192 | 16384 | 0 | 0 | 0 | 3 | 2020-02-09 17:35:33 | NULL | NULL | utf8_general_ci | NULL | | |
| beauty | InnoDB | 10 | Dynamic | 12 | 1365 | 16384 | 0 | 0 | 0 | 13 | 2020-02-09 17:35:33 | NULL | NULL | utf8_general_ci | NULL | | |
| boys | InnoDB | 10 | Dynamic | 4 | 4096 | 16384 | 0 | 0 | 0 | 5 | 2020-02-09 17:35:33 | NULL | NULL | utf8_general_ci | NULL | | |
| stuinfo | InnoDB | 10 | Dynamic | 2 | 8192 | 16384 | 0 | 0 | 0 | NULL | 2020-07-27 22:51:31 | NULL | NULL | utf8_general_ci | NULL | | |
+---------+--------+---------+------------+------+----------------+-------------+-----------------+--------------+-----------+----------------+---------------------+-------------+------------+-----------------+----------+----------------+---------+
4 rows in set (0.02 sec)
mysql> show table status from girls like 'b%'; # 表名以b开头的表的信息
+--------+--------+---------+------------+------+----------------+-------------+-----------------+--------------+-----------+----------------+---------------------+-------------+------------+-----------------+----------+----------------+---------+
| Name | Engine | Version | Row_format | Rows | Avg_row_length | Data_length | Max_data_length | Index_length | Data_free | Auto_increment | Create_time | Update_time | Check_time | Collation | Checksum | Create_options | Comment |
+--------+--------+---------+------------+------+----------------+-------------+-----------------+--------------+-----------+----------------+---------------------+-------------+------------+-----------------+----------+----------------+---------+
| beauty | InnoDB | 10 | Dynamic | 12 | 1365 | 16384 | 0 | 0 | 0 | 13 | 2020-02-09 17:35:33 | NULL | NULL | utf8_general_ci | NULL | | |
| boys | InnoDB | 10 | Dynamic | 4 | 4096 | 16384 | 0 | 0 | 0 | 5 | 2020-02-09 17:35:33 | NULL | NULL | utf8_general_ci | NULL | | |
+--------+--------+---------+------------+------+----------------+-------------+-----------------+--------------+-----------+----------------+---------------------+-------------+------------+-----------------+----------+----------------+---------+
2 rows in set (0.00 sec)
mysql> show table status from girls like 'b%'\G
*************************** 1. row ***************************
Name: beauty
Engine: InnoDB
Version: 10
Row_format: Dynamic
Rows: 12
Avg_row_length: 1365
Data_length: 16384
Max_data_length: 0
Index_length: 0
Data_free: 0
Auto_increment: 13
Create_time: 2020-02-09 17:35:33
Update_time: NULL
Check_time: NULL
Collation: utf8_general_ci
Checksum: NULL
Create_options:
Comment:
*************************** 2. row ***************************
Name: boys
Engine: InnoDB
Version: 10
Row_format: Dynamic
Rows: 4
Avg_row_length: 4096
Data_length: 16384
Max_data_length: 0
Index_length: 0
Data_free: 0
Auto_increment: 5
Create_time: 2020-02-09 17:35:33
Update_time: NULL
Check_time: NULL
Collation: utf8_general_ci
Checksum: NULL
Create_options:
Comment:
2 rows in set (0.00 sec)
'\g'
相当与分号 ';'
'\G'
相当与 ';'
加 查询结果按列打印(即,将查询到的结构旋转90度变为纵向)# 文字
-- 文字
--
和注释的文字间要有一个空格/* 文字 */