Download as pdf or txt
Download as pdf or txt
You are on page 1of 38

The Relational Database Model

Fundamental concepts
!

! ! !

The relational database model is data model representing data in the form of relations (tables) Entity is a person, place, event, animal or thing about which we want to collect data Entity set is a grouping of related entities. Example: students taking IS&T 365 Attribute is a characteristic of an entity. Examples are SSN, Name, Age
2

A relation is a two dimensional table of rows of data about some entities F-Name John David

! Example: Student relation SSN L-Name 300 00 0000 Floyd 200 00 0000 Hansen

! Each column represents an attribute ! Note: the order of the rows and columns is immaterial to the user
3

! The degree of a relation is the number of attributes in a relation ! Each row (called also tuple) in the table represents a single entity ! Attribute domain is the set from which an attribute takes its value
!

Some domains are:


! ! ! ! Numeric: example: 4; 2.5; -30.354 Character (string): example: A; Ali, $ Date: example: 5/23/88 Logic: example: yes/no; true/false
4

Null value: a value given to an attribute in a tuple if the attribute is inapplicable or its value is unknown ! A null value is not a blank or zero, it is simply unknown or inapplicable and may be supplied at later time ! A relationship is a linking between entities of two entity sets ! A recursive relationship is relationship that relates an entity set to itself
!

Employee

Supervise

Employee
5

Functional dependency ! An attribute Y is functionally dependent on X if and only if each value in column X determines one and only one value in column Y. We say X is the determinant ! Note: X and Y can be sets of attributes rather than single attributes. ! Notation: X --------> Y

Example: what are the different functional dependencies in the following relation? A a1 a1 a2 a2 B b1 b2 b1 b2 C c1 c1 c1 c1 D d4 d4 d3 d3 E e6 e7 e6 e7 B------->C E------->B
7

A------->C B------->E D------->A

A------->D D------->C

Superkey: an attribute (or a combination of attributes) that uniquely identifies each row in a relation candidate key: a minimal set of attributes that uniquely identifies each row in a table. That is, a superkey that does not contain a subset of attributes that is itself a superkey Primary key: The candidate key designated for principal use in uniquely identifying tuples in a relation

Secondary key: an attribute (or a combination of attributes) used strictly for data retrieval purposes. Example: use of name or phone number instead of customer number Foreign key: an attribute (or a combination of attributes) in one relation that constitutes a primary key in another relation. It is used to indicate logical links between relations Key attribute: an attribute that is part of the primary key of a relation

