Tutorial 2: Retail Chain Management System (RCMS)
Tutorial 2: Retail Chain Management System (RCMS)
In this tutorial, an end to end implementation of a schema for a specific system is shown along with
illustrations of querying using the schema. A system is described by specifying the functional and
non-functional requirements. Based on this description, the major entities are identified and
modeled. Further the relationships are modeled to form the initial schema. The schema is further
refined by removing redundancies through normalization. Also based on the query requirements,
the schema is remodeled to facilitate querying. Finally, an illustration of various queries to extract
required information from the system is shown using MYSQL.
1. System Specifications
2. Design of Initial Schema
3. Schema refinement using functional dependencies and normalization
4. Schema refinement using query requirements
5. Illustration of querying the system using MYSQL
1. SYSTEM SPECIFICATIONS
A retail chain has multiple stores across the city of Mumbai. It specializes in garment items. Every
item is characterized by an item number, name, gender and segment. The segment is required to
differentiate between the materials of the garment. The gender is required to understand whether
the garment is designed for male, female or kids.
The sales of every garment are tracked by its item number and the store from which it is bought.
Hence every sale is characterized by the item number, item name, quantity, date of purchase, the
store details like the store name, store location, store manager name and address.
The inventory is stocked in various warehouses across the city. Every item is stocked in various
warehouses. A certain item can be stocked in more than one warehouse. The quantity stored per
warehouse is maintained along with the item number and warehouse number, location, manager id
and address.
Each of these vendors can also supply the same item in parallel. Also there is no upper limit on the
types of item which can be supplied by these vendors. Type A vendors are most preferred.
Every purchase is tracked through the item number, the vendor details (like id, name, type, and
address), the date of purchase and the date of delivery.
Likewise, a delivery for an item is tracked through the item number, vendor details, the warehouse
number and location (to which the item will be delivered), the actual date of delivery and delay if
applicable.
Employees of the retail chain either work at the store or at the warehouse. For every store worker,
the worker id, name, address is stored along with the store details where the worker is employed. A
worker can work only at one store.
Similarly, for every warehouse worker, the worker id, name, address is stored along with the
warehouse details where the worker is employed. A worker can work only at one warehouse.
A worker cannot be employed at the warehouse and the store simultaneously. He can either work at
the warehouse or store.
4. Find the item with highest sales in the year 2009 for store ‘A’
5. Find the store recording the highest sales of item ‘A’ in the year 2010
6. Find the item with the highest quantity in store in warehouse ‘B’ in the month of July in
2015.
8. Check and list all the vendors who have delivered after the initial date of delivery and
the reasons for delay
9. Check for the workers residing in address ‘C’ and list their names and id.
10. and so on
Entity: item
A retail chain has several sections to manage its operation. It has multiple stores across the city of
Mumbai. It specializes in garment items. Every item is characterized by an item number, name,
gender and segment. The segment is required to differentiate between the materials of the
garment. The gender is required to understand whether the garment is designed for male, female or
kids.
Entity
• item
Attributes:
• item_no
• item_name
• item_segment
• gender
Key: item_no
Entity: sales
The sales of every garment are tracked by its item number and the store from which it is bought.
Hence every sale is characterized by the item number, item name, quantity, date of purchase, the
store details like the store name, store location, store manager name and address.
Entity
• sales
Attributes:
• item_no
• item_name
• item_segment
• quantity
• store_id
• date
• store_name
• store_location
• store_manager_name is composite
• store_manager_id
• store_manager_address
Entity: stock
The inventory is stocked in various warehouses across the city. Every item is stocked in various
warehouses. A certain item can be stocked in more than one warehouse. The quantity stored per
warehouse is maintained along with the item number and warehouse number, location, manager id
and address.
Entity Set:
• stock
Attributes:
• item_no
• quantity_in_store
• month
• year
• warehouse_number
• warehouse_location
• warehouse_manager_name is composite
• warehouse_manager_id
• warehouse_manager_address
A worker cannot be employed at the warehouse and the store simultaneously. He can either work at
the warehouse or store.
Entity Set:
• store workers
Attributes:
• worker_id
• worker_name
• worker_address
• store_id
• store_manager_id
Key: worker_id
Similarly, for every warehouse worker, the worker id, name, address is stored along with the
warehouse details where the worker is employed. A worker can work only at one warehouse.
A worker cannot be employed at the warehouse and the store simultaneously. He can either work at
the warehouse or store.
Entity Set:
• warehouse workers
Attributes:
• worker_id
• worker_name
• worker_address
• warehouse_num
• warehouse_manager_id
Key: worker_id
Entity: vendors
Each of these vendors can also supply the same item in parallel. Also there is no upper limit on the
types of item which can be supplied by these vendors. Type A vendors are most preferred
Entity Set:
• vendor
Attributes:
• vendor_id
• vendor_name
• vendor_address
• vendor_type
Key: vendor_id
Relationship: buy
Every purchase is tracked through the item number, the vendor details (like id, name, type, and
address), the date of purchase and the date of delivery.
Relationship:
• buy
Entities Involved:
Item, Vendor
• item_no
• vendor_id
• vendor_name
• vendor_address
• vendor_type
Relationship Attributes:
• date_purchase
• date_delivery_intial
Relationship: deliver
Likewise, a delivery for an item is tracked through the item number, vendor details, the warehouse
number and location (to which the item will be delivered), the actual date of delivery and delay if
applicable.
Relationship:
• deliver
Entities Involved:
Item, Vendor
• item_no
• vendor_id
• vendor_name is composite
• warehouse_number
• warehouse_location
• date_delivery_intial
• date_delivery_final
• reason_if_delay
The initial schema
3. SCHEMA REFINEMENT USING DEPENDENCIES AND NORMALISATION
In this section, initial schema will be refined by analyzing functional dependencies and normalizing
the schemas to remove the redundancies.
Functional Dependencies
Ø sales(item_no, item_name, item_segment, quantity, store_id, date, store_name,
store_location, store_manager_name, store_manager_id, store_manager_address )
Functional Dependencies
RESULTS IN
Relationship: sales
Ø stock(item_no, quantity_in_store, warehouse_number, warehouse_location,
warehouse_manager_name, warehouse_manager_id, warehouse_manager_address,
month, year)
Functional Dependencies
• warehouse_manager_idàwarehouse_manager_name,
warehouse_manager_address violates 3NF
RESULTS IN
Entity: warehouse
Relationship: stock
Ø store_workers(worker_id, worker_name, worker_address, store_id, store_manager_id)
Functional Dependencies
Ø warehouse_workers(worker_id, worker_name, worker_address, warehouse_number,
warehouse_manager_id)
Functional Dependencies
Ø vendor(vendor_id, vendor_name, vendor_address, vendor_type)
Functional Dependencies
Ø buy(item_no, vendor_id, vendor_name, vendor_address, vendor_type, date_purchase,
date_delivery_intial)
Functional Dependencies
RESULTS IN
Relationship: buy
Ø delivery(item_no, vendor_id, warehouse_number, date_delivery_intial, date_delivery_final,
reason_if_delay)
Functional Dependencies
Refined Schema
warehouse_manager_id)
reason_if_delay)
4. SCHEMA REFINEMENT USING QUERY REQUIREMENTS OF THE SYSTEM
Based on query requirements, the entities will be remodeled in this section.
Query: Check for the workers residing in address ‘C’ and list their names and id.
As the format for Store_id’s and Warehouse numbers are different, we can keep two separate
columns
Entity: workers
Entity Set:
• workers
Attributes:
• worker_id
• worker_name
• worker_address
Key: worker_id
Refined Schema
Ø sales(item_no, store_id, quantity, date) FK: item_no, store_id
Ø item(item_no, item_name, item_segment, gender)
Ø store(store_id, store_name, store_location, store_manager_id)
Ø stock(item_no, warehouse_number, month, year, quantity_in_store) FK: item_no,
warehouse_number
Ø warehouse(warehouse_number, warehouse_location, warehouse_manager_id)
Ø workers(worker_id, worker_name, worker_address, type, warehouse_number, store_id,
manager_id)
Ø buy(item_no, vendor_id, date_purchase, date_delivery_intial) FK: item_no, vendor_id
Ø vendor(vendor_id, vendor_name, vendor_address, vendor_type)
Ø delivery(item_no, vendor_id, date_delivery_intial, warehouse_number,
date_delivery_final, reason_if_delay) FK: item_no, vendor_id, warehouse_number
The underlined attributes are the primary keys of the relation. The foreign key are
mentioned alongside the relation as FK.
5. ILLUSTRATION OF QUERYING THE SYSTEM USING MYSQL
In this section, various instances of querying the system using MYSQL will be shown. The results are
also shown.
(Note: When you will be trying the queries from this section, please don’t copy the queries from this
document and paste it directly in the mysql prompt, as it might not run, due to insertion of
unwanted hidden special characters while copying. Please refer the queries, but type them in the
prompt)
MySQL
Install SQL in Ubuntu machine by issuing the below command in terminal ($ is shell prompt)
Note:
$ mysql -u root -p
After the above command, your screen will looks like this
You can see that our previous HMSystem along with few other databases are present in the MySQL
server.
For our Retail Chain Management System (RCMS), we create our database by
Now,
mysql>SHOW DATABASES;
From the above screen, we can see that our new database RCMS has been added into the system
databases.
To create the tables in the newly created database RCMS, we need to open it.
Your command prompt should say Empty set (0.00 sec) because we have not yet added any tables.
item_name VARCHAR(50),
After adding the table, we can check for the added fields in the table as
store_name VARCHAR(50),
store_location VARCHAR(100),
warehouse_location VARCHAR(100),
To create tableworkersuse
worker_name VARCHAR(50),
worker_address VARCHAR(100),
To create tablevendoruse
vendor_name VARCHAR(50),
vender_address VARCHAR(100),
To create tablesalesuse
date DATE,
To create tablebuyuse
date_purchase DATE,
date_delivery_initial DATE,
To create tabledeliveryuse
warehouse_number NUMERIC(2,0),
date_delivery_final DATE,
reason_if_delay VARCHAR(200),
Finally, SHOW TABLES should list all the tables created in RCMS
Adding Tuples into item Relation
mysql>INSERT INTO item (item_no, item_name, item_segment, gender) VALUES (1234, "Dress",
"C", "W");
mysql> INSERT INTO item(item_no, item_name, item_segment, gender) VALUES (1256, "Shirt",
"C", "M");
mysql> INSERT INTO item(item_no, item_name, item_segment, gender) VALUES (1254, "Trousers",
"C", "M");
mysql> INSERT INTO item(item_no, item_name, item_segment, gender) VALUES (1200, "Kurtas",
"C", "W");
mysql> INSERT INTO item(item_no, item_name, item_segment, gender) VALUES (1222, "Ties", "R",
"M");
We can view the tuples in item using SELECT statement shown below
Similarly, we can add the tuples to other tables as shown below
INSERT INTO store(store_id, store_name, store_location, manager_id) VALUES (11, "High Street",
"Circus Road", 451234);
INSERT INTO store(store_id, store_name, store_location, manager_id) VALUES (12, "Quick Buy",
"Central Park", 451212);
SELECT query:
FROM workers;
Using
WHE
RE
clause
To display the attributes of all workers who stays in Park Circus Road 8 with names in ascending
order
This will add a gender column to the workers table with all entries initialized to null initially.
To drop the gender column from workers and to check if the gender column is dropped from
workers
First add the salary column to the workers and the update the salary to each worker
mysql>UPDATE workers
SET salary=10000
WHERE worker_id=411134;
Similarly update the salary of other workers with the worker_id as unique tuple identifier.
GROUP BY clause
Aggregate functions
BETWEEN clause
EXISTS clause
To select all employee names and salary whose salary >= 10000 using EXISTS clause
Using CURSORS
This is an example to use the cursor to fetch the worker_name if the warehouse_number matches
the id passed by the user through procedure curserdemo().
mysql> delimiter $$
begin
declare cur1 cursor for select worker_name from workers where warehouse_number=id;
open cur1;
select name;
close cur1;
end $$
mysql> delimiter ;