Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 11

MARCH 20, 2018

ASSIGNMENT
DBI202 – DATABASE SYSTEM OF DORM
FPT UNIVERSITY
STUDENT NAME: PHAM NGOC HOA | SE05740
STUDENT NAME: NGUYEN HAI NAM | SE05123
Teacher: nguyen quynh chi
DBI202

TABLE OF CONTENTS
I) INTRODUCE THE PROBLEM…………………………………………………………………………………………….
1) DESCRIBE THE PROBLEM……………………………………………………………………………………………………….
2) MANAGEMENT OBJECTIVES…………………………………………………………………………………………………..

II) ENTITY – RELATIONSHIP – ER-CODE…………………………………………………………………


1) DIFINITION ENTITY – ATTRIBUTE…………………………………………………………………………………………..
2) SET-UP ENTITY – RELATIONSHIP…………………………………………………………………………………………….
3) INSERT CODE……………………………………………………………………………………………………………………….

I) Introduce the problem

I) INTRODUCE THE PROBLEM


1) DESCRIBE THE PROBLEM
1. Introduction
DBI202

This report describes the structure and management of a database designed to manage the
information of a sample company. This database was developed to support the company's daily
business operations, including managing suppliers, customers, products, employees, orders, and order
details.

2. Database Structure

This database includes the following main tables:

NhaCungCap: Stores information about commodity suppliers.


LoaiSanPham: Contains information about product types.
SanPham: Used to store detailed information about products.
NhanVien: Save information about the company's employees.
HoaDon: This table contains information about orders.
HoaDonChiTiet: Stores detailed information about the items in each order.
KhachHang: Contains information about the company's customers.
3. Primary Key and Foreign Key

Each table in the database has a primary key that uniquely identifies each record. Additionally,
relationships between tables are defined through foreign keys.

4. Action Matters

Add Data: Data has been added to the tables, including information about suppliers, products,
customers, employees, and orders.
Updating Data: Some update operations were performed to calculate fields such as Total Amount in the
HoaDonChiTiet table and Total Amount in the HoaDon table.
Triggers and Procedures: A trigger has been created to update the Delivery Date when a new order is
added. A procedure is also created to get information about a product based on the product ID.
5. Normalization Process

Normalization was performed to ensure data integrity and minimize duplication. Normalization steps
include converting HoaDonChiTiet to 1NF, removing columns that do not match 2NF and 3NF.

6. Query Operations
DBI202

This database supports many query operations to retrieve, update and process data, including statistics
on sales, total number of products and total revenue.

7. Report Format Integration

A view has been created to generate an overview of orders, including information about customers,
products, and quantities sold.

8. Conclusion

The database is designed with a clear structure and supports the daily business activities of the sample
company. Normalization and primary key-foreign key rules have been implemented to ensure data
integrity and performance.
2) MANAGEMENT OBJECTIVES
-OUTPUT:
1.SUM TOTAL MONEY CUSTOMER
2. DISPLAY ITEMS
3.SUM TOTAL ITEMS
4.THE NUMBERS OF PRODUCTS

II) ENTITY – RELATIONSHIP – ER


1) DIFINITION ENTITY – ATTRIBUTE
Base on the problem description and management objectives, we can present several entities and
attributes of the entity as follow:

1. Bảng NhaCungCap:

Khóa Chính (Primary Key): MaNhaCC (nvarchar(10)) - Định danh duy nhất cho mỗi nhà cung cấp.

Các Thuộc Tính (Attributes):

MaNhaCC: Mã nhà cung cấp.

TenNhaCC: Tên nhà cung cấp.

DiaChi: Địa chỉ nhà cung cấp.


DBI202

Phone: Số điện thoại liên hệ.

Fax: Số fax của nhà cung cấp.

HomePage: Địa chỉ trang web của nhà cung cấp.

2. Bảng LoaiSanPham:

Primary Key: MaLoaiSP (nvarchar(10)) - Unique identifier for each product type.

Attributes:

MaLoaiSP: Product type code.

TenLoaiSP: Product type name.

MoTa: Description of the product type.

3. SanPham Table:

Primary Key: MaSP (nvarchar(10)) - Unique identifier for each product.

