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.
To prevent insertion of data in the COST table without corresponding entries in the UNIFORM table, we need to define UCode as a foreign key in the COST table that references the UCode (primary key) in the UNIFORM table.
This can be done using the following SQL command: sql ALTER TABLE COST ADD FOREIGN KEY (UCode) REFERENCES UNIFORM(UCode);
This foreign key constraint ensures referential integrity, meaning data can only be inserted into the COST table if the UCode value already exists in the UNIFORM table. Any attempt to insert a record with UCode = 7 (which does not exist in UNIFORM) will be rejected by the database.
Explanation
The context clearly states that UCode is the primary key in the UNIFORM table and a common attribute between both tables. It explicitly mentions: 'Hence, we need to define Ucode as foreign key in the Price table while creating this table.' The foreign key constraint enforces referential integrity between the two tables, preventing orphan records in the COST table that have no matching parent record in UNIFORM.
Solution Steps
Step 1: Identify that UCode is the primary key in UNIFORM table and needs to be linked to COST table.
Step 2: Define UCode as a foreign key in COST table referencing UNIFORM(UCode).
Step 3: This constraint will reject any INSERT operation in COST where UCode doesn't exist in UNIFORM.