Example: STUDENT(SSN, Name, Address) TAKE-COURSE(SSN, C-Call#) COURSE(C-Call#, C.Name, Room, Building) C-Call# in the relation TAKE-COURSE is a foreign key that references C-Call# in the relation COURSE. Also, SSN in TAKE-COURSE is a foreign key

10

Integrity constraints (rules)


! A integrity constraint is a rule that restricts the values in a database ! There are two constraints:
1.! Entity integrity: no key attribute in any row in a table may be null. The values of the primary key should be unique/distinct. Example: it is not acceptable to include a student in the table STUDENT without entering her/his SSN
11

2.! Referential integrity: the value of a non-null foreign key must be a value of the primary key in the other corresponding relation
Example: 302 55 2121 BA530 is a tuple in TAKE-COURSE. Since BA530 is a nonnull value of the foreign key C-Call# in the relation TAKE-COURSE, therefore a course with call # BA530 must be included in the relation COURSE
12

Relation database operators 1. SELECT: creates a new relation by selecting only rows from an original an original relation that satisfy a specified condition. The operators that can be used in the conditions =, <, >, _, _, NOT, AND, OR and combinations of these operators.

13

Example: COURSE Course# MA330 CIS720 STUDENT S# 200 335 200 Name John Mary John Course# MA330 MA330 CIS720
14

C-H 2 5

LOC HH220 BS321

Query: Give full information about courses having more than 4 credit hours Result: Course# CHour LOC CIS720 5 BS321 2. PROJECT: creates a new table by selecting only specified columns from an original table Query: What are the numbers of courses offered? Solution: PROJECT COURSE on Course# Result: Course# MA330 CIS720
15

3. JOIN: combines two or more relations 3.1 Natural JOIN: using two tables A and B, Natural JOIN creates a new table that contains every row from A combined with every row from B.

16

Example: the Natural JOIN of COURSE and STUDENT is Course# C-H LOC S# NameCourse# MA330 2 HH220 200 John MA330 MA330 2 HH220 335 Mary MA330 MA330 2 HH220 200 John CIS720 CIS720 5 BS321 200 John MA330 CIS720 5 BS321 335 Mary MA330 CIS720 5 BS321 200 John CIS720

17

3.2 Equi JOIN: It eliminates from the table obtained with the Natural JOIN operator all unmatched pairs using the common attribute. Example: The Equi JOIN of COURSE and STUDENT is
Course# C-H LOC S# Name Course#

MA330 MA330 CIS720

2 2 5

HH220 HH220 BS321

200 John MA330 335 Mary MA330 200 John CIS720

18

The JOIN operator can be combined with other operators Example: List the S# and Names of students taking a course with more than 3 credits hours Solution: a. Equi JOIN COURSE and STUDENT to obtain relation A b. SELECT from A only tuples in which the value of the attribute C-H is greater than 3. We obtain relation B c. PROJECT table B on S# and Name
19

Result: S# Name 201 John

4. UNION: creates a new table as the combination


(union) of two or more union compatible relations Two relations are union compatible if they have equivalent columns as to their numbers and domains

20

Example: Relation R1: S# NameCourse# 200 John MA330 335 Mary MA330 200 John CIS720 Relation R2: S# NameCourse# 500 John MA330 335 Mary MA330 250 David BU200
21

R1 UNION R2: S# NameCourse# 200 John MA330 335 Mary MA330 200 John CIS720 500 John MA330 250 David BU200

22

5. INTERSECTION: creates a new table that includes all rows that appear in both tables Example: R1 INTERSECT R2: S# Name Course# 335 Mary MA330

23

6. DIFFERENCE (SUBTRACTION): using two tables, this operator creates a new table that includes rows that are in the first relation but not in the second relation Example: R1 SUBTRACT R2: S# NameCourse# 200 John MA330 200 John CIS720

24

7. PRODUCT: creates a new table by listing all pairs of rows from two tables Example: R1 R2 R1 PRODUCT R2 A B C A B C a1 b1 c1 a1 b1 c1 a2 b2 c2 a1 b1 c2 a2 b2 c1 a2 b2 c2

25

8. DIVIDE: creates a new relation by selecting the rows in one relation that match every row in the other relation. To be included in the new table, a value (s) in the "unshared" column (s) in table 1 (in the following example LOC) must be associated (in the dividing table 2) with every value in table 1.

26

Example: Table 1 Code Loc a 5 b 5 c 6 d 7 d 8 e 8 a 6 b 6 b 3

Table 2
Nb 2 2 2 3 1 5 2 3 5 Code Nb a 2 b 2

New table
Loc 5

27

Relationships within the relation database


! The Entity-Relationship (E-R) model shows entities and their interrelationships.
or or indicates "one" indicates "many"

! The direction of the arrow indicates the direction of the relationship

28

1. 1:M relationship

DEPARTMENT
1 M

Example: DEPARTMENT D# D.Name Loc 100 Finance A1 120 Markg A2 150 Actg A3

EMPLOYEE

EMPLOYEE E# E.Name 1500 John 1210 Mary 2200 David 3500 Paul

D# 100 100 120 100


29

The 1:M relationship is implemented in the relational database model by including the primary key of the "one" in the "many" table

2. M:M relationship
COURSE
M M

Student 200 John

Courses taken 365 database 212 OS 170 Hardware 365 212 400 database OS Commun
30

STUDENT

300 Mary

STUDENT table S# S.Name C# 200 John 365 200 John 212 200 John 170 300 Mary 365 300 Mary 212 300 Mary 400

COURSE table C# C.Name 365 Database 365 database 212 OS 212 OS 170 Hardware 400 Commun

S# 200 300 200 300 200 300

31

The two tables contain a lot of data redundancy. To overcome this problem, we create a composite or bridge entity set for which we create a new table whose primary key is composed of the key attributes of the tables that must be linked.
COURSE
1 M TAKE M

STUDENT
1

32

STUDENT table S# S.Name 200 John 300 May

COURSE table C# C.Name 365 Database 212 OS 170 Hardware 400 Commun

TAKE table S# 200 200 200 300 300 300

C# 365 212 170 365 212 400


33

This table contains the minimum amount of duplicated data.

Mapping Supertype/Subtype Relationships


! Example: There are three groups of employees: Hourly employees, Salaried employees, and Consultants. Supertype relation Subtype relations

34

Mapping Supertype/Subtype Relationships (cont.)

35

Indexes: used to speed up the retrieval of data in a database. An index table is conceptually composed of: ! Index key: usually the primary key of the "one" relation ! Set of pointers: the row numbers in the "many" table to which the index points

36

Example:
DEPARTMENT EMPLOYEE D# D.Name Loc E# E.Name D# 100 Finance A1 1500 John 100 120 Markg A2 1210 Mary 100 150 Actg A3 2200 David 120 3500 Paul 150 The index table looks like this: 100 1 2 120 3 150 4

37

38

You might also like