前言
今天主要学习SQL语言
1、SQL是什么?
2、SQL的语法;
3、SQL的分类(☆)
1.DDL(Data Definition Language)数据定义语言
2.DML(Data Manipulation Language)数据操作语言
3.DQL(Data Query Language)数据查询语言
4.DCL(Data Control Language)数据控制语言(了解)
一、SQL是什么?
SQL (Structured Query Language)是一种结构化查询语言;用于操作所有的关系型数据库。相当于我们生活中的 “普通话”,可以在很多国家使用。
二、SQL的语法
1、SQL语句可以单行或者多行书写,以分号结尾;
2、可以使用空格和缩进 来增强语句的可读性;
3、MySQL数据库的SQL语句不区分大小写,关键字建议使用大写;
4、3种注释:
-
1.单行注释:①--( - 与 注释内容之间有一个空格)注释内容 如:-- mysql的启动服务 ② #注释内容(mysql特有的)
-
2.多行注释:“/*注释内容 */”
三、SQL的分类
1、DDL(Data Definition Language)数据定义语言
用于定义数据库、表、列等,关键字:create,drop,alter等。
1.操作数据库 CRUD
(1)C(Create)创建:
- 创建数据库:
create database 数据库名称;
- 创建数据库,判断不存在,再创建:
create database if not exists 数据库名称;
- 创建数据库,并指定字符集:
create database 数据库名称 character set 字符集名;
例:创建db1数据库,判断是否存在,并制定字符集为gbk;
create database if not exists db1 charater set gbk;
(2)R(Retrieve)查询:
- 查询所有数据库名称:
show databases;
- 查询某个数据库的字符集,查询某个数据库的创建语句:
show create database 数据库名称;
(3)U(Update)修改:
- 修改数据库的字符集:
alter database 数据库名称 charater set 字符集名称;
(4)D(Delete)删除:
- 删除数据库:
drop database 数据库名称;
- 判断数据库是否存在,存在再删除:
drop database if exists 数据库名称;
(5)使用数据库:
- 查询当前正在使用的数据库:
select database();
(使用的mysql中的一个全局函数) - 使用数据库:
use 数据库名称;
2.操作表 CRUD
(1)C(Create)创建:
语法:
-
注意:最后一列,不需要加逗号create table 表名( 列名1 数据类型1, 列名2 数据类型2, ..... 列名n 数据类型n );
数据类型:
- ① int :整数类型
例如:age int, - ② double:小数类型
score double(5,2), - ③date:日期 只包含年月日,YYYY-MM-DD
- ④datatime:日期 ,包含年月日时分秒,YYYY-MM-DD HH-MM-SS
- ⑤timestap:时间戳类型,包含年月日时分秒,YYYY-MM-DD HH-MM-SS(如果没有给列名赋值,或者赋值为null,则默认使用当前的系统的时间来自动赋值)
- ⑥varchar:字符串
例如: name varchar(20),-------说明姓名最大20个字符
zhangsan 8个字符,张三2个字符
例如:创建一个学生表student表
create table student(
id int,
name varchar(32),
age int ,
score double(4,1),
birthday date,
insert_time timestamp
);
创建一个和student表一样的表stu
语法:create table 表名 like 被复制的表名;
create table stu like student;
(2)R(Retrieve)查询:
- 查询某个数据库中所有的表:
show tables;
- 查询表结构:
desc 表名;
(3)U(Update)修改:
- 修改表名:
alter table 表名 rename to 新的表名;
alter table student rename to stu;
- 修改表的字符集:
alter table 表名 charater set 字符集名称;
首先查看一下student表的字符集:show create table student;
修改studnet表的字符集为utf8:alter table student charater set utf8;
- 添加新的一列:
alter table 表名 add 添加的列名 数据类型;
alter table student add sex varchar(10);
- 修改列名称和类型:
alter table 表名 change 列名 新列名 新数据类型;或alter table 表名 modify 列名 新数据类型;
alter table student change sex gender varchar(20);
alter table student modify sex varchar(40);
- 删除列:
alter table 表名 drop 列名;
alter table student drop sex;
(4)D(Delete)删除:
- 删除一个表:
drop table 表名;
drop table student;
- 删除一个存在的表:
drop table if exists 表名;
drop table if exists student;
2、DML(Data Manipulation Language)数据操作语言(很重要)
用于对数据库中表的数据进行增删改,关键字:insert,delete,update等。
添加数据:
语法:insert into 表名 (列名1,...,列名n)values (值1,...,值n); 或 insert into 表名 values (值1,值2,....,值n);
例如:
insert into student (id,name,age,score,birth) values(1,'zhangsan',18,98,'1998-09-23');
所有的列是id,name,age,sex,score,birth
insert into student values(2,'lisi',19,'男',88.8,“1999-11-11”);
添加多条数据: insert into student (id,name,age,score1,score2) values (1,"张三",17,98,88),(2,"李四",23,78,98),(3,"王五",34,77,89),(4,"任嘉伦",34,100,100),(5,"易烊千玺",22,89,99);
注意:
- 列名与值一一对应;
- 如果表名后面不定义类名,就是默认给所有的列添加值(也就是添加数据的第二种写法)
- 除了int 、double等数字类型,其他的需要用单引号或者双引号
删除数据:
语法:delete from 表名 where 条件;
delete from student where id=1;
不加where条件删除的是整个表,不建议使用:delete from student;
注意:
delete from 表名
不建议使用,会删除表中的所有数据;
truncate table 表名,
建议使用,它表示删除表中的数据,同时又会创建一个与原先一样的表;
修改数据:
语法:update 表名 set 列名1=值1,列名2=值2 where条件;
一个条件:update student set age=33 where id=1;
两个条件:update student set age=12,name="zhangsan" where id=2;
不加条件(不建议使用):update student set age=20;
注意:
- 不加where条件的话,会修改表中所有的age为20;
3、DQL(Data Query Language)数据查询语言
用于查询数据库中表的记录,即数据,关键字:select,where等。
以下所有的查询基于添加的这些数据:
1.基础查询
- 查询表中所有的内容:
select * from 表名;
查询student表中所有的内容:select * from student;
- 查询表中一个列名:
select 列名 form 表名;
查询student表中的name列:select name from student;
- 查询表中多个列名:
select 列名1,列名2 from 表名;
查询student表中的name和age列:select name,age from student;
- 指定列的别名查询:
select 列名1 as 别名,列名2 as 别名 from 表名;(as可以省略)
select name as 姓名,age as 年龄 from student;
select score1 as 成绩1,score2 成绩2 from student;
- 查询指定列,且不出现重复值:
select distinct 列名 from 表名;
select distinct age from student;
- 查询指定列的运算:select 列名1+列名2 from 表名;
查询指定的name列,score1、score2的和:select name,score1,score2,score1+score2 from student;
- 说明:
①列的计算,一般是数值类型的列进行计算,如果列出现了null,用方法②;
②ifnull(表达式1,表达式2),null参与的计算结果都为null
表达式1:哪个字段需要判断是否为null,像例子中的english就是需要判断的字段
**表达式2:**如果该字段为null后的替换值
举个例子:
select name,math,english,math+ifnull(english,0)from student;
运行结果:
- 结合别名和计算看一下:
select name,math,english,math+ifnull(english,0) as 总分 from student;
运行结果:
2.条件查询
条件查询:就是where后面跟上条件;
以下面的表为基础举例子:
- 查询年龄大于20岁
select * from student where age >20;
运行结果:
- 查询年龄大于等于20岁
select * from student where age >=20;
运行结果:
- 查询年龄等于20岁
select * from student where age=20;
*查询年龄不等于20岁
方法1:select * from student where age !=20;
方法2:select * from student where age <>20;
- 查询年龄大于等于20岁,小于等于30岁的
方法1:select * from student where age >=20 && age <=30;
方法2:select * from student where age >=20 and age <=30;
方法3:select * from student where age between 20 and 30;
*查询年龄为19岁,22岁,25岁的
select * from student where age=19 or age=22 or age=25;
select * from student where age in(19,22,25);
- 查询英语成绩为null的
select * from student where English is null;
- 查询英语成绩不为null的
select * from student where English is not null;
- 查询姓马的人
select * from student where name like '马%';
- 查询姓名第二字是化的人
select * from student where name like '_化%';
- 查询姓名是三个字的人
select * from student where name like '___';(三个下划线)
- 查询 姓名中包含德的人
select * from student where name like ‘%德%’;
运行结果:
解释一下_和%的意思:
_:匹配单个任意占位符
%:匹配任意个占位符
3.排序查询
- ASC:升序,默认就是升序;
- DESC:降序;
- 语法:
order by 排序字段1 排序方式1,排序字段2 排序方式2;
按照数学成绩升序:
select * from student order by math;
select * from student order by math ASC;
运行结果:
按照数学成绩降序:
select * from student order by math DESC;
运行结果:
按照数学成绩升序,数学成绩一样就按照英语成绩降序
select * from student order by math ASC,English DESC;
运行结果:
注意:有多个排序时,当前面的排序一样,则执行第二个排序。
4.聚合函数
定义:将一列数据作为整体,进行纵向的计算。
前提:聚合函数选择计算的列都是非空的列,我们想使用聚合函数应该怎么解决呢?①选择非空的列 ②选择ifnull 函数
- count:计算个数,一般选择非空的列进行计算或者使用count(*)
计算student表中有多少人:
select count( * ) from student;
select count(math) from student;
select count(ifnull(english,0)) from student;
- max:计算最大值
计算最高数学成绩:
select max(math) from student;
- min:计算最小值
计算数学成绩最少的值
select min(math) from student;
- sum:求和
计算数学成绩的总和
select sum(math) from student;
- avg:计算平均值
计算数学成绩的平均值
select avg(math) from student;
5.分组查询
语法:group by 分组字段
按照性别分组,分别计算男女生数学平均值
select sex,avg(math) from student group by sex;
按照性别分组,分别计算男女生数学平均值和男女生人数
select sex,avg(math),count(id) from studnet group by sex;
按照性别分组,分别查询男女生数学平均值,分数低于70分的人,不参与分组
select sex,avg(math),count(id) from student where math >70 group by sex;
按照性别分组,分别查询男女生的数学平均值,分数低于70分的人,不参与分组,且分组之后人数要大于2个人
方法①:select sex,avg(math),count(id) from student where math >70 group by sex having count(id) >2;
方法②:select sex,avg(math),count(id) as 人数 from student where math >70 group by sex having 人数>2;
分组查询总结一下where和having条件的区别吧:
where:用在分组之前,不满足条件不进行分组,where后面不能跟聚合函数
having:用在分组之后,不满足条件不会被查询出来,having后面可以进行聚合函数的判断
6.分页查询
语法:limit 开始的索引,每页查询的条数
公式:开始的索引 = (当前的页码 - 1) * 每页显示的条数
按照每页3条数据进行分页查询
select * from student limit 0,3;-- 第一页
select * from student limit 3,3;-- 第二页
select * from student limit 6,3;-- 第三页
select * from student limit 3,3;-- 第二页
运行结果:
~~limit分页查询是MySQL语言中特有的“方言”,不能用于Oracle和SQL Server中
4、DCL(Data Control Language)数据控制语言
用于定义数据库的访问权限和安全级别,及创建用户,关键字:GRANT,REVOKE等。