一.创建表
1.本次共使用到了5张表,分别是
(1)Users(用户表),
(2)Orders(订单表),
(3)Types(商品类型表),
(4)Products(商品表),
(5)Details(详细信息表)
具体的字段和结构如下图所示:
2.设计表
(1)创建数据库
create database mall;
(2)Users
create table users(uid int primary key auto_increment, name varchar(20) not null unique, pass varchar(20) not null, phone varchar(11) );
insert into users values(null, 'wukong', '888888', '13333333333');
insert into users values(null, 'bajie', '888888', '13999999999');
(3)Orders
create table orders(oid varchar(32) primary key, price double, addr varchar(40), payType varchar(10), uid int);
insert into orders values(replace(uuid(), '-', ''), 30998, 'beijingxisanqi', 'zhibubao', 1);
(4)Types
create table types(tid varchar(32) primary key, name varchar(20) not null unique);
insert into types values(replace(uuid(), '-', ''), 'house'),(replace(uuid(), '-', ''), 'digit'), (replace(uuid(), '-', ''), 'sports');
(5)Products
create table products(pid varchar(32) primary key, name varchar(20), img varchar(40), price double, tid varchar(32) references types(tid));
insert into products values(replace(uuid(), '-', ''), 'mac pro', 'mac.jpg', 21999, 'dd6501cb628111eaad320242ac110003');
//最后一个 value为 Types表的tid值,即uuid产生的随机值
(6)Details
create table details(did varchar(32) primary key, count int, pid varchar(32), oid varchar(32));
Insert into details values(replace(uuid(), '-', ''), 2, '321607c2629b11eaad320242ac110003', 'd44e970b629d11eaad320242ac110003');
Insert into details values(replace(uuid(), '-', ''), 1, 'ba23b88a629b11eaad320242ac110003', 'd44e970b629d11eaad320242ac110003');
Insert into details values(replace(uuid(), '-', ''), 1, 'f07de571628211eaad320242ac110003', 'd44e970b629d11eaad320242ac110003');
(7)查询用户的所有订单信息
select u.name, u.phone, p.name, p.price, d.count, t.name, o.price
from users u
inner join orders o on o.uid = u.uid
inner join details d on o.oid = d.oid
inner join products p on d.pid = p.pid
inner join types t on p.tid = t.tid;
select u.name, u.phone, p.name, p.price, d.count, t.name, o.price
from users u
inner join orders o on o.uid = u.uid
inner join details d on o.oid = d.oid
inner join products p on d.pid = p.pid
inner join types t on p.tid = t.tid
where u.name = 'wukong';
select u.name, u.phone, p.name, p.price, d.count, t.name, o.price
from users u
inner join orders o on o.uid = u.uid
inner join details d on o.oid = d.oid
inner join products p on d.pid = p.pid
inner join types t on p.tid = t.tid
where o.oid = 'd44e970b629d11eaad320242ac110003';
今天是我在千峰线上学习的第22天,加油!!!