Untitled

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 83

DBMS

Snapshot of Unit-1
 DATABASE – set of interrelated data
 DBMS – software or tool which provides an environment to store the
data set of programs to access those data in an efficient manner

 DATA Model – 5 Model


 Database System Architecture
 DB Users
 Query Processor
 Storage Manager
 Data Storage
Snapshot of Unit-1
 File System vs DBMS

 ER Model
 Entity
 Entity Set
 Attributes
 Relationship
 Relationship set
 Keys
 ER Symbols
 ER Diagrams
 Extended ER Features
 Design Issues
University Questions
 Explain the database system architecture with neat diagram?
 Explain three basic notations in E-R models, with example?
 Discuss about
 Data models
 Keys
  Write about
 Basic Concepts of ER model
 ER diagram
  Explain E-R diagram with example?
  Explain the design of an E-R database schema?
  Describe in detail about the Extended E-R Features.
Unit-2

Relational Model
Relational Model
Relational Model - Structure of Relational Databases

Relational Algebra – Extended - Relational Algebra Operations – Modification


of Database-Views

Relational Calculus - Tuple Relational Calculus & Domain Relational Calculus.

SQL – Basic Structure – Set - Operations – Aggregate Functions – Null Values


– Nested Sub-queries – Views – Complex Queries –Modification of the database
–Joined Relations – Data-Definition Language.
Relation Model - Example
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 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


Relation Schema

 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
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
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
customer Relation

depositor Relation
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 - deals with how to design relational schemas
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)
 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
Keys
 K is a candidate key if K is minimal
Example: {customer_name} is a candidate key for Customer, since 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
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.
Schema Diagram
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.
Query Languages
 It is a language in which a user requests information from the db.

Query Languages

Procedural Language Non Procedural Language

Specifies what data are Specifies what data are


required & specify how to get required without specify how
those data to get those data
e.g Relational Algebra e.g. Relational calculus
Relational Algebra
Relational Calculus
SQL
Relational Algebra
 It consists of set of operations that take one or more relations as input and
produces new relation as output.

 The operations can be divided into,


 Basic operations: Select, Project, Union, rename, set difference and
Cartesian product
 Additional operations: Set intersections, natural join, division and
assignment.
 Extended operations: Aggregate operations and outer join

23
Relational Algebra
 Six basic operators
 select: 
 project: 
 union: 
 set difference: –
 Cartesian product: x
 rename: 
 The operators take one or two relations as inputs and produce a
new relation as a result.
Relational Algebra
 Six basic operators
 select: - Selects a subset of rows from relation (horizontal).
 project:  - Retains only wanted columns from relation (vertical).
 union:  - Tuples in r1 and/or in r2.
 set difference: – Tuples in r1, but not in r2.
 Cartesian product: x - Allows us to combine two relations.
 rename: 
 The operators take one or two relations as inputs and produce a new
relation as a result.
Basic operations
 Select
 It selects tuples that satisfy a given predicate,
 To denote selection.
(Sigma) is used.

Syntax:

condition (Table name)


26
Select
 E.g..

Sal>1000 (Employee)

Selects tuples whose emp sal is > 1000

27
Selection ()

sid sname rating age  rating 8(S2)


28 yuppy 9 35.0
31 lubber 8 55.5
44 guppy 5 35.0
58 rusty 10 35.0

sid sname rating age


28 yuppy 9 35.0
58 rusty 10 35.0
Project Operation
 It selects attributes from the relation.
 ∏ – Symbol for project

(Table name)
Syntax: ∏
<Attribute list>

E.g… ∏ (employee)
eid,sal
Projection

 Examples:  age;(S2)  sname,rating(S2)

 Retains only attributes that are in the “projection list”.

 Schema of result:
 exactly the fields in the projection list, with the same
names that they had in the input relation.

 Projection operator has to eliminate duplicates


sname rating
Projection yuppy 9
lubber 8
guppy 5
rusty 10
sid sname rating age  sname,rating (S 2)
28 yuppy 9 35.0
31 lubber 8 55.5
44 guppy 5 35.0
58 rusty 10 35.0 age
S2 35.0
55.5
 age(S2)
Combination Select & Project Operation
 Combining Select & Project Operation

E.G..
∏ ( Sal>1000
(employee)
)
eid,sal

