A shop called Wonderful Garments that sells school uniforms maintain a database SCHOOL_UNIFORM as shown below. It consisted of two relations — UNIFORM and PRICE. They made UniformCode as the primary key for UNIFORM relation. Further, they used UniformCode and Size as composite keys for PRICE relation. By analysing the database schema and database state, specify SQL queries to rectify the following anomalies.
UNIFORM
| UCode | UName | UColor |
|---|---|---|
| 1 | Shirt | White |
| 2 | Pant | Grey |
| 3 | Skirt | Grey |
| 4 | Tie | Blue |
| 5 | Socks | Blue |
| 6 | Belt | Blue |
PRICE
| UCode | Size | 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) The PRICE relation has an attribute named Price. In order to avoid confusion, write SQL query to change the name of the relation PRICE to COST.
b) M/S Wonderful Garments also keeps handkerchiefs of red color, medium size of ₹100 each. Insert this record in COST table.
c) When you used the above query to insert data, you were able to enter the values for handkerchief without entering its details in the UNIFORM relation. Make a provision so that the data can be entered in COST table only if it is already there in UNIFROM table.
d) Further, you should be able to assign a new UCode to an item only if it has a valid UName. Write a query to add appropriate constraint to the SCHOOL_UNIFORM database.
e) ALTER table to add the constraint that price of an item is always greater than zero.
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 →This exercise walks through the classic integrity toolkit: rename a table (ALTER TABLE … RENAME TO), insert a row, then close the loopholes the insert exposed — a FOREIGN KEY so COST rows must reference UNIFORM, a NOT NULL on UName so no item exists without a name, and a CHECK (Price > 0) so prices stay sensible.
a) Rename the relation PRICE to COST
Having a table named PRICE with a column also named Price is confusing, so:
ALTER TABLE PRICE RENAME TO COST;
Why: ALTER TABLE changes a table's definition; the RENAME TO clause changes its name without touching the data. (MySQL also accepts RENAME TABLE PRICE TO COST; — same effect.) All 16 rows are preserved; only the name changes.
b) Insert the handkerchief record
Handkerchief is a new item, so it needs a fresh code — the highest existing UCode is 6, so we use 7. COST stores only (UCode, Size, Price):
INSERT INTO COST (UCode, Size, Price)
VALUES (7, 'M', 100);
COST now additionally contains:
| UCode | Size | Price |
|---|---|---|
| 7 | M | 100 |
Notice the anomaly: the name "Handkerchief" and colour "Red" have nowhere to go in COST — they belong in UNIFORM, where no UCode 7 exists yet. The database happily accepted a price for an item it knows nothing about. That is exactly the problem part (c) fixes.
c) COST rows only for items that exist in UNIFORM — a FOREIGN KEY
A referential-integrity (foreign key) constraint makes the DBMS reject any COST row whose UCode is absent from UNIFORM:
ALTER TABLE COST
ADD CONSTRAINT fk_cost_uniform
FOREIGN KEY (UCode) REFERENCES UNIFORM(UCode);
Why the order matters: the handkerchief row (UCode 7) inserted in (b) is currently an orphan — adding the constraint would fail while it violates the rule. So first make the data consistent by registering the item:
INSERT INTO UNIFORM (UCode, UName, UColor)
VALUES (7, 'Handkerchief', 'Red');
After that, the ALTER TABLE succeeds, and from then on any INSERT into COST with an unknown UCode is rejected with a foreign-key error.
d) A new UCode only with a valid UName — NOT NULL
An item without a name is meaningless, so forbid empty names at the schema level:
ALTER TABLE UNIFORM
MODIFY UName VARCHAR(20) NOT NULL;
Why: MODIFY redefines the column, and NOT NULL makes the DBMS reject any UNIFORM row that omits UName. Combined with (c), a price can now exist only for a coded item, and a coded item must carry a name. …
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.