You are on page 1of 3

TEMASEK POLYTECHNIC SCHOOL OF INFORMATICS & IT Database Information Systems (CFI1C07) AY2011/2012 Oct Semester  Tutorial 3 – Database Constraints Objectives - On completion of this tutorial session, the

students will be able to :• Discuss how constraints enforce data consistency.


What is a primary key and why is it needed? A column or set of columns used to uniquely identify a particular row in a table. Every table must have a primary key. It has to be unique and not null.


What is a composite key? A composite key is a primary key that’s consists of more than one attribute that uniquely identifies each tuple in a relation


Why is referential integrity important? Referential integrity is important because it maintains the consistency of a database by ensuring that links between tables always refer to legitimate rows. Referential integrity exists when a value for a foreign key can be entered only if the corresponding value exists in the originating table


AY06/07 April Semester Main Exam question.

DBIS (CFI1C07) Tutorial 3 Database Constraints

The following tables are part of a company’s database showing the information of salesmen and their sales. The Commission_Rate value is in percentage which means that value 2.5 refers to 2.5%. Salesman Salesman_I d S001 S002 S003 Name WL TAN ALLAN YEO KELLY TOH Commission_Rat e 2.5 5.0 4.5

Invoice Invoice_No 1001 1002 1003 1004

Invoice_Date 12-JAN-06 10-FEB-06 8-JAN-06 25-JAN-06

Salesman_I d S002 S001 S002 S001

Sales_Amoun t 5000.00 2150.00 3800.00 3000.00

Primary keys in the tables are underlined. Salesman_Id in the Invoice table is a foreign key referencing Salesman table. Assume that all integrity constraints are implemented for the Salesman and Invoice tables. State whether the following operations will succeed or fail. Briefly explain the outcome of each operation with reference to the relational constraints of a database. DO NOT write SQL statements in your answers. a) Delete from Invoice where Invoice_No = 1003; Succeed. It does not violate any constraint. (3 marks) b) Insert into Invoice values (1005, ’30-DEC-2005’, ‘S007’, 1500); Fail. There is no salesman with salesman ID of ‘S007’ in the salesman table. It violates the referential integrity constraint (3 marks) c) Delete from Salesman where Name = ‘WL TAN’;

DBIS (CFI1C07) Tutorial 3 Database Constraints


Fail. Salesman WL TAN has a Salesman ID of ‘S001’ and there are still records in the invoice table referring to S001. It violates the referential integrity constraint. (3 marks) d) Insert into Salesman values(‘S003’, ‘JOHN SMITH’, 1.5); Fail. Salesman_ID S003 is used. Duplicate value for primary is not allowed. It violates the primary key constraint. (3 marks) e) Allan Yeo has resigned from the company and the company has decided to delete all his records from the salesman and the invoice tables. Suggest a way to do the deletion without violating the referential integrity constraint. Use ‘ON DELETE CASCADE’ on the invoice table. OR Delete all the invoices with Salesman ID = ‘S002’ from the invoice table first and then delete array S002 from the salesman table. (3 marks) ~ End ~

DBIS (CFI1C07) Tutorial 3 Database Constraints