Learning Objectives

By the end of this chapter, you will be able to:

  • Define a database model and state what it fixes before any table is built.
  • Group legacy models as tree and graph and state where each breaks.
  • Explain the relational core with tables, keys, and SQL for a retail or hotel case.
  • Group modern models as object and NoSQL families and match each to a BBM use.
  • Choose a model for a mart or lodge that must report VAT, with cost in NPR dated BS 2081.

One mart, three ways to store the same member

A retail chain in Rupandehi has 4,800 loyalty members. On 2081-06-15 the owner asks: list members who hold a recurring coupon and an unpaid bill in the same outlet. One staff searches sheets that act like a tree. One follows pointers in an old graph. One runs a JOIN. Only the tables answer in one query. The question is not hard. The model makes it hard or easy. A database model sets how data is shaped, how records link, and how queries travel.

flowchart TB
  Q[Question: members with coupon and bill] --> H[Tree: walk from root]
  Q --> N[Graph: follow pointers]
  Q --> R[Tables: JOIN on MemberID]

Framework 1: Legacy geometry, tree and graph

Group these because both use pointers. The split is how many parents a child may have.

A. Hierarchical, one parent per child

Data sits in a tree. IBM IMS used this for retail chains in the 1960s.

  • Shape: parent to child, one to many only.
  • Navigation: start at root, walk down.
  • Fit: one to many that never changes, such as outlet to member.
  • Break: many to many requires duplication.

For a mart: Outlet as root, Members under Outlet, Bills under Member. Fast for bills of one member. Slow for members across outlets with the same SKU.

B. Network, many parents per child

Network extends the tree and lets a child have many parents through pointers. A member can link to several SKUs at once. The shape is a graph. Fit improves for many to many. Cost is complexity and heavy schema change.

Model Structure Cap Query Lesson for a mart
Hierarchical Tree, single parent One to many Walk from root Single outlet look up
Network Graph, many parents Many to many via pointers Follow links Shared SKU logic now retired

Both bury logic in navigation. Change the question, change the code.

Framework 2: Relational core, tables that stand alone

E. F. Codd proposed this in 1970. One entity per table. Rows are instances. Columns are attributes. Rules live in keys, not in paths.

  • Primary key: identifies one row only. MemberID M-2144 appears once in Members.
  • Foreign key: repeats that value elsewhere to point back. MemberID M-2144 in Bills and Loyalty points to Members.
  • Relationships: one to one such as member to PAN, one to many such as member to bills, many to many through a link table such as Members to SKUs via Purchase.

Tables for the chain on 2081-06-15:

Members

MemberID (PK) Name Outlet Phone
M-2144 Sita Sharma Butwal 9851000222
M-2145 Ram Thapa Butwal 9851000333

Loyalty

CouponID (PK) MemberID (FK) Product Balance NPR
C101 M-2144 Recurring 5,000
C102 M-2144 Festival 3,000

Bills

BillID (PK) MemberID (FK) SKU Total NPR
B201 M-2144 Rice 25kg 4,200
B202 M-2145 Oil 5L 1,800

One query answers the owner without walking a tree:

SELECT m.MemberID, m.Name
FROM Members m
JOIN Loyalty l ON m.MemberID = l.MemberID AND l.Product = 'Recurring'
JOIN Bills b ON m.MemberID = b.MemberID
WHERE m.Outlet = 'Butwal';

Result is M-2144 Sita Sharma. The same schema supports the VAT ask: sum sales by outlet.

SELECT Outlet, SUM(Total) FROM Bills
WHERE BillDate BETWEEN '2081-04-01' AND '2081-06-15'
GROUP BY Outlet;

Relational wins because tables are predictable, SQL is declarative, and integrity blocks duplicates.

Framework 3: Flexible geometry, object and NoSQL

Group these because they relax the table shape for scale or variety.

  • Object: attributes plus behaviour. Fits lodge floor maps.
  • Document: JSON rows with varied fields. MongoDB. Fits catalogues where tea has grade and soap has expiry.
  • Key value: Redis. Fits session tokens for a delivery app.
  • Column family: Cassandra. Fits high write POS logs.
  • Graph: Neo4j. Fits supply routes where the edge is the question, such as farmer to collection centre to mart.

Core ledger stays relational for VAT and audit. Flexible stores sit at the edge beside the ledger, not in place of it.

Nepal worked example: tree cost versus table cost on BS 2081-06-15 (NPR)

Taxable retail service fees for the quarter are Rs 2,40,000. A clerk keeps each outlet as a subtree in sheets. Adding hsn_code for IRD needs vendor redesign at Rs 45,000. The relational change is one DDL: ALTER TABLE Members ADD hsn_code VARCHAR(20). Cost is DBA 2 hours at Rs 1,500 per hour = Rs 3,000 plus testing Rs 1,200 = Rs 4,200. Saving = 45,000 - 4,200 = Rs 40,800.

VAT proof: output at 13 percent on Rs 2,40,000 = 2,40,000 x 0.13 = Rs 31,200. Branch split from one GROUP BY is Butwal Rs 18,200 and Sunwal Rs 13,000. The sheet total missed a late Rs 22,000 line, so understated VAT = 22,000 x 0.13 = Rs 2,860. One table removes that risk.

A lodge with the same PMS can reuse the pattern: Rooms plus Guests plus Bills joined on GuestID, no tree.

Self-check

Try this: keys and VAT (calculable)

Members 4,800 rows, Loyalty 6,200, Bills 3,400 on 2081-06-15.

  1. In Members which column is primary key and why can phone not serve.
  2. In Loyalty and Bills which column is foreign key and where does it point.
  3. JOIN Loyalty to Members on MemberID, one to many. Maximum result rows before filter equals rows in which table and why.
  4. VAT output on Rs 2,40,000 at 13 percent is what. Incremental VAT if Rs 22,000 was late. What is corrected total.
Answer sketch 1. MemberID. Phone can change and duplicate, so it cannot guarantee identity. 2. MemberID in both tables, points to Members.MemberID. 3. Loyalty at 6,200. Bounded by the many side, each loyalty row matches at most one member. 4. VAT = 2,40,000 x 0.13 = NPR 31,200. Incremental = 22,000 x 0.13 = NPR 2,860. Corrected = 34,060.

Summary

  • Tree forces root walks, graph adds pointers, both hide logic in paths.
  • Relational uses keys and SQL. One Member row supports many bills and loyalty coupons.
  • Modern stores add flexibility beside the ledger. Schema change costs Rs 4,200 versus Rs 45,000 and VAT Rs 31,200 removes Rs 2,860 gap.

Next: database management systems, advantages of databases, business intelligence.

Key Takeaways

  • Name the model by how records link.
  • Primary key fixes identity, foreign key fixes belonging.
  • NoSQL is not one model. Document, key value, column family, and graph differ.

Discussion Questions

  1. Why does a mart prefer relational for bills but document for a catalogue where each SKU has different attributes.
  2. Draw two tables Members and Orders with a foreign key and explain the one to many.
  3. When would a supply chain use a graph model for farmer to mart routes.

Exercises

Short answer

  1. Define a database model and name two legacy and two modern families.
  2. Differentiate primary key and foreign key with M-2144 Sita Sharma.
  3. What is NoSQL and when does a retail chain use it beside MySQL.

Long answer

  1. Compare hierarchical, network, and relational on structure and query. Use 4,800 members to show why JOIN beats tree walk and how VAT Rs 31,200 repeatably splits by outlet.
  2. Describe relational tables for a lodge that links Guests, Bookings, and Bills with GuestID. Write the JOIN that lists guests with a booking and an unpaid bill.