任务一.安装并配置MySQL
1.打开控制台
2.登录MySQL
任务二.数据库、表的基本操作
1.创建电子商城数据库“mall_姓名全拼”
mysql> create database mall_77;
2.使用电子商城数据库
use mall_77;
3.创建用户表“user_姓名全拼”,表中字段信息如下:
字段名 | 数据类型 | 长度 | 主、外键 | 其他约束 | 备注信息 |
char | 11 | 主键 | 注册手机号 | ||
username | varchar | 20 | 非空,唯一 | 用户名 | |
password | varchar | 20 | 非空 | 密码 | |
question | text | 非空 | 找回密码问题 | ||
answer | text | 非空 | 找回密码问题答案 |
mysql> create table user_77(
-> phone char(11) comment"注册手机号" primary key,
-> username varchar(20) not null unique comment"用户名",
-> password varchar(20) not null comment"密码",
-> question text not null comment"找回密码问题",
-> answer text not null comment"找回密码问题答案");
4.创建卖家信息表“seller_姓名全拼”,表中字段信息如下:
字段名 | 数据类型 | 长度 | 主、外键 | 其他约束 | 备注信息 |
id | char | 16 | 主键 | 卖家ID(S_DATE_XXXXX) | |
phone | char | 11 | 外键(user.phone) | 非空,唯一 | 注册手机号 |
open_date | date | 非空 | 开业时间 | ||
name | varchar | 50 | 非空 | 店铺名称 | |
nickname | varchar | 30 | 非空 | 掌柜昵称 |
mysql> create table seller_77(
-> 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_phone foreign key(phone) references user_77(phone)
-> );
5.创建买家信息表“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 |
mysql> create table buyer_77(
-> id char(16) primary key comment "卖家ID(S_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_phone2 foreign key(phone) references user_77(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_77(
-> 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_buyer_id foreign key(buyer_id) references buyer_77(id)
-> );
7.创建产品种类表“product_type_姓名全拼”,表中字段信息如下:
字段名 | 数据类型 | 长度 | 主、外键 | 其他约束 | 备注信息 |
code | char | 6 | 主键 | 产品种类编码(TXXXXX) | |
name | varchar | 30 | 非空 | 产品种类名称 |
create table product_type_77(
-> code char(6) primary key comment"产品种类编码(TXXXXX)",
-> name varchar(30) not null comment "产品种类名称"
-> );
8.创建产品表“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_77(
-> 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_seller_id foreign key(seller_id) references seller_77(id),
-> constraint fk_type_id foreign key(type_id) references product_type_77(code)
-> );
9.创建订单表“order_姓名全拼”,表中字段信息如下:
create table order_77(
-> 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_seller_id2 foreign key(seller_id) references seller_77(id),
-> constraint fk_buyer_id2 foreign key(buyer_id) references buyer_77(id),
-> constraint fk_address_id foreign key(address_id) references address_77(id)
-> );
10.创建订单详情表“order_detail_姓名全拼”,表中字段信息如下:
create table order_detail_77(
-> id int(10) primary key auto_increment,
-> order_id char(16) not null comment "订单编号",
-> product_id char(16) not null comment "产品编号",
-> purchase_quantity int(3) default"1" comment "采 购数量",
-> discount_unit_price double not null comment "产 品折后价",
-> constraint fk_order_id foreign key(order_id) references order_77(id),
-> constraint fk_product_id foreign key(product_id) references product_77(id)
-> );