37
Database Management System Lecture 6 The Relational Database Model – Keys, Integrity Rules

Database Management System Lecture 6 The Relational Database Model – Keys, Integrity Rules

Embed Size (px)

Citation preview

Page 1: Database Management System Lecture 6 The Relational Database Model – Keys, Integrity Rules

Database Management System

Lecture 6

The Relational Database Model – Keys, Integrity Rules

Page 2: Database Management System Lecture 6 The Relational Database Model – Keys, Integrity Rules

Learning Outcome

• Understand and being able to identify primary, candidate and foreign keys.

• Grasp and being able to apply the data integrity rules of a relational database.

Page 3: Database Management System Lecture 6 The Relational Database Model – Keys, Integrity Rules

Terminology

• Primary Key An attribute or a set of attribute that enable data in a table to be uniquely identified. It is specified in table schema as a primary key.

Page 4: Database Management System Lecture 6 The Relational Database Model – Keys, Integrity Rules

Terminology- (Cont.)

• Candidate Key: An attribute or a set of attribute that enable data in a table to be uniquely identified. There could be more than Candidate Key in a table. However, one only can be specified as a primary key

Page 5: Database Management System Lecture 6 The Relational Database Model – Keys, Integrity Rules

Terminology (Cont.)

• Foreign Key: A set of attributes in table T1 that reference another set of corresponding primary key attributes in table T2. The foreign key attributes in T1 must have the same domain as the corresponding primary key attributes in T2

Page 6: Database Management System Lecture 6 The Relational Database Model – Keys, Integrity Rules

Primary Keys

Having two identical rows in a table can cause data redundancy and inconsistency problems

• A primary key ensures that each row in a table unique with no duplicate

• It is an attribute or a set of attribute that enable data in a table to be uniquely identified

Page 7: Database Management System Lecture 6 The Relational Database Model – Keys, Integrity Rules

Primary Keys (Cont.)

• A table in a relational database must have a primary key.

Page 8: Database Management System Lecture 6 The Relational Database Model – Keys, Integrity Rules

Primary Keys (Cont.)

• For example,

• Consider a university library database which includes a student member table shown in Figure 6.1.

• The table has attributes such as name, phone number, matriculation number and department number

Page 9: Database Management System Lecture 6 The Relational Database Model – Keys, Integrity Rules

Primary Keys (Cont.)

• The student’s matriculation number can be used as a primary key as it can uniquely identify a student record.

• Attributes of a primary key are usually underlined.

Page 10: Database Management System Lecture 6 The Relational Database Model – Keys, Integrity Rules

Primary Keys (Cont.)

The main characteristics of a primary key

• Must be minimal to avoid errors in data• Must consist of attributes that can never have

NULL values• Must be fixed and should never be changed

once the table is populated with data

Page 11: Database Management System Lecture 6 The Relational Database Model – Keys, Integrity Rules

Primary Keys (Cont.)

• Note that there could be more than one alternative attribute(s) that can uniquely identify rows in a table.

• Only one of them can be chosen to be the primary key.

• The other alternatives are called Candidate Keys

Page 12: Database Management System Lecture 6 The Relational Database Model – Keys, Integrity Rules

Matric Number Name Phone Number Department Number

200965611 Ahmad Saleh 075811123 121

201013456 Ameer Jabir 075845678 100

101022778 Hamza Yussuf 075846467 231

Student Member Table

Page 13: Database Management System Lecture 6 The Relational Database Model – Keys, Integrity Rules

Foreign Keys

• A foreign key is a set of attributes in table T1 that reference another set of corresponding primary key attributes in table T2.

• Foreign keys enable tables in a relational database to be linked together.

Page 14: Database Management System Lecture 6 The Relational Database Model – Keys, Integrity Rules

Foreign Keys (Cont.)

• The foreign key attributes in T1 must have the same domain as the corresponding primary key attributes in T2.

• Each value that occurs in the foreign key must also occur in the primary key.

Page 15: Database Management System Lecture 6 The Relational Database Model – Keys, Integrity Rules

Foreign Keys (Cont.)

• For example

• If a library database has two tables: student member and department shown in Figure 6.2.

• The student member table has primary key matriculation number and the department table has primary key department number.

Page 16: Database Management System Lecture 6 The Relational Database Model – Keys, Integrity Rules

Foreign Keys (Cont.)

• The two tables can be linked with each other using the department number attribute.

• Thus, department number in the student member table will be a foreign key linked to the department number attribute in the department table which is its primary key.

• Note that a foreign key is usually denoted by an asterisk (*).

Page 17: Database Management System Lecture 6 The Relational Database Model – Keys, Integrity Rules
Page 18: Database Management System Lecture 6 The Relational Database Model – Keys, Integrity Rules

Integrity Rules

• A Relational Database Management System (RDMS) enforces integrity rules to ensure the accuracy and accessibility of the data.

