- 安装并配置MySQL
- 打开控制台
- 登录MySQL
- 数据库、表的基本操作
- 创建电子商城数据库“mall_姓名全拼”
- 使用电子商城数据库
use mall_yangbin;
- 创建用户表“user_姓名全拼”,表中字段信息如下:
字段名 | 数据类型 | 长度 | 主、外键 | 其他约束 | 备注信息 |
char | 11 | 主键 | 注册手机号 | ||
username | varchar | 20 | 非空,唯一 | 用户名 | |
password | varchar | 20 | 非空 | 密码 | |
question | text | 非空 | 找回密码问题 | ||
answer | text | 非空 | 找回密码问题答案 |
create table user_yangbin(
phone char(11) primary key comment"注册手机号",
username varchar(20) not null unique comment"用户名",
password varchar(20) not null comment"密码",
question text not null comment"找回密码问题",
answer text not null comment"找回密码问题答案"
);
创建卖家信息表“seller_姓名全拼”,表中字段信息如下:
字段名 | 数据类型 | 长度 | 主、外键 | 其他约束 | 备注信息 |
id | char | 16 | 主键 | 卖家ID(S_DATE_XXXXX) | |
phone | char | 11 | 外键(user.phone) | 非空,唯一 | 注册手机号 |
open_date | date | 非空 | 开业时间 | ||
name | varchar | 50 | 非空 | 店铺名称 | |
nickname | varchar | 30 | 非空 | 掌柜昵称 |
create table seller_yangbin(
id char(16) primary key comment"卖家ID(S_DATE_XXXXX)",
phone char(11) not null unique comment"注册手机号",
open_date date not null comment"开业时间",
name varchar(50) not null comment"店铺名称",
nickname varchar(30) not null comment"掌柜昵称",
constraint fk_seller_fengyi_phone foreign key (phone) references user_yangbin(phone)
);
创建买家信息表“buyer_姓名全拼”,表中字段信息如下:
字段名 | 数据类型 | 长度 | 主、外键 | 其他约束 | 备注信息 |
id | char | 16 | 主键 | 买家ID(B_DATE_XXXXX) | |
phone | char | 11 | 外键(user.phone) | 非空,唯一 | 注册手机号 |
nickname | varchar | 30 | 非空 | 买家昵称 | |
gender | enum(“miss”,”mr”) | 默认miss | 性别 | ||
height | int | 3 | 身高cm | ||
weight | double | 体重kg |
create table buyer_yangbin(
id char(16) primary key comment"买家ID(B_DATE_XXXXX)",
phone char(11) not null unique comment"注册手机号",
nickname varchar(30) not null comment"买家昵称",
gender enum("miss","mr") default"miss" comment"性别",
height int(3) comment"身高cm",
weight double comment"体重kg",
constraint fk_buyer_fengyi_phone foreign key (phone) references user_yangbin(phone)
);
6.创建地址表“address_姓名全拼”,表中字段信息如下:
字段名 | 数据类型 | 长度 | 主、外键 | 其他约束 | 备注信息 |
id | char | 16 | 主键 | 地址ID (A_DATE_XXXXX) | |
buyer_id | char | 16 | 外键(buyer.id) | 非空 | 买家ID |
contact_phone | char | 11 | 非空 | 收货人联系方式 | |
detail_address | text | 非空 | 详细地址 | ||
is_default | enum(“yes”,”no”) | 默认 no | 是否默认地址 |
create table address_yangbin(
id char(16) primary key comment"地址ID (A_DATE_XXXXX)",
buyer_id char(16) not null comment"买家ID",
contact_phone char(11) not null comment"收货人联系方式",
detail_address text not null comment"详细地址",
is_default enum("yes","no") default"no" comment"是否默认地址",
constraint fk_address_fengyi_buyer_id foreign key (buyer_id) references buyer_yangbin(id)
);
7. 创建产品种类表“product_type_姓名全拼”,表中字段信息如下:
字段名 | 数据类型 | 长度 | 主、外键 | 其他约束 | 备注信息 |
code | char | 6 | 主键 | 产品种类编码(TXXXXX) | |
name | varchar | 30 | 非空 | 产品种类名称 |
create table product_type_yangbin(
code char(6) primary key comment"产品种类编码(TXXXXX)",
name varchar(30) not null comment"产品种类名称"
);
创建产品表“product_姓名全拼”,表中字段信息如下:
字段名 | 数据类型 | 长度 | 主、外键 | 其他约束 | 备注信息 |
id | char | 16 | 主键 | 产品编号(P_DATE_XXXXX) | |
seller_id | char | 16 | 外键(seller.id) | 非空 | 卖家ID |
type_id | char | 6 | 外键(product_type.code) | 非空 | 产品种类编码 |
name | varchar | 100 | 非空 | 产品名称 | |
picture | text | 产品展示图 | |||
unit_price | double | 非空 | 单价 | ||
quantity | int | 10 | 默认 100 | 库存数量 |
create table product_yangbin(
id char(16) primary key comment"产品编号(P_DATE_XXXXX)",
seller_id char(16) not null comment"买家ID",
type_id char(6) not null comment"产品种类编码",
name varchar(100) not null comment"产品名称",
picture text comment"产品展示图",
unit_price double not null comment"单价",
quantity int(10) default"100" comment"库存数量",
constraint fk_product_yangbin_seller_id foreign key (seller_id) references seller_yangbin (id),
constraint fk_product_yangbin_type_id foreign key (type_id ) references product_type_yangbin (code)
);
创建订单表“order_姓名全拼”,表中字段信息如下:
字段名 | 数据类型 | 长度 | 主、外键 | 其他约束 | 备注信息 |
id | char | 16 | 主键 | 订单编号(O_DATE_XXXXX) | |
seller_id | char | 16 | 外键(seller.id) | 非空 | 卖家ID |
buyer_id | char | 16 | 外键(buyer.id) | 非空 | 买家ID |
address_id | char | 16 | 外键(address.id) | 非空 | 地址ID |
total_price | double | 默认0 | 总价 | ||
actrual_payment | double | 默认0 | 实付款 |
create table order_yangbin(
id char(16) primary key comment"订单编号(O_DATE_XXXXX)",
seller_id char(16) not null comment"卖家ID",
buyer_id char(16) not null comment"买家ID",
address_id char(16) not null comment"地址ID",
total_price double default"0" comment"总价",
actrual_payment double default"0" comment"实付款",
constraint fk_order_yangbin_seller_id foreign key (seller_id) references seller_yangbin (id),
constraint fk_order_yangbin_buyer_id foreign key (buyer_id) references buyer_yangbin (id),
constraint fk_order_yangbin_address_id foreign key (address_id) references address_yangbin (id));
创建订单详情表“order_detail_姓名全拼”,表中字段信息如下:
字段名 | 数据类型 | 长度 | 主、外键 | 其他约束 | 备注信息 |
id | int | 10 | 主键 | 自增 | |
order_id | char | 16 | 外键(order.id) | 非空 | 订单编号 |
product_id | char | 16 | 外键(product.id) | 非空 | 产品编号 |
purchase_quantity | int | 3 | 默认1 | 采购数量 | |
discount_unit_price | double | 非空 | 产品折后价 |
create table order_detail_yangbin(
id int(10) primary key auto_increment,
order_id char(16) not null comment"订单编号",
product_id char(16) not null comment"产品编号",
purchase_quantity char(16) default"1" comment"采购数量",
discount_unit_price double not null comment"产品折后价",
constraint fk_order_detail_yangbin_order_id foreign key (order_id) references order_yangbin (id),
constraint fk_order_detail_yangbin_product_id foreign key (product_id) references product_yangbin (id));
任务三、对表中数据进行基本操作
- 所有字段批量插入用户表数据
username | password | question | answer | |
13812345678 | anne | annnepassword | favorite book | harry potter |
18212345678 | frank | Frankpassword | Favorite song | lonely |
13212345678 | alan | Alanpassword | First love | carry |
13112345678 | peter | Peterpassword | Who is your father | jack |
insert into user_yangbin (phone,username,password,question,answer) values
(13812345678,"anne","annnepassword","favorite book","harry potter"),
(18212345678,"frank","Frankpassword","Favorite song","lonely"),
(13212345678,"alan","Alanpassword","First love","carry"),
(13112345678,"peter","Peterpassword","Who is your father","jack");
所有字段批量插入卖家信息表数据
phone | open_date | name | nickname | |
S_20200703_00001 | 13812345678 | 2020-07-03 | ledin | ledin |
S_20201030_00001 | 18212345678 | 2020-10-30 | hla | hla |
insert into seller_yangbin values
("S_20200703_00001",13812345678,"2020-07-03","ledin","ledin"),
("S_20201030_00001",18212345678,"2020-10-30","hla","hla");
指定字段批量插入买家信息表数据
phone | nickname | height | weight | |
B_20200422_00001 | 13212345678 | funny shop | 168 | 52 |
B_20200911_00001 | 13112345678 | cool girl | 165 | 47 |
- 指定字段批量插入地址表数据
id | buyer_id | contact_phone | detail_address |
A_20201103_00004 | B_20200422_00001 | 13212345678 | gray street |
A_20201103_00005 | B_20200422_00001 | 13212345678 | funny street |
A_20201103_00006 | B_20200422_00001 | 13212345678 | frank street |
A_20201103_00007 | B_20200911_00001 | 13112345678 | rock street |
- 所有字段批量插入产品种类表数据
name | |
T00001 | coat |
T00002 | shirt |
T00003 | shorts |
T00004 | pants |
T00005 | jeans |
T00006 | polo |
- 指定字段插入产品表数据
id | seller_id | type_id | name | picture | unit_price |
P_20190102_00001 | S_20200703_00001 | T00003 | blue shorts | p123.jpg | 168.8 |
- 所有字段插入产品表数据
seller_id | type_id | name | picture | unit_price | quantity | |
P_20190102_00002 | S_20200703_00001 | T00001 | coat | coat1.jpg | 62.2 | 43 |
- 指定字段插入产品表数据
id | seller_id | type_id | name | unit_price |
P_20190203_00001 | S_20201030_00001 | T00006 | black polo | 239.9 |
- 所有字段插入产品表数据
id | seller_id | type_id | name | picture | unit_price | quantity |
P_20190203_00002 | S_20201030_00001 | T00005 | jeans | 12.jpg | 198.8 | 23 |
- 查看产品表所有字段数据
- 订单表指定字段插入数据
id | seller_id | buyer_id | address_id |
O_20201102_00001 | S_20200703_00001 | B_20200422_00001 | A_20201103_00004 |
- 订单详情表指定字段插入数据
order_id | product_id | purchase_quantity | discount_unit_price |
O_20201102_00001 | P_20190102_00001 | 1 | 150 |
O_20201102_00001 | P_20190102_00002 | 2 | 40 |
- 修改订单详情表中O_20201102_00001订单P_20190102_00002产品的采购数量为1
- 查看O_20201102_00001订单的订单编号、产品编号、库存数量、采购数量、采购后数量(库存数量-采购数量)、产品单价、折后单价
- 修改产品表中库存数量为采购后数量
- 根据订单号分组查看订单号、订单总价(sum(采购数量*产品单价))、实付款(sum(采购数量*折扣单价))
- 根据上述代码计算出的值修改订单表中O_20201102_00001订单的总价、实付款数据
- 查看O_20201102_00001订单的订单编号、店铺名称、买家昵称、详细地址、产品名称、采购数量、折后价格