- Selects tuples where sal>1000 & from them only eid and salary
attributes are selected.
sid sname rating age
28 yuppy 9 35.0
31 lubber 8 55.5
44 guppy 5 35.0
58 rusty 10 35.0
 rating 8(S2)

sname rating
yuppy 9
rusty 10

 sname,rating( rating 8(S2))


Mathematical Set Operations

 Union Operation:
 R1 U R2 - implies that tuples either from R1 or tuples from
R2 or both R1 & R2.
 E.G..

EMP1 EMP2

eid ename eid dno dname


1 A 1 10 XXX
2 B 4 50 YYY
3 C 5 60 ZZZ
4 D 6 70 UUU

34
Union Operation:

∏ (EMP1)
eid U ∏ eid
(EMP2)

eid
1
2
3
4
5
6

35
Union
S1 S2
sid sname rating age sid sname rating age
22 dustin 7 45.0 28 yuppy 9 35.0
31 lubber 8 55.5
31 lubber 8 55.5 44 guppy 5 35.0
58 rusty 10 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
S1 S2
SET DIFFERENCE
 R1 – R2 implies tuples present in R1 but not in R2.

∏ (EMP1)
- ∏ (EMP2)
eid eid

eid
2
3

37
Set Difference
sid sname rating age
22 dustin 7 45.0 sid sname rating age
31 lubber 8 55.5 22 dustin 7 45.0
58 rusty 10 35.0
S1 S2
S1

sid sname rating age


28 yuppy 9 35.0 sid sname rating age
31 lubber 8 55.5 28 yuppy 9 35.0
44 guppy 5 35.0 44 guppy 5 35.0
58 rusty 10 35.0 S2 – S1
S2
CARTESIAN PRODUCT

 R1 R2 allows to combine tuples from


any two relations.

 E.G.. Emp1 Emp2

39
CARTESIAN PRODUCT

EMP1 EMP2

eid ename eid dno dname


1 A 1 10 XXX
2 B 4 50 YYY
3 C 5 60 ZZZ
4 D 6 70 UUU

Emp1.eid ename Emp2.eid dno dname

1 A 1 10 XXX
Emp1 Emp2 1 A 4 50 YYY
1 A 5 60 ZZZ
1 A 6 70 UUU
2 B 1 10 XXX
Cross Product Example
sid sname rating age
sid bid day
22 dustin 7 45.0
22 101 10/10/96 31 lubber 8 55.5
58 103 11/12/96 58 rusty 10 35.0
R1
S1

(sid) sname rating age (sid) bid day


22 dustin 7 45.0 22 101 10/10/96
S1 X R1 = 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
Union Operation – Example
 Relations r, s: A B A B

 1  2
 2  3
 1 s
r

A B

 r  s:  1
 2
 1
 3
Set Difference Operation – Example
 Relations r, s:
A B A B

 1  2
 2  3
 1 s
r

 r – s:
A B

 1
 1
Cartesian-Product Operation – Example

 Relations r, s:
A B C D E

 1  10 a
 10 a
 2  20 b
r  10 b
s
 r x s:
A B C D E
 1  10 a
 1  10 a
 1  20 b
 1  10 b
 2  10 a
 2  10 a
 2  20 b
 2  10 b
Rename Operation
 Used to name and to refer the results of relational-algebra expressions.
 Allows us to refer to a relation by more than one name.
 Example:
 x (E)

returns the expression E under the name X


 If a relational-algebra expression E has arity n, then
 x ( A , A ,..., A ) ( E )
1 2 n

returns the result of expression E under the name X, and with the
attributes renamed to A1 , A2 , …., An .
Banking Example
branch (branch_name, branch_city, assets)

customer (customer_name, customer_street, customer_city)

account (account_number, branch_name, balance)

loan (loan_number, branch_name, amount)

depositor (customer_name, account_number)

borrower (customer_name, loan_number)


Example Queries

 Find all loans of over 1200

amount > 1200 (loan)


 Find the loan number for each loan of an amount greater than 1200

loan_number (amount > 1200 (loan))


 Find the names of all customers who have a loan, an account, or both,
from the bank

customer_name (borrower)  customer_name (depositor)


Example Queries

 Find the names of all customers who have a loan at the Perryridge branch.

customer_name (branch_name=“Perryridge”

(borrower.loan_number = loan.loan_number(borrower x loan)))


 Find the names of all customers who have a loan at the Perryridge branch but do not have
an account at any branch of the bank.

customer_name (branch_name = “Perryridge”

(borrower.loan_number = loan.loan_number(borrower x loan))) –

