数据库服务基础应用操作
数据库语句应用实践
1)DCL和数据库安全有关的操作
- grant/revoke # 和授权有关操作
- create use/alter user/drop user # 和用户相关操作
- commit 可以确保DML语句操作后,产生的数据信息合理存储到磁盘中
- rollback 可以实现DML语句操作后,将改动调整的数据做回退操作
- DML- insert update delete 可以保证存储语句的执行成功
复制代码 2)DQL可以实现查询数据库数据信息
查询情况1
- DQL 可以实现查询数据库数据信息
- show select
- 查询情况1:数据库中变量信息查询(变量--配置项信息/监控信息)######################################################
- show variables like 'XXX'; 查看配置项变量信息
- show global status like 'xxx'; 查看监控项变量信息
- select @@max_connections; -- 查看具体某个变量信息
- 扩展说明:数据库配置项变量如何改动
- 两种方式可以改动配置项
- 方式一:永久改动
- max_connections 设置数据库最大并发连接数
- 在vim /etc/my.cnf中更改
- [mysqld]
- max_connections 永久修改配置需要重启数据库服务
- 方式二:临时改动
- set global max_connextions=3; 配置会影响全部用户功能/当会话断开重新连接配置不会失效(全局改动)
- set session sql_log_bin='off'; 配置只会影响当前用户/当会话断开重新连接配置会失效(会话改动)
复制代码 查询情况2
- 查询情况2:可以查看具体数据或对象信息########################################################################
- show 查看表或库信息或用户或权限或索引...
- select 查看具体数据信息
- 单表数据信息查看
- -- 创建单表查看测试数据:
- https://dev.mysql.com/doc/index-other.html
- 加载测试数据-导入数据表到数据库中进行操作
- mysql -uroot -p66666 -S /tmp/mysql.sock <world.sql
- mysql> show databases;
- +--------------------+
- | Database |
- +--------------------+
- | world |
- mysql> desc city;
- +-------------+----------+------+-----+---------+----------------+
- | Field | Type | Null | Key | Default | Extra |
- +-------------+----------+------+-----+---------+----------------+
- | ID | int | NO | PRI | NULL | auto_increment |
- | Name | char(35) | NO | | | |
- | CountryCode | char(3) | NO | MUL | | |
- | District | char(20) | NO | | | |
- | Population | int | NO | | 0 | |
- +-------------+----------+------+-----+---------+----------------+
复制代码 附加:如何导入数据表到mysql中
- 1.把world.sql移动到数据库安装目录下
- [root@mysql local]#ll # 当前在/usr/local
- total 603964
- ............
- -rw-r--r-- 1 root root 398629 Mar 1 07:05 world.sql⭐
- ............
- 2.使用命令导入world.sql数据表
- [root@mysql local]#mysql -uroot -p666666 -S /tmp/mysql.sock <world.sql
- mysql: [Warning] Using a password on the command line interface can be insecure.
- # 用管理员身份导入world.sql
- 3.进入数据库查看
- [root@mysql local]#mysql -p666666
- mysql: [Warning] Using a password on the command line interface can be insecure.
- Welcome to the MySQL monitor. Commands end with ; or \g.
- Your MySQL connection id is 30
- Server version: 8.0.36 MySQL Community Server - GPL
- Copyright (c) 2000, 2024, 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> show databases; # 展示所有数据库,发现world数据库
- +--------------------+
- | Database |
- +--------------------+
- | information_schema |
- | mysql |
- | oldboy |
- | performance_schema |
- | sys |
- | world |
- +--------------------+
- 6 rows in set (0.00 sec)
- mysql> use world; # 使用world数据库
- Reading table information for completion of table and column names
- You can turn off this feature to get a quicker startup with -A
- Database changed
- mysql> show tables; # 查看所有数据表
- +-----------------+
- | Tables_in_world |
- +-----------------+
- | city |
- | country |
- | countrylanguage |
- +-----------------+
- 3 rows in set (0.00 sec)
- world.sql导入成功
- 可以进行操作来查看具体内容,比如数据表结构、数据表内容
- mysql> desc course; # 查看数据表结构
- +-------+-------------+------+-----+---------+-------+
- | Field | Type | Null | Key | Default | Extra |
- +-------+-------------+------+-----+---------+-------+
- | cno | int | NO | PRI | NULL | |
- | cname | varchar(20) | NO | | NULL | |
- | tno | int | NO | | NULL | |
- +-------+-------------+------+-----+---------+-------+
- 3 rows in set (0.00 sec)
- mysql> select * from course; # 查看数据表内容
- +------+--------+-----+
- | cno | cname | tno |
- +------+--------+-----+
- | 1001 | linux | 101 |
- | 1002 | python | 102 |
- | 1003 | mysql | 103 |
- | 1004 | go | 105 |
- +------+--------+-----+
- 4 rows in set (0.00 sec)
复制代码 单表数据查看练习(基础查看)
1.单表所有数据查看
- 1.单表所有数据查看
- select * from 库名.表名;
- select 列名01,列表02.. from 库名.表名;
- 备注:以上表中全部数据查看操作,尽量少用
复制代码 2.单表数据过滤查看 (where)⭐
- 2.单表数据过滤查看 (where)
- 1)等值条件查看数据信息####################################################################################
- 查看北京城市人口情况;
- mysql> show databases; # 查看所有数据库
- +--------------------+
- | Database |
- +--------------------+
- | information_schema |
- | mysql |
- | oldboy |
- | performance_schema |
- | school |
- | sys |
- | world |
- +--------------------+
- 7 rows in set (0.02 sec)
- mysql> use world; # 使用我们需要的数据库
- Reading table information for completion of table and column names
- You can turn off this feature to get a quicker startup with -A
- Database changed
- mysql> show tables; # 展示所有数据表
- +-----------------+
- | Tables_in_world |
- +-----------------+
- | city |
- | country |
- | countrylanguage |
- +-----------------+
- 3 rows in set (0.00 sec)
- mysql> desc city; # 查看city数据表的结构
- +-------------+----------+------+-----+---------+----------------+
- | Field | Type | Null | Key | Default | Extra |
- +-------------+----------+------+-----+---------+----------------+
- | ID | int | NO | PRI | NULL | auto_increment |
- | Name | char(35) | NO | | | |
- | CountryCode | char(3) | NO | MUL | | |
- | District | char(20) | NO | | | |
- | Population | int | NO | | 0 | |
- +-------------+----------+------+-----+---------+----------------+
- 5 rows in set (0.00 sec)
- mysql> select * from city; # 查看city数据表的内容
- +------+------------------------------------+-------------+------------------------+------------+
- | ID | Name | CountryCode | District | Population |
- +------+------------------------------------+-------------+------------------------+------------+
- | 1 | Kabul | AFG | Kabol | 1780000 |
- | 2 | Qandahar | AFG | Qandahar | 237500 |
- | 3 | Herat | AFG | Herat | 186800 |
- | 4 | Mazar-e-Sharif | AFG | Balkh | 127800 |
- | 5 | Amsterdam | NLD | Noord-Holland | 731200 |
- | 6 | Rotterdam | NLD | Zuid-Holland | 593321 |
- | 7 | Haag | NLD | Zuid-Holland | 440900 |
- | 8 | Utrecht | NLD | Utrecht | 234323 |
- ............
- #可以得出name是城市名,countrycode是国家,district是省份,population是人口
- mysql> select name,population from city where name='peking';
- +--------+------------+
- | name | population |
- +--------+------------+
- | Peking | 7472000 |
- +--------+------------+
- 1 row in set (0.01 sec)
- 2)范围条件查看数据信息####################################################################################
- ①查看人口数量大于1000w城市和国家信息
- mysql> select name,countrycode,population from city where population>10000000;
- +-----------------+-------------+------------+
- | name | countrycode | population |
- +-----------------+-------------+------------+
- | Mumbai (Bombay) | IND | 10500000 |
- +-----------------+-------------+------------+
- # 数值做范围查看 > < >= <= <>/!= ⭐⭐⭐
- ②查看北京上海深圳重庆等一线城市人口数量
- mysql> select name,population from city where name in ('peking','shanghai','shenzhen','chongqing');
- +-----------+------------+
- | name | population |
- +-----------+------------+
- | Shanghai | 9696300 |
- | Peking | 7472000 |
- | Chongqing | 6351600 |
- | Shenzhen | 950500 |
- +-----------+------------+
- # 字符信息做范围查看 in () / not in () ⭐⭐⭐
- 3) 多个条件列的逻辑关联查询################################################################################
- 多个条件逻辑关联方式
- and --- 将多个条件做交集处理(将过滤条件做逐步处理 得到需要的查询结果)
- or --- 将多个条件做并集处理(将过滤条件做分别处理 交结果进行合并显示)
- ①查看中国和美国所有城市以及人口数量情况
- mysql> select name,population from city where countrycode='chn' or countrycode='usa';
- ②查看城市人口数量大于100w并小于300w城市信息
- mysql> select name,population from city where population>1000000 and population<3000000;
- # 或者另一种表达方式--> and=between and
- mysql> select name,population from city where population between 1000000 and 3000000;
- 4) 条件列做模糊过滤查询####################################################################################
- 查询广州城市人口信息
- mysql> select * from city where countrycode='chn' and district like '%guang%' and name like '%guang%';
- +------+--------------------+-------------+-----------+------------+
- | ID | Name | CountryCode | District | Population |
- +------+--------------------+-------------+-----------+------------+
- | 1897 | Kanton [Guangzhou] | CHN | Guangdong | 4256300 |
- +------+--------------------+-------------+-----------+------------+
- 5) 取重查询数据信息#######################################################################################
- 获取中国有多少个省份
- select distinct District from city where countrycode='chn';
- select count(district) from city where countrycode='chn';
- # 统计省份一共有多少
- 6) 查看空值列信息#########################################################################################
- mysql> select * from city where population is null;
-
- 查看非空列信息
- mysql> select * from city where population is not null;
复制代码 单表数据查看练习(进阶查看)
1.分组聚合查看数据信息
①聚合查看数据
- ①聚合查看数据 # 查询数据需要做统计分析 实现聚合查看数据需要应用聚合函数
- 1.查看中国人口总数
- select countrycode,sum(population) from city where countrycode='chn';
- +-------------+-----------------+
- | countrycode | sum(population) |
- +-------------+-----------------+
- | CHN | 175953614 |
- +-------------+-----------------+
- 2.查看中国城市数量
- select countrycode,count(name) from city where countrycode='chn';
- +-------------+-------------+
- | countrycode | count(name) |
- +-------------+-------------+
- | CHN | 364 |
- +-------------+-------------+
- 3.查看中国人口最多的城市信息/查看中国人口最少的城市信息
- mysql> select countrycode,max(population) from city where countrycode='chn';
- +-------------+-----------------+
- | countrycode | max(population) |
- +-------------+-----------------+
- | CHN | 9696300 |
- +-------------+-----------------+
- 1 row in set (0.05 sec)
- mysql> select countrycode,min(population) from city where countrycode='chn';
- +-------------+-----------------+
- | countrycode | min(population) |
- +-------------+-----------------+
- | CHN | 89288 |
- +-------------+-----------------+
- avg() -- 取某个数值列的平均数
- max() -- 取某个数值列的最大值
- min() -- 取某个数值列的最小值
- -- 直接查看列的最大值或最小值
- 额外:显示查看中国人口最多的城市信息,显示人口信息与城市名字
- SELECT name,countrycode,population
- FROM city
- WHERE countrycode = 'chn'
- ORDER BY population DESC
- LIMIT 1;
- +----------+-------------+------------+
- | name | countrycode | population |
- +----------+-------------+------------+
- | Shanghai | CHN | 9696300 |
- +----------+-------------+------------+
- 执行过程(精讲版):
- 我们可以把它拆解成五个简单的步骤来理解:
- 1. FROM city
- 第一步:去哪个表里找?
- 告诉数据库:“请打开名为 city 的那张表(里面存着所有城市的数据)。”
- 2. WHERE countrycode = 'chn'
- 第二步:筛选哪些行?
- 告诉数据库:“在这张表里,我只关心 countrycode(国家代码) 等于 'chn' (中国)的那些行。其他的国家(如 'usa', 'jpn')全部忽略。”
- 此时,手里剩下的全是中国的城市数据。
- 3. ORDER BY population DESC
- 第三步:怎么排队?
- 告诉数据库:“把刚才筛选出来的中国城市,按照 population(人口) 这一列进行排序。”
- DESC 是 Descend(下降)的缩写,意思是从大到小排。
- 此时,人口最多的城市排在了第 1 位,人口最少的排在了最后。
- # 升序用ORDER BY population,降序用ORDER BY population DESC
- 4. LIMIT 1
- 第四步:只要几个?
- 告诉数据库:“排好队后,我只要第 1 个(也就是排在最前面、人口最多的那一个),后面的都不要了。”
- 如果没有这句话,数据库会把所有中国城市按人口从大到小全部列出来。
- 5. SELECT name, countrycode, population
- 第五步:给我看什么?
- 告诉数据库:“对于最后留下的这唯一一行数据,我只需要看这三列信息:name(城市名)、countrycode(国家代码) 和 population(人口数)。其他的信息(比如所属省份 District、城市 ID 等)不用显示给我。”
复制代码 ②分组查看数据⭐
- ②分组查看数据 # 表示将某个列相同信息分为一组,然后将分组后数据分别做分析处理
- 1.查看全球范围内,每个国家的人口总数
- select countrycode,sum(population) from city group by countrycode;
- 2.查看中国境内,每个省份的人口总数
- select district,sum(population) from city where countrycode='CHN' group by district;
- 知识扩展说明:分组聚合查看数据常见问题
- 3.查看中国境内,每个省份的城市数量,以及显示省份的城市信息
- select district,count(name),name from city where countrycode='CHN' group by district;
- ERROR 1140 (42000):
- In aggregated query without GROUP BY,
- expression #2 of SELECT list contains nonaggregated column 'world.city.Name';
- this is incompatible with sql_mode=only_full_group_by
- 在聚合查看数据时,需要使用group by做分组操作
- 在分组聚合处理输出信息时,某些非聚合处理列,无法正常显示内容
- sql_mode=only_full_group_by 无法实现数据表中一行信息对多行信息关联显示
- 分组聚合查询原理:
- 1)会先根据分组列信息,做排序处理
- 2)会将分组后的相同数据列信息合并成一行
- 3)会将需要分析统计列做对应聚合处理
- 解决以上分组聚合输出问题,可以将多行信息转换成一行
- select district,count(name),group_concat(name) from city where countrycode='CHN' group by district;
- 命令的执行过程:
- 1. GROUP BY district —— 先分类
- 动作:把全中国的城市,按照 district(省份/地区)分成不同的堆。
- 结果:比如“广东”一堆,“江苏”一堆,“北京”一堆……
- 2. COUNT(name) —— 数个数
- 动作:在每一堆里,数一数有多少个城市名字。
- 结果:得到该地区的城市数量。
- 例如:广东 -> 50个,江苏 -> 40个。
- 3. GROUP_CONCAT(name) —— 连名字
- 动作:把每一堆里的所有城市名字,用逗号 , 连接成一个长长的字符串。
- 结果:得到该地区的城市名单列表。
- 例如:广东 -> "广州,深圳,珠海,佛山..."
- # 分行显示
- select district,count(name),group_concat(name) from city where countrycode='CHN' group by district\G;
复制代码 2.分组聚合过滤查看数据信息 -- having⭐
- 2)分组聚合过滤查看数据信息 -- having
- 查看全球范围内,每个国家的人口总数,将人口总数大于5000w国家信息显示出来
- mysql> select countrycode,sum(population)
- -> from city
- -> group by countrycode
- -> having sum(population)>50000000
- -> ;
- +-------------+-----------------+
- | countrycode | sum(population) |
- +-------------+-----------------+
- | BRA | 85876862 |
- | CHN | 175953614 |
- | IND | 123298526 |
- | JPN | 77965107 |
- | MEX | 59752521 |
- | RUS | 69150700 |
- | USA | 78625774 |
- +-------------+-----------------+
- having -- 会将聚合列作为条件过滤 (统计分析的数据做过滤)
- where -- 会将现有数据列作为条件过滤 (表中有的数据做过滤)
复制代码 3.数据信息排序查询
- 3)数据信息排序查询
- 查看全球范围内,每个国家的人口总数,将人口总数大于5000w国家信息显示出来,并排序显示输出
- mysql> select countrycode,sum(population)
- -> from city
- -> group by countrycode
- -> having sum(population)>50000000
- -> order by sum(population)
- -> ;
- +-------------+-----------------+
- | countrycode | sum(population) |
- +-------------+-----------------+
- | MEX | 59752521 |
- | RUS | 69150700 |
- | JPN | 77965107 |
- | USA | 78625774 |
- | BRA | 85876862 |
- | IND | 123298526 |
- | CHN | 175953614 |
- +-------------+-----------------+
- -- order by默认排序为升序排序⭐
- 查看全球范围内,每个国家的人口总数,将人口总数大于5000w国家信息显示出来,并按降序显示输出
- mysql> select countrycode,sum(population)
- -> from city
- -> group by countrycode
- -> having sum(population)>50000000
- -> order by sum(population) DESC
- -> ;
- +-------------+-----------------+
- | countrycode | sum(population) |
- +-------------+-----------------+
- | CHN | 175953614 |
- | IND | 123298526 |
- | BRA | 85876862 |
- | USA | 78625774 |
- | JPN | 77965107 |
- | RUS | 69150700 |
- | MEX | 59752521 |
- +-------------+-----------------+
复制代码 4.截取部分输出信息显示
- 4) 截取部分输出信息显示
- 显示全球人口总数大于5000w的前3名
- mysql> select countrycode,sum(population)
- -> from city
- -> group by countrycode
- -> having sum(population)>50000000
- -> order by sum(population) DESC
- -> limit 3
- -> ;
- +-------------+-----------------+
- | countrycode | sum(population) |
- +-------------+-----------------+
- | CHN | 175953614 |
- | IND | 123298526 |
- | BRA | 85876862 |
- +-------------+-----------------+
- 单表查询数据常用语句:
- select 输出列信息 from 库.表 where+group by+having+order by[desc]+limit;
复制代码 小知识点总结❗️
[code]1.order by排序的升序和降序升序用ORDER BY population降序用ORDER BY population DESC 即order by默认就是升序,降序只需要加一个DESC####################################################################2.sum()和count()的区别简单一句话总结:COUNT() 是数个数(有多少行?有多少个非空值?)。SUM() 是加总值(这些数字加起来是多少?)。问 “多少个?” (How many?) -> 用 COUNT。问 “总共多少?” (How much total?) -> 用 SUM。####################################################################3.where 和 havinghaving -- 会将聚合列作为条件过滤 (统计分析的数据做过滤) where -- 会将现有数据列作为条件过滤 (表中有的数据做过滤)使用where的时候要后group by分组,使用having的时候要先group by分组❗️where不能使用聚合函数,having可以使用聚合函数,如HAVING COUNT(*) > 5的表达方式####################################################################4.SQL的逻辑执行顺序①FROM (找表)②WHERE (
来源:程序园用户自行投稿发布,如果侵权,请联系站长删除
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作! |