官方数据支撑
数据库概述
MySQL图解应用 :
数据库相关概念
数据库:DataBase (DB)
- 按照一定格式存储数据的一些文件的组合
- 存储数据的仓库,数据文件,日志文件。具有一定特定格式的数据。
数据库管理系统:DataBase Management (DBMS)
- 专门用来管理数据库中的数据的,数据库管理系统可以对数据当中的数据进行增删改查
SQL :结构化查询语言
- 使用DBMS负责执行SQL语句,来完成数据库中的增删改查。
- SQL是一套标准语言,主要学习SQL语句。SQL语句可以在MySQL、Oracle、DB2中使用。
三者之间的关系:
DBMS ----执行-----> SQL -------操作----> DB
使用MySQL
MySQL必知必会~~~~
- 命令输入在mysql>之后;
- 命令用;或\g结束,换句话说,仅按Enter不执行命令;
- 输入help或\h获得帮助,也可以输入更多的文本获得特定命令的
- 帮助(如,输入help select获得使用SELECT语句的帮助);
- 输入quit或exit退出命令行实用程序。
连接
- 主机:如果连接到本地MySQL服务器,为localhost
- 端口:(如果使用默认端口3306之外的端口
- 用户名
- 密码
本机连接:
mysql -u root -p
C:\Users\yang>mysql -u root -p Enter password: **** Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 382 Server version: 5.7.37-log MySQL Community Server (GPL) Copyright (c) 2000, 2022, Oracle and/or its affiliates. Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. mysql>
远程数据库连接:
mysql -h mysql服务器的IP地址 -P 端口号(通常为3306) -u 用户名 -p密码
mysql -h mysql服务器的IP地址 -P 端口号(通常为3306) -u 用户名 -p密码 -h: mysql服务器的IP地址 -P: 大写的P选项表示端口号,端口号默认为3306,可省略 -u: 用户名 -p: 小写的p表示密码,当-p后输入密码时,会直接登陆。当-p后不输入密码时,会要求输入密码,但密码不显示 markdown
C:\Users\yang>mysql -h ************* -P *** -u ************* -p Enter password: ************* Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. mysql>
查看数据库
show databases;
mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | cloud | | db_admin3 | | db_membership | | hmdp | | jpa | | learningplat | | lovefinder | | mysql | | performance_schema | | recruit | | report | | shrio | | sys | | yupao | | yupi | +--------------------+ 16 rows in set (0.00 sec)
选择数据库
use learningplat;
mysql> use learningplat; Database changed
查看表
show tables;
mysql> show tables; +------------------------+ | Tables_in_learningplat | +------------------------+ | mistakerecord | | post | | questions | | user | +------------------------+ 4 rows in set (0.00 sec)
查看列
show columns from TABLE;
DESCRIBE TABLE;
MySQL支持用DESCRIBE作为SHOW COLUMNS FROM的一种快捷方式。换句话说,DESCRIBE user;是SHOW COLUMNS FROM user;的一种快捷方式。
其他show:
- SHOW STATUS,用于显示广泛的服务器状态信息;
- SHOW CREATE DATABASE和SHOW CREATE TABLE,分别用来显示创建特定数据库或表的MySQL语句;
- SHOW GRANTS,用来显示授予用户(所有用户或特定用户)的安全权限;
- SHOW ERRORS和SHOW WARNINGS,用来显示服务器错误或警告消息。
检索数据
检索单列
mysql> select username from user; +------------+ | username | +------------+ | tom | | pom6mqpkin | +------------+ 2 rows in set (0.00 sec)
🎃如果没有明确排序查询结果,则返回的数据的顺序没有特殊意义。返回数据的顺序可能是数据被添加到表中的顺序,也可能不是。只要返回相同数目的行,就是正常的。
- 多条SQL语句必须以分号(;)分隔。单条不需要,命令行必须分号分隔。
- SQL语句不区分大小写。
检索多列
mysql> select id,username from user; +----+------------+ | id | username | +----+------------+ | 3 | tom | | 7 | pom6mqpkin | +----+------------+ 2 rows in set (0.00 sec)
检索所有的列
mysql> select * from user;
⚠️一般,除非你确实需要表中的每个列,否则最好别使用*通配符。虽然使用通配符可能会使你自己省事,不用明确列出所需列,但检索不需要的列通常会降低检索和应用程序的性能。
去重
☀️上面都是用的我自己的数据库,然后我把官方的数据下载了。数据放最上面啦。
mysql> select vend_id from products; +---------+ | vend_id | +---------+ | 1001 | | 1001 | | 1001 | | 1002 | | 1002 | | 1003 | | 1003 | | 1003 | | 1003 | | 1003 | | 1003 | | 1003 | | 1005 | | 1005 | +---------+ 14 rows in set (0.00 sec)
使用distinct去重:
mysql> select distinct vend_id from products; +---------+ | vend_id | +---------+ | 1001 | | 1002 | | 1003 | | 1005 | +---------+ 4 rows in set (0.00 sec)
🔔DISTINCT关键字应用于所有列而不仅是前置它的列。
如果给出SELECT DISTINCT vend_id, prod_name,除非指定的两个列都相同,否则所有行都将被检索出来。
mysql> select distinct vend_id,prod_name from products; +---------+----------------+ | vend_id | prod_name | +---------+----------------+ | 1001 | .5 ton anvil | | 1001 | 1 ton anvil | | 1001 | 2 ton anvil | | 1003 | Detonator | | 1003 | Bird seed | | 1003 | Carrots | | 1002 | Fuses | | 1005 | JetPack 1000 | | 1005 | JetPack 2000 | | 1002 | Oil can | | 1003 | Safe | | 1003 | Sling | | 1003 | TNT (1 stick) | | 1003 | TNT (5 sticks) | +---------+----------------+ 14 rows in set (0.00 sec)
限制结果
显示前三条数据
mysql> select * from products limit 3; +---------+---------+--------------+------------+----------------------------------------------------------------+ | prod_id | vend_id | prod_name | prod_price | prod_desc | +---------+---------+--------------+------------+----------------------------------------------------------------+ | ANV01 | 1001 | .5 ton anvil | 5.99 | .5 ton anvil, black, complete with handy hook | | ANV02 | 1001 | 1 ton anvil | 9.99 | 1 ton anvil, black, complete with handy hook and carrying case | | ANV03 | 1001 | 2 ton anvil | 14.99 | 2 ton anvil, black, complete with handy hook and carrying case | +---------+---------+--------------+------------+----------------------------------------------------------------+
忽略前3条,展示5条数据
mysql> select * from products limit 3,5; +---------+---------+--------------+------------+-------------------------------------------------+ | prod_id | vend_id | prod_name | prod_price | prod_desc | +---------+---------+--------------+------------+-------------------------------------------------+ | DTNTR | 1003 | Detonator | 13.00 | Detonator (plunger powered), fuses not included | | FB | 1003 | Bird seed | 10.00 | Large bag (suitable for road runners) | | FC | 1003 | Carrots | 2.50 | Carrots (rabbit hunting season only) | | FU1 | 1002 | Fuses | 3.42 | 1 dozen, extra long | | JP1000 | 1005 | JetPack 1000 | 35.00 | JetPack 1000, intended for single use | +---------+---------+--------------+------------+-------------------------------------------------+ 5 rows in set (0.00 sec)
使用限定名
select 表.列 from 库.表;
mysql> select products.prod_name -> from mysql_test.products; +----------------+ | prod_name | +----------------+ | .5 ton anvil | | 1 ton anvil | | 2 ton anvil | | Detonator | | Bird seed | | Carrots | | Fuses | | JetPack 1000 | | JetPack 2000 | | Oil can | | Safe | | Sling | | TNT (1 stick) | | TNT (5 sticks) | +----------------+ 14 rows in set (0.00 sec)
排序
单个排列
使用order by排序,默认升序
升序 ASC 降序 DESC
mysql> select prod_name from products order by prod_name; +----------------+ | prod_name | +----------------+ | .5 ton anvil | | 1 ton anvil | | 2 ton anvil | | Bird seed | | Carrots | | Detonator | | Fuses | | JetPack 1000 | | JetPack 2000 | | Oil can | | Safe | | Sling | | TNT (1 stick) | | TNT (5 sticks) | +----------------+ 14 rows in set (0.00 sec)
降序排列
mysql> select prod_name from products order by prod_name DESC; +----------------+ | prod_name | +----------------+ | TNT (5 sticks) | | TNT (1 stick) | | Sling | | Safe | | Oil can | | JetPack 2000 | | JetPack 1000 | | Fuses | | Detonator | | Carrots | | Bird seed | | 2 ton anvil | | 1 ton anvil | | .5 ton anvil | +----------------+ 14 rows in set (0.00 sec)
多个排列
首先按价格,然后再按名称排序。仅在多个行具有相同的prod_price值时才对产品按prod_name进行排序。
mysql> select prod_id,prod_price,prod_name from products order by prod_price,prod_name; +---------+------------+----------------+ | prod_id | prod_price | prod_name | +---------+------------+----------------+ | FC | 2.50 | Carrots | | TNT1 | 2.50 | TNT (1 stick) | | FU1 | 3.42 | Fuses | | SLING | 4.49 | Sling | | ANV01 | 5.99 | .5 ton anvil | | OL1 | 8.99 | Oil can | | ANV02 | 9.99 | 1 ton anvil | | FB | 10.00 | Bird seed | | TNT2 | 10.00 | TNT (5 sticks) | | DTNTR | 13.00 | Detonator | | ANV03 | 14.99 | 2 ton anvil | | JP1000 | 35.00 | JetPack 1000 | | SAFE | 50.00 | Safe | | JP2000 | 55.00 | JetPack 2000 | +---------+------------+----------------+ 14 rows in set (0.00 sec)
where数据过滤
匹配检查
操作符 | 说明 |
= | 等于 |
<> | 不等于 |
!= | 不能与 |
< | 小于 |
<= | 小于等于 |
大于 | |
>= | 大于等于 |
BETWEEN | 在指定值之间 |
mysql> select prod_id,prod_price,prod_name from products where prod_price between 5 and 10 ; +---------+------------+----------------+ | prod_id | prod_price | prod_name | +---------+------------+----------------+ | ANV01 | 5.99 | .5 ton anvil | | ANV02 | 9.99 | 1 ton anvil | | FB | 10.00 | Bird seed | | OL1 | 8.99 | Oil can | | TNT2 | 10.00 | TNT (5 sticks) | +---------+------------+----------------+ 5 rows in set (0.00 sec)