文章目录
SQL语句的应用
一.DDL语句
1.介绍
数据定义语句:针对数据库各种资源的创建修改和删除
数据库的资源
- 用户
- 库
- 表
- 视图
- 存储过程
- 触发器
create
alter
drop
2.库的管理
创建库
create database 库名;
show databases;
# 查看库属性
show create database `库名`;
## 创建字符集为utf8 排序规则utf8_bin 库名test2
create database `test2` default character set utf8 collate utf8_bin;
修改库
alter database `test2` default character set latin1;
删库
drop database 库名;
库名要求
- 库名不能以数字开头
- 库名中不能有除__以外的特殊符号
- 库名中不能有空格
3.表的管理
创建表
CREATE TABLE `test2`.`vip list`(
`id` INT(8) NOT NULL AUTO_INCREMENT COMMENT '主键所在列',
`name` VARCHAR(10) NOT NULL DEFAULT '佚名',
`gender` ENUM('男','女','多元性别') NOT NULL DEFAULT '多元性别',
`balance` INT(64),
`age` TINYINT,
`phone number` BIGINT NOT NULL DEFAULT 110,
`date` DATE, PRIMARY KEY (`id`)
) ENGINE=INNODB CHARSET=utf8mb4 COLLATE=utf8mb4_bin;
#在test2库创建学员名单
create table `test2`.`student` (
`id` int(64) primary key auto_increment,
`name` varchar(4) not null default '狗蛋',
`age` int(32) default '18',
`sex` enum('男','女')
)engine=MyISAM charset=utf8 collate=utf8_bin;
查看表结构
desc test2.`student`;
修改表
#修改表名
alter table test2.`student` rename `dashen`;
#修改已存在列的属性
alter table test2.`dashen` modify `name` varchar(4) not null default '二狗子' unique;
#修改列的名称
alter table test2.`dashen` change `sex` `gender` enum('m',''w')
#添加一个爱好列
alter table test2.`dashen` add `hobby` varchar(32) default '无';
##添加到指定列
alter table test2.`dashen` add `hobby` varchar(32) default '无' after `name`;
alter table test2.`dashen` add `hobby` varchar(32) default '无' first;
#删除一个列
alter table 库.表 drop 列名;
alter table test2.`dashen` drop `hobby`;
删除表
drop table test2.dashen;
二.DML语句
1.介绍
针对表中的数据进行增删改操作
insert:进行数据的写入
方式1:
insert into test2.dashen (各列的名称) values(各列的值);
insert into test2.`dashen` (`id`,`name`,`age`,`gender`,`hobby`) values(1,'海龙',80,'w','洗脚');
#查看数据
select * from test2.`dashen`;
##默认值,自增.可以为空 都可以省略
方式2:
insert into test2.dashen values(各列的值);
update
针对数据进行修改
update 表名 set 列名=值 where 条件;
update test2.dashen set name='峰哥' where id=2;
update test2.dashen set hobby='洗澡' where id>=3;
delete
针对数据进行删除
delete from 表名 where 条件;
delete from test2.`dashen` where age=18;
#清空表所有数据行
truncate 表名;
##注意
delete是一个逻辑删除
truncate是物理删除
三.DQL语句
数据查询语句
1.show语句
show
用于查询系统中元数据信息
help show;
show databases; #查询库
show tables from 库; #查询库中的表
#查询系统参数
show variables like 'port' #查询端口
show variables like '%log%'
2.select语句
1.执行函数
什么是函数
预先定义好的一个可以直接去调用处理特定数据的一些方法
select 函数();
now() #输出当前时间
user() #输出当前链接用户
database() #输出当前所在库
version() #输出当前数据库版本
#查询函数
help contents
help functions
help 函数分布
help 函数
#聚合函数
max() #显示最大值
min() #显示最小值
avg() #显示平均值
sum() #求和
count() #统计个数
distinct() #去重
concat() #字符串拼接
group_concat() #行列置换函数
3.计算器
4.数据查询功能
select * from 库.表
select 列名1,列名2 from 库.表 子句
常用子句顺序
where 子句
group by 子句
having 子句
order by 子句
limit 子句
2.select查询参数
select @@port;
3.计算器
4,数据查询功能
select * from 库.表
select 列名1,列名2 from 库.表 子句
常用子句顺序
where 子句
group by 子句
having 子句
order by 子句
limit 子句
1.where子句
作用:进行数据条件判断,筛选过滤的
## 查询中国各城市的人口数
select * from world.`city` where CountryCode='CHN';
select * from world.`city` where name='taiyuan';
## 范围判断:查询全球人口数小于10000的城市
select * from world.`city` where population<10000;
## 多条件判断:同时查看中国和美国的城市人口
select * from world.`city` where CountryCode='CHN' or countrycode='USA';
select * from world.`city` where countrycode in ('CHN','USA','AUS');
## 练习:请查询 中国人口数大于100w的城市有哪些?
## 模糊判断:查看日本的人口数
select * from world.`city` where countrycode like 'J%';
select * from world.`city` where countrycode like 'JP_';
## 结合函数:中国的总人口数
select sum(population) from world.`city` where CountryCode='CHN';
## 练习:中国的总城市数量
国家 总城市数
select `countrycode`,count(`name`) from world.`city` where CountryCode='CHN';
2.group by子句
作用:用于实现展示数据时,按照条件分组展示
使用group by出现sql_mode报错的原因:数据分组之后无法对其导致的
1.作为查询条件的列都要在group by中接上
2.如果有些列不可以放在groupby中,但是作为查询条件,该列通过函数做运算
-- groupby: 查询全球范围内,每个国家的人口总数
select countrycode,sum(population) from world.city group by countrycode;
-- 练习:查询中国,每个省份有哪些城市
select countrycode,District,group_concat(name) from world.`city` where CountryCode='CHN' group by District;
-- 练习:查询每个国家的城市总数量
国家名 城市数量
select countrycode,count(name) from world.`city` group by countrycode;
3.having子句
作用:在前面子句处理完成的基础上进一步做条件判断
having和where的区别:having支持将一个列先做函数运算,进一步做条件判断
-- 查询:全球总城市数小于50个的国家有哪些
select countrycode,count(name) from world.`city` group by countrycode having count(name)<50;
-- 练习:查询人口总数小于100w的国家有哪些
select CountryCode,sum(population) from world.`city` group by CountryCode having sum(population)<1000000;
4.order by子句
作用:实现将前面查询的数据按照一定条件排序
select CountryCode,sum(population) from world.`city`
group by CountryCode having sum(population)<1000000 order by sum(population);
select CountryCode,sum(population) from world.`city`
group by CountryCode having sum(population)<1000000 order by sum(population) desc;
5.limit子句
作用:限制只显示部分数据
-- 查看全球人口数排名前5的城市
select name,population from world.`city` order by population desc limit 5;
-- 查看全球人口数排名前3-5的城市
select name,population from world.`city` order by population desc limit 2,3;
533

被折叠的 条评论
为什么被折叠?



