chapter 2 relational data model. definition of database shared collection of logically related data...
TRANSCRIPT
CHAPTER 2
RELATIONAL DATA MODEL
DEFINITION OF DATABASE
Shared collection of logically related data (and a description of this data), designed to meet the information needs of an organization.
RDBMS
Relational Database Management System (RDBMS) is a database base on the relational model developed by Dr. E.F. Codd of IBM Research in 1970.
A DBMS that manages the relational database.
The model represent data in form of table.
COMPONENTS & CHARACTERISTICS OF RDBMS
RELATIONIs a table with column and rowsA relation(table) consists of unique attributes
(column) and tuples (rows)
ATTRIBUTE (FIELD)An attribute is named column of relationUsed to hold information about the objects to be
represented in database
RECORD (TUPLE)A record is a row of a relation
COMPONENTS & CHARACTERISTICS OF RDBMS
DOMAINA domain is the set of allowable values for one
or more attributes
DEGREEThe degree of relation is the number of
attributes it containsRelationship degree: The number of entities
associated with the relationship.
CARDINALITY• The cardinality of relation is the number of
rows it contain.
• Relationship cardinality: Express the specific number of entity occurrences associated with occurrences of the related entity
•In data modelling terms, cardinality is how one table relates to another.1-1 (1:1) (one row in table A relates to one row in tableB) 1-Many (1:m) (one row in table A relates to many rows in tableB)Many-Many (m:n) (Many rows in table A relate to many rows in table B)
COMPONENTS & CHARACTERISTICS OF RDBMS
RELATION INSTANCE Relation: made up of 2 parts:
1. SCHEMA : specifies name of relation, plus name
and type of each columnE.G.Student(sid: char(10), name: char(20), age: integer, gpa: real, login: char(20)).
2. INSTANCE : a table of values in rows and columns Relation instance is a set of rows (tuples, records) As a set, it is not supposed to have duplicates!
RELATION KEYSThere are no duplicate tuples within a relation.
Therefore, we need to be able to identify one or more attributes (called relational keys) that uniquely identifies each tuple in a relation.
There are several types of relational keys;Primary KeyCandidates KeyForeign Key
RELATION KEYSPRIMARY KEY
is a column (or columns) in a table that uniquely identifies the rows in that table.
For example, in the table above, CustomerNo is the primary key.
The values placed in primary key columns must be unique for each row: no duplicates can be tolerated. In addition, nulls are not allowed in primary key columns.
RELATION KEYSCANDIDATES KEY
is a column that meets all of the requirements of a primary key. In other words, it has the potential to be a primary key.
RELATION KEYSFOREIGN KEY
are columns that point to primary key columns.
So, for example, OrderNo is the primary key of the table ORDERS below and CustomerNo is a foreign key that points to the primary key in the CUSTOMERS table.
Integrity RulesRules of constraints or restrictions that apply to
all instances of the database.
A feature provided by relational database management system (RDBMS), which prevents users or applications from entering inconsistent data.
There are 2 types of integrity rules;Entity IntegrityReferential Integrity
INTEGRITY RULESENTITY INTEGRITY
No attribute of a primary key can be null.
If we allow null for any part of primary key, we are implying that not all the attributes are needed to distinguish between tuples.
NULLNull mean that no value has yet been supplied.Null not the same with zero numeric value or
text string filled with spaces; zeros and spaces are values
A null represent the absence of a value.
INTEGRITY RULESREFERENTIAL INTEGRITY
This rule applies to a foreign keys.
If a foreign key exist in a relation, either the foreign key value must match a candidate key value of some tuples in its home relation or the foreign key value must be wholly null.
A table which has a foreign key referring to its own candidate key is known as self-referencing table.
DATABASE DESIGN TECHNIQUE• ER diagram is widely used in database design
– Represent conceptual level of a database system
– Describe things and their relationships• ERD is a top-down approach to database
design.• ERD consists 3 basic concepts: - Entity - Attribute - Relationship
Entity• Relational database design starts by defining the
required entities• Entities are set of object you want to store data
about– people (students, customers, employees, etc.)– places (resorts, cities, countries, restaurants
etc.)– things (products, invoices, movies, paintings,
books, buildings, contracts, etc.)– events (elections, presentations, earthquakes,
etc.)• Represented by a rectangular box
EntityName
AttributeAttribute hold values that describe the properties
of entitiesE.g. student entity could have attributes
such as name, address, DOB etc.•Each entity has one or more attributes
associated with it.•Represented by a ellipse
Attribute Name
Types of Attributes Definition Example
Simple attribute Cannot be divided into simpler components
Gender of the employee
Composite attribute Can be split into components
Address of the employee
Single valued Can take on only a single value for each entity instance
Age of the employee
Multi values Can take up many values
Skill set of the employee
Stored attribute Attribute that need to be stored permanently
Date of joining of the employee
Derived attribute Attribute that can be calculated based on other attributes
Years of the service of the employee
Type of Attributes
Composite Attribute
EMPLOYEEEmpNo
EmpName
Designation
DOB
Building
Street
Address
Poscode
State
MULTI VALUED ATTRIBUTE
EMPLOYEEEmpNo
EmpName
Designation
DOB
Building
Street
Address
Poscode
State
Skill
STORED & DERIVED ATTRIBUTE
EMPLOYEEEmpNo
EmpName
Designation
Building
Street
Address
Poscode
State
DateEmployed
YearsEmployed
ATTRIBUTES OF A RELATIONSHIP
VENDORRAW
MATERIALSSuppli
es
VendorID VendorNa
me
VendorAddress
MaterialID MaterialNa
me
StandardCost
SupplyUnitPrice
RELATIONSHIP• Relationship specify the relations among entities Relationship specify the relations among entities
from two or more entity setsfrom two or more entity sets• Describe a meaningful interaction between entity.Describe a meaningful interaction between entity.• Represented by a diamond
Relationship Name
Degree of Relationship• Relationship degree: The number of entities
associated with the relationship. Degree: the number of entity types involved One Unary Two Binary Three Ternary
Types of Relationship Degrees1. Unary Relationship
– exist when an association is maintained within a single entity
- also known as RECURSIVE RELATIONSHIP
Types of Relationship Degrees2. Binary Relationship
– exist when 2 entities are associated
Types of Relationship Degrees2. Ternary Relationship
– exist when 3 entities are associated
RELATIONAL MODEL RELATIONSHIPS
Relationship Cardinality: Express the specific number of entity occurrences associated with occurrences of the related entity
There are 3 types of relationships;One to One (1:1)One to Many (1:M)Many to One (M:1)Many to Many (M:N)
Eg;Employee is assigned to parking place (1:1)Department offers course (1:M)Employee work in department (M:1)Employee is assigned to project (M:N)
RELATIONAL MODEL RELATIONSHIPS
ONE TO ONE (1:1)One occurrence of an entity relates to only
one occurrence in another entity
A B
1 1
RELATIONAL MODEL RELATIONSHIPS
ONE TO MANY (1:M)– One occurrence in an entity relates to many
occurrences in another entity.
A
B1
B2
B3
M1
RELATIONAL MODEL RELATIONSHIPS
MANY TO ONE (M:1)many occurrences in an entity relate to one
occurrences in another entity
AB2
B3
B1
1M
RELATIONAL MODEL RELATIONSHIPSMANY TO MANY (M:N)
many occurrences in an entity relate to many occurrences in another entity
B3
B2
B1A1
A2
M N
STEPS IN ER MODELING:
Identify the entities
Find the relationship
Identify the key attributes for every entities
Identify other relevant attributes
Draw complete ER-modelling with all attributes including primary key
RELATIONAL ALGEBRARelational algebra is a (high-level) procedural
language associated with the relational model.Basic operations in relational algebra:
Unary Relational Operations: Selection Projection
Cartesian productUnionSet DifferenceIntersectionBinary Relational Operations
Division operations Join
RELATIONAL ALGEBRA
5 BASIC OPERATIONSSelection ( ) Selects a subset of rows from
relation (horizontal).
Projection ( ) Retains only wanted columns from relation (vertical).
Cross-product (x) Allows us to combine two relations.
Set-difference (–) Tuples in r1, but not in r2.
Union ( ) Tuples in r1 and/or in r2.
Example InstancesR1 S1
S2
Boats
bid bname color 101 Interlake blue 102 Interlake red 103 Clipper green 104 Marine red
sid bid day
22 101 10/10/96 58 103 11/12/96
sid sname rating age
22 dustin 7 45.0
31 lubber 8 55.5 58 rusty 10 35.0
sid sname rating age 28 yuppy 9 35.0 31 lubber 8 55.5 44 guppy 5 35.0 58 rusty 10 35.0
SELECT operation ()Used to select a subset of the tuples from a
relation that satisfies a selection condition
Symbol -> σ (sigma)
Syntax : σ<selection condition>( R )
sid sname rating age 28 yuppy 9 35.0 31 lubber 8 55.5 44 guppy 5 35.0 58 rusty 10 35.0
sname rating yuppy 9 rusty 10
SELECT operation ()
rating
S8
2( )
sname rating rating
S,
( ( ))8
2
SELECT operation ()Eg1: Select the STUDENT tuples whose Semester is 3.
Eg2: Select the MANAGER tuples whose Salary is greater
than RM25,000.
Eg3: Select the tuples for all SUPERVISOR who either work in IT Department and Salary less than RM20,000 per year, or work in HR Department
and Salary less than RM30,000.
σSem=3(STUDENT)
σSalary>25000(MANAGER)
σ(Dept=“IT” AND Salary<20000) OR Dept=“HR” AND Salary<30000))
(SUPERVISOR)
PROJECT operation
Select certain columns from the table and discard the other columns
Symbol -> Л (pi)
Syntax – Л <attribute list> (R)
PROJECT operation
S2 sid sname rating age
28 yuppy 9 35.0 31 lubber 8 55.5 44 guppy 5 35.0 58 rusty 10 35.0
sname rating
yuppy 9 lubber 8 guppy 5 rusty 10
age
35.0 55.5
)2(, Sratingsname
age S( )2
PROJECT operationEg1: List each student’s Regs.No, First Name and
Last Name.
Eg2: List IDNum, Name and Age of all supervisors who
work in IT department.
ЛRegs.No, Fname,
Lname(STUDENT)
ЛIDNum,Name,Age (σDept=“IT”(SUPERVISOR))
UNION operationUNION compares tuples in two relations and
create a new relation that contains some of the tuples from each of the input relations.
Symbol -> υ (cup)
R υ S
UNION operation
S1
S2
sid sname rating age
22 dustin 7 45.0
31 lubber 8 55.5 58 rusty 10 35.0
sid sname rating age 28 yuppy 9 35.0 31 lubber 8 55.5 44 guppy 5 35.0 58 rusty 10 35.0
sid sname rating age
22 dustin 7 45.0 31 lubber 8 55.5 58 rusty 10 35.0 44 guppy 5 35.0 28 yuppy 9 35.0
S S1 2
UNION operationEg1: List all IDNum where there is either a
Manager or Supervisor for IT Department.
Eg2: List Regs.No for students those register at Department and Hostel.
ЛIDNum(σDept=“IT”(MANAGER)) υ ЛIDNum(σDept=“IT”(SUPERVISOR))
ЛRegs.No(DEPARTMENT) υ ЛRegs.No(HOSTEL)
CROSS PRODUCT operation• Defines a relation that is the concatenation of
every tuple of relation R with every tuple of relation S.
Denoted by X
R X S
CROSS PRODUCT operation
R1
S1
sid sname rating age
22 dustin 7 45.0
31 lubber 8 55.5 58 rusty 10 35.0
sid bid day
22 101 10/10/96 58 103 11/12/96
(sid) sname rating age (sid) bid day
22 dustin 7 45.0 22 101 10/ 10/96
22 dustin 7 45.0 58 103 11/ 12/96
31 lubber 8 55.5 22 101 10/ 10/96
31 lubber 8 55.5 58 103 11/ 12/96
58 rusty 10 35.0 22 101 10/ 10/96
58 rusty 10 35.0 58 103 11/ 12/96
R1 X S1
CARTESIAN PRODUCT operationEg1: List the Regs.No, Name and NRIC of all
students who registred CourseID, CourseName for
course.ЛRegs.No,Name,NRIC(Student) X (Л Regs.No,CourseID,
CourseName(Course))
INTERSECTION operationDefines a relation consisting of the set of all
tuples that are in both R and S.
Symbol -> ∩ (cap)
R ∩ S
INTERSECTION operation
S1S2
sid sname rating age 31 lubber 8 55.5 58 rusty 10 35.0
sid sname rating age
22 dustin 7 45.0
31 lubber 8 55.5 58 rusty 10 35.0
sid sname rating age 28 yuppy 9 35.0 31 lubber 8 55.5 44 guppy 5 35.0 58 rusty 10 35.0
S S1 2
INTERSECTION operationEg1: List Regs.No for students those having
register at both Department and Hostel.
ЛRegs.No(DEPARTMENT) ∩ ЛRegs.No(HOSTEL)
SET DIFFERENCE operationDefines a relation consisting of the tuples that
are in relation R, but not in S.
Symbol -> - (minus)
R - S
SET DIFFERENCE operation
S1 S2
sid sname rating age
22 dustin 7 45.0
31 lubber 8 55.5 58 rusty 10 35.0
sid sname rating age 28 yuppy 9 35.0 31 lubber 8 55.5 44 guppy 5 35.0 58 rusty 10 35.0
sid sname rating age
22 dustin 7 45.0
sid sname rating age 28 yuppy 9 35.0 44 guppy 5 35.0
cS S1 2S2 – S1
SET DIFFERENCE operationEg1: List Regs.No for students those register at Department but no at Hostel.
ЛRegs.No(σcourse=“DNS3A”(DEPARTMENT)) – ЛRegs.No(σcourse=“DNS3A”(HOSTEL))
JOIN operation
Compute R X S
Select rows where attributes that appear in both relations have equal values
Project all unique atttributes and one copy of each of the common ones.
NATURAL JOIN operation
R1
S1
sid sname rating age bid day
22 dustin 7 45.0 101 10/10/9658 rusty 10 35.0 103 11/12/96
sid sname rating age
22 dustin 7 45.0
31 lubber 8 55.5 58 rusty 10 35.0
sid bid day
22 101 10/10/96 58 103 11/12/96
cR1 S1
Natural JoinThe tuples in the resulting relation are
obtained by combining tuples in the operands with equal values on the common attributes.
The common attributes often form a key of one of the operands (remember: references are realized by means of foreign keys, and we join in order to follow references)
OUTER JOIN“Pads With Nulls”A variant of the join, to keep all pieces of
information from the operands.Three Variants :
Left – only tuples of the left operand are padded.
Right – only tuples of the right operand are padded.
Full – tuples of both operands are padded
R1
R2
Employee Department
Smith Sales
Black Production
White Production
Department Head
Production Mori
Purchasing Brown
OUTER LEFT JOINR1 LEFT R2 EMPLOYEE DEPARTMEN
THEAD
Smith Sales Null
Black Production Mori
White Production Mori
OUTER RIGHT JOINR1 RIGHT R2
EMPLOYEE DEPARTMENT
HEAD
Black Production Mori
White Production Mori
Null Purchasing Brown
OUTER FULL JOINR1 RIGHT R2
EMPLOYEE DEPARTMENT
HEAD
Smith Sales Null
Black Production Mori
White Production Mori
Null Purchasing Brown