Chapter 4 Relational model. The main contents of this chapter include all of the following Relational model concept, relational data model, relational database, relation schema, relational algebra, relational query languages,...
Trang 1Chapter 4
Relational Model
Trang 2Relational Model Concept
Relational database: a set of relations
Each relation resembles a table of values
Relation: made up of 2 parts
◦ Instance: a table, with rows and columns
◦ Schema: specifies name of relation, plus name
and type of each column
The terms commonly used User Model Programmer
Column Attribute Field
Table Relation File
Trang 3Relational Data Model
The relational model uses a collection of
tables to represent both data and the relationships among those data
Each relation resembles a table of values
Tables are logical structures maintained
by the database manager
Ex:
STUDENT (NO., NAME, DATE_OF_BIRTH, GENDER)
Relation Set of attributes
Trang 4Relational Database
Ex:
◦ A row is called tuple
◦ A column is called attribute
◦ The table is called relation.
Trang 5Relational Database
For each column of a table, there is a set
of possible values called its domain
Domain is the set of valid values for an attribute
Trang 6Relation Schema
Relation Schema R, denoted by R(A1, A2,…, An), is made up of relation name R and a list of attributes A1, A2, …,An
Each attribute Ai is the name of a role played by some domain D in the relation schema R
D is called the domain of Ai and is denoted by dom(Ai)
R is called the name of the relation
The degree of a relation is the number of attributes n of its relation schema
Trang 8Relational Data Model
The relational model is a combination of three components:
◦ Structural Part: the database as a collection
of relations
◦ Integrity Part: maintained in the relational
model using primary and foreign keys
and relational calculus are the tools used to manipulate data in the database
Trang 9Concept of Key
An attribute or group of attributes, which
is used to identify a row in a relation
Can be classified into 3 types:
◦ Super key
◦ Candidate key
◦ Primary key
Trang 11Concept of Key
Candidate key:
◦ a minimal super key
◦ a minimal set of attributes whose values uniquely identify tuples in the corresponding relation
Trang 12◦ employee salary, etc
Super keys can be: employee ID, employee name, employee age, employee experience, etc
Candidate keys can be: employee ID, employee name, employee age
Primary key: employee ID
Trang 13Concept of Key
Trang 14Relational Query Languages
A major strength of the relational model: supports simple, powerful querying of data
Queries can be written intuitively, and the DBMS is responsible for efficient evaluation
The SQL Query Language is developed
by IBM in the 1970s
Trang 16◦ If PLAYER’s name’s used as the primary key
cannot insert any data in the relation PLAYER without entering the name of the player
Trang 18non-Relational Integrity
Domain Integrity Constraint
◦ are based upon the semantics of the real world enterprise that is being described in the database relations
◦ are used in the relational model to define the characteristics of the columns of a table
Ex:
◦ The age of the person cannot have any letter from the alphabet The age should be a numerical value
Trang 19Relational Integrity
Referential Integrity
◦ a database must not contain any unmatched foreign key values
◦ not imply a foreign key cannot be null
◦ Either each foreign key value must match a primary key value in another relation or the foreign key value must be null
Trang 20ER design to Relational
Entity sets to tables
EMPLOYEE (Emp_ID, Name, Age)
CREATE TABLE Employees
(Emp_ID CHAR(11), Name CHAR(20), Age INTEGER, PRIMARY KEY (Emp_ID)) EMPLOYEE
Emp_ID
Name
Age
Trang 21Relational Algebra
Query languages: Allow manipulation and
retrieval of data from a database
Relational model supports simple, powerful QLs:
◦ Strong formal foundation based on logic
◦ Allows for much optimization
Query Languages != programming languages!
◦ QLs not intended to be used for complex calculations
◦ QLs support easy, efficient access to large data sets
Trang 22Relational Algebra
Relational algebra
* Selection
* Projection
Trang 23Selection
Work on a single relation R
Define a relation that contains only those tuples of R that satisfy the specified condition
Trang 24Selection
Ex:
Selection operation is associative
Trang 25Selection _ example
List all students who get GPA mark > 8
Student Roll No Name GPA
Trang 26Projection
Define a relation that contains a vertical subject of R, extracting the values of specified attributes and elimination duplicates
Syntax: pa1,a2, ,an( R)
Trang 27 pName, salary ( STAFF)
Give the name and Date of birth of the all the staff
pName, Date of birth ( STAFF)
Trang 29 Define a relation consisting of the set of all tuples that are in both R and S
Expression: R S
Ex:
Intersection
R S = { t | tR tS }
Trang 30 Define a relation consisting of the tuples that arein relation R but not in S
Expression: R - S
Ex:
Intersection
R - S = { t | tR tS }
Trang 31◦ Expression: Original relation: R(A, B, C)
◦ Attribute A will be change into X
ρS(R)
ρX,B,C(R)
Trang 32◦ E mp_Dep4 σDepNo=4 (STAFF))
◦ R pFName, LName ( Emp_Dep4 )
◦ ρFirstName, LastName(R)
Trang 33Cartesian Product
Define a relation that is the concatenation
of every tuples of relation R with every tuples of relation S
The result of Cartesian product contains
all attributes from both relations R and S
Expression: R x S
Trang 34Cartesian Product
Ex:
Trang 35Cartesian Product
Ex:
Trang 36Cartesian Product
Ex:
Trang 37 3 types of join operation:
◦ Natural Join
◦ Equi Join
◦ Theta Join
Trang 38Equi Join
A special case of condition joins where the condition C contains only equality
Expression:
Trang 39Equi Join
Trang 40◦ Enforce equality on all column attributes
◦ Eliminate one copy of common attribute
Trang 41Natural Join
Trang 42Theta Join
A conditional join in which we impose condition other than equality condition
Trang 43Outer Join
In outer join, matched pairs are retained unmatched values in other tables are left null
Full outer join
Left outer join
Right outer join
Trang 44Outer Join
Trang 45Outer Join
Trang 46Outer Join
Trang 47Outer Join