Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Database Management System Lecture 6 The Relational Database Model – Keys, Integrity Rules."— Presentation transcript:

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

2 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.

3 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.

4 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

5 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

6 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

7 Primary Keys (Cont.) A table in a relational database must have a primary key.

8 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

9 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.

10 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

11 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

12 Matric NumberNamePhone NumberDepartment Number 200965611Ahmad Saleh075811123121 201013456Ameer Jabir075845678100 101022778Hamza Yussuf075846467231 Student Member Table

13 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.

14 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.

15 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.

16 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 (*).

17

18 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

19 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

20 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

21 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.

22 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.

23 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

24 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

25

26 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:

27 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

28 The Referential Integrity Rule (Cont.) 3. The foreign key value in T1 must be equal to the value of T2’s primary key.

29 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.

30 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.

31 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

32

33 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

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

35 Deleting the 3 rd 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 1 st row of the student member table to be invalid.

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

37 Summary In this lesson, the following topics were discussed: Primary Keys. Foreign Keys. Integrity Rules. Enforcing the Integrity Rules.


Download ppt "Database Management System Lecture 6 The Relational Database Model – Keys, Integrity Rules."

Similar presentations


Ads by Google