1、数据库概述
数据库-应用服务-前端服务
存储系统所需数据的仓库,项目开发的前期需要进行数据库设计和创建;
数据库分类
- 关系型数据库(SQl)
- 面向操作:如MYSQL、Oracle、SQLServer
- 面向数据分析:如Google BigQuery
- 非关系型数据库(NOSQL)
- 面向操作:如Redis
- 面向数据分析:如Hadoop
2、SQL基础语法
1、DDL—数据定义语言(Data Define Language):
create(创建),alter(修改),drop(删除),TRUNCATE(截断),RENAME(重命名);
2、DML—数据操纵语言(Data Manipulation Language):
select(查询),delete(删除),update(更新),insert(新增);
3、DCL—数据控制语言(Data Control Language):
grant(添加权限),revoke(回收权限);
3、商城系统的核心数据结构
- 商品
商品编号、商品名称、分类、价格、库存数量 - 客户
客户编号、客户名称、手机号、帐户余额 - 订单
订单编号、客户编号、商品编号、购买数量、交易时间
4、创建表
DROP TABLE goods;
/* 创建商品表 */
CREATE TABLE `goods` (
`goods_id` int(4) NOT NULL COMMENT '商品编号' ,
`goods_name` varchar(128) NULL COMMENT '商品名称' ,
`goods_type` varchar(64) NULL COMMENT '分类' ,
`goods_price` numeric(12,2) NULL COMMENT '商品价格' ,
`goods_count` int(4) NULL COMMENT '商品库存数量' ,
PRIMARY KEY (`goods_id`)
)
;
/* 创建客户表 */
CREATE TABLE `customer` (
`customer_id` int(4) NOT NULL COMMENT '客户编号' ,
`customer_name` varchar(64) NULL COMMENT '客户名称' ,
`customer_mobile` varchar(16) NULL COMMENT '手机号' ,
`customer_money` numeric(12,2) NULL COMMENT '帐户余额' ,
PRIMARY KEY (`customer_id`)
)
;
/* 创建订单表 */
CREATE TABLE `order` (
`order_id` int(4) not null comment '订单编号',
`customer_id` int(4) NOT NULL COMMENT '客户编号' ,
`goods_id` int(4) NOT NULL COMMENT '商品编号' ,
`order_count` int(4) NULL COMMENT '购买数量' ,
`order_time` datetime NULL COMMENT '交易时间' ,
PRIMARY KEY (`order_id`)
)
;
5、添加测试数据
/* 添加客户数据 */
insert into customer values(2,'李四',null,99.55);
insert into customer values(3,'晨晨','12345676543',299.15);
/* 添加商品数据 */
insert into goods values(1,'白菜','食品',12,10);
insert into goods values(2,'猪蹄','食品',22.5,3);
insert into goods values(3,'萝卜','食品',1.5,20);
/* 添加订单数据 */
insert into `order` values(1,2,1,2,'2022-02-11 12:00:20');
/*删除数据*/
delete from goods
where goods_name='萝卜';
/* 修改数据 */
update goods
set goods_price=1.2
where goods_id=1 ;
/*查询数据*/
select * from customer;
select * from goods;
select * from `order`;
6、编写常用场景的SQL语句
/*注册*/
insert into `user` values(4,'zhangsan','123');
/*登录*/
select *
from `user`
where username='zhangsan' and `password`='123';
/*添加商品*/
insert into goods values(4,'斗罗大陆','书籍',50,20);
/*商品调价*/
update goods
set goods_price=65
where goods_id=4;
/*商品下架*/
/*给商品表添加一个状态字段*/
alter table goods add column `goods_status` varchar(8);
/*修改商品状态*/
update goods
set goods_status = '正常';
update goods
set goods_status = '下架'
where goods_id=3;
select * from goods;
/*搜索商品*/
select * from goods
where goods_status='正常' and goods_name like '%菜%';
/*销售*/
update goods
set goods_count=goods_count-1
where goods_id = 2;
update customer
set customer_money = customer_money - (select goods_price from goods where goods_id=2) /*使用子查询获取商品价格*/
where customer_id = 1;
insert into `order` value(2,1,2,1,'2022-02-12 17:54:23');
select * from customer;
/*查询订单*/
select
a.order_id,
b.customer_name,
c.goods_name,
c.goods_price,
a.order_count,
c.goods_price*a.order_count as 'total_price',
a.order_time
from `order` a
join customer b
on a.customer_id = b.customer_id
join goods c
on a.goods_id = c.goods_id
;
- 扩展场景:
商品分类
购物车
订单状态
折扣