IT Learning HubIT Learning Hub
SHARED SCHEMA · IT004

Sales Management Database

5 relationsBTTH2 · BTTH3 · BTTH5

This schema is shared across several practice exercises from Week 2 through Week 5. Read the definitions and attribute tables carefully before starting - understanding what the data actually means makes writing accurate queries far easier than just memorizing column names.

OVERVIEW

The sales management database consists of the following relations

KHACHHANG (MAKH, HOTEN, DCHI, SODT, NGSINH, DOANHSO, NGDK)

Definition: The customer relation stores information for member customers, including: customer ID, name, address, phone number, date of birth, registration date, and sales total (the total value of invoices for this member customer).

NHANVIEN (MANV, HOTEN, NGVL, SODT)

Definition: Each sales employee needs a recorded name, hire date, and contact phone number; employees are distinguished from one another by employee ID.

SANPHAM (MASP, TENSP, DVT, NUOCSX, GIA)

Definition: Each product has a code, a name, a unit of measure, a country of manufacture, and a selling price.

HOADON (SOHD, NGHD, MAKH, MANV, TRIGIA)

Definition: When making a purchase, each customer receives an invoice, which records the invoice number, purchase date, the employee who made the sale, the invoice value, and the customer's ID if they're a member customer.

CTHD (SOHD, MASP, SL)

Definition: Details the line items within each invoice - which products, and in what quantity.

Relationship Diagram

KHACHHANG HOADON NHANVIEN CTHD SANPHAM

Relationship Description

KHACHHANG (1) ➜ HOADON (N): a member customer can have multiple invoices.

NHANVIEN (1) ➜ HOADON (N): an employee sells multiple invoices.

HOADON (1) ➜ CTHD (N): an invoice has multiple line items.

SANPHAM (1) ➜ CTHD (N): a product appears in multiple line items, across multiple different invoices.

Common mix-up: HOADON.MAKH can be left blank (NULL) - not every buyer is a member customer. See invoices 1022 and 1023 in the sample data; neither has a MAKH attached.

RELATION 1 · KHACHHANG

KHACHHANG

Stores information for member customers: customer ID, name, address, phone number, date of birth, membership registration date, and sales total (the total value of invoices this member customer has purchased).
AttributeData typeDescription
MAKHchar(4)Customer ID - primary key
HOTENvarchar(40)Full name
DCHIvarchar(50)Address
SODTvarchar(20)Phone number
NGSINHsmalldatetimeDate of birth
NGDKsmalldatetimeMembership registration date
DOANHSOmoneyTotal value of invoices purchased

SAMPLE DATA (all 10 customers)

MAKHHOTENDCHISODTNGSINHNGDKDOANHSO
KH01Nguyen Van A731 Tran Hung Dao, Q5, TpHCM882345122/10/196022/07/200613.060.000
KH02Tran Ngoc Han23/5 Nguyen Trai, Q5, TpHCM90825647803/04/197430/07/2006280.000
KH03Tran Ngoc Linh45 Nguyen Canh Chan, Q1, TpHCM93877626612/06/198008/05/20063.860.000
KH04Tran Minh Long50/34 Le Dai Hanh, Q10, TpHCM91732547609/03/196510/02/2006250.000
KH05Le Nhat Minh34 Truong Dinh, Q3, TpHCM824610810/03/195028/10/200621.000
KH06Le Hoai Thuong227 Nguyen Van Cu, Q5, TpHCM863173831/12/198124/11/2006915.000
KH07Nguyen Van Tam32/3 Tran Binh Trong, Q5, TpHCM91678356506/04/197112/01/200612.500
KH08Phan Thi Thanh45/2 An Duong Vuong, Q5, TpHCM93843575610/01/197113/12/2006365.000
KH09Le Ha Vinh873 Le Hong Phong, Q5, TpHCM865476303/09/197914/01/200770.000
KH10Ha Duy Lap34/34B Nguyen Trai, Q1, TpHCM876890402/05/198316/01/200767.500

RELATION 2 · NHANVIEN

NHANVIEN

Each sales employee is recorded with: employee ID, name, hire date, and contact phone number.
AttributeData typeDescription
MANVchar(4)Employee ID - primary key
HOTENvarchar(40)Full name
SODTvarchar(20)Phone number
NGVLsmalldatetimeHire date

SAMPLE DATA (complete - only 5 employees)

MANVHOTENSODTNGVL
NV01Nguyen Nhu Nhut092734567813/04/2006
NV02Le Thi Phi Yen098756739021/04/2006
NV03Nguyen Van B099704738227/04/2006
NV04Ngo Thanh Tuan091375849824/06/2006
NV05Nguyen Thi Truc Thanh091859038720/07/2006

RELATION 3 · SANPHAM

SANPHAM

Each product has a code, name, unit of measure, country of manufacture, and selling price.
AttributeData typeDescription
MASPchar(4)Product code - primary key
TENSPvarchar(40)Product name
DVTvarchar(20)Unit of measure (piece, box, book, item...)
NUOCSXvarchar(40)Country of manufacture
GIAmoneySelling price

