MySQL--增、删、改、查,

  • 数据库的概述、发展、现状、历史、分类

  • MySQL关系型数据库、架构(C/S)

  • window系统安装MySQL数据库

  • Linux系统【选学】

  • 数据库对象------数据库(database)

  • show、create、drop命令

  • 数据库对象------表(table)

  • 数据类型

  • 数据库表的约束

  • 表结构的调整(alter)

  • 数据库授权和权限管理(grant)

  • 数据库的编码情况【编码一致,建议统一采用utf8mb4】

今日目标

  1. 数据库的CRUD

  2. 单表的增删改查

  3. 多表的关联查询

SQL分类

  • DDL 数据定义语言 create drop

  • DML 数据管理语言 CRUD

    • 增删改 (更新)

    • DQL 数据查询语言

  • DCL 数据控制语言

DML语句

添加数据

insert指令

注意:SQL语句不区分大小写,也就是大小写一致!!

sql 复制代码
# 语法结构
insert into  表名称[(字段1 [,字段2, ......])] {values|value}(字段值, [......]) [,()];
​
​
# 注意:如果主键自增,可以使用null或者default关键字填充
# 其他字段,如果存在默认值,则使用default关键字填充
INSERT INTO t_user values(null, "刘建宏", 20, "陕西西安", "110", default);
# 也可以自己指定默认值存在项
INSERT INTO t_user values(7, "刘建宏", 20, "陕西西安", "110", 30);
​
INSERT INTO t_user value(8, "赵帅", 25, "陕西安康", "120", 25);
​
insert into t_user(name) values ("流川枫");
删除数据

DELETE指令

注意:where关键字,必须存在条件时才能出现

SQL中,单引号和双引号是一样的

复制代码
delete from 表名称 [where 一个或者多个条件];

truncate指令

该指令也可以删除数据,注意:这种数据删除不通过数据字典,是无法恢复的,请慎用!!!!

主要使用场景:清除测试数据!!!!

增加数据

update指令

