Q.A shop called Wonderful Garments who sells school uniforms maintains a database SCHOOLUNIFORM as shown below. It consisted of two relations - UNIFORM and COST. They made UniformCode as the primary key for UNIFORM relations. Further, they used UniformCode and Size to be composite keys for COST relation. By analysing the database schema and database state, specify SQL queries to rectify the following anomalies.
When the above query is used to insert data, the values for the handkerchief without entering its details in the UNIFORM relation is entered. Make a provision so that the data can be entered in the COST table only if it is already there in the UNIFORM table.
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 →Add referential integrity (foreign key), NOT NULL, and CHECK constraints to prevent orphan records, enforce mandatory fields, and validate price values in the SCHOOLUNIFORM database.
Understanding Database Constraints and Integrity
The question presents a classic scenario of database anomalies that arise when a schema lacks proper constraints. The shop's database allows inconsistent data — rows in COST that reference non-existent uniforms, missing uniform names, and invalid prices. Each sub-part asks you to write an ALTER TABLE statement that enforces a specific integrity rule.
The three constraint types you need are:
- Foreign Key — ensures every
UCodeinCOSTexists inUNIFORM(referential integrity). - NOT NULL — prevents inserting a row without a required field (domain integrity).
- CHECK — enforces a condition on column values (domain integrity).
Let's tackle each anomaly in turn.
(a) Insert handkerchiefs into the database
The shop wants to add a new item: red handkerchiefs, medium size, ₹100 each. Because UNIFORM holds item details and COST holds size-price combinations, you must insert into both tables.
Step 1: Insert the item into UNIFORM (assign a new UniformCode, say 7, and describe the item).
INSERT INTO UNIFORM (UniformCode, UName, Colour)
VALUES (7, 'Handkerchief', 'Red');
Step 2: Insert the size-price pair into COST.
INSERT INTO COST (UCode, Size, Price)
VALUES (7, 'M', 100);
The composite key (UCode, Size) in COST means one uniform can have multiple size-price rows (e.g., S/M/L). You must insert the UNIFORM row first; otherwise, part (b)'s anomaly occurs.
(b) Prevent orphan records in COST — add a Foreign Key constraint
The problem states that the query
INSERT INTO COST (UCode, Size, Price) VALUES (7, 'M', 100);
succeeds even when UniformCode = 7 does not exist in UNIFORM. This creates an orphan record — a cost entry for a non-existent item.
The solution is a foreign key from COST.UCode to UNIFORM.UniformCode. This constraint rejects any insert/update in COST if the referenced UCode is absent from UNIFORM.
ALTER TABLE COST
ADD CONSTRAINT fk_cost_uniform
FOREIGN KEY (UCode) REFERENCES UNIFORM(UniformCode);
Why this works: Before inserting (7, 'M', 100) into COST, the DBMS checks whether UniformCode = 7 exists in UNIFORM. If not, the insert is rejected with a foreign-key violation error.
If COST already contains orphan rows (e.g., UCode = 7 but no matching UNIFORM row), the ALTER TABLE will fail. You must clean the data first — delete orphans or insert the missing UNIFORM rows.
(c) Enforce that every uniform has a valid UName — add a NOT NULL constraint
The requirement is: "assign a new UCode only if it has a valid UName." In SQL, "valid" here means not null — every uniform must have a name.
ALTER TABLE UNIFORM
MODIFY UName VARCHAR(50) NOT NULL;
(Syntax varies slightly by DBMS; MySQL uses MODIFY, PostgreSQL uses ALTER COLUMN … SET NOT NULL, Oracle uses MODIFY.)
Why this works: After this constraint, any INSERT or UPDATE that leaves UName as NULL is rejected. You cannot create a uniform without naming it.
If the column already allows NULL and some rows have NULL in UName, update those rows first:
UPDATE UNIFORM SET UName = 'Unknown' WHERE UName IS NULL;
Then apply the NOT NULL constraint.
(d) Ensure price is always positive — add a CHECK constraint
Prices cannot be zero or negative. A CHECK constraint enforces this business rule at the database level.
ALTER TABLE COST
ADD CONSTRAINT chk_price_positive
CHECK (Price > 0);
Why this works: Before every insert or update on COST, the DBMS evaluates Price > 0. If false, the operation is rejected. …
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.