SAMPLE DATA (all 24 products)

MASPTENSPDVTNUOCSXGIA
BC01But chicaySingapore3.000
BC02But chicaySingapore5.000
BC03But chicayViet Nam3.500
BC04But chihopViet Nam30.000
BB01But bicayViet Nam5.000
BB02But bicayTrung Quoc7.000
BB03But bihopThai Lan100.000
TV01Tap 100 giay mongquyenTrung Quoc2.500
TV02Tap 200 giay mongquyenTrung Quoc4.500
TV03Tap 100 giay totquyenViet Nam3.000
TV04Tap 200 giay totquyenViet Nam5.500
TV05Tap 100 trangchucViet Nam23.000
TV06Tap 200 trangchucViet Nam53.000
TV07Tap 100 trangchucTrung Quoc34.000
ST01So tay 500 trangquyenTrung Quoc40.000
ST02So tay loai 1quyenViet Nam55.000
ST03So tay loai 2quyenViet Nam51.000
ST04So tayquyenThai Lan55.000
ST05So tay mongquyenThai Lan20.000
ST06Phan viet banghopViet Nam5.000
ST07Phan khong buihopViet Nam7.000
ST08Bong bangcaiViet Nam1.000
ST09But longcayViet Nam5.000
ST10But longcayTrung Quoc7.000

RELATION 4 · HOADON

HOADON

When making a purchase, a customer receives an invoice: invoice number, purchase date, selling employee, invoice value, and customer ID (if the buyer is a member customer).
AttributeData typeDescription
SOHDintInvoice number - primary key
NGHDsmalldatetimeInvoice date
MAKHchar(4)Which customer made the purchase - foreign key to KHACHHANG, can be NULL
MANVchar(4)Which employee made the sale - foreign key to NHANVIEN
TRIGIAmoneyInvoice value

SAMPLE DATA (all 23 invoices)

SOHDNGHDMAKHMANVTRIGIA
100123/07/2006KH01NV01320.000
100212/08/2006KH01NV02840.000
100323/08/2006KH02NV01100.000
100401/09/2006KH02NV01180.000
100520/10/2006KH01NV023.800.000
100616/10/2006KH01NV032.430.000
100728/10/2006KH03NV03510.000
100828/10/2006KH01NV03440.000
100928/10/2006KH03NV04200.000
101001/11/2006KH01NV015.200.000
101104/11/2006KH04NV03250.000
101230/11/2006KH05NV0321.000
101312/12/2006KH06NV015.000
101431/12/2006KH03NV023.150.000
101501/01/2007KH06NV01910.000
101601/01/2007KH07NV0212.500
101702/01/2007KH08NV0335.000
101813/01/2007KH08NV03330.000
101913/01/2007KH01NV0330.000
102014/01/2007KH09NV0470.000
102116/01/2007KH10NV0367.500
102216/01/2007NULLNV037.000
102317/01/2007NULLNV01330.000

RELATION 5 · CTHD

CTHD (Invoice Line Items)

Details the line items within each invoice: which products it contains, and in what quantity. The primary key is the pair (SOHD, MASP) - since within a single invoice, a product can appear on at most 1 line.
AttributeData typeDescription
SOHDintInvoice number - primary key, foreign key to HOADON
MASPchar(4)Product code - primary key, foreign key to SANPHAM
SLintQuantity purchased

SAMPLE DATA (all 48 rows)

SOHDMASPSL
1001TV0210
1001ST015
1001BC015
1001BC0210
1001ST0810
1002BC0420
1002BB0120
1002BB0220
1003BB0310
1004TV0120
1004TV0210
1004TV0310
1004TV0410
1005TV0550
1005TV0650
1006TV0720
SOHDMASPSL
1006ST0130
1006ST0210
1007ST0310
1008ST048
1009ST0510
1010TV0750
1010ST0750
1010ST08100
1010ST0450
1010TV03100
1011ST0650
1012ST073
1013ST085
1014BC0280
1014BB02100
1014BC0460
SOHDMASPSL
1014BB0150
1015BB0230
1015BB037
1016TV015
1017TV021
1017TV031
1017TV045
1018ST046
1019ST051
1019ST062
1020ST0710
1021ST085
1021TV017
1021TV0210
1022ST071
1023ST046

FULL SCRIPT

Create the database and load sample data

A script that creates the QLBH_2025 database, creates 5 tables with primary/foreign keys, and loads the full set of sample data (10 customers, 5 employees, 24 products, 23 invoices, 48 line items). Run it directly in SSMS before starting the exercises.

Sales Management Database Script (QLBH_2025) .sql file · creates tables, constraints, and the full sample data
Download ↓
Applies to: BTTH2 (Week 2), BTTH3 (Week 3), and the Trigger examples in BTTH5 (Week 5) all share this schema - you only need to run the script once.