news 2026/7/31 0:57:56

关系型数据库

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
关系型数据库

一.SQL语言

1.全称:结构化查询语言

2.分类

1.DDL 数据定义语言

1.作用:

①定义数据库

②定义数据表

③定义字段

2.关键字:

①creat

②drop

③alter

2.DML 数据操作语言

1.作用:操作数据表的结构
2.关键字:

①insert into

②delete

③update

3.DQL 数据查询语言

1.作用:查询表中的数据
2.关键字:

①select

②from

③where

4.DCL 数据控制语言

3.通用规则

1.每条SQL语句以; 结尾

2.SQL语句可以使用空格和换行格式化展示

3.注释

1.单行注释

①#注释内容

②--注释内容

2.多行注释

/*

多行

*/

二.MySQL数据库

1.安装数据库

2.连接数据库

①命令

mysql -h主机地址 -P端口号 -u用户名 -p密码

②工具

3.DDL

①创建数据库

create database [if not exists] 库名;

# TODO 1.数据库的创建和查看 -- 创建数据库test1 CREATE DATABASE test1; -- IF NOT EXISTS: 如果存在就忽略,不存在就创建 CREATE DATABASE IF NOT EXISTS test2; CREATE DATABASE IF NOT EXISTS test3; -- use 库名: 切换数据库 USE test3; -- 查看所有数据库 SHOW DATABASES; # TODO 2.库中表的创建和查看 # 前题: 先创建并使用库 # 创建数据库 CREATE DATABASE my_db; USE my_db;

②创建表(含字段)

create table [if not exists] 表名(
字段名 字段类型 [字段约束] ,
...
);

# 创建学生表 CREATE TABLE student ( name varchar(30), age int ); # 创建教师表 CREATE TABLE teacher ( name varchar(30), age int );

4.DML

①插入数据

insert into 表名(字段名...) values(字段值...);

# 先创建taobao_db库 create database taobao_db; # 再创建users表 create table taobao_db.users( id int , name varchar(30), phone bigint ); # 最后插入数据 insert into taobao_db.users(id,name,phone) values(1,'张三',18866669999); insert into taobao_db.users(id,name,phone) values(1,'张三',18866669999),(2,'李四',16677778888); # 注意: 不指定字段,默认等于指定了所有字段 insert into taobao_db.users values(1,'张三',18866669999); insert into taobao_db.users values(1,'张三',18866669999),(2,'李四',16677778888); insert into taobao_db.users(name) values('王五');

②更新数据

update 表名 set 字段名 = 新值 [where 条件];

# 演示如何修改数据 update taobao_db.users set phone = '17788889999' where name = '李四'; # 注意: 如果忘记了添加where条件,就变成了修改所有 update taobao_db.users set phone = '17788889999';

③删除数据

delete from 表名 [where 条件] ;

# 演示如何删除数据 delete from taobao_db.users where name = '张三'; # 注意: 如果忘记了添加where条件,就变成删除所有 delete from taobao_db.users ; # 演示truncate删除数据(如果真的有清空数据需求,建议用truancate) # 因为上面delete已经删除干净了,再次插入数据用于删除 insert into taobao_db.users(name) values('张三'),('李四'),('王五'); truncate table taobao_db.users;

5.DQL

1.基础查询

select 字段名 from 表名;

