2
Chapter 2
The Relational Database Model
2
2
Hachim Haddouti and Rob & Coronel, Ch2
In this chapter, you will learn:
• That the relational database model takes a logical view of data
• That the relational database model’s basic components are entities and their attributes, and relationships among entities
• How entities and their attributes are organized into tables
• About relational database operators, the data dictionary, and the system catalog
• How data redundancy is handled in the relational database model
• Why indexing is important
3
2
Hachim Haddouti and Rob & Coronel, Ch2
Logical View of Data
• Relational Database – Designer focuses on logical representation rather
than physical
– Use of table advantageous• Structural and data independence• Related records stored in independent tables• Logical simplicity
– Allows for more effective design strategies
4
2
Hachim Haddouti and Rob & Coronel, Ch2
Logical View of Data (con’t.)
• Entities and Attributes – Entity is a person, place, event, or thing about which
data is collected– Attributes are characteristics of the entity
• Tables– Holds related entities or entity set– Also called relations– Comprised of rows and columns
5
2
Hachim Haddouti and Rob & Coronel, Ch2
Table Characteristics
• Two-dimensional structure with rows and columns
• Rows (tuples) represent single entity
• Columns represent attributes
• Row/column intersection represents single value
• Tables must have an attribute to uniquely identify each row
6
2
Hachim Haddouti and Rob & Coronel, Ch2
Table Characteristics (con’t.)
• Column values all have same data format
• Each column has range of values called attribute domain
• Order of the rows and columns is immaterial to the DBMS
7
2
Hachim Haddouti and Rob & Coronel, Ch2
Keys
• One or more attributes that determine other attributes– Key attribute– Composite key
• Functional dependence ( knowing STU_NUM you can determine STU_LNAME)
• Entity integrity– Uniqueness– No ‘null’ value in key ( NULL= 0?)
• Example: AUI ID#, Invoice#, CIN#
8
2
Hachim Haddouti and Rob & Coronel, Ch2
Keys (con’t.)• Superkey
– Uniquely identifies each entity in a table
• Candidate key– Minimal superkey
• Primary key– Candidate key to uniquely identify all other
attributes in a given row
• Secondary key– Used only for data retrieval
• Foreign key (logical pointer)– Values must match primary key in another table
9
2
Hachim Haddouti and Rob & Coronel, Ch2
Keys (con’t.)
10
2
Hachim Haddouti and Rob & Coronel, Ch2
Integrity Rules
• Entity integrity– Ensures all entities are unique– Each entity has unique primary key (not null)
• Referential integrity– Foreign key must have null value or match primary key
values (every non-null FK must reference an existing primary key)
– Makes it impossible to delete row whose primary key has mandatory matching foreign key values in another table
– No dangling references ( Links in HTML?)
11
2
Hachim Haddouti and Rob & Coronel, Ch2
Relational Database Operators
• Relational algebra determines
table manipulations• Basic operators
– SELECT
– PROJECT
– JOIN
• Other operators– INTERSECT
– UNION
– DIFFERENCE
– PRODUCT
12
2
Hachim Haddouti and Rob & Coronel, Ch2
UnionCombines all rows
• Set-oriented union, duplicates will be eliminated
• (columns and domains must be identical), same Attribute characteristics
13
2
Hachim Haddouti and Rob & Coronel, Ch2
Yields rows that appear in both tables
Intersect
Figure 2.6
14
2
Hachim Haddouti and Rob & Coronel, Ch2
Yields rows not found in other tables
Difference
Figure 2.7
• Set-oriented difference
• columns and domains must be identical, same Attribute characteristics
15
2
Hachim Haddouti and Rob & Coronel, Ch2
Yields all possible pairs from two tablesProduct (Cartesian product)
16
2
Hachim Haddouti and Rob & Coronel, Ch2
Yields a subset of rows based on specified criterion
Select
Figure 2.9
17
2
Hachim Haddouti and Rob & Coronel, Ch2
Yields all values for selected attributes(vertical subset of a table)
Project
18
2
Hachim Haddouti and Rob & Coronel, Ch2
Information from two or more tables is combined by equal AGENT_CODE
Join
19
2
Hachim Haddouti and Rob & Coronel, Ch2
• Links tables by selecting rows with common values in common attribute(s)
• Three-stage process– Product creates one table
– Select yields appropriate rows
– Project yields single copy of each attribute to eliminate duplicate columns
Natural Join Process
20
2
Hachim Haddouti and Rob & Coronel, Ch2
Other Joins• EquiJOIN
– Links tables based on equality condition that compares specified columns of tables
– Does not eliminate duplicate columns – Join criteria must be explicitly defined
• Theta JOIN– EquiJOIN that compares specified columns of
each table using operator other than equality one• Outer JOIN
– Matched pairs are retained – Unmatched values in other tables left null– Two types: right and left
21
2
Hachim Haddouti and Rob & Coronel, Ch2
Data Dictionary and System Catalog
• Data dictionary– Provides detailed account of all tables found within
database– Metadata– Attribute names and characteristics
• System catalog– Detailed data dictionary– System-created database – Stores database characteristics and contents– Tables can be queried just like any other tables– Automatically produces database documentation
22
2
Hachim Haddouti and Rob & Coronel, Ch2
• Points to location• Makes retrieval of data faster
Indexes
Figure 2.31
23
2
Hachim Haddouti and Rob & Coronel, Ch2
Example 1
Identify and describe the components of the database table shown below using correct terminology.
24
2
Hachim Haddouti and Rob & Coronel, Ch2
Example 1 cont.
1 entity set: EMPLOYEE. 5 attributes: EMP_NUM, EMP_LNAME, etc. 10 entities: the workers Friedman, Olansky, Fontein, and
Cruazona. one primary key: the attribute EMP_NUM because it
identifies each row uniquely.• two foreign keys: the attribute DEPT_CODE, which
probably references a department to which the employee is assigned and the attribute JOB_CODE which probably references another table in which you would find the description of the job and perhaps additional information pertaining to the job.
25
2
Hachim Haddouti and Rob & Coronel, Ch2
Example1 cont.
• Cutomer_no is superkey, (Customer_no, Customer_Name) is also superkey for the entity set CUSTOMER
• The combination of primary keys of the participating entity sets forms a super key of a relationship set.
– (customer-id, account-number) is the super key of depositor Concept of superkey is not sufficient
PS: depositor (customer-name, account-number)
26
2
Hachim Haddouti and Rob & Coronel, Ch2
Example 2: Banking
branch (branch-name, branch-city, assets)
customer (customer-name, customer-street, customer-only)
account (account-number, branch-name, balance)
loan (loan-number, branch-name, amount)
depositor (customer-name, account-number)
borrower (customer-name, loan-number)
Stands for PROJECT, for SELECT, & for Union &
INTERSECTION
27
2
Hachim Haddouti and Rob & Coronel, Ch2
Example 2 Queries• Find all loans of over DH1200
Find the loan number for each loan of an amount greater than DH1200
amount > 1200 (loan)
loan-number (amount > 1200 (loan))
28
2
Hachim Haddouti and Rob & Coronel, Ch2
Example 2 Queries• Find the names of all customers who have a loan,
an account, or both, from the bank
Find the names of all customers who have a loan and an
account at bank.
customer-name (borrower) customer-name (depositor)
customer-name (borrower) customer-name (depositor)
29
2
Hachim Haddouti and Rob & Coronel, Ch2
Example 2 Queries• Find the names of all customers who have a loan at the
Ifrane branch of an amount greater than DH1200.
customer-name ( amount > 1200
(borrower.loan-number = loan.loan-number(borrower x loan)))
• Find the names of all customers who have a loan at the branch IFRANE.
customer-name (branch-name=“IFRANE”
(borrower.loan-number = loan.loan-number(borrower x loan)))
Top Related