• Such rules govern which operations can be performed on the data

• In addition to putting constraints on the structure of the database

Page 19: Database Management System Lecture 6 The Relational Database Model – Keys, Integrity Rules

Integrity Rules (Cont.)

• There are three integrity rules that a relational database follow which are:

1. the Key Integrity rule,

2. the Entity Integrity rule

3. the Referential Integrity rule

Page 20: Database Management System Lecture 6 The Relational Database Model – Keys, Integrity Rules

The Key Integrity Rule

• The Key Integrity rule specifies that rows in a table must be unique with no row duplication.

• This is useful to remove any data ambiguity

• Thus, tables are defined as a unique set of rows with no two rows having the same combination of values for their attributes

Page 21: Database Management System Lecture 6 The Relational Database Model – Keys, Integrity Rules

The Key Integrity Rule (Cont.)

• Key constraints enforce this rule as follows (which we discussed earlier):

• Candidate Key: that is an attribute or a minimal combination of attributes that uniquely identify any row in a table.

• There could be more than one candidate key in a

table.

Page 22: Database Management System Lecture 6 The Relational Database Model – Keys, Integrity Rules

The Key Integrity Rule (Cont.)

• Primary Key: is the candidate key chosen in the definition of the table schema to uniquely identify each row in the table.

Page 23: Database Management System Lecture 6 The Relational Database Model – Keys, Integrity Rules

The Entity Integrity Rule

• The Entity Integrity rule states that no attribute that is part of a primary key is allowed to have value NULL.

• If NULL values are allowed for primary key attributes, then two rows can have NULL as primary key values and thus the two rows can’t be uniquely identified

Page 24: Database Management System Lecture 6 The Relational Database Model – Keys, Integrity Rules

The Entity Integrity Rule (Cont.)

• This causes the Key Integrity rule to be violated.

• An illustration on how the Entity Integrity rule is shown in Figure 5.3 with the student member table having two rows with NULL values for the primary key attribute

Page 25: Database Management System Lecture 6 The Relational Database Model – Keys, Integrity Rules
Page 26: Database Management System Lecture 6 The Relational Database Model – Keys, Integrity Rules

The Referential Integrity Rule

• The Referential Integrity rule is concerned with the linking between tables using foreign keys.

• It states that an attribute in a table schema T1 is a foreign key if:

Page 27: Database Management System Lecture 6 The Relational Database Model – Keys, Integrity Rules

The Referential Integrity Rule (Cont.)

1. It has a relationship with a primary key in T2,

2. The domain of the foreign key in T1 is the same as the domain of the key in T2, and

Page 28: Database Management System Lecture 6 The Relational Database Model – Keys, Integrity Rules

The Referential Integrity Rule (Cont.)

3. The foreign key value in T1 must be equal to the value of T2’s primary key.

Page 29: Database Management System Lecture 6 The Relational Database Model – Keys, Integrity Rules

The Referential Integrity Rule (Cont.)

• Thus, the Referential Integrity rule ensures that any foreign key value is always pointing to a primary key row in the other table.

• References to non-existing primary keys are not allowed.

Page 30: Database Management System Lecture 6 The Relational Database Model – Keys, Integrity Rules

Enforcing the Integrity Rules

• Integrity rules must be checked whenever an operation is executed that updates the database.

• Three types of operations can violate the integrity rules which are: inserting a row, deleting a row and updating a row.

Page 31: Database Management System Lecture 6 The Relational Database Model – Keys, Integrity Rules

• Examples of how Integrity rules can be violated

• Consider the library database shown in Figure 6.4 with the two tables student member and department

Page 32: Database Management System Lecture 6 The Relational Database Model – Keys, Integrity Rules
Page 33: Database Management System Lecture 6 The Relational Database Model – Keys, Integrity Rules

• Adding the row below to the student member table would violate the Key Integrity rule and would not be allowed by the database. This is because there is already a row in the table with primary key value 101022778

•  

Page 34: Database Management System Lecture 6 The Relational Database Model – Keys, Integrity Rules

• Similarly, the row below would violate the Key Integrity rule as its primary key value is NULL.

Page 35: Database Management System Lecture 6 The Relational Database Model – Keys, Integrity Rules

• Deleting the 3rd row in the department table would violate the Referential Integrity rule as primary key value ‘121’ will be deleted. This will cause the foreign key in the 1st row of the student member table to be invalid.

Page 36: Database Management System Lecture 6 The Relational Database Model – Keys, Integrity Rules

• Similarly, updating the 3rd row in the department table would be invalid as the foreign key in the 1st row of the student member table will be pointing to a primary key with a different value.

Page 37: Database Management System Lecture 6 The Relational Database Model – Keys, Integrity Rules

Summary

• In this lesson, the following topics were discussed:

• Primary Keys.• Foreign Keys.• Integrity Rules.• Enforcing the Integrity Rules.