# 创建数据库: create database 库名; CREATE DATABASE IF NOT EXISTS tb_db CHARSET=utf8; # 使用数据库: use 库名; USE tb_db; # 创建表: create table 表名(字段名 字段类型 [约束],...); # 建测试表 drop table if EXISTS products; CREATE TABLE IF NOT EXISTS products ( id INT PRIMARY KEY AUTO_INCREMENT, -- 商品ID name VARCHAR(24) NOT NULL, -- 商品名称 price DECIMAL(10, 2) NOT NULL, -- 商品价格 score DECIMAL(5, 2), -- 商品评分,可以为空 is_self VARCHAR(8), -- 是否自营 category_id INT -- 商品类别ID ); drop table if EXISTS category; CREATE TABLE IF NOT EXISTS category ( id INT PRIMARY KEY AUTO_INCREMENT, -- 商品类别ID name VARCHAR(24) NOT NULL -- 类别名称 ); # 插入数据: insert into 表名 (字段名,字段名) values(字段值,字段值),(字段值,字段值); # 添加测试数据 INSERT INTO category VALUES (1, '手机'), (2, '电脑'), (3, '美妆'), (4, '家居'); INSERT INTO products VALUES (1, '华为Mate50', 5499.00, 9.70, '自营', 1), (2, '荣耀80', 2399.00, 9.50, '自营', 1), (3, '荣耀80', 2199.00, 9.30, '非自营', 1), (4, '红米note 11', 999.00, 9.00, '非自营', 1), (5, '联想小新14', 4199.00, 9.20, '自营', 2), (6, '惠普战66', 4499.90, 9.30, '自营', 2), (7, '苹果Air13', 6198.00, 9.10, '非自营', 2), (8, '华为MateBook14', 5599.00, 9.30, '非自营', 2), (9, '兰蔻小黑瓶', 1100.00, 9.60, '自营', 3), (10, '雅诗兰黛粉底液', 920.00, 9.40, '自营', 3), (11, '阿玛尼红管405', 350.00, NULL, '非自营', 3), (12, '迪奥996', 330.00, 9.70, '非自营', 3); # 查看自增效果 insert into category(name) values('食品'); insert into products(name,price) values('辣条',0.5); # 1.基础查询 # 需求: 查询所有信息 select * from products; # 需求: 查询商品的名称和价格 select name,price from products; # 给名称和价格起别名展示 select name as n,price as p from products; select name as 名称,price as 价格 from products; # 需求: 查询商品的类别编号有哪些 select DISTINCT category_id from products;

2.条件查询

select 字段名 from 表名 where 条件;

# 需求: 查询价格大于4199并且小于6000的商品信息 select * from products where price > 4199 and price < 6000; # 需求: 查询价格不等于4199的商品信息 select * from products where price != 4199; select * from products where price <> 4199; # 需求: 查询价格是4199 或 4499 的商品信息 select * from products where price = 4199 or price = 4499.9; select * from products where price in (4199,4499.9); # 需求: 查询价格大于等于4199并且小于等于6000的商品信息 select * from products where price >= 4199 and price <= 6000; select * from products where price BETWEEN 4199 and 6000; # 需求: 查询价格不在4199-6000之间的商品信息 select * from products where price < 4199 or price > 6000; select * from products where not (price >= 4199 and price <= 6000); select * from products where price not BETWEEN 4199 and 6000; # 需求: 查询商品名称以"华"开头的商品信息 select * from products where name like '华%'; # 需求: 查询商品名称第三个字是"小"的商品信息 select * from products where name like '__小%'; # 需求: 查询商品名称中带'兰'字的商品信息 select * from products where name like '%兰%'; # 需求: 查询已经评分的商品信息 select * from products where score is not null; # 需求: 查询未评分的商品信息 select * from products where score is null;
1.比较运算符

> < >= <= != <>

2.逻辑运算符

and or not

3.范围查询

①between x and y

②in(x,y)

4.模糊查询
1.关键字:

like

2.符号

①%: 任意0个或者多个字符

②_ :任意1个字符

5.非空判断

①is null

②is not null

3.聚合查询

select 聚合函数(字段名) from 表名;

# 需求: 查询商品的价格总和,平均值,最大值,最小值 select sum(price),avg(price),max(price),min(price) from products; # 需求: 查询所有商品个数 select count(*) from products; # 推荐 select count(id) from products; # 需求: 查询已评分的商品个数 select count(*) from products where score is not null; # 需求: 查询已评分的商品平均价格 select avg(price) from products where score is not null;

4.分组查询

select 分组字段名,聚合函数(字段名) from 表名 [where 非聚合条件] group by 分组字段名 [having 聚合条件];

