Вы находитесь на странице: 1из 37

Relational Databases

Professor Navneet Goyal


Department of Computer Science & Information Systems
BITS, Pilani
Lecture Topics
n Relational Model Concepts
n Relational Model Constraints
n Relational Database Schema
n Query Language

© Prof. Navneet Goyal, BITS, Pilani


Relational Model
Concepts
n Relational Model of data is based on
the concept of RELATION
n A Relation is a Mathematical concept
based on idea of SETS
n The strength of the relational
approach to data management comes
from the formal foundation provided
by the theory of relations

© Prof. Navneet Goyal, BITS, Pilani


Relational Model
Concepts
The model was first proposed by Dr.
E.F. Codd of IBM 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 Ted Codd the coveted
ACM Turing Award in 1981
Codd’s Turing Award Lecture

© Prof. Navneet Goyal, BITS, Pilani


Some Terms
n Table n Relation
n Row or Record n Tuple
n Column or Field n Attribute
n No. of Rows n Cardinality
n No. of Columns n Degree or Arity
n Unique n Primary key
Identifier
n Pool of Legal n Domain
Values
© Prof. Navneet Goyal, BITS, Pilani
Example Relation

Cardinality = 5
Degree = 7
Primary Key is SSN

© Prof. Navneet Goyal, BITS, Pilani


Relation - Definition
ACCOUNT

• Let D1 denote the set of all account numbers


• Let D2 denote the set of all branch names

• Let D3 denote the set of all balances


© Prof. Navneet Goyal, BITS, Pilani
Relation - Definition
ACCOUNT

• Any tuple of account must consist of a 3-tuple (v1, v2, v3),


where v1 is an account no.(ie,v1 is in domain D1), v2 is a
branch name (ie,v2 is in domain D2), , & v3 is a balance (ie,v3
is in domain D3),
© Prof. Navneet Goyal, BITS, Pilani
Relation - Definition
ACCOUNT

• In general, account will contain only a subset of all possible rows


• Account is a subset of D1 x D2 x D3
• In general, a relation of degree n is a subset of D1 x D2 x … x Dn
© Prof. Navneet Goyal, BITS, Pilani
Relation - Definition
n Mathematically, a relation is a subset
of a cartesian product of a list of
domains
n Corresponds almost exactly with our
definition of a relation
n Mathematicians rely on numeric
names, using 1 to denote the
attribute whose domain appears first
in the list of domains, 2 for the
attribute whose domain appears
second and so on..

© Prof. Navneet Goyal, BITS, Pilani


Domains & Data Types
n Smallest semantic of data
n Individual Part Number, Individual
Supplier number, Individual City
name etc.
n Atomic values or scalar values
n Domain is a named set of atomic
values
n Pool of legal values
n Example: Supplier number an integer
[0, 10000]
n Different from domain of functions
© Prof. Navneet Goyal, BITS, Pilani
Domains & Data Types
n Significance of domains
n Domain-constrained comparisons
Select …..
From P, SP
Where P.P# = SP.P#

Select …..
From P, SP
Where P.weight = SP.qty
Both are valid queries in SQL, but second one
makes no sense!!
n Domains implemented as Data-Types?

© Prof. Navneet Goyal, BITS, Pilani


Relational Systems
n In relational systems, the DB is
perceived by the user as relations &
nothing else
n Relations are only logical structures
n At the physical level, the system is
free to store the data in any way it
likes – using sequential files,
indexing, hashing…
n Provided it can map stored
representations to relations

© Prof. Navneet Goyal, BITS, Pilani


Relational Systems
n Information Principle
n The entire information content
of the DB is represented in one
& only one way, namely as
explicit values in attribute
positions in tuples in relations
n NO POINTERS connecting one
relation to another

© Prof. Navneet Goyal, BITS, Pilani


