MySQL(六)MySQL 案例

1. MySQL 案例

1.1. 设计数据库

  1、首先根据相关业务需求(主要参考输出输入条件)规划出表的基本结构

  2、根据业务规则进行状态字段设计

  3、预估相关表的数据量进行容量规划

  4、确定主键

  5、根据对相关处理语句的分析对数据结构进行相应的变更。

  设计表的时候每个表的功能要独立,优点:结构清晰,操作数据库的时候提高性能

1.2. 实现数据库

  (1)新建user表

  (2)新建order表

  (3)新建product表

  (4)新建category表

1.3. 操作数据库

1.3.1. 插入记录

复制代码
insert into 表名(列名1,列名2,列名3......) values (值1,值2,值3......)



1.3.2. 修改表记录

复制代码
update 表名 set 字段名=值,字段名=值,字段名=值...... where 条件

1.3.3. 删除表记录

复制代码
delete from 表名 where id=4;


1.3.4. 查询操作语法

复制代码
select [distinct]*| 列名,列名 from 表名 [where条件]

  (1)查询所有商品

复制代码
select * from category;

  (2)查询商品名和商品价格

复制代码
SELECT product_name,product_price FROM product;

  (3)查询商品名,使用列别名

复制代码
select product_name as "商品名称" from product;

  (4)去掉重复值(按照价格)

复制代码
select distinct(product_price) from product;

  (5)将所有的商品的价格+10进行显示

复制代码
select product_name, product_price+10 from product ;

1.3.5. 条件查询

  (1)查询商品名称为"华为pura70"的商品信息

复制代码
select * from product where product_name='华为pura70';

  (2)查询商品名称含有"pura"字的商品信息(模糊查询)

复制代码
select * from product where product_name like '%pura%';

  (3)查询商品id在(1,3)范围内的所有商品信息

复制代码
select * from product where product_id in (1,3);

  (4)查询商品名称含有"pura"字并且id为2的商品信息

复制代码
select * from product where product_name like '%pura%'and product_id=2;

  (5)查询id为1或者3的商品信息

复制代码
select * from product where product_id=1 or product_id=3;

1.3.67 排序

  (1)查询所有的商品,按价格进行排序(升序、降序)

复制代码
select * from product order by product_price asc;
select * from product order by product_price desc;

  (2)查询名称査"pura"的商品信息并且按照价格降序排序

复制代码
select * from product where product_name like '%pura%' order by product_price desc;

1.3.6. 聚合函数

  (1)获得所有商品的价格的总和

复制代码
select sum(product_price) from product;

  (2)获得所有商品的平均价格

复制代码
select avg(product_price) from product;

  (3)获得所有商品的个数

复制代码
select count(product_name) from product;

1.3.7. 分组操作

  (1)根据category_id字段分组

复制代码
select category_id,count(*) from product group by category_id;

  (2)根据category_id分组,分组统计每组商品的平均价格,并且平均价格大于200元

复制代码
select category_id,avg(product_price) from product group by category_id having avg(product_price)>200;
相关推荐
zcmodeltech12 分钟前
风力发电沙盘模型控制系统设计与灯光联动实现方案——基于STM32与Modbus RTU,服务范围覆盖全国的多场景风力发电沙盘模型定制
网络·数据库·stm32·单片机·嵌入式硬件·能源
程序员黎剑20 分钟前
MySQL-JOIN优化-NLJ与BNL的区别与实战
数据库·mysql
Java小白笔记21 分钟前
MyBatis XML Mapper 标签与 CRUD 原理实战
xml·数据库·mybatis
JavaPub-rodert1 小时前
一个后台系统如何同时支持 MySQL、PostgreSQL、SQLite、SQL Server?从 ShiyuAdmin 的 GORM 多数据库适配说起
数据库·mysql·postgresql
小翰生信1 小时前
转录组下游分析全流程:DESeq2 差异分析、GO/KEGG 富集与 GSEA 实战
数据库·矩阵·golang
2603_966437261 小时前
拷贝中断文件丢失,电脑资料抢救实操方案
数据库·电脑
harmony&1 小时前
MySQL 数据库从入门到实战:原理、安装与 SQL 详解
数据库·sql·mysql
艺杯羹1 小时前
全栈信创落地实录:基于银河麒麟V10与达梦数据库DM8的SpringBoot工业级适配指南
java·数据库·spring boot·后端·spring
坚定信念,勇往无前6 小时前
mongodb的日志分割
数据库·mongodb
oradh10 小时前
Oracle TX 锁 Mode 4(Share)问题排查总结
数据库·oracle·tx 锁 mode 4·oracle tx 锁