# 需求: 查询商品的价格总和,平均值,最大值,最小值 select sum(price),avg(price),max(price),min(price) from products; # 需求: 查询所有商品个数 select count(*) from products; # 推荐 select count(id) from products; # 需求: 查询已评分的商品个数 select count(*) from products where score is not null; # 需求: 查询已评分的商品平均价格 select avg(price) from products where score is not null; # 5.分组查询 # 需求: 查询商品表中的分类id select DISTINCT category_id from products; select category_id from products group by category_id; # 需求: 查询每个分类中商品平均价格 # 笨方式 select 1,avg(price) from products where category_id = 1; select 2,avg(price) from products where category_id = 2; select 3,avg(price) from products where category_id = 3; select null,avg(price) from products where category_id is null; # group by方式 select category_id,avg(price) from products group by category_id; # 以下代码在mysql老版本中能够正常使用但是没有意义,新版本中因为底层设置了sql_mode=only_full_group_by,让它报错 select name,avg(price) from products group by category_id; select @@sql_mode; # set SQL_MODE = 'STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION';

5.排序查询

select 字段名 from 表名 where 条件 order by 排序字段名 asc | desc;

# 需求: 按照评分升序排序 select * from products order by score asc; # 因为默认升序,此处警告是说多余的 select * from products order by score ; # 需求: 按照评分降序排序 select * from products order by score desc; # 需求: 先按照评分降序排序,然后按照价格降序排序 select * from products order by score desc,price desc;

6.limit查询

select 字段名 from 表名 where 条件 order by 排序字段名 asc | desc limit x,y;

# topn需求: # 需求: 查询价格最高的商品信息 select * from products order by price desc limit 1; # 需求: 查询价格最高的5个商品信息 select * from products order by price desc limit 0,5; # 分页需求 # 假设每页展示4条数据,要求获取每页的数据 # 第1页 select * from products limit 0,4; # 第2页 select * from products limit 4,4; # 第3页 select * from products limit 8,4; # 第4页 select * from products limit 12,4; # 结论:limit x,y: x=(页数-1)*y

7.SQL顺序

①书写顺序

select -> 聚合函数 -> from -> where -> group by -> having ->order by -> limit

②执行顺序

from -> where -> group by -> 聚合函数 -> having -> select -> order by -> limit

8.多表查询

本质就是先把多个表连接(join)成一个表,再去查询

版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/7/31 11:28:58

3步搞定大数据可视化:ECharts服务端渲染终极指南

3步搞定大数据可视化&#xff1a;ECharts服务端渲染终极指南 【免费下载链接】echarts Apache ECharts is a powerful, interactive charting and data visualization library for browser 项目地址: https://gitcode.com/gh_mirrors/echarts16/echarts 还在为前端图表加…

作者头像 李华
网站建设 2026/7/30 18:49:25

最全词典整合收录:打造专业英语学习利器

最全词典整合收录&#xff1a;打造专业英语学习利器 【免费下载链接】最全词典整合收录词典刺客 本仓库提供了一个名为“最全词典整合收录(词典刺客)”的资源文件下载。该资源文件包含了以下词典的整合收录&#xff1a;- 柯林斯双解&#xff08;mddmdx&#xff09;- 朗文双解&a…

作者头像 李华
网站建设 2026/7/31 9:35:44

用户目录能不能放到其他盘?

用户目录能不能放到其他盘&#xff1f;是的, 你可以把用户文件夹移动到另一个磁盘, 但你应该小心操作. 许多人想要腾出系统盘空间或把个人文件放在单独的磁盘上. 移动用户文件夹可以缓解空间限制并简化备份, 但如果方法不当也可能引发问题. 本文解释了安全的选项, 需要遵循的步…

作者头像 李华
网站建设 2026/7/30 4:27:59

Captura音频质量优化终极指南:告别爆音与杂音困扰

Captura音频质量优化终极指南&#xff1a;告别爆音与杂音困扰 【免费下载链接】Captura Capture Screen, Audio, Cursor, Mouse Clicks and Keystrokes 项目地址: https://gitcode.com/gh_mirrors/ca/Captura Captura是一款功能强大的开源屏幕录制工具&#xff0c;支持屏…

作者头像 李华
网站建设 2026/7/28 23:11:28

终于交出焚诀了,运营新思路:短视频动漫化

作为开发者、内容创作者或职场人&#xff0c;日常总会遇到图片处理需求 —— 老照片修复、商品图抠图、低清图高清化&#xff0c;要么靠复杂 PS 耗时费力&#xff0c;要么付费工具成本太高。其实用 AIEnhancer.ai 按 “选场景→验效果→优流程” 的思路&#xff0c;就能零门槛实…

作者头像 李华