当前位置: 首页 > news >正文

关系型数据库

一.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)成一个表,再去查询

http://www.cnnetsun.cn/news/26989.html

相关文章:

  • 计算机毕业设计springboot基于Spark++Vue.js的学生管理系统 Spark+Vue 高校学生综合信息管理平台 基于 SpringBoot+Spark+Vue 的全链路学生事务中心
  • JavaScript 集合操作的哈希碰撞:攻击者如何利用特殊 Key 导致 Map/Set 性能降级到 O(N)
  • 为什么 C盘空间会莫名其妙减少(即使没装新软件)?
  • 17、深入理解 Linux 文件系统机制与结构
  • 29、Linux 软件使用与故障排除指南
  • 从入门到转行:网络安全自学与跳槽的终极建议
  • 网络安全小白自学之路,别拜师了,求人不如求己_网络安全小白怎么自学
  • 从系统运维到网络安全工程师,8个月转行真实经验分享!
  • 算法系列(Algorithm)- 快速排序
  • RobotStudio2025全功能授权
  • IsaacLab中UR机械臂与Robotiq夹爪的5大配置难点与解决方案
  • cmark Markdown解析器终极指南:从入门到精通
  • 4-bit量化FLUX模型:让专业AI绘图走进寻常百姓家
  • Excel VBA快速入门:7天从零到精通终极指南
  • AutoHotkey鼠标轨迹自动化终极指南:从零开始实现精准操作回放
  • UxPlay 终极指南:在 Linux 系统上实现 AirPlay 镜像的完整教程
  • 1-2 惜败!国安亚冠连败 中超 16 强魔咒难破
  • 一键解锁阅读3.0书源终极合集:1629个精品资源任你选
  • 一般人不懂Windows
  • Java 基于多线程机制的专项实验
  • 51、Linux 系统中 shell 环境管理与脚本编写全解析
  • 53、编写高效 Shell 脚本:从基础到实践
  • 解锁共享单车数据:从入门到精通的完整分析指南
  • KOOM:如何快速解决Android应用内存泄漏的终极方案
  • API 测试- Postman Vs Rest Assured
  • 如何在React Native应用中实现语音交互?
  • 突破创意瓶颈:BlenderMCP如何用AI重塑3D建模工作流
  • 生产环境出现问题,测试人如何做工作复盘?
  • 测试工程师:这锅我不背,什么情况测试容易背锅以及化解妙招
  • Python自定义HTTP客户端:12306抢票项目的网络请求管理