DBMS All
DBMS All
DBMS All
Levels of Abstraction
Physical level describes how a record (e.g.,
customer) is stored.
Logical level: describes data stored in database, and
the relationships among the data.
type customer = record
name : string;
street : string;
city : integer;
end;
View level: application programs hide details of data
types. Views can also hide information (e.g., salary)
for security purposes.
View of Data
An architecture for a database system
Instances and Schemas
Similar to types and variables in programming languages
Schema – the logical structure of the database
e.g., the database consists of information about a set of customers and
accounts and the relationship between them)
Analogous to type information of a variable in a program
Physical schema: database design at the physical level
Logical schema: database design at the logical level
Instance – the actual content of the database at a particular point in
time
Analogous to the value of a variable
Physical Data Independence – the ability to modify the physical
schema without changing the logical schema
Applications depend on the logical schema
In general, the interfaces between the various levels and components
should be well defined so that changes in some parts do not seriously
influence others.
Data Models
A collection of tools for describing
data
data relationships
data semantics
data constraints
Entity-Relationship model
Relational model
Other models:
object-oriented model
semi-structured data models
Older models: network model and hierarchical
model
Entity-Relationship Model
pe n de n c e:
y s ic al i nde
ViewP1h View n g2s . . . . View n
n i se th i
Re -or ga
here… SQL … . w it h o
ut affec
anythin t in g
g
here.
Logical Level
Internal Level
The ANSI-SPARC 3-level Architecture
SQL … . w it h o
ut affec
anythin t in g
g
here.
Logical Level
Logical ind
Re-organi ependence
Internal Level se things :
here…
The ANSI-SPARC 3-level Architecture
Internal Level … . w it h o
ut affec
anythin t in g
g
here.
Data Independence: what’s the secret?
A Data Catalog
Data Catalog
Data catalog holds metadata (data about
data)
DBMS uses metadata to ‘translate’
descriptions at one level to descriptions at the
next level.
Catalog exist in different forms as System
caalog,
Metadata:
Names of tables
Names of columns and their data types
Names of indexes
Names of files
Names of views
How these all relate to one another.
Analogy: Independence
In your phone a Phone Number of Raju
Mishra has been stored as:
Raju Mishra – 9841486419
We can call Raju by selecting ‘Raju’ in
list of names.
Now, Raju changed his cell number;
Given query:
SELECT name FROM Customer
WHERE cust_id = ‘A112’
DBMS will use cust_PK_idx to find the record with cust_id
= ‘A112’ and return bits 33-272 as the name required.
This work is carried out by the ‘run time processor’
Data Independence
Lots of things can change:
The name or location of the file holding Customer
records
The index can be removed or replaced
The data types of the columns in Customer can be
changed (eg address becomes variable length)
New columns can be added to ‘Customer’
Transaction
Manager
Storage Manager
Database
& Catalog
Other requirements of a DBMS
Concurrency control, backup and recovery
handled by a “transaction manager”;
Security/privacy handled by a security
manager
Query compiler/optimiser provides fast,
consistent performance.
The catalog provides additional help:
Ownership and read/write permissions;
Integrity constraints
Statistics on data volumes and frequencies of
access
Database Users
Users are differentiated by the way they expect to interact
with the system
Application programmers – interact with system through
DML calls
Sophisticated users – form requests in a database query
language
Specialized users – write specialized database applications
same domain
Each tuple is distinct – no duplicate
tuples
Order of tuples is immaterial
Example of Relational Model
Student table tells facts about students
Faculty table shows facts about faculty
Enroll
Faculty stuId classNumber grade
S1001 ART103A A
facId name department rank S1001 HST205A C
F101 Adams Art Professor S1002 ART103A D
F105 Tanaka CSC Instructor S1002 CSC201A F
F110 Byrne Math Assistant S1002 MTH103C B
F115 Smith History Associate S1010 ART103A
F221 Smith CSC Professor
S1010 MTH103C
Figure 4.1(b) The Faculty Table
S1020 CSC201A B
S1020 MTH101B A
Figure 4.1(d) The Enroll Table
change
Cardinality: the number of tuples
Changes as tuples are added or deleted
A property of the extension – changes often
Keys
Integrity constraints
Relation Keys
Relations never have duplicate tuples, so you can always tell tuples
apart; implies there is always a key (which may be a composite of
all attributes, in worst case)
Types of constraints
Domain constraint - limits set of values for attribute
Entity integrity: no attribute of a primary key can have a null
value
Referential integrity: each foreign key value must match the
primary key value of some tuple in its home relation or be
completely null.
General constraints or business rules: may be expressed
as table constraints or assertions
Representing Relational Database Schemas
Can have any number of relation schemas
For each relation schema list name of relation
followed by list of attributes in parentheses
Underline primary key in each relation schema
Indicate foreign keys (We use italics – arrows are
best)
Database schema actually includes domains, views,
character sets, constraints, stored procedures,
authorizations, etc.
Example: University database schema
Student (stuId, lastName, firstName, major, credits)
Class (classNumber, facId, schedule, room)
Faculty (facId, name, department, rank)
Enroll(stuId,classNumber,grade)
Lecture : 6
28th June, 2010
Representing Relational Database Schemas
The outline of databases in conceptual and Internal
level with or without data nature is called schema.
For each relation schema list name of relation followed
by list of attributes in parentheses
Underline primary key in each relation schema
Indicate foreign keys (We use italics – arrows are best)
Database schema actually includes domains, views,
character sets, constraints, stored procedures,
authorizations, etc.
and ungroup
SELECT Operation
Student
PROJECT Example
PROJECT Student OVER major GIVING Temp
Temp = major (Student)
Student
stuId lastName firstName major credits Temp
major
S1001 Smith Tom History 90
History
S1002 Chin Ann Math 36
S1005 Lee Perry History 3 Math
S1010 Burns Edward Art 63 Art
S1013 McCarthy Owen Math 0 CSC
S1015 Jones Mary Math 42
S1020 Rivera Jane CSC 15
PROJECT Class OVER (facId, room)
facId, room (Class) Contd.
Class
classNumber facId schedule room
facId room
ART103A F101 MWF9 H221 F101 H221
CSC201A F105 TuThF10 M110 F105 M110
CSC203A F105 MThF12 M110 F115 H221
HST205A F115 MWF11 H221 F110 H225
MTH101B F110 MTuTh9 H225
MTH103C F110 MWF11 H225
Temp Result
stuId lastName firstName major credits lastName firstName stuId
S1001 Smith Tom History 90 Smith Tom S1001
S1005 Lee Perry History 3 Lee Perry S1005
Product and Theta-join
Binary operations – apply to two tables
Product: Cartesian product – cross-product of A and B – A TIMES B,
written A x B
all combinations of rows of A with rows of B
Degree of result is deg of A + deg of B
Cardinality of result is (card of A) * (card of B)
THETA join: TIMES followed by SELECT
A |x| B = (A x B)
Student X Enroll (Produce 63 tuples)
Student Enroll
stuId classNumber grade
stuId lastName firstName major credits S1001 ART103A A
S1001 Smith Tom History 90 S1001 HST205A C
S1002 ART103A D
S1002 Chin Ann Math 36
S1002 CSC201A F
S1005 Lee Perry History 3
S1002 MTH103C B
S1010 Burns Edward Art 63 X S1010 ART103A
S1013 McCarthy Owen Math 0 S1010 MTH103C
S1015 Jones Mary Math 42
S1020 CSC201A B
S1020 Rivera Jane CSC 15 S1020 MTH101B A
classNo,grade((lastName=‘Chen’ ^ firstName=‘Ann’(Student))|X|
Enroll)
More Complicated Queries (cont.)
SELECT Student WHERE lastName=‘Chin’ AND firstName=‘Ann’ GIVING Temp1
Student Temp1
stuId lastName firstName major credits stuId lastName firstName major credits
S1001 Smith Tom History 90 S1002 Chin Ann Math 36
S1002 Chin Ann Math 36
S1005 Lee Perry History 3
S1010 Burns Edward Art 63 Temp1 JOIN Enroll GIVING Temp2
S1013 McCarthy Owen Math 0
S1015 Jones Mary Math 42
S1020 Rivera Jane CSC 15
Temp2 Enroll
stuId lastName firstName major credits classNumber grade stuId classNumber grade
S1002 Chin Ann Math 36 ART103A D S1001 ART103A A
S1002 Chin Ann Math 36 CSC201A F S1001 HST205A C
S1002 Chin Ann Math 36 MTH103C B S1002 ART103A D
S1002 CSC201A F
S1002 MTH103C B
S1010 ART103A
PROJECT Temp2 OVER (classNo, grade) S1010 MTH103C
GIVING Answer S1020 CSC201A B
S1020 MTH101B A
Answer
classNumber grade
ART103A D
CSC201A F
MTH103C B
More Complicated Queries (cont.)
Since we only need the stuId from Temp1, we can do a
PROJECT on Temp1 before we make the JOIN
classNo,grade(stuId(lastName=‘Chen’ ^ firstName=‘Ann’(Student))|X|
Enroll)
Enroll
Student
stuId classNumber grade
lastNam firstNam
stuId major credits S1001 ART103A A
e e
S1001 Smith Tom History 90 S1001 HST205A C
S1002 Chin Ann Math 36 S1002 ART103A D
S1005 Lee Perry History 3 S1002 CSC201A F
S1010 Burns Edward Art 63 |X S1002 MTH103C B
S1013 McCarthy Owen Math 0 S1010 ART103A
S1015 Jones Mary Math 42 S1010 MTH103C
S1020 Rivera Jane CSC 15 S1020 CSC201A B
S1020 MTH101B A
Club Stu
ClubName StuNumb StuLastName StuNumb StuLastName
Computing S1001 Smith S1001 Smith
Computing S1002 Chin S1002 Chin
Drama S1001 Smith S1005 Lee
Drama S1002 Chin
Drama S1005 Lee
Karate S1001 Smith
Karate S1002 Chin
Karate S1005 Lee
ClubName
Drama
Karate
Set Operations
Tables must be union compatible –
have same basic structure
Have the same degree
Attributes in corresponding position have
same domain
Third column in first relation must have same
domain as third column in second relation
Set Operations (cont.)
A UNION B: set of tuples in either or both of
A and B, written A B
Remove duplicates
MainFac BarnchFac
FacID name department rank
FacID name department rank
F101 Adams Art Processor
F105 Tanaka CSC Instructor
F101 Adams Art Processor
F110 Byre Math Assistant
F221 Smith CSC Processor
F115 Smith History Associate
F221 Smith CSC Processor
MainFac BarnchFac
FacID name department rank
FacID name department rank
F101 Adams Art Processor
F105 Tanaka CSC Instructor
∩ F101 Adams Art Processor
F110 Byre Math Assistant
F221 Smith CSC Processor
F115 Smith History Associate
F221 Smith CSC Processor
BarnchFac
-
MainFac FacID name department rank
FacID name department rank F101 Adams Art Processor
F101 Adams Art Processor F110 Byre Math Assistant
F105 Tanaka CSC Instructor F115 Smith History Associate
F221 Smith CSC Processor F221 Smith CSC Processor
BranchFac - MainFac
BarnchFac MainFac
FacID name department rank FacID name department rank
F101
F110
Adams
Byre
Art
Math
Processor
Assistant - F101
F105
Adams Art
Tanaka CSC
Processor
Instructor
F115 Smith History Associate F221 Smith CSC Processor
F221 Smith CSC Processor
Example 3: Find the first and last names of all students who are
enrolled in at least one class that meets in room H221.
{S.firstName, S.lastName | S Student ^ EXISTS E(E
Enroll ^ E.stuId = S.stuId ^ EXISTS C (C Class ^
C.classNo = E.classNo ^ C.room = ‘H221’))}
Domain-oriented predicate calculus
Uses variables that take their values from domains
Query has form
{<x1,x2,...,xn> P(x1,x2,...,xm )}
x1,x2,...,xn are domain variables
P(x1,x2,...,xm) is a predicate
n<=m
Means set of all domain variables x1,x2,...,xn for which
predicate P(x1,x2,...,xm) is true
Predicate must be a formula
Often test for membership condition, <x,y,z > X
Example Domain-oriented Relational Calculus
Example 1: Find the name of all faculty members who are
professors in CSC Department
{LN | FI, DP, RK (<F1, LN, DP, RK> Faculty ^ RK =
‘Professor’ ^ DP = ‘CSC’)}
Example 2: Find the last names of all students enrolled in
CSC201A.
{LN | SI, FN, MJ, CR (<SI, LN, FN, MJ, CR> Student ^ CN,
GR (<SI, CN, GR> Enroll ^ CN = ‘CSC201A’))}
Example 3: Find the first and last names of all students who
are enrolled in at least one class that meets in room H221.
{FN, LN | SI, MJ, CR (<SI, LN, FN, MJ, CR> Student ^ CN,
GR (<SI, CN, GR> Enroll ^ FI, SH, RM (<CN, FI, SH, RM>
Class ^ RM = ‘H221’)))}
SI: stuId, LN: lastName, FN: firstName, MJ: major, CR: credits, CN: calssNo,
FI: facId, SH: schedule, RM: room, DP: department, RK: rank, GR: grade
Views
External models in 3-level architecture are called
external views
Relational views are slightly different
Relational view is constructed from existing (base)
tables
View can be a window into a base table (subset)
View can contain data from more than one table
View can contain calculated data
Views hide portions of database from users
External model may have views and base tables
Mapping ER to Relational Model
Each strong entity set becomes a table
Non-composite, single-valued attributes become attributes of table
Composite attributes: either make the composite a single attribute
or use individual attributes for components, ignoring the
composite
Multi-valued attributes: remove them to a new table along with
the primary key of the original table; also keep key in original
table
Weak entity sets become tables by adding primary key of owner
entity
Binary Relationships:
1:M-place primary key of 1 side in table of M side as foreign key
1:1- make sure they are not the same entity. If not, use either key as
foreign key in the other table
M:M-create a relationship table with primary keys of related entities,
along with any relationship attributes
Ternary or higher degree relationships: construct relationship
table of keys, along with any relationship attributes