customer_name(depositor)
Additional Operations

 Additional Operations

 Set intersection
 Natural join
 Aggregation

 Outer Join
 Division
Set-Intersection Operation – Example
 Relation r, s:
A B A B
 1  2
 2  3
 1

r s

 rs
A B

 2
Intersection

sid sname rating age


22 dustin 7 45.0
31 lubber 8 55.5 sid sname rating age
58 rusty 10 35.0 31 lubber 8 55.5
S1 58 rusty 10 35.0
sid sname rating age
28
31
yuppy
lubber
9
8
35.0
55.5
S1 S2
44 guppy 5 35.0
58 rusty 10 35.0
S2
INTERSECTION

 R1
U
R2 implies tuples present in both R1 & R2

∏ (EMP1)
U ∏ (EMP2)
eid eid

eid
1
4

52
Natural Join Operation – Example
 Relations r, s:

A B C D B D E

 1  a 1 a 
 2  a 3 a 
 4  b 1 a 
 1  a 2 b 
 2  b 3 b 
r s

 r s
A B C D E
 1  a 
 1  a 
 1  a 
 1  a 
 2  b 
NATURAL JOIN OR EQUI JOIN

 Used to combine related tuples from two relations.

 It requires that the two join attributes have the same name,
otherwise renaming operation is applied first and then join
operation is applied.

 Symbol:
NATURAL JOIN OR EQUI JOIN

 Syntax:

 relation1 relation2 – Natural Join

 relation1 condition relation2 – EQUI Join

55
NATURAL JOIN OR EQUI JOIN

 E.g..
EMPLOYEE DEPARTMENT

eid ename sal dno Dno Dname mgr


1 X 10000 10
10 XXX ABC
2 Y 20000 20
20 YYY PQR
3 Z 30000 10
30 ZZZ MNO
4 A 16000 40

56
Employee Dept
or

Employee Dept
Employee.dno = department.dno

eid ename sal dno dname mgr


1 X 10000 10 XXX ABC
2 Y 20000 20 YYY PQR
3 Z 30000 10 xxx ABC

57
OUTER JOIN

 It is an extension of the join operation to deal with missing information.


 In natural join, only the matching tuples comes in the result and the
unmatched tuples are lost. To avoid this loss of information we use outer
join.
 There are 3 forms of outer join operation They are Left outer join, Right
outer join and full outer join
LEFT OUTER JOIN-
 It takes all tuples in the left relation that did not match with
any tuple in the right relation and pads the tuples with null
values for all other attributes from the right relation and adds
them to the result of the natural join.
 E.g…
LEFT OUTER JOIN-
EMPLOYEE DEPARTMENT

eid ename sal dno Dno Dname mgr


1 X 10000 10 10 XXX ABC
2 Y 20000 20 20 YYY PQR
3 Z 30000 40 30 ZZZ MNO

EMPLOYEE DEPARTMENT
LEFT OUTER JOIN-

Eid Ename Sal Dno Dname mgr


1 X 10000 10 XXX ABC
2 Y 20000 20 YYY PQR
3 Z 30000 40 NULL NULL

61
RIGHT OUTER JOIN-

 It takes all tuples in the right relation that did not match with
any tuple in the left relation and pads the tuples with null values
for all other attributes and adds them to the result of the natural
join.
RIGHT OUTER JOIN-

∏ (EMPLOYEE DEPARTMENT)
Eid,dname,sal

eid sal dname


1 10000 XXX
2 20000 ZZZ
3 30000 ZZZ
null null MNO
FULL OUTER JOIN

 Padding tuples from the left relation that didn’t match any from the right
relation, as well as tuples from the right relation that did not match any from
the left relation & adding them to the result of the join.
FULL OUTER JOIN

∏ Eid,dname,sal
(EMPLOYEE DEPARTMENT)

eid sal dname


1 10000 XXX
2 20000 YYY
3 30000 null
NULL NULL AAA
Division Operation

 Itis denoted by . It is suited to queries


that include the phrase ‘for all’.
 E.G.. Consider three relations

Acc-No. Branch- Balance


Account name
B-101 P 5000
B-102 Q 4000
B-103 R 9000
B-104 S 7000
B-105 T 3500

66
Division Operation

Depositor Branch
:
Customer- Acc-No. Branch- Branch-city assets
Name Name

Anu B-102 Q Adayar 10000


