You are on page 1of 6

sonnv22@fpt.edu.

vn

Sale management database has relations as following:


Customer(customerID, customerName, customerPhone, customerAddress)
It stores the information of customers
Staff(staffID, staffName, staffPhone, staffAddress)
It stores the information of staff
Product(productID, productName, Unit, Price)
It stores the information of products
Bill(billID, billDate, customerID, staffID)
It stores the information of bills
BillDetail(billID, productID, quantity)
It stores the information of bill details

Part 1:
Students have to complete all following tasks:
1/ Logic database design: use Draw.io tool to create ERD (Entity relationship
diagram).

2/ Physical database design: Detailed description of each table in the database


Table: Customer
Field name Data type Allow nulls Description
customerID varchar(6) X Customer code, primary key
sonnv22@fpt.edu.vn

customerName varchar(50) X Full name of customer


customerPhone varchar(10)  Phone number of customers
customerAddress varchar(50)  Address Information of customer

Table: Staff
Field name Data type Allow nulls Description
staffID varchar(6) X Staff code, primary key
staffName varchar(50)  Full name of staff
staffPhone varchar(10)  Phone number of staff
staffAddress varchar(50)  Address information of staff

Table: Product
Field name Data type Allow nulls Description
prodcutID varchar(6) X Product code, primary key
productName varchar(50)  Name of product
Unit varchar(10) X Unit of product
Price float  Price of product

Table: Bill
Field name Data type Allow nulls Description
billID varchar(6)  Bill code, primary key
billDate date X Bill date
customerID varchar(6)  Customer code, foreign key
staffID varchar(6)  Staff code, foreign key

Table: BillDeatail
Field name Data type Allow nulls Description
billID varchar(6)  Bill code, foreign key
productID varchar(6)  Product code, foreign key
quantity int X Quantity of bills

3/ Create Databse called “Sales” and all its tables by SQL


create database Sales
use Sales
go

create table Customer(


customerID varchar(6) primary key not null,
customerName varchar(50),
customerPhone varchar(15),
customerAddress varchar(50)
);
create table Staff(
sonnv22@fpt.edu.vn

staffID varchar(6) primary key not null, staffName varchar(50), staffPhone


varchar(10), staffAddress varchar(50)
);
create table Product(
productID varchar(6) primary key not null, productName varchar(50),
Unit varchar(10),
Price float,
);
create table Bill(
billID varchar(6) primary key not null, billDate date,
customerID varchar(6),
staffID varchar(6),
foreign key (customerID) references Customer, foreign key (staffID) references Staff,
);
create table BillDetail(
billID varchar(6),
productID varchar(6),
quantity float,
foreign key (billID) references Bill, foreign key (productID) references Product,
constraint PK_BD primary key (billID,ProductID)
)

4/ Show database diagram:

5/ Add data into all tables:


Table Staff
insert into Staff(staffID, staffName, staffPhone, staffAddress)
values('01','Long','0812211521','10 Le Duan');
insert into Staff(staffID, staffName, staffPhone, staffAddress)
values('02','Tuan','0990235482','20 Nguyen Thi Minh Khai');
insert into Staff(staffID, staffName, staffPhone, staffAddress)
values('03','Ngoc','0908355746','100 Le Loi');
insert into Staff(staffID, staffName, staffPhone, staffAddress)
values('04','Hoang','0900612253','15 Hai Ba Trung');
go

Table Customer
sonnv22@fpt.edu.vn

insert into Customer(customerID,customerName,customerPhone,customerAddress)


values('01', 'Quang Minh', '0908355836','3 Le Loi');
insert into Customer(customerID,customerName,customerPhone,customerAddress)
values('02', 'Hoang Long', '0903658459','1 Le Lai');
insert into Customer(customerID,customerName,customerPhone,customerAddress)
values('03', 'Lam Nghi', '0309626658','3 Nam Ky Khoi Nghia');
go

Table Product

insert into Product(productID, productName, Unit, Price) values('LAP01','Laptop


Dell','piece',500);
insert into Product(productID, productName, Unit, Price) values('LAP02','Laptop
Lenovo','piece',600);
insert into Product(productID, productName, Unit, Price) values('IP01','Ipad Apple
x','piece',300);
insert into Product(productID, productName, Unit, Price) values('IP02','Ipad
Samsung','piece',400);
go

Table Bill
insert into Bill(billID, billDate, staffID, customerID) values ('222',
'04/04/2020','01','01');
insert into Bill(billID, billDate, staffID, customerID) values ('333',
'03/04/2020','03','01');
go

Table BillDetail
insert into BillDetail(billID,productID,quantity) values('111','LAP02',10);
insert into BillDetail(billID,productID,quantity) values('111','IP01',20);
insert into BillDetail(billID,productID,quantity) values('222','IP01',5);
insert into BillDetail(billID,productID,quantity) values('222','IP02',3);
insert into BillDetail(billID,productID,quantity) values('222','LAP01',30);
insert into BillDetail(billID,productID,quantity) values('333','LAP01',1);
insert into BillDetail(billID,productID,quantity) values('333','LAP02',2);
insert into BillDetail(billID,productID,quantity) values('333','IP01',3);
insert into BillDetail(billID,productID,quantity) values('333','IP02',4);
go

Part 2:
1/ List all items with informations as following table:
sonnv22@fpt.edu.vn

select B.billID, B.billDate, C.customerName,S.staffName, P.productName,


P.Price, BD.quantity
from Bill B, Customer C, Staff S, Product P, BillDetail BD
where B.billID = BD.billID and B.customerID =
C.customerID and B.staffID = S.staffID and BD.productID
= P.productID;

2/ List all items with informations as following table:


Amount = Price * Quantity

------------------------------------------------------
select B.billID, B.billDate, C.customerName,S.staffName, P.productName,
P.Price, BD.quantity, P.Price * BD.quantity as Amount from Bill B,
Customer C, Staff S, Product P, BillDetail BD where B.billID =
BD.billID and B.customerID = C.customerID and B.staffID = S.staffID
and BD.productID = P.productID;

3/ List all items with informations as following table and Amount >= 1000:

select B.billID, B.billDate, C.customerName,S.staffName, P.productName,


P.Price, BD.quantity, P.Price * BD.quantity as Amount from Bill B,
Customer C, Staff S, Product P, BillDetail BD where B.billID =
BD.billID and B.customerID = C.customerID and B.staffID = S.staffID
and BD.productID = P.productID
and P.Price * BD.quantity > = 1000;

4/ Statistics by staff as the following table:


sonnv22@fpt.edu.vn

-------------------Statatics by staff------------------------
select S.staffID,S.staffName, sum(BD.quantity * P.Price) as Revenue
from Staff S, Bill B, BillDetail BD, Product P where S.staffID =
B.staffID
and B.billID = BD.billID
and BD.productID = P.productID
group by S.staffID, S.staffName;

5/ Statistics by customers as the following table:

-------------------Statatics by Customer------------------------ select


C.customerID, C.customerName, SUM(BD.quantity * P.Price) as Total from
Customer C, Bill B, BillDetail BD, Product P where C.customerID =
B.customerID
and B.billID = BD.billID
and Bd.productID = P.productID
group by C.customerID, C.customerName;

6/ Statistics by products as the following table:

-------------------Statatics by Products------------------------ select


P.productID,P.productName, Sum (BD.quantity) as Quantity,
Sum(BD.quantity * P.Price) as Revenue
from BillDetail BD, Product P where
Bd.productID = P.productID group by
P.productID,P.productName go

You might also like