-- 展示所有的数据库
show databases;
-- 创建数据库(文件夹):
creat databases [数据库名称];
create database day06;//如果有相同名字的数据库已经存在了那么程序会报错。
create database [ if not exists ] 数据库名;//数据库不存在,则创建该数据库;如果存在则不创建
-- 删除数据库语法
drop databases 数据库名称;//数据库不存在将会报错
drop database day06;
drop database [ if exists ] 数据库名 ;//数据库存在时删除
-- 使用/切换数据库(■ 切换文件夹意思)
use [数据库名称];
use day06;
查询当前数据库:
select database();
2、创建表
复制代码
★ 创建表
/*
语法:
create table student(
列名1 列的数据类型1,
列名2 列的数据类型2,
..........
列名n 列的数据类型n
);
*/
注意事项:最后一行不能有逗号。
数据类型:/*
int:整数
double:小数
varchar(文字个数): 文字(字符串)
data/datetime:日期 年月日/年月日时分秒
*/
create table tb_user (
id int comment 'ID,唯一标识', # id是一行数据的唯一标识(不能重复)
username varchar(20) comment '用户名',
name varchar(10) comment '姓名',
age int comment '年龄',
gender char(1) comment '性别'
) comment '用户表';
/*刚创建的表它是不存在数据的。
*/
★ /* 约束 */
● not null:非空约束
限制该字段值不能为null
● unique:唯一约束
保证字段的所有数据都是唯一、不重复的
● primary key:主键约束
主键是一行数据的唯一标识,要求非空且唯一
● default:默认约束
保存数据时,如果未指定该字段值,则采用默认值
● foreign key:外键约束
让两张表的数据建立连接,保证数据的一致性和完整性
★ 注意:约束是作用于表中字段上的,可以在创建表/修改表的时候添加约束。
★ 主键自增:auto_increment
- 每次插入新的行记录时,数据库自动生成id字段(主键)下的值
- 具有auto_increment的数据列是一个正数序列开始增长(从1开始自增)
★ 注意:主键增长只会增长,不会因为成员的删除而减少对应的值。
create table tb_user (
id int primary key auto_increment comment 'ID,唯一标识', #主键自动增长
username varchar(20) not null unique comment '用户名',
name varchar(10) not null comment '姓名',
age int comment '年龄',
gender char(1) default '男' comment '性别'
) comment '用户表';
/******************************************************************************/
★ 数据类型:
update语法:
update 表名 set 字段名1 = 值1 , 字段名2 = 值2 , .... [where 条件] ;
案例1:将tb_emp表中id为1的员工,姓名name字段更新为'张三'
update tb_emp set name='张三',update_time=now() where id=1;
将tb_emp表的所有员工入职日期更新为'2010-01-01'
update tb_emp set entrydate='2010-01-01',update_time=now();
注意事项:
1. 修改语句的条件可以有,也可以没有,如果没有条件,则会修改整张表的所有数据。
2. 在修改数据时,一般需要同时修改公共字段update_time,将其修改为当前操作时间。
二、DQL查询语句
复制代码
/*语法格式:
SELECT
字段列表
FROM
表名列表
WHERE
条件列表
GROUP BY
分组字段列表
HAVING
分组后条件列表
ORDER BY
排序字段列表
LIMIT
分页参数
*/
-- 基础查询:
查询多个字段
select 字段1, 字段2, 字段3 from 表名;
select id,name,chinese,math,birthday from day06.student;
-- 基础查询(*的方式不推荐 ,不见名起意)
查询所有字段(通配符)
select * from 表名;
select * from day06.student;
-- 设置别名
select 字段1 [ as 别名1 ] , 字段2 [ as 别名2 ] from 表名;
-- 去除重复记录
select distinct 字段列表 from 表名;
-- 查询指定列
select name,chinese from day06.student;
-- 查询指定列并且去除重复元素,去除重复必须查询的列的值都相同才算重复!!!!
select distinct name,chinese from day06.student;
select distinct name from day06.student;
-- 给列起别名,使用as关键字来起别名,而且as还可以省略!!!!
select name as 姓名, chinese 语文成绩 from day06.student;
1、条件查询
复制代码
select 字段列表 from 表名 where 条件列表 ; -- 条件列表:意味着可以有多个条件
构造条件的运算符分为两类:
- 比较运算符
- 逻辑运算符
**比较运算符** **功能**
> 大于
>= 大于等于
< 小于
<= 小于等于
= 等于
<> 或 != 不等于
between ... and ... 在某个范围之内(含最小、最大值)
in(...) 在in之后的列表中的值,多选一
like 占位符 模糊匹配(_匹配单个字符, %匹配任意个字符)
is null 是null
-- ****************************************************************************** --
常用的逻辑运算符如下:
**逻辑运算符** **功能**
and 或 && 并且 (多个条件同时成立)
or 或 || 或者 (多个条件任意一个成立)
not 或 ! 非 , 不是
2、聚合函数
复制代码
语法:
select 聚合函数(字段列表) from 表名 ;
/*************************************************************************/
函数===============功能
count :按照列去统计有多少行数据。
sum : 计算指定列的数值和,如果不是数值类型,那么计算结果为0
max : 计算指定列的最大值
min : 计算指定列的最小值
avg : 计算指定列的平均值
注意 : 聚合函数会忽略空值,对NULL值不作为统计。
-- count 不会对null值进计算的。
select count(gender) from tb_emp;
-- 获取总数据量的方式
select count('1') from tb_emp;
select count('*') from tb_emp;
-- 推荐使用
-- 获取某条的最小值
select min(entrydate) from tb_emp;
-- 获取某条的最大值
select max(entrydate) from tb_emp;
-- 获取某条的平均值
select avg(id) from tb_emp;
-- 求取某条总和
select sum(id) from tb_emp;
3、分组查询
复制代码
● 分组: 按照某一列或者某几列,把相同的数据进行合并输出。
● 分组其实就是按列进行分类(指定列下相同的数据归为一类),然后可以对分类完的数据进行合并计算。
分组查询通常会使用聚合函数进行计算。
● 语法:
select 字段列表 from 表名 [where 条件] group by 分组字段名 [having 分组后过滤条件];
> 注意事项:
> 分组之后,查询的字段一般为聚合函数和分组字段,查询其他字段无任何意义
> 执行顺序:where > 聚合函数 > having
★★★ where与having区别:
- 执行时机不同:where是分组之前进行过滤,不满足where条件,不参与分组;而having是分组之后对结果进行过滤。
- 判断条件不同:where不能对聚合函数进行判断,而having可以。
-- 根据性别分组
select gender, count(*) from tb_emp group by gender;
-- 对入职时间在2015-01-01之前的职位进行分组
select job, count(*) from tb_emp where entrydate <= '2015-01-01' group by job;
-- 上一条的数量大于等于的的组数
select job, count(*) from tb_emp where entrydate <= '2015-01-01' group by job having count(*) >= 2;
4、排序查询
复制代码
有升序排序,也有降序排序。
select 字段列表 from 表名 [where 条件列表] [group by 分组字段 ] order by 字段1 排序方式1 , 字段2 排序方式2 … ;
★ 排序方式:
- ASC :升序(默认值)
- DESC:降序
-- 按照入职时间进行升序排列
select *from tb_emp order by entrydate asc;
-- 按照入职时间进行降序排列
select *from tb_emp order by entrydate desc;
-- 按照入职时间进行升序排列,当入职时间相同时,按照更新时间进行降序排列
select *from tb_emp order by entrydate asc, update_time desc;
注意事项:如果是升序, 可以不指定排序方式ASC
注意事项:如果是多字段排序,当第一个字段值相同时,才会根据第二个字段进行排序
5、分页查询
复制代码
select 字段列表 from 表名 limit 起始索引, 查询记录数 ;
从起始索引0开始查询员工数据, 每页展示5条记录
select id, username, password, name, gender, image, job, entrydate, create_time, update_time
from tb_emp
limit 0 , 5; -- 从索引0开始,向后取5条记录
-- limit 5; 如果查询的是第1页数据,起始索引可以省略,直接简写为:limit 条数
注意事项:
1. 起始索引从0开始。
计算公式 :起始索引=(查询页码 - 1)* 每页显示记录数
2. 分页查询是数据库的方言,不同的数据库有不同的实现,MySQL中是LIMIT
3. 如果查询的是第一页数据,起始索引可以省略,直接简写为 limit 条数
-- 分页查询
-- 查询第一页五条数据
select *from tb_emp
limit 0,5;
-- 查询第二页5条数据
select *from tb_emp
limit 5,5;
select *from tb_emp where name like '张%' and gender = 1 and entrydate between '2000-01-01' and '2015-12-31'
limit 0,10;
select *from tb_emp where name like '%张%' and gender = 1 and entrydate between '2000-01-01' and '2015-12-31'
limit 0,10;
-- ***************************************************************************
-- if(条件表达式, true取值 , false取值)
select if(gender=1,'男性员工','女性员工') AS 性别, count(*) AS 人数
from tb_emp
group by gender;
> if(表达式, tvalue, fvalue) :当表达式为true时,取值tvalue;当表达式为false时,取值fvalue
-- case 表达式 when 值1 then 结果1 when 值2 then 结果2 ... else result end
select (case job
when 1 then '班主任'
when 2 then '讲师'
when 3 then '学工主管'
when 4 then '教研主管'
else '未分配职位'
end) AS 职位 ,
count(*) AS 人数
from tb_emp
group by job;
case 表达式 when 值1 then 结果1 [when 值2 then 结果2 ...] [else result] end
在实际开发中,尽量避免用到if或者case when then end,因为这种相当于是逻辑判断的工作最好交给代码业务层来处理,而不是数据库