SQL语句

SQL是结构化的查询语句

SQL的种类:

DDL:数据定义语句

DCL:数据控制语言

DML:数据操作语言

DQL:数据查询语言

DDL数据定义语句

对库或者表进行操作的语句

创建数据库

create database db01;
# 创建数据库
create database DB01;
# 数据库名区分大小写(注意windows里面不区分)
show variables like 'lower_case_table_names';
show databases;
# 查看数据库(DQL)
show create database db01;
# 查看创建数据库语句
help create database;
# 查看创建数据库语句帮助
create database db02 charset utf8;
# 创建数据库的时候添加属性

删除数据库

drop database db02;
# 删除数据库db02

修改定义库

alter database db01 charset utf8;
show create database db01;

创建表

help create table;
# 查看创表语句的帮助
create table student(
sid int,
sname varchar(20),
sage tinyint,
sgender enum('m','f'),
comtime datetime
)
# 创建表,并且定义每一列
create table student(
sid int not null primary key auto_increment comment '学号',
sname varchar(20) not null comment '学生姓名',
sage tinyint unsigned not null comment '年龄',
sgender enum('m','f') not null default 'm' comment '学生性别',
cometime datetime not null comment '入学时间'
)charset utf8 engine innodb;
# 带数据属性创建学生表
show create table student;
# 查看建表语句
show tables;
# 查看表
desc student;
# 查看表中列的定义信息

删除表

drop table student;

修改表的定义

alter table student rename stu;
# 修改表名
alter table stu add age int;
# 添加列和列数据类型的定义
alter table stu add test varchar(20),add qq int;
# 添加多个列
alter table stu add classid varchar(20) first;
# 指定位置进行添加列(表首)
alter table stu add phone int after age;
# 指定位置进行添加列(指定列)
alter table stu drop qq;
# 删除指定的列及定义
alter table stu modify sid varchar(20);
# 修改列及定义(列属性)
alter table stu change phone telphone char(20);
# 修改列及定义(列名及属性)

DCL数据控制语言

DCL是针对权限进行控制

授权

grant all on *.* to root@'192.168.175.%' identified by '123456';
# 授予root@'192.168.175.%'用户所有权限(非超级管理员)
grant all on *.* to root@'192.168.175.%' identified by '123456' with grant
option;
# 授权一个超级管路员
with
max_queries_per_hour:一个用户每小时可发出的查询数量
max_updates_per_hour:一个用户每小时可发出的更新数量
max_connections_per_hour:一个用户每小时可连接到服务器的次数
max_user_connections:允许同时连接数量

收回权限

revoke select on *.* from root@'192.168.175.%';
# 收回select权限
show grants for root@'192.168.175.%';
# 查看权限

DML数据操作语言

操作表中的数据

插入数据

insert into stu valus('linux01',1,NOW(),'zhangsan',20,'m',NOW(),110,123456);
# 基础用法,插入数据
insert into stu(classid,birth.sname,sage,sgender,comtime,telnum,qq)
values('linux01',1,NOW(),'zhangsan',20,'m',NOW(),110,123456);
# 规范用法,插入数据
insert into stu(classid,birth.sname,sage,sgender,comtime,telnum,qq)
values('linux01',1,NOW(),'zhangsan',20,'m',NOW(),110,123456),
('linux02',2,NOW(),'zhangsi',21,'f',NOW(),111,1234567);
# 插入多条数据

更新数据

update student set sgender='f';
# 不规范
update student set sgender='f' where sid=1;
# 规范update修改
update student set sgender='f' where 1=1;
# 如果非要全表修改
update mysql.user set password=PASSWORD('123456') where user='root' and
host='localhost';
# 修改密码,需要刷新权限flush privileges

删除数据

delete from student;
# 不规范
delete from student where sid=3;
# 规范删除(危险)
truncate table student;
# DDL清空表中的内容

使用伪删除

有时候一些重要数据不能直接删除,只能伪删除,因为以后还得使用呢

使用update代替delete,将状态改成删除状态,在查询的时候就可以不显示被标记删除的数

alter table student add status enum('1','0') default 1;
# 额外添加一个状态列
update student set status='0' where sid=1;
# 使用update
select * from student where status=1;
# 应用查询存在的数据

DQL数据查询语言

select:基础用法

演示用的SQL文件下载:https://download.s21i.faiusr.com/23126342/0/0/ABUIABAAGAAgzcXw

hQYozuPv2AE?f=world.sql&v=1622942413

mysql -uroot -p123 < world.sql
# 常用用法
select countrycode,district from city;
# 常用用法
select countrycode from city;
# 查询单列
select countrycode,district from city limit 2;
select id,countrycode,district from city limit 2,2;
# 行级查询
select name,population from city where countrycode='CHN';
# 条件查询
select name,population from city where countrycode='CHN' and
district='heilongjiang';
# 多条件查询
select name,population,countrycode from city where countrycode like '%H%'
limit 10;
# 模糊查询
select id,name,population,countrycode from city order by countrycode limit
10;
# 排序查询(顺序)
select id,name,population,countrycode from city order by countrycode desc
limit 10;
# 排序查询(倒序)
select * from city where population>=1410000;
# 范围查询(>,<,>=,<=,<>)
select * from city where countrycode='CHN' or countrycode='USA';
# 范围查询OR语句
select * from city where countrycode in ('CHN','USA');
# 范围查询IN语句
select country.name,city.name,city.population,country.code from city,country
where city.countrycode=country.code and city.population < 100;
# 多表查询

select的高级用法

多表连接查询(连表查询)

create table t1(id int primary key auto_increment,name varchar(20))
ENGINE=InnoDB CHARSET=utf8;
create table t2(id int primary key auto_increment,score int) ENGINE=InnoDB
CHARSET=utf8;
insert into t1(name) values('cs'),('tj'),('lz');
insert into t2(score) values(30),(80),(82);
select * from t1;
select * from t2;

传统连接(只能内连接,只能取交集)

select t1.name,t2.score from t1,t2 where t1.id=t2.id and t2.score > 60;
# 查出及格
#世界上小于100人的人口城市是哪个国家的?
select city.name,city.countrycode,country.name
from city,country
where city.countrycode=country.code
and city.population<100;
# 世界上小于100人的人口城市是哪个国家,说的什么语言?
国家人口数量 城市名 国家名 语言
country.population, city.name, country.name,
countrylanguage.Language
select country.population,city.name,country.name,countrylanguage.Language
from city,country,countrylanguage where city.countrycode=country.code and
countrylanguage.countrycode=country.code and country.population<100;

NATURAL JOIN(自连接的表要有共同的列名字)

SELECT city.name,city.countrycode ,countrylanguage.language ,city.population
FROM city natural join countrylanguage
WHERE population > 1000000
ORDER BY population;

建议:使用join语句时,小表在前,大表在后。

外连接

select city.name,city.countrycode,country.name
from city left join country
on city.countrycode=country.code
and city.population<100;

UNION(合并查询)

mysql> select * from city where countrycode='CHN' or countrycode='USA';
#范围查询OR语句
mysql> select * from city where countrycode in ('CHN','USA');
#范围查询IN语句
替换为:
mysql> select * from city where countrycode='CHN'
union all
select * from city where countrycode='USA' limit 10

union:去重复合并

union all :不去重复

使用情况:union<union all