0% found this document useful (0 votes)
263 views31 pages

Relational Model PDF

Uploaded by

Jopel Main
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
263 views31 pages

Relational Model PDF

Uploaded by

Jopel Main
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 31

Relational Model (MIX)

Sumber: Silberschatz, Korth and Sudarshan, Database System


Concepts, 6th Ed.
Jeffrey A. Hoffer, Mary B. Prescott, Fred R. McFadden, Modern
Database Management, 8th Ed.

Program Studi Teknik Informatika


Institut Teknologi Bandung

3/27/2019 Relational Model (Taken from the slides of the original books and modified by TW) 1
Learning Outcomes
• Define the fundamental terminology used in the
relational data model
• Describe the basic principles of the relational data
model
OBJECTIVES
• How relational database model takes a logical view of data
• Understand how the relational model’s basic components are
relations implemented through tables in a relational DBMS
• How relations are organized in tables composed of rows
(tuples) and columns (attributes)
• Use 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/27/2019 Relational Model 2


Overview Database Modeling Process
EKSTERNAL - USER Clients Inputs
DUA
PENDEKATAN
ER/EER Model BERBEDA,
LOGICAL LEVEL TETAPI
MEMBERIKAN
Relational Model MAKNA YANG
SAMA

Relational Database System


PHYSICAL LEVEL

Client App 2
Client App 1

INTERNAL - STORAGE

3/27/2019 Relational Model 3


Relational Model Concepts
• A Relation is a mathematical concept based on
the ideas of sets
• The model was first proposed by Dr. E.F. Codd of
IBM Research in 1970 in the following paper:
– "A Relational Model for Large Shared Data Banks,"
Communications of the ACM, June 1970
• The above paper caused a major revolution in the
field of database management and earned Dr.
Codd the coveted ACM Turing Award

3/27/2019 Relational Model (©Silberschatz, Korth and Sudarshan) 4


Why Study the Relational Model?
• Most widely used model.
– Vendors: IBM, Informix, Microsoft, Oracle,
Sybase, etc.
• “Legacy systems” in older models
– E.G., IBM’s IMS
• Recent competitor: object-oriented model
– ObjectStore, Versant, Ontos
– A synthesis emerging: object-relational model
• Informix Universal Server, UniSQL, O2, Oracle, DB2

3/27/2019 Relational Model (©Silberschatz, Korth and Sudarshan) 5



Relation
Definition: A relation is a named, two-dimensional
table of data
• Table consists of rows (records) and columns (attribute
or field)
• Requirements for a table to qualify as a relation:
– It must have a unique name
– Every attribute value must be atomic (not multivalued, not composite)
– Every row must be unique (can’t have two rows with exactly the same
values for all their fields)
– Attributes (columns) in tables must have unique names
– The order of the columns must be irrelevant
– The order of the rows must be irrelevant

NOTE: all relations are in 1st Normal form

6
3/27/2019 Relational Model (© 2009 Pearson Education, Inc. Publishing as Prentice Hall)
Example of a Relation

3/27/2019 Relational Model (©Silberschatz, Korth and Sudarshan) 7


Attribute Types
• Each attribute of a relation has a name
• The set of allowed values for each attribute is called the domain of the
attribute
• Attribute values are (normally) required to be atomic; that is, indivisible
– E.g. the value of an attribute can be an account number,
but cannot be a set of account numbers
• Domain is said to be atomic if all its members are atomic
• The special value null is a member of every domain
• The null value causes complications in the definition of many operations
– We shall ignore the effect of null values in our main presentation and consider
their effect later
• Meanings for NULL values
– Value unknown
– Value exists but is not available
– Attribute does not apply to this tuple (also known as value undefined)
• IMPORTANT: NULL  NULL
3/27/2019 Relational Model (©Silberschatz, Korth and Sudarshan, modified by TW) 8
Relation Schema
• Formally, given domains D1, D2, …. Dn a relation r
is a subset of
D1 x D2 x … x Dn
Thus, a relation is a set of n-tuples (a1, a2, …, an)
where each ai  Di
• Schema of a relation consists of
– attribute definitions
• name
• type/domain
– integrity constraints

3/27/2019 Relational Model (©Silberschatz, Korth and Sudarshan) 9


