Computer Science
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:
- The same bill cannot be generated for different orders.
- 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?
Answer
(a) To store each item name along with its price in the canteen database, we can create a relation (table) called "Items" with the following attributes:
Items table
| Attributes | Datatype | Constraints |
|---|---|---|
| ItemNo | Integer | Primary Key, Unique, non-null value |
| ItemName | VARCHAR | Non-null value |
| Price | Float | Non-null value |
The restriction that should be used while defining the "Items" relation is to set the "ItemNo" attribute as the primary key. This ensures that each item number is unique and that each item and its price are stored only once in the database.
(b) Yes, the item sale information should be stored in a separate relation, say "SaleOrders".
SaleOrders table
| Attributes | Datatype | Constraints |
|---|---|---|
| orderno | integer | Primary Key Unique, Non-null value |
| ItemNo | integer | Foreign key,Unique, Non-null value |
| Quantity | integer | Non-null value |
| Price | Float | Non-null value |
With this design, we satisfy both restrictions:
Each order has a unique OrderNo, ensuring that the same bill cannot be generated for different orders. The foreign key constraint on ItemNo ensures that bills can only be generated for available items in the canteen.
(c) 'Calories' should be stored in the "Items" table because they are directly associated with specific items.
Related Questions
A school has a rule that each student must participate in a sports activity. So each one should give only one preference for sports activity. Suppose there are five students in a class, each having a unique roll number. The class representative has prepared a list of sports preferences as shown below. Answer the following:
Table: Sports Preferences
Roll_no Preference 9 Cricket 13 Football 17 Badminton 17 Football 21 Hockey 24 NULL NULL Kabaddi - Roll no 24 may not be interested in sports. Can a NULL value be assigned to that student’s preference field ?
- Roll no 17 has given two preferences in sports. Which property of relational DBMS is violated here ? Can we use any constraint or key in the relational DBMS to check against such violation, if any?
- Kabaddi was not chosen by any student. Is it possible to have this tuple in the Sports Preferences relation ?
In another class having 2 sections, the two respective class representatives have prepared 2 separate Sports Preferences tables, as shown below:
Sports preference of section 1 (arranged on roll number column)
Table: Sports Preferences
Roll_no Sports 9 Cricket 13 Football 17 Badminton 21 Hockey 24 Cricket Sports preference of section 2 (arranged on Sports name column, and column order is also different)
Table: Sports Preferences
Sports Roll_no Badminton 17 Cricket 9 Cricket 24 Football 13 Hockey 21 Are the states of both the relations equivalent? Justify.
An organisation wants to create a database EMPDEPENDENT to maintain following details about its employees and their dependent.
EMPLOYEE(AadharNumber, Name, Address, Department, EmployeeID) DEPENDENT(EmployeeID, DependentName, Relationship)(a) Name the attributes of EMPLOYEE, which can be used as candidate keys.
(b) The company wants to retrieve details of dependent of a particular employee. Name the tables and the key which are required to retrieve this detail.
(c) What is the degree of EMPLOYEE and DEPENDENT relation?
School uniform is available at M/s Sheetal Private Limited. They have maintained SCHOOL_UNIFORM Database with two relations viz. UNIFORM andCOST. The following figure shows database schema and its state.
School Uniform Database
Attributes and Constraints
Table: UNIFORM
Attribute UCode UName UColor Constraints Primary Key Not Null - Table: COST
Attribute UCode Size Price Constraints Composite Primary Key >0 Table: UNIFORM
UCode UName UColor 1 Shirt White 2 Pant Grey 3 Skirt Grey 4 Tie Blue 5 Socks Blue 6 Belt Blue Table: COST
UCode Size COST Price 1 M 500 1 L 580 1 XL 620 2 M 810 2 L 890 2 XL 940 3 M 770 3 L 830 3 XL 910 4 S 150 4 L 170 5 S 180 5 L 210 6 M 110 6 L 140 6 XL 160 (a) Can they insert the following tuples to the UNIFORM Relation ? Give reasons in support of your answer.
- 7, Handkerchief, NULL
- 4, Ribbon, Red
- 8, NULL, White
(b) Can they insert the following tuples to the COST Relation ? Give reasons in support of your answer.
- 7, S, 0
- 9, XL, 100