Relational Systems
n Consider the relations:
Dept(dept#, dname, budget)
D1 MKTNG 10M
D2 DEV 12M
D3 RES 5M

Emp(emp#, ename, dept#, salary)


E1 LOPEZ D1 40K
There is a connection between tuples E1 & D1. The connection is represented,
not by a pointer, but by the occurrence of value D1 in E1.
In non-relational systems, such information is typically represented by some
kind of pointer that is visible to the user.

© Prof. Navneet Goyal, BITS, Pilani


Relational Systems
n In relational systems, there are
no pointers at the logical level
n Pointers will be there at the
physical level
n Physical storage details are
concealed from the user in
relational systems

© Prof. Navneet Goyal, BITS, Pilani


Properties of Relations
n There are no duplicate tuples
• Body of a relation is a mathematical set
n Tuples are unordered, top to bottom
• Body of a relation is a mathematical set
• No such thing as fifth tuple, next tuple ..
• No concept of positional addressing
n Attributes are unordered, left to right
• Heading of a relation is a mathematical set
• No concept of positional addressing
n All attribute values are atomic
• Normalized (1st Normal Form)

© Prof. Navneet Goyal, BITS, Pilani


Types of Relations
n Base Relations
• The original (given) relations
n Derived Relations
• Relations obtained from base relations
n Views
• “Virtual” derived relation
• Only definition is stored in the catalog
• Definition executed at run-time
n Snapshots
• “Real” derived relation
n Query Result
• Unnamed derived relation

© Prof. Navneet Goyal, BITS, Pilani


Operations on
Relations
n Restrict
n Project
n Join
n Divide Relational Operations
n Union Set Operations
n Intersection
n Difference
n Product

© Prof. Navneet Goyal, BITS, Pilani


Restrict & Project

© Prof. Navneet Goyal, BITS, Pilani


Union, Intersection &
Difference

© Prof. Navneet Goyal, BITS, Pilani


Union, Intersection &
Difference
Union Compatibility: r U s is valid if:
n Relations r & s have the same arity
n Domains of the ith attribute of r is the
same as the domain of the ith attribute of
s, ⍱ i.
Note that r & s can be either database
relations or derived relations

© Prof. Navneet Goyal, BITS, Pilani


Product & Divide
A X A X A X X
DIVIDE BY
B Y A Y A Y Z
C B X A Z
B Y B X
A
PRODUCT C X C Y
C Y

© Prof. Navneet Goyal, BITS, Pilani


Divide
DIVIDE

S1
S1 P1
P2 S2
S1 P2
S3
S2 P3
P2 S4
S2 P4
P4
S2 P1 S1

S2 P2 S4

S3 P2 P1
S2
S4 P2 P2
S4 P4 P4

Shipment Parts
© Prof. Navneet Goyal, BITS, Pilani
Join
A1 B1 B1 C1
A2 B1 B2 C2
A3 B2 B3 C3
A1 B1 C1
A2 B1 C1
NATURAL
JOIN A3 B2 C2

© Prof. Navneet Goyal, BITS, Pilani


Types of Joins
n θ-­‐‑ join or conditional join
n Equijoin (equality condition)
n Natural join
n Left-outer join
n Right-outer join
n Full-outer join
n NVL function of PLSQL

© Prof. Navneet Goyal, BITS, Pilani


Outer Join
Store_Information Geography
store_name Sales Date region_name store_name

Los Angeles $1500 Jan-05-1999 East Boston

San Diego $250 Jan-07-1999 East New York

West Los Angeles


Los Angeles $300 Jan-08-1999
West San Diego
Boston $700 Jan-08-1999

-­ We  want  to  find  out  the  sales  amount  for  all  of  the  stores
-­ If  we  do  a  regular  join,  we  will  not  be  able  to  get  what  we  want  because  we  
will          have  missed  "New  York,"  since  it  does  not  appear  in  the  
Store_Information table   store_name    SALES
SELECT  A1.store_name,  SUM(A2.Sales)  SALES   Boston                      $700
FROM  Geography  A1,  Store_Information  A2   New  York                              
WHERE  A1.store_name  =  A2.store_name  (+)   Los  Angeles    $1800
GROUP  BY  A1.store_name San  Diego            $250
NVL Function
nIn Oracle/PLSQL, the NVL function lets you substitutes a value when a null
value is encountered.
NVL (string1, replace_with )
string1 is the string to test for a null value. Replace_with is the value
returned if string1 is null.
Example #1:
select NVL (supplier_city, 'n/a')
from suppliers;
The SQL statement above would return 'n/a' if the supplier_city field
contained a null value. Otherwise, it would return the supplier_city value.
Example #2:
select supplier_id,
NVL (supplier_desc, supplier_name)
from suppliers;
This SQL statement would return the supplier_name field if the
supplier_desc contained a null value. Otherwise, it would return the
supplier_desc.
Example #3:
select NVL (commission, 0)
from sales;
This SQL statement would return 0 if the commission field contained a null
value. Otherwise, it would return the commission field.
Atomicity of Values
n Cell values are atomic
n 1 NF
n Repeating groups
n Nested relations

© Prof. Navneet Goyal, BITS, Pilani


Closure
n A relation is closed under
relational and set operators
n The result of these operators on
relation(s) is another relation
n Output from one operation can
become input to another
n Nested Expressions possible

© Prof. Navneet Goyal, BITS, Pilani


Relational Algebra
n Collection of operations on relations
n 8 operators + Rename operator
n Codd’s 8 operators do not form a minimal
set
n Some of them are not primitive
n Join, Intersect, & Divide can be defined in
terms of other 5
n None of these 5 can be defined in terms of
the remaining 4
n MINIMAL SET
n Join, Intersect, & Divide are important &
are supported directly

© Prof. Navneet Goyal, BITS, Pilani


Integrity Constraints
n Data Integrity
n Key Constraints
n Domain (check) constraints
n Not null constraints
n Referential Integrity
n Foreign Keys
n Referential Integrity Diagram
n Assertions
• A condition that the database must always satisfy
• Domain constraints and referential integrity
constraints are special cases
• Every loan has atleast one customer who maintains
an account with a balance of minimum 10000.

© Prof. Navneet Goyal, BITS, Pilani


NULLS
n What are nulls?
n What purpose they serve?
n What problems they create?
n NULLS a digression?
n Can we do away with NULLS?
n PK & Nulls (entity integrity)
n CK & Nulls
n FK & Nulls (referential integrity)
n 3-valued logic!!
© Prof. Navneet Goyal, BITS, Pilani
3-valued logic
n 3 thruth Values ( T, F & Unknown)

© Prof. Navneet Goyal, BITS, Pilani


NULLS
n Entity Integrity: No component
of the PK on any base relation is
allowed to have NULLS
(tuples in base relations represent real
world entities)
n Referential Integrity: The DB
must not contain any unmatched
FK value

© Prof. Navneet Goyal, BITS, Pilani


Q&A
Thank You

Вам также может понравиться