一.SQL通用语法
- SQL语句可以单行或者多行书写,以分号结尾
- MySQL数据库的SQL语句不区分大小写,关键字建议使用大写
- 注释:
单行注释: – 注释内容(注意–后有空格) 或 #注释内容(MySQL特有)
多行注释:/* 注释内容 */
二.SQL分类
- DDL(Data Definition Language)数据定义语言,用来定于数据库对象:数据库,表,列等
- DML(Data Manipulation Language)数据操作语言,用来对数据库中表的数据进行增删改
- DQL(Data Query Language)数据查询语言,用来查询数据库中表的记录(数据)
- DCL(Data Control Language)数据控制语言,用来定义数据库的访问权限和安全级别,及创建用户
三.基础语法
1.DDL
1.1DDL – 操作数据库
a)查询
show databases;
b)创建
create database 数据库名称;
create database 数据库名称 if not exists;#创建数据库判之前断改数据库是否不存在
c)删除
drop database 数据库名称;
drop database 数据库名称 if exists;#删除数据库之前判断数据库是否存在
d)使用数据库
select database();#查询当前使用的数据库
use 数据库名称;#使用数据库
1.2DDL – 操作表
a)创建
create table 表名(
字段名1 数据类型,
字段名2 数据类型,
~
字段名n 数据类型
);
/* 注意:最后一行行末尾,不能加逗号 */
数据类型:
实例:
create table students(
id int,
name varchar(10),
sex char(1),
birth date,
score double(5,2),
email varchar(64),
phonenumber varchar(15),
status tinyint
);
b)查询
show table;#查询当前数据库下所有表名称
desc 表名;#查询表结构
c)修改
#修改表名
alter table 表名 rename to 新的表名;
#添加一列
alter table 表名 add 列名 数据类型;
#修改数据类型
alter table 表名 modify 列名 新的数据类型;
#修改列名和数据类型
alter table 表名 change 列名 新的列名 新的数据类型;
#删除列
alter table 表名 drop 列名;
d)删除
dorp table 表名;#删除表
drop table if exists 表名;#删除表是判断是否存在
2.DML
2.1添加数据
#给指定列添加数据
insert into 表名(列名1,列名2,……) values(值1,值2,……);
#给全部列添加数据
insert into 表名 values(值1,值2,……);
#批量添加数据
insert into 表名(列名1,列名2,……) values(值1,值2,……),(值1,值2,……),……;
insert into 表名 values(值1,值2,……),(值1,值2,……),……;
2.2修改数据
update 表名 set 列名1=值1,列名2=值2,…… where 条件;
#注意修改语句如果不加条件,则将所有数据都修改
2.3删除数据
delete from 表名 where 条件;
#删除语句中如果不加条件,则将所有数据都删除
3.DQL
3.1基础查询
#查询多个字段
select 字段列表(列名) from 表名;
select * from 表名; -- 查询所有数据
#去除重复元素
select distinct 字段列表 from 表名;
#起别名
select 列名 as 别名 from 表名; -- as可以省略 列名和别名至少有一个空格
3.2条件查询
#条件查询语法
select 字段列表 from 表名 where 条件列表;
#实例
select * from 表名 where age > 20;
select * from 表名 where age >= 20;
select * from 表名 where birth between '1900-02-02' and '2020-09-15';
select * from 表名 where age >= 20 && age <= 30;
select * from 表名 where age between 20 and 30;
-- 判断是否等于用 = (不使用==) 不等于用 != <> 两种表示
-- and表示并 or表示或
select * from 表名 where age = 18 or age = 19 or age = 20;
select * from 表名 where age in(18,19,20);
-- 判断null null值的比较不能使用 = != 需要使用 is, is not
select * from 表名 where math is null;
select * from 表名 where english is not null;
#模糊查询
-- 查询姓王的同学
select * from 表名 where name like '马%';
-- 查询第二个字是'子'的同学
select * from 表名 where name like '_花%';
-- 查询名字中包含'德'的同学
select * from 表名 where name like '%德%';
条件表
3.3排序查询
select 字段列表 from 表名 order by 排序字段1 排序方式1, 排序字段2 排序方式2, ……;
#排序方式: asc升序排序(默认) desc降序排序
#如果有多个排序条件,当前面条件一样时,才会根据第二条件进行排序
#实例:
select * from 表名 order by age asc;
select * from 表名 order by math desc, english acs;
3.4聚合函数
概念:将一列数据作为一个整体,进行纵向计算
聚合函数分类:
select 聚合函数名(列名) from 表名;
-- null值不参与所有聚合函数运算
#实例:
select count(id) from 表名;#count取值两种情况: 1.主键 2.*
select count(*) from 表名;
select max(math) from 表名;
select min(math) from 表名;
select sum(math) from 表名;
select avg(math) from 表名;
3.5分组查询
select 字段列表 from 表名 where 分组前条件限定 group by 分组字段名 having 分组后条件过滤;
#分组之后,查询的字段为聚合函数和分段字段,查询其他字段无任何意义
#实例
select sex, avg(math) from 表名 group by sex;
select sex, avg(math) from 表名 where math > 70 group by sex;
select sex, avg(math) from 表名 where math > 70 group by sex having count(*) > 2;
where 和 having 的区别
- 执行时机不一样:where是分组之前进行限定,不满足where条件,则不参与分组,而having是对分组之后对结果进行过滤
- 可判断条件不一样:where不能对聚合函数进行判断,having可以
- 执行顺序:where > 聚合函数 > having
3.6分页查询
select 字段列表 from 表名 limit 起始索引, 查询条目;
- 起始索引:从0开始
- 计算公式;起始索引 = (当前页码 - 1)* 每页显示的条数
tip:
- 分页查询limit是MySQL数据库的方言
- Oracle分页查询使用rownumber
- SQL Server分页查询使用top
3.6分页查询
select 字段列表 from 表名 limit 起始索引, 查询条目;
- 起始索引:从0开始
- 计算公式;起始索引 = (当前页码 - 1)* 每页显示的条数
tip:
- 分页查询limit是MySQL数据库的方言
- Oracle分页查询使用rownumber
- SQL Server分页查询使用top