Computer Science

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.

(a) M/S Wonderful Garments also keeps handkerchiefs of red colour, medium size of Rs. 100 each.

(b) INSERT INTO COST (UCode, Size, Price) values (7, 'M', 100);

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.

(c) Further, they should be able to assign a new UCode to an item only if it has a valid UName. Write a query to add appropriate constraints to the SCHOOLUNIFORM database.

(d) Add the constraint so that the price of an item is always greater than zero.

SQL Queries

3 Likes

Answer

(b)

ALTER TABLE COST ADD CONSTRAINT fk_uniform_ucode_size FOREIGN KEY (UCode) REFERENCES UNIFORM (UCode);

(c)

ALTER TABLE UNIFORM ADD CONSTRAINT CK_UName_UCode 
CHECK (UName IS NOT NULL);

(d)

ALTER TABLE COST ADD CONSTRAINT CK_Price_Positive CHECK (Price > 0);

Answered By

2 Likes


Related Questions