Attributes:

MaSP: Product code.

TenSP: Product name.

MaNhaCC: Foreign key referring to MaNhaCC in the NhaCungCap table.

MaLoaiSP: Foreign key referring to MaLoaiSP in the LoaiSanPham table.

DVT: Unit of product.

DonGia: Price of the product.

SoLuong: Inventory quantity.

SoLuongTT: Minimum quantity in stock.

Discontinued: Product status.


DBI202

4. NhanVien table:

Primary Key: MaNV (nvarchar(10)) - Unique identifier for each employee.

Attributes:

MaNV: Employee code.

HoTen: Full name of employee.

Title: Employee's title.

Date of Birth: Employee's date of birth.

DateNhanViec: Date of starting work.

DiaChi: Employee address.

Phone: Contact phone number.

Photo: Staff image.

GhiChu: Notes.

5. HoaDon Table:

Primary Key: MaHD (nvarchar(10)) - Unique identifier for each order.

Attributes:

MaHD: Order code.

MaKH: Foreign key refers to MaKH in the KhachHang table.

MaNV: Foreign key referring to MaNV in NhanVien table.

DateLapHD: Date of order creation.

DateNhanHang: Date of receipt of goods.


DBI202

Money: Total amount of the order.

Tax: Tax of the order.

TongSoTien: Total amount after tax calculation.

6. HoaDonChiTiet table:

Primary Key: None, but MaHD and MaSP are used as foreign keys.

Attributes:

MaHD: Foreign key referencing MaHD in HoaDon table.

MaSP: Foreign key referring to MaSP in SanPham table.

DonGia: Unit price of the product in the order.

SoLuong: Number of products in the order.

GiamGia: Discount applies to products.

ThanhTien: Cost of the product after applying the discount.

7. KhachHang Table:

Primary Key: MaKH (nvarchar(10)) - Unique identifier for each customer.

Attributes:

MaKH: Customer code.

TenKH: Customer name.

DiaChi: Customer address.

Phone: Contact phone number.

Email: Customer's email address.


DBI202

2) SET-UP ENTITY – RELATIONSHIP


* Some symbols used in the model
 Key Attibute

 Attribute description / description Attribute Attribute

 Entity ENTITY

 Relationship
Relationship
 Connectivity (force) = 1

 Connectivity = N
DBI202

3) INSERT CODE
1.CREATE TABLE
CREATE TABLE NhaCungCap(
MaNhaCC nvarchar(10) not null,
TenNhaCC nvarchar(40),
DiaChi nvarchar(60),
Phone nvarchar(24),
Fax nvarchar(24),
HomePage ntext
)
CREATE TABLE LoaiSanPham(
MaLoaiSP nvarchar(10) not null,
TenLoaiSP nvarchar(15),
MoTa ntext
)
CREATE TABLE SanPham(
MaSP nvarchar(10) not null,
DBI202

TenSP nvarchar(40),
MaNhaCC nvarchar(10),
MaLoaiSP nvarchar(10),
DVT nvarchar(20),
DonGia smallint,
SoLuong smallint,
SoLuongTT smallint,
Discontinued bit
)
CREATE TABLE NhanVien(
MaNV nvarchar(10) not null,
HoTen nvarchar(20),
ChucDanh nvarchar(30),
NgaySinh date,
NgayNhanViec date,
DiaChi nvarchar(50),
Phone nvarchar(20),
Photo image,
GhiChu ntext
)
CREATE TABLE HoaDon(
MaHD nvarchar(10) not null,
MaKH nvarchar(10),
MaNV nvarchar(10),
NgayLapHD smalldatetime,
NgayNhanHang smalldatetime,
Tien int,
Thue real,
TongSoTien int
)
CREATE TABLE HoaDonChiTiet(
MaHD nvarchar(10),
MaSP nvarchar(10),
DonGia int,
SoLuong smallint,
GiamGia real,
ThanhTien int
)
CREATE TABLE KhachHang(
MaKH nvarchar(10) not null,
TenKH nvarchar(30),
DiaChi ntext,
Phone nvarchar(12),
Email nvarchar(30)
)
ALTER TABLE NhaCungCap add constraint pk_MaNhaCC primary key(MaNhaCC)
ALTER TABLE LoaiSanPham add constraint pk_MaLoaiSP primary key(MaLoaiSP)
ALTER TABLE SanPham add constraint pk_MaSP primary key(MaSP)
ALTER TABLE NhanVien add constraint pk_MaNV primary key(MaNV)
ALTER TABLE HoaDon add constraint pk_MaHD primary key(MaHD)
ALTER TABLE KhachHang add constraint pk_MaKH primary key(MaKH)
ALTER TABLE SanPham add constraint fk_MaNhaCC foreign key (MaNhaCC) references
NhaCungCap(MaNhaCC),
constraint fk_MLoaiSP foreign key(MaLoaiSP) references LoaiSanPham(MaLoaiSP)
Alter table HoaDon add constraint fk_MaNV foreign key(MaNV) references NhanVien(MaNV),
constraint fk_MaKH foreign key(MaKH) references KhachHang(MaKH)
ALTER TABLE HoaDonChiTiet add constraint fk_MaHD foreign key(MaHD) references
HoaDon(MaHD),
DBI202