Relation Instance
• The current values (relation instance) of a relation are specified
by a table
• An element t of r is a tuple, represented by a row in a table
• Order of tuples is irrelevant (tuples may be stored in an arbitrary
order)
attributes
(or columns)
customer_name customer_street customer_city

Jones Main Harrison


Smith North Rye tuples
Curry North Rye (or rows)
Lindsay Park Pittsfield

customer

3/27/2019 Relational Model (©Silberschatz, Korth and Sudarshan) 10


Example Instance of Students
Relation

sid name login age gpa


53666 Jones jones@cs 18 3.4
53688 Smith smith@eecs 18 3.2
53650 Smith smith@math 19 3.8

Cardinality = 3, degree = 5, all rows distinct

3/27/2019 Relational Model (©Silberschatz, Korth and Sudarshan) 11


Formal Definitions - Summary
• Formally,
– Given R(A1, A2, .........., An)
– r(R)  dom (A1) X dom (A2) X ....X dom(An)
• R(A1, A2, …, An) is the schema of the relation
• R is the name of the relation
• A1, A2, …, An are the attributes of the relation
• r(R): a specific state (or instance or "value" or “population”)
of relation R – this is a set of tuples (rows)
– r(R) = {t1, t2, …, tn} where each ti is an n-tuple
– ti = <v1, v2, …, vn> where each vj element-of dom(Aj)

3/27/2019 Relational Model (©Silberschatz, Korth and Sudarshan) 12


Database Schema & Relation Schema
• Database:
– Database schema -> logical design of database
– Database instance -> snapshot of the data in db
• Database schema:
– Set of Relational schema
– Relational schema -> list of attributes and their
corresponding domains
• Examples:
– Branch = (branch-name, branch-city, assets)
– Account = (account-number, branch-name, balance)

3/27/2019 Relational Model (©Silberschatz, Korth and Sudarshan) 13


Database
• A database consists of multiple relations
• Information about an enterprise is broken up into parts,
with each relation storing one part of the information
• E.g.
account : information about accounts
depositor : which customer owns which account
customer : information about customers

3/27/2019 Relational Model (©Silberschatz, Korth and Sudarshan) 14


The customer Relation

3/27/2019 Relational Model (©Silberschatz, Korth and Sudarshan) 15


The depositor Relation

3/27/2019 Relational Model (©Silberschatz, Korth and Sudarshan) 16


Why Split Information Across Relations?
• Storing all information as a single relation such as
bank(account_number, balance, customer_name, ..)
results in
– repetition of information
• e.g.,if two customers own an account (What gets repeated?)

– the need for null values


• e.g., to represent a customer without an account

• Normalization theory (later topic) deals with how to design


relational schemas

3/27/2019 Relational Model (©Silberschatz, Korth and Sudarshan) 17


Keys
• Let K  R
• K is a superkey of R if values for K are sufficient to identify a
unique tuple of each possible relation r(R)
– by “possible r ” we mean a relation r that could exist in the enterprise
we are modeling.
– Example: {customer_name, customer_street} and
{customer_name}
are both superkeys of Customer, if no two customers can possibly have
the same name
• In real life, an attribute such as customer_id would be used instead of
customer_name to uniquely identify customers, but we omit it to keep our
examples small, and instead assume customer names are unique.

3/27/2019 Relational Model (©Silberschatz, Korth and Sudarshan) 18


Keys (Cont.)
• K is a candidate key if K is minimal
Example: {customer_name} is a candidate key for Customer,
since it is a superkey and no subset of it is a superkey.
• Primary key: a candidate key chosen as the principal means
of identifying tuples within a relation
– Should choose an attribute whose value never, or very rarely,
changes.
– E.g. email address is unique, but may change
• Keys can be simple (a single field) or composite (more than
one field)
• Keys usually are used as indexes to speed up the response to
user queries
3/27/2019 Relational Model (©Silberschatz, Korth and Sudarshan) 19
Foreign Keys
• A relation schema may have an attribute that corresponds to
the primary key of another relation. The attribute is called a
foreign key.
– E.g. customer_name and account_number attributes of depositor are
foreign keys to customer and account respectively.
– Only values occurring in the primary key attribute of the referenced
relation may occur in the foreign key attribute of the referencing
relation.

