ch2-abbrev
-
Upload
mary-jansi -
Category
Documents
-
view
215 -
download
0
Transcript of ch2-abbrev
-
7/28/2019 ch2-abbrev
1/25
Database System Concepts, 6th Ed.
Silberschatz, Korth and Sudarshan
See www.db-book.com for conditions on re-use
Chapter 2: Intro to Relational Model
http://www.db-book.com/http://www.db-book.com/http://www.db-book.com/http://www.db-book.com/ -
7/28/2019 ch2-abbrev
2/25
Silberschatz, Korth and Sudarshan2.2Database System Concepts - 6th Edition
Example of a Relation
attributes(or columns)
tuples(or rows)
-
7/28/2019 ch2-abbrev
3/25
Silberschatz, Korth and Sudarshan2.3Database System Concepts - 6th Edition
Attribute Types
The set of allowed values for each attribute is called thedomain of the attribute
Attribute values are (normally) required to be atomic; thatis, indivisible
The special valuenul l is a member of every domain
The null value causes complications in the definition ofmany operations
-
7/28/2019 ch2-abbrev
4/25
Silberschatz, Korth and Sudarshan2.4Database System Concepts - 6th Edition
Relation Schema and Instance
A1,A2, ,Anare attributes
R = (A1,A2, ,An ) is a relation schema
Example:
instructor = (ID, name, dept_name, salary)
Formally, given sets D1, D2, . Dn a relationr 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
The current values (relation instance) of a relation are specified by atable
An element tofr is a tuple, represented by a rowin a table
-
7/28/2019 ch2-abbrev
5/25
Silberschatz, Korth and Sudarshan2.5Database System Concepts - 6th Edition
Relations are Unordered
Order of tuples is irrelevant (tuples may be stored in an arbitrary
order) Example: instructorrelation with unordered tuples
-
7/28/2019 ch2-abbrev
6/25
Silberschatz, Korth and Sudarshan2.6Database System Concepts - 6th Edition
Database
A database consists of multiple relations
Information about an enterprise is broken up into parts
instructor
student
advisor
Bad design:
univ (instructor -ID, name, dept_name, salary, student_Id, ..)
results in
repetition of information (e.g., two students have the same
instructor) the need for null values (e.g., represent an student with no
advisor)
Normalization theory (Chapter 7) deals with how to design good
relational schemas
-
7/28/2019 ch2-abbrev
7/25Silberschatz, Korth and Sudarshan2.7Database System Concepts - 6th Edition
Keys
Let K R
Kis a superkeyof R if values for Kare sufficient to identify aunique tuple of each possible relation r(R)
Example: {ID} and {ID,name} are both superkeys of instructor.
Superkey Kis a candidate key if Kis minimal
Example: {ID} is a candidate key for Instructor
One of the candidate keys is selected to be the primary key.
which one?
Foreign key constraint: Value in one relation must appear inanother
Referencing relation
Referenced relation
-
7/28/2019 ch2-abbrev
8/25Silberschatz, Korth and Sudarshan2.8Database System Concepts - 6th Edition
Schema Diagram for University Database
-
7/28/2019 ch2-abbrev
9/25Silberschatz, Korth and Sudarshan2.9Database System Concepts - 6th Edition
Relational Query Languages
Procedural vs.non-procedural, or declarative
Pure languages:
Relational algebra
Tuple relational calculus
Domain relational calculus
Relational operators
-
7/28/2019 ch2-abbrev
10/25Silberschatz, Korth and Sudarshan2.10Database System Concepts - 6th Edition
Selection of tuples
Relation r
Select tuples with A=Band D > 5
A=B and D > 5(r)
Quiz Q1:A B OR D < 7 (r) has (1) 1 tuple (2) 2 tuples (3) 3 tuples (4) 4 tuples
-
7/28/2019 ch2-abbrev
11/25Silberschatz, Korth and Sudarshan2.11Database System Concepts - 6th Edition
Selection of Columns (Attributes)
Relation r:
Select A and C
Projection
A, C(r)
Quiz Q2:The projection operation (1) removes duplicates (2) does not remove duplicates
-
7/28/2019 ch2-abbrev
12/25Silberschatz, Korth and Sudarshan2.12Database System Concepts - 6th Edition
Joining two relations Cartesian Product
Relations r, s:
rx s:
-
7/28/2019 ch2-abbrev
13/25Silberschatz, Korth and Sudarshan2.13Database System Concepts - 6th Edition
Composition of Operations Can build expressions using multiple operations
Example: A=C(r x s)
r x s
A=C(r x s)
-
7/28/2019 ch2-abbrev
14/25Silberschatz, Korth and Sudarshan2.14Database System Concepts - 6th Edition
Union of two relations
Relations r, s:
r s:
-
7/28/2019 ch2-abbrev
15/25Silberschatz, Korth and Sudarshan2.15Database System Concepts - 6th Edition
Set difference of two relations
Relations r, s:
r s:
-
7/28/2019 ch2-abbrev
16/25Silberschatz, Korth and Sudarshan2.16Database System Concepts - 6th Edition
Set Intersection of two relations
Relation r, s:
rs
-
7/28/2019 ch2-abbrev
17/25Silberschatz, Korth and Sudarshan2.17Database System Concepts - 6th Edition
Natural Join Example
Relations r, s:
Natural Join
r s
Quiz Q3: The natural join operation matches tuples (rows) whose valuesfor common attributes are (1) not equal (2) equal (3) weird Greek letters (4) null
-
7/28/2019 ch2-abbrev
18/25Silberschatz, Korth and Sudarshan2.18Database System Concepts - 6th Edition
Joining two relations Natural Join
Let rand s be relations on schemas R and S respectively.Then, the natural join of relations R and S is a relation onschema R S obtained as follows:
Consider each pair of tuples trfrom rand ts from s.
If trand ts have the same value on each of the attributes
in RS, add a tuple t to the result, where
thas the same value as tron r
thas the same value as ts on s
r s
-
7/28/2019 ch2-abbrev
19/25
-
7/28/2019 ch2-abbrev
20/25Silberschatz, Korth and Sudarshan2.20Database System Concepts - 6th Edition
Aggregate Functions and Operations
Aggregation function takes a collection of values and returns a single
value as a result.avg: average valuemin: minimum valuemax: maximum valuesum: sum of valuescount: number of values
Aggregate operation in relational algebra
Eis any relational-algebra expression
G1, G2, Gn is a list of attributes on which to group (can be empty) Each Fiis an aggregate function
EachAiis an attribute name
Note: Some books/articles use (gamma) instead of (Calligraphic G)
)( )(,,(),(,,, 221121 Ennn AFAFAFGGG
-
7/28/2019 ch2-abbrev
21/25Silberschatz, Korth and Sudarshan2.21Database System Concepts - 6th Edition
Aggregate Operation Example
Relation r:
A B
C
7
7
3
10
sum(c
) (r)sum(c )
27
-
7/28/2019 ch2-abbrev
22/25Silberschatz, Korth and Sudarshan2.22Database System Concepts - 6th Edition
Aggregate Operation Example
Find the average salary in each department
dept_name avg(salary) (instructor)
-
7/28/2019 ch2-abbrev
23/25
Silberschatz, Korth and Sudarshan2.23Database System Concepts - 6th Edition
Aggregate Functions (Cont.)
Result of aggregation does not have a name
Can use rename operation to give it a name For convenience, we permit renaming as part of aggregate
operation
dept_name avg(salary) as avg_sal(instructor)
-
7/28/2019 ch2-abbrev
24/25
Database System Concepts, 6th Ed.
Silberschatz, Korth and Sudarshan
See www.db-book.com for conditions on re-use
End of Chapter 2
http://www.db-book.com/http://www.db-book.com/http://www.db-book.com/http://www.db-book.com/ -
7/28/2019 ch2-abbrev
25/25
Division Operator
Given relations r(R) and s(S), such that S R, r s is the largest
relation t(R-S) such thatt x s r
E.g. let r(ID, course_id) = ID, course_id(takes ) and
s(course_id) = course_id(dept_name=Biology(course )then r s gives us students who have taken all courses in the Biology
department Can write rs as
temp1R-S (r)
temp2R-S ((temp1 x s )R-S,S (r))
result= temp1 temp2
The result to the right of the is assigned to the relation variable on
the left of the .
May use variable in subsequent expressions.