Skip to content
Exercises · Q8

Q.The school canteen wants to maintain records of items available in the school canteen and generate bills when students purchase any item from the canteen. The school wants to create a canteen database to keep track of items in the canteen and the items purchased by students. Design a database by answering the following questions:

a) To store each item name along with its price, what relation should be used? Decide appropriate attribute names along with their data type. Each item and its price should be stored only once. What restriction should be used while defining the relation?
b) In order to generate bill, we should know the quantity of an item purchased. Should this information be in a new relation or a part of the previous relation? If a new relation is required, decide appropriate name and data type for attributes. Also, identify appropriate primary key and foreign key so that the following two restrictions are satisfied:
i) The same bill cannot be generated for different orders.
ii) Bill can be generated only for available items in the canteen.
c) The school wants to find out how many calories students intake when they order an item. In which relation should the attribute ‘calories’ be stored?
Assam AhsecTextbookSubjective· 4mImportance★★★★★est
64% · 9/14 Questions
🔒 Locked · start free trial →

You're viewing a preview — the full solution, concept, methods & PYQ mapping are locked.

Start your 14-day free trial to unlock the full solution →

Two relations do the job: ITEM(ItemName, Price) with ItemName as primary key (each item stored once), and a new BILL(BillNo, ItemName, Quantity) where BillNo is the primary key (no bill reused across orders) and ItemName is a foreign key into ITEM (bills only for items the canteen actually has). Calories, being a property of an item, belongs in ITEM.

a) Storing items and prices — relation, attributes, restriction

Item name and price describe the item entity, so use one relation:

CREATE TABLE ITEM (
  ItemName VARCHAR(30) PRIMARY KEY,   -- unique + NOT NULL: each item stored exactly once
  Price    DECIMAL(6,2) NOT NULL
);
  • Attributes and types: ItemName VARCHAR(30) (names are text), Price DECIMAL(6,2) (money should be exact decimal, not FLOAT).
  • Restriction: declare ItemName as the PRIMARY KEY. The primary key's uniqueness + not-NULL rules are exactly the requirement "each item and its price should be stored only once" — a second INSERT of ('Samosa', …) is rejected as a duplicate key. (An alternative design adds a numeric ItemCode as primary key with ItemName UNIQUE; either way, the restriction doing the work is a unique key on the item.)

b) Recording quantity for billing — a new relation

Quantity purchased is not a property of an item — it is a property of a purchase (bill). Putting Quantity inside ITEM would overwrite it at every sale and could not keep two students' purchases apart. So a new relation is required:

CREATE TABLE BILL (
  BillNo   INT PRIMARY KEY,           -- restriction (i): a bill number can never repeat
  ItemName VARCHAR(30) NOT NULL,
  Quantity INT NOT NULL CHECK (Quantity > 0),
  FOREIGN KEY (ItemName) REFERENCES ITEM(ItemName)   -- restriction (ii)
);
  • Attributes and types: BillNo INT, ItemName VARCHAR(30), Quantity INT.
  • Primary key = BillNo. Uniqueness of the primary key means the same bill number cannot be generated for two different orders — restriction (i) enforced by the DBMS itself.
  • Foreign key = ItemName → ITEM(ItemName). Referential integrity means a bill can name only an item that already exists in ITEM — i.e. bills can be generated only for items available in the canteen — restriction (ii). Trying INSERT INTO BILL VALUES (101, 'Pizza', 2); when Pizza is not in ITEM fails with a foreign-key violation.
  • The bill amount needs no stored column — it is derived on demand:
SELECT B.BillNo, B.ItemName, B.Quantity, B.Quantity * I.Price AS Amount
FROM   BILL B JOIN ITEM I ON B.ItemName = I.ItemName;
BillNoItemNameQuantityAmount
101Samosa230.00

Unlock everything free for 14 days

  • Full step-by-step solutions
  • Concept-first explanations
  • Methods, shortcuts & mistakes
  • PYQ mapping + timed mock tests

Full access for 14 days. No credit card required.