3/27/2019 Relational Model (©Silberschatz, Korth and Sudarshan) 20


Schema Diagram

Foreign Keys

3/27/2019 Relational Model (©Silberschatz, Korth and Sudarshan) 21


Integrity Constraints
• Domain Constraints
– Allowable values for an attribute. See Table 5-1 (next
slide)
– Typically include:
• Numeric data types for integers and real numbers
• Characters
• Booleans
• Fixed-length strings
• Variable-length strings
• Date, time, timestamp
• Money
• Other special data types

3/27/2019 Relational Model (© 2009 Pearson Education, Inc. Publishing as Prentice Hall, modified by TW) 22
Domain definitions enforce domain integrity constraints

3/27/2019 Relational Model (© 2009 Pearson Education, Inc. Publishing as Prentice Hall) 23
Integrity Constraints
• Entity Integrity
– No primary key attribute may be null. All primary
key fields MUST have data
• Action Assertions
– Application-based or semantic constraints or
business rules
• Cannot be directly expressed in schemas
• Expressed and enforced by application program

3/27/2019 Relational Model (© 2009 Pearson Education, Inc. Publishing as Prentice Hall, modified by TW) 24
Integrity Constraints
• Referential Integrity–rule states that any foreign key value
MUST match a primary key value in the referenced relation
(Or the foreign key can be null)
– Formal Definition
• Let r1(R1) and r2(R2) be relations with primary keys K1 and K2
respectively.
• The subset  of R2 is a foreign key referencing K1 in relation r1, if for
every t2 in r2 there must be a tuple t1 in r1 such that t1[K1] = t2[].
– For example: Delete Rules
• Restrict–don’t allow delete of “parent” side if related rows exist in
“dependent” side
• Cascade–automatically delete “dependent” side rows that correspond
with the “parent” side row to be deleted
• Set-to-Null–set the foreign key in the dependent side to null if deleting
from the parent side  not allowed for weak entities

3/27/2019 Relational Model (© 2009 Pearson Education, Inc. Publishing as Prentice Hall, modified by TW) 25
Figure 5-5
Referential integrity constraints (Pine Valley Furniture)

Referential
integrity
constraints are
drawn via arrows
from dependent to
parent table

3/27/2019 Relational Model (© 2009 Pearson Education, Inc. Publishing as Prentice Hall) 26
Figure 5-6 SQL table definitions

Referential
integrity
constraints are
implemented with
foreign key to
primary key
references

3/27/2019 Relational Model (© 2009 Pearson Education, Inc. Publishing as Prentice Hall) 27
Query Languages
• Language in which user requests information from the
database.
• Categories of languages
– Procedural
– Non-procedural, or declarative
• “Pure” languages:
– Relational algebra
– Tuple relational calculus
– Domain relational calculus
• Pure languages form underlying basis of query languages
that people use.

3/27/2019 Relational Model (©Silberschatz, Korth and Sudarshan) 28


Exercise 1
• Consider the following relations for a database that keeps track
of student enrollment in courses and the books adopted for
each course:
– STUDENT(SSN, Name, Major, Bdate)
– COURSE(Course#, Cname, Dept)
– ENROLL(SSN, Course#, Quarter, Grade)
– BOOK_ADOPTION(Course#, Quarter, Book_ISBN)
– TEXT(Book_ISBN, Book_Title, Publisher, Author)

• Draw a relational schema diagram specifying the foreign


keys for this schema.

3/27/2019 Relational Model (©Silberschatz, Korth and Sudarshan) 29


Exercise 2

Exercise 2:
Relational
Schema
Diagram for
COMPANY
database
3/27/2019 Relational Model (© 2007 Ramez Elmasri and Shamkant B. Navathe) 30
Exercise 2
• Give potential constraint violations when
updating the info of a departement.
– Dname
– Dnumber
– Mgr_SSN
– Mgr_start_date
• Actions and consequences to avoid constraint
violations when deleting a department
– REJECT/RESTRICT
– CASCADE
– SET TO NULL

3/27/2019 Relational Model (© 2007 Ramez Elmasri and Shamkant B. Navathe) 31

You might also like