Tải bản đầy đủ (.pdf) (47 trang)

Lecture Database management systems Chapter 4 Relational model

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (1.56 MB, 47 trang )

Chapter 4

Relational Model


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


TheUser
terms commonly
Model usedProgrammer
Row
Column
Table

Tuple
Attribute
Relation

Record
Field
File



Relational 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


Relational Database


Ex:

◦ A row is called tuple.
◦ A column is called attribute.
◦ The table is called relation.


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


Relation 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


Relational Algebra


Symbol





A1, A2, A3, … , An: attribute
D1, D2, D3, … , Dn : domain
R (A1:D1, A2:D2, …, An:Dn): relational model
r(R): relation
 r = {t1, t2, t3, …, tn}
 ti : tuple


Relational 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
◦ Manipulative Part: The relational algebra
and relational calculus are the tools used to
manipulate data in the database.


Concept 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


Concept of Key


Super key:
◦ a subset of attributes of an entity-set that
uniquely identifies the entities.
◦ Represent a constraint that prevents two
entities from ever having the same value for
those attributes.


Concept of Key



Candidate key:
◦ a minimal super key.
◦ a minimal set of attributes whose values
uniquely identify tuples in the corresponding
relation.



Primary key:
◦ a designated candidate key.
◦ not be null



Foreign key:
◦ Set of fields or attributes in one relation that is
used to “refer” to a tuple in another relation.


Concept of Key


Ex: Employee characterized by these attributes:







employee ID
employee name
employee age
employee experience
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.


Concept of Key


Relational 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



Relational Integrity



Four types of data integrity constraints:





Entity
NULL
Domain Integrity Constraint
Referential integrity.


Entity Integrity:
Imply that a primary key cannot accept
null value
 Ex:


◦ The attributes of the entity PLAYER are
Name, Age, Nation, and Rank.
◦ 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.


Null Integrity



Imply that the data on attributes can be
NULL value are or are not permitted



Ex:
◦ The relation PERSON must have a valid, nonNULL value for the Name attribute, then
Name of PERSON is constrained to be NOT
NUL


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.



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


ER design to Relational


Entity sets to tables
Name
Age

Emp_ID

EMPLOYEE



CREATE TABLE Employees
(Emp_ID CHAR(11),
Name CHAR(20),
Age INTEGER,
PRIMARY KEY (Emp_ID))


EMPLOYEE (Emp_ID, Name, Age)


Relational 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
languages!

!=

programming

◦ QLs not intended to be used for complex
calculations.
◦ QLs support easy, efficient access to large data
sets.



Relational Algebra
Relational
algebra
Set Operations

Combined set
* Union ()
* Cartesian (x)
* Intersection () * Join (
)
* Difference (-)
* Rename (ρ)

Database
Operations
* Selection
* Projection


Selection
Work on a single relation R
 Define a relation that contains only those
tuples of R that satisfy the specified
condition
 Syntax:
σ <selection condition>(R)


◦ In selection condition:
 Comparison operator : >, >=, <, <=, =, ≠

 Relational operator: , , ¬


Selection


Ex:



Selection operation is associative.


Selection _ example
Student Roll. No
001

Name

GPA

Aravind

7.2

002
003
004

Anand

Balu
Chitra

7.5
8.2
8

005
006
007

Deepa
Govind
Hari

8.5
7.2
6.5



List all students who get GPA mark > 8


×