1、數(shù)據(jù)庫概述
數(shù)據(jù)庫-應(yīng)用服務(wù)-前端服務(wù)
存儲(chǔ)系統(tǒng)所需數(shù)據(jù)的倉庫,項(xiàng)目開發(fā)的前期需要進(jìn)行數(shù)據(jù)庫設(shè)計(jì)和創(chuàng)建;
數(shù)據(jù)庫分類
- 關(guān)系型數(shù)據(jù)庫(SQl)
- 面向操作:如MYSQL、Oracle、SQLServer
- 面向數(shù)據(jù)分析:如Google BigQuery
- 非關(guān)系型數(shù)據(jù)庫(NOSQL)
- 面向操作:如Redis
- 面向數(shù)據(jù)分析:如Hadoop
2、SQL基礎(chǔ)語法
1、DDL—數(shù)據(jù)定義語言(Data Define Language):
create(創(chuàng)建),alter(修改),drop(刪除),TRUNCATE(截?cái)啵?,RENAME(重命名);
2、DML—數(shù)據(jù)操縱語言(Data Manipulation Language):
select(查詢),delete(刪除),update(更新),insert(新增);
3、DCL—數(shù)據(jù)控制語言(Data Control Language):
grant(添加權(quán)限),revoke(回收權(quán)限);
3、商城系統(tǒng)的核心數(shù)據(jù)結(jié)構(gòu)
- 商品
商品編號(hào)、商品名稱、分類、價(jià)格、庫存數(shù)量 - 客戶
客戶編號(hào)、客戶名稱、手機(jī)號(hào)、帳戶余額 - 訂單
訂單編號(hào)、客戶編號(hào)、商品編號(hào)、購買數(shù)量、交易時(shí)間
4、創(chuàng)建表
DROP TABLE goods;
/* 創(chuàng)建商品表 */
CREATE TABLE `goods` (
`goods_id` int(4) NOT NULL COMMENT '商品編號(hào)' ,
`goods_name` varchar(128) NULL COMMENT '商品名稱' ,
`goods_type` varchar(64) NULL COMMENT '分類' ,
`goods_price` numeric(12,2) NULL COMMENT '商品價(jià)格' ,
`goods_count` int(4) NULL COMMENT '商品庫存數(shù)量' ,
PRIMARY KEY (`goods_id`)
)
;
/* 創(chuàng)建客戶表 */
CREATE TABLE `customer` (
`customer_id` int(4) NOT NULL COMMENT '客戶編號(hào)' ,
`customer_name` varchar(64) NULL COMMENT '客戶名稱' ,
`customer_mobile` varchar(16) NULL COMMENT '手機(jī)號(hào)' ,
`customer_money` numeric(12,2) NULL COMMENT '帳戶余額' ,
PRIMARY KEY (`customer_id`)
)
;
/* 創(chuàng)建訂單表 */
CREATE TABLE `order` (
`order_id` int(4) not null comment '訂單編號(hào)',
`customer_id` int(4) NOT NULL COMMENT '客戶編號(hào)' ,
`goods_id` int(4) NOT NULL COMMENT '商品編號(hào)' ,
`order_count` int(4) NULL COMMENT '購買數(shù)量' ,
`order_time` datetime NULL COMMENT '交易時(shí)間' ,
PRIMARY KEY (`order_id`)
)
;
5、添加測試數(shù)據(jù)
/* 添加客戶數(shù)據(jù) */
insert into customer values(2,'李四',null,99.55);
insert into customer values(3,'晨晨','12345676543',299.15);
/* 添加商品數(shù)據(jù) */
insert into goods values(1,'白菜','食品',12,10);
insert into goods values(2,'豬蹄','食品',22.5,3);
insert into goods values(3,'蘿卜','食品',1.5,20);
/* 添加訂單數(shù)據(jù) */
insert into `order` values(1,2,1,2,'2022-02-11 12:00:20');
/*刪除數(shù)據(jù)*/
delete from goods
where goods_name='蘿卜';
/* 修改數(shù)據(jù) */
update goods
set goods_price=1.2
where goods_id=1 ;
/*查詢數(shù)據(jù)*/
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);
/*商品調(diào)價(jià)*/
update goods
set goods_price=65
where goods_id=4;
/*商品下架*/
/*給商品表添加一個(gè)狀態(tài)字段*/
alter table goods add column `goods_status` varchar(8);
/*修改商品狀態(tài)*/
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) /*使用子查詢獲取商品價(jià)格*/
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
;
- 擴(kuò)展場景:
商品分類
購物車
訂單狀態(tài)
折扣