sql 复制代码
update 表名称 set 字段名称=新的值 [,字段=xxx [,......] [where 条件];
replace语句

replace语句结构和insert的语法结构一模一样

sql 复制代码
replace into 表名称[(字段1 [,字段2, ......])] {values|value}(字段值, [......]) [,()];
​

注意:replace 的sql语句,是集更新和插入于一体的一个SQL。

如果插入的数据不存在(主键、unqiue修饰的字段),执行insert执行

如果插入的数据存在(主键、unqiue修饰的字段),则先执行删除语句,再执行insert语句。

查询语句(select)

使用select查询数据库表中的某些数据。

select 语法结构

sql 复制代码
select {字段|函数|其他} from 表名称 [where]
复制代码
​
​
select的特殊使用

select 可以用来计算数据,在开发中,如果数据类那些个不是数值,注意,从第一个位置它会尽可能转换为数值。

sql 复制代码
select 100 + 200;
​
select '100' + 200;
select '100' + '200';
​
select 100 + "刘建宏";
​
select 100 + "200刘建宏";
​
select 100 + "刘建宏200";
​
select 'a' + "b";   # 0
​
select null + 30 + '刘建宏';   # 只要其中一个为NULL,则结果为NULL
select也支持函数使用。

select user();      # 查询当前登录用户
select database();  # 查询所属的数据库
select uuid()       # 生成一个UUID值, uuid值的特点是,永不重复的一个字符串
select查询数据

select 字段  from 表名称
​
​
select * from user;
select id, name from user;
select id, name, gender, address, age from user;
复制代码

早晨内容回顾

  1. 前三章的内容回顾

  2. 增删改查的基本语法

    insert into 表名称 values()

    delete from 表名称 [where 条件]

    truncate语句 truncate 表名称

    update 表名称 set 字段 = 新值 ,xxxx

    repalce into 表名称 values()

    select 表达式;

    select 函数()

    select {* | 字段名称 [, 字段名称..] } from 表名称

select条件查询
sql 复制代码
select * from t_user;
select id, name, age from t_user;
​
# 等值条件 =
select address from t_user where id = 1;
select age from user where name="刘建宏";
​
# 关系条件
-- >  <  >= <= != =   <>
​
​
# 判断是否为空
# is关键字  is null  is not null
select * from user where address is NULL;
select * from user where address is not NULL;
​
# <=> 符号
# 充当等号的作用
# 可以用来判断空
select * from user where age <=>18;
select * from user where gender <=> null;
​
# 多个条件
# 逻辑运算符 
# and   并且
# or    或者
# not   不是,取反
​
select * from user where age = 18 and gender = "男" and id > 10;
select * from user where age = 18 or gender = "女";
select name from user where gender is not null;
select name from user where not gender = "男";
​
# 范围
update user set age = 25 where id >= 6 and id <= 10;
update user set age = 16 where id between 12 and 15;
 select * from user where not (age < 20 or age > 25);
 
 # 注意:条件的执行顺序问题!!!
 select * from user where not age < 20 or age > 25;
 
 # 列举 in not in
 select * from user where id in (1,3,8,10);
 select * from user where id not in (1,3,8,10);
 
 # 去重效果
 # 使用distinct关键字去掉重复值
 select distinct age from user;

日期的格式:使用字符串来表示

复制代码
"yyyy-mm-dd"    如  '2000-03-30'
‘yyyy/mm/dd’    如   '2024/5/11'
'hh:mm:ss'      如   ‘12:12:21’
​
"yyyyy-mm-dd hh:mm:ss"  ‘2020-3-4 16:05:30’
模糊查询

使用like关键字进行模糊匹配

% 匹配0到多位

_ 匹配一个具体的位

sql 复制代码
select * from user where name like "%亮";
select * from user where name like "%亮%";
select * from user where name like "%张%";
select * from user where name like "张%";
​
# 表示第二个字是"绣"
select * from user where name like "_绣%";
正则查询
复制代码
select * from user where name regexp "^张";
select * from user where name regexp "亮$";

复杂查询

分组查询

将数据相同的,会放在同一个组中,也就是,不会出现重复数据。往往是用来做数据分析。

sql 复制代码
select 字段
from  表名
[where 条件]
group by 字段 [, 字段 [,......]]
​

案例

复制代码
-- 统计不同性别的人数
select count(gender), gender from user group by gender;
​
having语句

having语句,是配合分组使用,是分组后的筛选!!!!

复制代码
select 字段
from  表名
[where 条件]
group by 字段 [, 字段 [,……]]
having 筛选条件

案例:

sql 复制代码
select gender, count(gender) from user group by gender having count(gender) > 8;
​
select gender, count(gender) from user where age >= 18 group by gender having count(gender) > 5;
聚会函数-count

统计数据

sql 复制代码
select count(id) from user;
select count(gender) from user;
select count(id) from user;
select count(1) from user;
select count(1) from user where gender="男";
排序

order by 字段 [{asc | desc }]

如果存在排序,必须是在分组之后

复制代码
select 字段
from  表名
[where 条件]
[group by 字段 [, 字段 [,……]] ]
[having 筛选条件]
order by 字段 [{asc |  desc  }]  [, 字段 [{asc |  desc  }]]

案例

sql 复制代码
select * from user order by age ;
select * from user order by age desc;
select * from user order by age asc;
​
select * from user order by age , id desc;
​
复制代码
别名的使用

别名(alias ),就是将原有名称,重新赋予一个新的名称

在查询SQL时,可以将查询的结果重新设置名称(字段、表名称)。

字段 as 别名 ,as可以省略

复制代码
select count(*) as count from user;
​
select count(*) count from user;

分页查询

查询非常多,此时全部展示是不现实的,因此,需要通过分页展示部分数据

注意:不同的关系型数据库,实现分页效果,可能SQL不一样!!!

MySQL使用 limit关键字实现分页!!!

sql 复制代码
select 字段
from  表名
[where 条件]
[group by 字段 [, 字段 [,......]] ]
[having 筛选条件]
order by 字段 [{asc |  desc  }]  [, 字段 [{asc |  desc  }]]
​
limit num [, num2]; 

limit num :表示要截取num条数据。

limit num1, num2 :表示num1为起始位【从0开始】, num2表示一页共显示多少数据。

分页实现时的几个关键变量:

当前页:默认为1,用户决定这个值 pageNow

页面的条数:一般有开发者定义 pageSIze

共有多少页:这个是需要算出来 pageCount

总条数:查询得到 allCount

复制代码
select * from user limit  pageSize * (pageNow - 1) , pageSize;

多表关联查询

为什么要设计多张表?不把数据存储在一张表上呢?

分表的必要性
相关推荐
Majoy23 分钟前
HBase
大数据·数据库·hbase
夜光小兔纸1 小时前
oracle查询出表中某几个字段值不唯一的数据
数据库·sql·oracle
deadknight93 小时前
Oracle密码过期处理方式
数据库·oracle
Ljubim.te3 小时前
数据库第01讲章节测验(选项顺序可能不同)
数据库
吱吱喔喔3 小时前
数据分表和分库原理
数据库·分表·分库
快乐非自愿3 小时前
KES数据库实践指南:探索KES数据库的事务隔离级别
数据库·oracle
一只fish3 小时前
Oracle的RECYCLEBIN回收站:轻松恢复误删对象
数据库·oracle
codemami3 小时前
oracle中的nocache的用法和例子
oracle
weixin_440401693 小时前
分布式锁——基于Redis分布式锁
java·数据库·spring boot·redis·分布式
TOR-NADO3 小时前
数据库概念题总结
数据库·oracle