Uma B-103 P Adayar 20000
Rekha B-101 R Velachery 9000
Ganesh B-105 T T-Nagar 871110
Rajesh B-104 S Kodambakkam 600000

67
Division Operation

 To find all customers who have an account at all the branches located in
Adayar.
 The query is,

∏ Customer-
(depositor account)

name, branch-
name

∏ branch-name
( Branch_city
= “Adayar” (branch)
)
Division Operation
 Step 1:
 List out all the branches located in Adayar
 Step2: R1=

∏ branch-name
( = “Adayar” (branch)
Branch_city
)
 Find all customer name, branch_name pairs for which the
customer has an account at a branch
 R2=
∏ (depositor
Customer-
account)

name, branch-
name
Division Operation
 Step3:
 We need to find customers who appear in r2 with every branch name in R1.

∏ Customer-
(depositor account)

name, branch-
name

∏ (
branch-name )
= “Adayar” (branch)
Branch_city
Aggregate Functions and Operations
 Aggregation function takes a collection of values and returns a single value as a result.
avg: average value
min: minimum value
max: maximum value
sum: sum of values
count: number of values
 Aggregate operation in relational algebra

G1 ,G2 ,,Gn
F ( A ),F ( A ,,F ( A ) (E )
1 1 2 2 n n

E is any relational-algebra expression


 G1, G2 …, Gn is a list of attributes on which to group (can be empty)
 Each Fi is an aggregate function
 Each Ai is an attribute name
Aggregate Operation – Example
 Relation r:
A B C

  7
  7
  3
  10

 g sum(c) (r) sum(c )

27

 Question: Which aggregate operations cannot be expressed


using basic relational operations?
Aggregate Operation – Example
 Relation account grouped by branch-name:

branch_name account_number balance


Perryridge A-102 400
Perryridge A-201 900
Brighton A-217 750
Brighton A-215 750
Redwood A-222 700

branch_name g sum(balance) (account)


branch_name sum(balance)
Perryridge 1300
Brighton 1500
Redwood 700
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

branch_name g sum(balance) as sum_balance (account)


Modification of the Database

 The content of the database may be modified using the following


operations:
 Deletion

 Insertion

 Updating

 All these operations are expressed using the assignment operator.


Deletion
 A delete request is expressed similarly to a query, except instead of
displaying tuples to the user, the selected tuples are removed from the
database.
 Can delete only whole tuples; cannot delete values on only particular
attributes
 A deletion is expressed in relational algebra by:
rr–E
where r is a relation and E is a relational algebra query.
Deletion Examples
 Delete all account records in the Perryridge branch.

account  account – branch_name = “Perryridge” (account )

 Delete all loan records with amount in the range of 0 to 50

loan  loan – amount 0and amount  50 (loan)


Insertion
 To insert data into a relation, we either:
 specify a tuple to be inserted
 write a query whose result is a set of tuples to be inserted
 in relational algebra, an insertion is expressed by:
r r  E
where r is a relation and E is a relational algebra expression.
 The insertion of a single tuple is expressed by letting E be a constant
relation containing one tuple.
Insertion Examples
 Insert information in the database specifying that Smith has 1200 in account A-973 at the
Perryridge branch.

account  account  {(“A-973”, “Perryridge”, 1200)}


depositor  depositor  {(“Smith”, “A-973”)}

 Provide as a gift for all loan customers in the Perryridge branch, a 200 savings account. Let the loan
number serve as the account number for the new savings account.

r1  (branch_name = “Perryridge” (borrower loan))


account  account  loan_number, branch_name, 200 (r1)
depositor  depositor  customer_name, loan_number (r1)
Updating
 A mechanism to change a value in a tuple without charging all
values in the tuple
 Use the generalized projection operator to do this task

r   F1,F2 ,,Fl , (r )
 Each Fi is either
 the I th
attribute of r, if the I th
attribute is not updated, or,
 if the attribute is to be updated Fi is an expression, involving
only constants and the attributes of r, which gives the new value
for the attribute
Update Examples
 Make interest payments by increasing all balances by 5 percent.

account   account_number, branch_name, balance * 1.05 (account)

 Pay all accounts with balances over $10,000 6 percent interest


and pay all others 5 percent

account   account_number, branch_name, balance * 1.06 ( BAL  10000 (account ))


  account_number, branch_name, balance * 1.05 (BAL  10000 (account))
Queries
Thank you

You might also like