constraint fk_MaSP foreign key(MaSP) references SanPham(MaSP)


2.SUBQUERY
SELECT * FROM HoaDonChiTiet
SELECT H.MaHD,Tien,ThanhTien FROM HoaDon H JOIN HoaDonChiTiet HD on H.MaHD=HD.MaHD

UPDATE HoaDon set Tien=(Select SUM(ThanhTien) from HoaDonChiTiet where MaHD=HoaDon.MaHD)


SELECT * FROM HoaDon
Update HoaDon set TongSoTien=Tien*(1-Thue)
SELECT TenLoaiSP,MaSP,TenSP FROM LoaiSanPham L JOIN SanPham S on L.MaLoaiSP=S.MaLoaiSP
SELECT TenSP,SUM(H.SoLuong) AS Total FROM SanPham S join HoaDonChiTiet H on S.MaSP=H.MaSP
GROUP BY TenSP
SELECT TenSP,SUM(ThanhTien) AS Total_money FROM SanPham S JOIN HoaDonChiTiet H ON
S.MaSP=H.MaSP
GROUP BY TenSP ORDER BY Total_money DESC
SELECT MaHD,h.MaSP,TenSP,h.DonGia,h.SoLuong,GiamGia,ThanhTien FROM SanPham s join
HoaDonChiTiet h on s.MaSP=h.MaSP
where GiamGia>0.01 AND ThanhTien<10000
SELECT TenLoaiSP,SUM(H.SoLuong) AS SO_LUONG FROM LoaiSanPham L INNER JOIN SanPham S on
L.MaLoaiSP=S.MaLoaiSP
INNER JOIN HoaDonChiTiet H ON S.MaSP=H.MaSP
GROUP BY TenLoaiSP
3.TRIGGER,PROCEDURE,VIEW
CREATE TRIGGER UpdateNgayNhanHang
ON HoaDon
AFTER INSERT
AS
BEGIN
UPDATE HoaDon
SET NgayNhanHang = GETDATE() -- Set NgayNhanHang to current date and time khi insert
WHERE MaHD IN (SELECT MaHD FROM inserted)
END;
CREATE PROCEDURE GetProductInfo
@ProductID nvarchar(10)
AS
BEGIN
SELECT *
FROM SanPham
WHERE MaSP = @ProductID;
END;
EXEC GetProductInfo '001';
CREATE VIEW SalesSummary AS
SELECT
HD.MaHD AS 'OrderID',
KH.TenKH AS 'CustomerName',
SP.TenSP AS 'ProductName',
SP.DonGia AS 'UnitPrice',
HDT.SoLuong AS 'Quantity',
HDT.ThanhTien AS 'TotalPrice'
FROM
HoaDon HD
INNER JOIN KhachHang KH ON HD.MaKH = KH.MaKH
INNER JOIN HoaDonChiTiet HDT ON HD.MaHD = HDT.MaHD
INNER JOIN SanPham SP ON HDT.MaSP = SP.MaSP;
SELECT * FROM SalesSummary

You might also like