0% found this document useful (0 votes)
32 views511 pages

1-4 DBMS

Uploaded by

Arnav Kumar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
32 views511 pages

1-4 DBMS

Uploaded by

Arnav Kumar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 511

Module 01

Partha Pratim
Das

Objectives &
Outline Database Management Systems
Why Databases?
Module 01: Course Overview
Know Your
Course

Course Outline

Course Text Book


Partha Pratim Das
Module Summary

Department of Computer Science and Engineering


Indian Institute of Technology, Kharagpur

[email protected]

Database Management Systems Partha Pratim Das 01.1


Module Objectives PPD

Module 01

Partha Pratim • To understand the importance of database management systems in modern day
Das
applications
Objectives &
Outline • To Know Your Course
Why Databases?

Know Your
Course

Course Outline

Course Text Book

Module Summary

Database Management Systems Partha Pratim Das 01.2


Module Outline PPD

Module 01

Partha Pratim • Why Databases?


Das
• KYC: Know Your Course
Objectives &
Outline ◦ Course Prerequisite
Why Databases? ◦ Course Outline
Know Your
Course
◦ Course Text Book
Course Outline

Course Text Book

Module Summary

Database Management Systems Partha Pratim Das 01.3


Why Databases? PPD

Module 01

Partha Pratim
Das

Objectives &
Outline

Why Databases?

Know Your
Course

Course Outline

Course Text Book

Module Summary

Why Databases?

Database Management Systems Partha Pratim Das 01.4


Database Management System (DBMS)

Module 01
• DBMS contains information about a particular enterprise
Partha Pratim
Das ◦ Collection of interrelated data
Objectives &
◦ Set of programs to access the data
Outline ◦ An environment that is both convenient and efficient to use
Why Databases?
• Database Applications:
Know Your
Course
◦ Banking: transactions
Course Outline
◦ Airlines: reservations, schedules
Course Text Book
◦ Universities: registration, grades
Module Summary
◦ Sales: customers, products, purchases
◦ Online retailers: order tracking, customized recommendations
◦ Manufacturing: production, inventory, orders, supply chain
◦ Human resources: employee records, salaries, tax deductions
◦ ···
• Databases can be very large
• Databases touch all aspects of our lives
Database Management Systems Partha Pratim Das 01.5
University Database Example

Module 01

Partha Pratim • Application program examples


Das
◦ Add new students, instructors, and courses
Objectives &
Outline ◦ Register students for courses, and generate class rosters
Why Databases? ◦ Assign grades to students, compute grade point averages (GPA) and generate
Know Your transcripts
Course

Course Outline • In the early days, database applications were built directly on top of file systems
Course Text Book

Module Summary

Database Management Systems Partha Pratim Das 01.6


Drawbacks of using file systems to store data

Module 01

Partha Pratim • Data redundancy and inconsistency


Das
◦ Multiple file formats, duplication of information in different files
Objectives &
Outline • Difficulty in accessing data
Why Databases?
◦ Need to write a new program to carry out each new task
Know Your
Course • Data isolation
Course Outline
◦ Multiple files and formats
Course Text Book

Module Summary • Integrity problems


◦ Integrity constraints (e.g., account balance > 0) become “buried” in program code
rather than being stated explicitly
◦ Hard to add new constraints or change existing ones

Database Management Systems Partha Pratim Das 01.7


Drawbacks of using file systems to store data (2)

Module 01
• Atomicity of updates
Partha Pratim
Das ◦ Failures may leave database in an inconsistent state with partial updates carried out
Objectives &
◦ Example: Transfer of funds from one account to another should either complete or
Outline not happen at all
Why Databases?
• Concurrent access by multiple users
Know Your
Course
◦ Concurrent access needed for performance
Course Outline
◦ Uncontrolled concurrent accesses can lead to inconsistencies
Course Text Book

Module Summary
. Example: Two people reading a balance (say 100) and updating it by
withdrawing money (say 50 each) at the same time
• Security problems
◦ Hard to provide user access to some, but not all, data
Database systems offer solutions to all the above problems

Database Management Systems Partha Pratim Das 01.8


Know Your Course PPD

Module 01

Partha Pratim
Das

Objectives &
Outline

Why Databases?

Know Your
Course

Course Outline

Course Text Book

Module Summary

Know Your Course

Database Management Systems Partha Pratim Das 01.9


Course Prerequisites: Essential PPD

Module 01
• Set Theory
Partha Pratim
Das ◦ Definition of a Set
Objectives & . Intensional Definition
Outline
. Extensional Definition
Why Databases?
. Set-builder Notation
Know Your
Course ◦ Membership, Subset, Superset, Power Set, Universal Set
Course Outline
◦ Operations on sets:
Course Text Book

Module Summary
. Union, Intersection, Complement, Difference, Cartesian Product
◦ De Morgan’s Law
◦ Courses
. MOOCs: Discrete Mathematics:
https://nptel.ac.in/courses/111/106/111106086/
. Online Degree Foundational Course: Mathematics for Data Science I
https://onlinedegree.iitm.ac.in/course_pages/BSCMA1001.html

Database Management Systems Partha Pratim Das 01.10


Course Prerequisites: Essential PPD

Module 01
• Relations and Functions
Partha Pratim
Das ◦ Definition of Relations
Objectives &
◦ Ordered Pairs and Binary Relations
Outline
. Domain and Range
Why Databases?
. Image, Preimage, Inverse
Know Your
Course . Properties: Reflexive, Symmetric, Antisymmetric, Transitive, Total
Course Outline
◦ Definition of Functions
Course Text Book
◦ Properties of Functions: Injective, Surjective, Bijective
Module Summary
◦ Composition of Functions
◦ Inverse of a Function
◦ Courses
. MOOCs: Discrete Mathematics:
https://nptel.ac.in/courses/111/106/111106086/
. Online Degree Foundational Course: Mathematics for Data Science I
https://onlinedegree.iitm.ac.in/course_pages/BSCMA1001.html

Database Management Systems Partha Pratim Das 01.11


Course Prerequisites: Essential PPD

Module 01
• Propositional Logic
Partha Pratim
Das ◦ Truth Values & Truth Tables
Objectives &
◦ Operators: conjunction (and), disjunction (or), negation (not), implication,
Outline equivalence
Why Databases?
◦ Closure under Operations
Know Your
Course ◦ Courses
Course Outline . MOOCs: Discrete Mathematics:
Course Text Book https://nptel.ac.in/courses/111/106/111106086/
Module Summary

Database Management Systems Partha Pratim Das 01.12


Course Prerequisites: Essential PPD

Module 01
• Predicate Logic
Partha Pratim
Das ◦ Predicates
Objectives &
◦ Quantification
Outline
. Existential
Why Databases?
. Universal
Know Your
Course ◦ Courses
Course Outline
. MOOCs: Discrete Mathematics:
Course Text Book
https://nptel.ac.in/courses/111/106/111106086/
Module Summary

Database Management Systems Partha Pratim Das 01.13


Course Prerequisites: Essential PPD

Module 01
• Data Structures
Partha Pratim
Das ◦ Array
Objectives &
◦ List
Outline ◦ Binary Search Tree
Why Databases?
. Balanced Tree
Know Your
Course ◦ B-Tree
Course Outline
◦ Hash Table / Map
Course Text Book
◦ Courses
Module Summary
. MOOCs: Design and Analysis of Algorithms:
https://nptel.ac.in/courses/106/106/106106131/
. MOOCs: Fundamental Algorithms – Design and Analysis:
https://nptel.ac.in/courses/106/105/106105157/

Database Management Systems Partha Pratim Das 01.14


Course Prerequisites: Essential PPD

Module 01
• Programming in Python
Partha Pratim
Das ◦ Courses
Objectives & . Online Degree Foundational Course - Programming in Python
Outline
https://onlinedegree.iitm.ac.in/course_pages/BSCCS1002.html
Why Databases?

Know Your
Course

Course Outline

Course Text Book

Module Summary

Database Management Systems Partha Pratim Das 01.15


Course Prerequisites: Desirable PPD

Module 01
• Algorithms and Programming in C
Partha Pratim
Das ◦ Sorting
Objectives & . Merge Sort
Outline
. Quick Sort
Why Databases?

Know Your
◦ Search
Course
. Linear Search
Course Outline
. Binary Search
Course Text Book

Module Summary
. Interpolation Search
◦ Courses
. MOOCs: Design and Analysis of Algorithms:
https://nptel.ac.in/courses/106/106/106106131/
. MOOCs: Introduction to Programming in C:
https://nptel.ac.in/courses/106/104/106104128/

Database Management Systems Partha Pratim Das 01.16


Course Prerequisites: Desirable PPD

Module 01
• Object-Oriented Analysis and Design
Partha Pratim
Das ◦ Courses
Objectives & . MOOCs: Object-Oriented Analysis and Design:
Outline
https://nptel.ac.in/courses/106/105/106105153/
Why Databases?

Know Your
Course

Course Outline

Course Text Book

Module Summary

Database Management Systems Partha Pratim Das 01.17


Course Outline PPD

Module 01

Partha Pratim
Das

Objectives &
Outline

Why Databases?

Know Your
Course

Course Outline

Course Text Book

Module Summary

Database Management Systems Partha Pratim Das 01.18


Course Textbook PPD

Module 01
Database System Concepts,
Partha Pratim
Das Sixth Edition,
Objectives &
Outline
Abraham Silberschatz,
Why Databases?
Henry Korth,
Know Your
Course S. Sudarshan,
Course Outline

Course Text Book


Publisher: McGraw Hill Education
Module Summary
ISBN: 0073523321

Website: http://db-book.com/
7th Edition will also do

Database Management Systems Partha Pratim Das 01.19


Module Summary PPD

Module 01

Partha Pratim • Elucidates the importance of database management systems in modern day applications
Das
• Introduced various aspects of the Course
Objectives &
Outline

Why Databases?

Know Your
Course

Course Outline

Course Text Book

Module Summary
Slides used in this presentation are borrowed from http://db-book.com/ with kind permission of the
authors.
Edited and new slides are marked with “PPD”.

Database Management Systems Partha Pratim Das 01.20


Module 02

Partha Pratim
Das

Objectives &
Outline Database Management Systems
Evolution of Data
Management Module 02: Why DBMS?/1
History

Module Summary

Partha Pratim Das

Department of Computer Science and Engineering


Indian Institute of Technology, Kharagpur

[email protected]

Database Management Systems Partha Pratim Das 02.1


Module Objectives PPD

Module 02

Partha Pratim • To understand the need for a DBMS from historical perspective
Das

Objectives &
Outline

Evolution of Data
Management

History

Module Summary

Database Management Systems Partha Pratim Das 02.2


Module Outline PPD

Module 02

Partha Pratim • Evolution of data management practices


Das
• History of DBMS
Objectives &
Outline

Evolution of Data
Management

History

Module Summary

Database Management Systems Partha Pratim Das 02.3


PPD

Module 02

Partha Pratim
Das

Objectives &
Outline

Evolution of Data
Management

History

Module Summary

Evolution of Data Management

Database Management Systems Partha Pratim Das 02.4


Data Management PPD

Module 02 Management of Data or Records is a basic need for human society:


Partha Pratim
Das
• Storage
Objectives &
• Retrieval
Outline
• Transaction
Evolution of Data
Management
• Audit
History

Module Summary
• Archival
For:
• Individual
• Small / Big Enterprise
• Global
There have been two major approaches in this practice:
• Physical
• Electronic
Database Management Systems Partha Pratim Das 02.5
Data Management: Physical PPD

Module 02

Partha Pratim
Physical Data or Records management, more formally known as Book Keeping, has been
Das using physical ledgers and journals for centuries.
Objectives &
Outline
The most significant development happened when Henry Brown, an American inventor,
Evolution of Data
Management patented a “receptacle for storing and preserving papers” on November 2, 1886.
History

Module Summary
Herman Hollerith adapted the punch cards used for weaving looms to act as the memory
for a mechanical tabulating machine, in 1890.

Database Management Systems Partha Pratim Das 02.6


Data Management: Electronic PPD

Module 02 Electronic Data or Records management moves with the advances in technology -
Partha Pratim especially of memory, storage, computing, and networking.
Das
• 1950s: Computer Programming started
Objectives &
Outline
• 1960s: Data Management with punch card / tapes and magnetic tapes
Evolution of Data
Management • 1970s:
History
◦ COBOL and CODASYL approach was introduced in 1971
Module Summary
◦ On October 14 in 1979, Apple II platform shipped VisiCalc, marking the birth of the
spreadsheet
◦ Magnetic disks became prevalent
• 1980s: RDBMS changed the face of data management
• 1990s: With Internet data management started becoming global
• 2000s: e-Commerce boomed, NoSQL was introduced for unstructured data
management
• 2010s: Data Science started riding high
Database Management Systems Partha Pratim Das 02.7
Electronic Data Management Parameters PPD

Module 02 Electronic Data or Records management depends on various parameters including:


Partha Pratim
Das
• Durability
Objectives &
• Scalability
Outline
• Security
Evolution of Data
Management
• Retrieval
History

Module Summary
• Ease of Use
• Consistency
• Efficiency
• Cost
• ...

Database Management Systems Partha Pratim Das 02.8


Book Keeping PPD

Module 02

Partha Pratim
Recall how shop owners used to maintain their accounts.
Das A book register was maintained on which the shop owner wrote the amount received from
Objectives & customers, the amount due for any customer, inventory details and so on.
Outline

Evolution of Data
Management Problems with such an approach of book-keeping:
History
• Durability: Physical damage to these registers is a possibility due to rodents, humidity,
Module Summary
wear and tear
• Scalability: Very difficult to maintain for many years, some shops have numerous
registers spanning over years
• Security: Susceptible to tampering by outsiders
• Retrieval: Time consuming process to search for a previous entry
• Consistency: Prone to human errors
Not only small shops but large organizations also used to maintain their transaction details
in book registers.
Database Management Systems Partha Pratim Das 02.9
Spreadsheet Files - A better solution PPD

Module 02

Partha Pratim
Spreadsheet Softwares like Google Sheets: Due to the disadvantages of maintaining
Das ledger registers, organizations dealing with huge amount of data shifted to using
Objectives & spreadsheet softwares for maintaining their records in files.
Outline

Evolution of Data
• Durability: These are computer applications and hence data is less prone to physical
Management
damage.
History

Module Summary
• Scalability: Easier to search, insert and modify records as compared to book ledgers
• Security: Can be password-protected
• Easy of Use: Computer applications are used to search and manipulate records in the
spreadsheets leading to reduction in manpower needed to perform routine computations
• Consistency: Not guaranteed but spreadsheets are less prone to mistakes than
registers.

Mostly useful for single user or small enterprise applications

Database Management Systems Partha Pratim Das 02.10


Why leave filesystems?

Module 02

Partha Pratim
Lack of efficiency in meeting growing needs PPD
Das
• With rapid scale up of data, there has been considerable increase in the time required
Objectives &
Outline
to perform most operations.
Evolution of Data • A typical spreadsheet file may have an upper limit on the number of rows.
Management

History • Ensuring consistency of data is a big challenge.


Module Summary
• No means to check violations of constraints in the face of concurrent processing.
• Unable to give different permissions to different people in a centralized manner.
• A system crash could be catastrophic.
The above limitations of filesystems paved the way for a comprehensive platform dedicated
to management of data - the Database Management Systems.

Database Management Systems Partha Pratim Das 02.11


History of DBMS PPD

Module 02

Partha Pratim
Das

Objectives &
Outline

Evolution of Data
Management

History

Module Summary

History of DBMS

Database Management Systems Partha Pratim Das 02.12


History of Database Systems

Module 02

Partha Pratim • 1950s and early 1960s:


Das
◦ Data processing using magnetic tapes for storage
Objectives &
Outline . Tapes provided only sequential access
Evolution of Data
Management
◦ Punched cards for input
History • Late 1960s and 1970s:
Module Summary
◦ Hard disks allowed direct access to data
◦ Network and hierarchical data models in widespread use
◦ Ted Codd defines the relational data model
. Would win the ACM Turing Award for this work
. IBM Research begins System R prototype
. UC Berkeley begins Ingres prototype
◦ High-performance (for the era) transaction processing

Database Management Systems Partha Pratim Das 02.13


History (2)

Module 02

Partha Pratim • 1980s:


Das
◦ Research relational prototypes evolve into commercial systems - SQL becomes
Objectives &
Outline industrial standard
Evolution of Data ◦ Parallel and distributed database systems
Management
◦ Object-oriented database systems
History

Module Summary • 1990s:


◦ Large decision support and data-mining applications
◦ Large multi-terabyte data warehouses
◦ Emergence of Web commerce
• Early 2000s:
◦ XML and XQuery standards
◦ Automated database administration
• Later 2000s:
◦ Giant data storage systems - Google BigTable, Yahoo PNuts, Amazon, . . .
Database Management Systems Partha Pratim Das 02.14
History (3): Evolution of Data Models PPD

Module 02

Partha Pratim
Das

Objectives &
Outline

Evolution of Data
Management

History

Module Summary

Database Management Systems Partha Pratim Das 02.15


History (4): Evolution of DB Technology PPD

Module 02

Partha Pratim
Das

Objectives &
Outline

Evolution of Data
Management

History

Module Summary

Database Management Systems Partha Pratim Das 02.16


History (5): Evolution of DB Architecture PPD

Module 02

Partha Pratim
Das

Objectives &
Outline

Evolution of Data
Management

History

Module Summary

Database Management Systems Partha Pratim Das 02.17


Module Summary PPD

Module 02

Partha Pratim • Walk through of evolution of Data and Records Management


Das
• History of DBMS
Objectives &
Outline

Evolution of Data
Management

History

Module Summary

Slides used in this presentation are borrowed from http://db-book.com/ with kind
permission of the authors.
Edited and new slides are marked with “PPD”.

Database Management Systems Partha Pratim Das 02.18


Module 03

Partha Pratim
Das

Objectives &
Outline Database Management Systems
File Systems vs
Databases Module 03: Why DBMS?/2
Python viz-a-viz SQL
Parameterized
Comparison

Module Summary
Partha Pratim Das

Department of Computer Science and Engineering


Indian Institute of Technology, Kharagpur

[email protected]

Database Management Systems Partha Pratim Das 03.1


Module Recap PPD

Module 03

Partha Pratim • Evolution of Data and Records Management


Das
• History of DBMS
Objectives &
Outline

File Systems vs
Databases
Python viz-a-viz SQL
Parameterized
Comparison

Module Summary

Database Management Systems Partha Pratim Das 03.2


Module Objectives PPD

Module 03

Partha Pratim • Comparison of File based data management and DBMS


Das

Objectives &
Outline

File Systems vs
Databases
Python viz-a-viz SQL
Parameterized
Comparison

Module Summary

Database Management Systems Partha Pratim Das 03.3


Module Outline PPD

Module 03

Partha Pratim • File handling by Python viz-a-viz DBMS - Bank Transaction example
Das
• Parameterized Comparison
Objectives &
Outline

File Systems vs
Databases
Python viz-a-viz SQL
Parameterized
Comparison

Module Summary

Database Management Systems Partha Pratim Das 03.4


PPD

Module 03

Partha Pratim
Das

Objectives &
Outline

File Systems vs
Databases
Python viz-a-viz SQL
Parameterized
Comparison

Module Summary

Case Study of Bank Transaction

Database Management Systems Partha Pratim Das 03.5


Case study: A bank transaction PPD

Module 03

Partha Pratim
Banking Transaction System
Das

Objectives & Consider a simple banking system where a person can open a new account, transfer fund to
Outline
an existing account and check the history of all her transactions till date.
File Systems vs
Databases
Python viz-a-viz SQL
Parameterized
The application performs the following checks:
Comparison

Module Summary
• If the account balance is not enough, it will not allow the fund transfer
• If the account numbers are not correct, it will flash a message and terminate the
transaction.
• If a transaction is successful, it prints a confirmation message.

Database Management Systems Partha Pratim Das 03.6


Case study: A bank transaction (2) PPD

Module 03

Partha Pratim
We will use this banking transaction system to compare various features of a file-based
Das (spreadsheet/.csv files) implementation viz-a-viz a DBMS-based implementation
Objectives &
Outline
• Account details are stored in
File Systems vs ◦ Accounts.csv for file-based implementation
Databases
Python viz-a-viz SQL
◦ Accounts table for DBMS implementation
Parameterized
Comparison • The transaction details are stored in
Module Summary
◦ Ledger.csv file for file-based implementation
◦ Ledger table for DBMS implementation
In the following slides we discuss a fund transfer transaction.

Source: https: // github. com/ bhaskariitm/ transition-from-files-to-db/ tree/ main

Database Management Systems Partha Pratim Das 03.7


PPD

Module 03

Partha Pratim
Das

Objectives &
Outline

File Systems vs
Databases
Python viz-a-viz SQL
Parameterized
Comparison

Module Summary

Python viz-a-viz SQL

Database Management Systems Partha Pratim Das 03.8


Bank Transaction: Python viz-a-viz SQL PPD

Module 03 Python SQL


Partha Pratim
Das
def begin_Transaction(creditAcc,
debitAcc, amount):
Objectives &
Outline temp = [] // Handled implicitly by the DBMS
File Systems vs success = 0
Databases
Python viz-a-viz SQL # Open file handles to retrieve and
Parameterized store transaction data
Comparison

Module Summary f_obj_Account1 =


open(’Accounts.csv’, ’r’)
f_reader1 =
csv.DictReader(f_obj_Account1)
f_obj_Account2 =
open(’Accounts.csv’, ’r’)
f_reader2 =
csv.DictReader(f_obj_Account2)
f_obj_Ledger =
open(’Ledger.csv’, ’a+’)
f_writer =
csv.DictWriter(f_obj_Ledger,
fieldnames=col_name_Ledger)
Database Management Systems Partha Pratim Das 03.9
Bank Transaction: Python viz-a-viz SQL (2) PPD

Module 03 Python SQL


Partha Pratim
Das try : do $$
for sRec in f_reader1 : begin
Objectives & # CONDITION CHECK FOR ENOUGH BALANCE
Outline
amt = 5000;
if sRec [ " AcctNo " ] == debitAcc and sendVal = ’1800090’;
File Systems vs int ( sRec [ " Balance " ]) > int ( amt ) : recVal = ’1800100’;
Databases
... select balance from accounts
Python viz-a-viz SQL
Parameterized
into sbalance
Comparison where account_no = sendVal;
Module Summary
if sbalance < amt then
...
$$

Database Management Systems Partha Pratim Das 03.10


Bank Transaction: Python viz-a-viz SQL (3) PPD

Module 03 Python SQL


Partha Pratim
Das try : do $$
for sRec in f_reader1 : begin
Objectives & # CONDITION CHECK FOR ENOUGH BALANCE
Outline
amt = 5000;
if sRec [ " AcctNo " ] == debitAcc and sendVal = ’1800090’;
File Systems vs int ( sRec [ " Balance " ]) > int ( amt ) : recVal = ’1800100’;
Databases
for rRec in f_reader2 : select balance from accounts
Python viz-a-viz SQL
if rRec [ " AcctNo " ] == creditAcc : into sbalance
Parameterized
Comparison sRec [ " Balance " ] = # DEBIT where account_no = sendVal;
Module Summary
str ( int ( sRec [ " Balance " ]) - int ( amt ) ) if sbalance < amt then
temp . append ( sRec ) raise notice "Insufficient balance";
... else
update accounts
set balance =
balance - amt
where account_no = sendVal;
...
$$

Database Management Systems Partha Pratim Das 03.11


Bank Transaction: Python viz-a-viz SQL (4) PPD

Module 03 Python SQL


Partha Pratim
Das try : do $$
for sRec in f_reader1 : begin
Objectives & # CONDITION CHECK FOR ENOUGH BALANCE
Outline
amt = 5000;
if sRec [ " AcctNo " ] == debitAcc and sendVal = ’1800090’;
File Systems vs int ( sRec [ " Balance " ]) > int ( amt ) : recVal = ’1800100’;
Databases
for rRec in f_reader2 : select balance from accounts
Python viz-a-viz SQL
if rRec [ " AcctNo " ] == creditAcc : into sbalance
Parameterized
Comparison sRec [ " Balance " ] = # DEBIT where account_no = sendVal;
Module Summary
str ( int ( sRec [ " Balance " ]) - int ( amt ) ) if sbalance < amt then
temp . append ( sRec ) raise notice "Insufficient balance";
# Critical point else
f_writer . writerow ({ update accounts
" Acct1 " : sRec [ " AcctNo " ] , set balance =
" Acct2 " : rRec [ " AcctNo " ] , balance - amt
" Amount " : amt , " D / C " : " D " }) where account_no = sendVal;
rRec [ " Balance " ] = # CREDIT insert into
str ( int ( rRec [ " Balance " ]) + int ( amt ) ) ledger(sendAc, recAc, amnt, ttype)
temp . append ( rRec ) values(sendVal, recVal, amt, ’D’);
... update accounts
set balance =
balance + amt
where account_no = recVal;
Database Management Systems Partha Pratim Das ... 03.12
Bank Transaction: Python viz-a-viz SQL (5) PPD

Module 03
Python SQL
Partha Pratim
try : do $$
Das
for sRec in f_reader1 : begin
# CONDITION CHECK FOR ENOUGH BALANCE amt = 5000;
Objectives & if sRec [ " AcctNo " ] == debitAcc and sendVal = ’1800090’;
Outline int ( sRec [ " Balance " ]) > int ( amt ) : recVal = ’1800100’;
for rRec in f_reader2 : select balance from accounts
File Systems vs if rRec [ " AcctNo " ] == creditAcc : into sbalance
Databases sRec [ " Balance " ] = # DEBIT where account_no = sendVal;
Python viz-a-viz SQL str ( int ( sRec [ " Balance " ]) - int ( amt ) ) if sbalance < amt then
Parameterized temp . append ( sRec ) raise notice "Insufficient balance";
Comparison # Critical point else
f_writer . writerow ({ " Acct1 " : sRec [ " AcctNo " ] , update accounts
Module Summary " Acct2 " : rRec [ " AcctNo " ] , set balance =
" Amount " : amt , " D / C " : " D " }) balance - amt
rRec [ " Balance " ] = # CREDIT where account_no = sendVal;
str ( int ( rRec [ " Balance " ]) + int ( amt ) ) insert into
temp . append ( rRec ) ledger(sendAc, recAc, amnt, ttype)
f_writer . writerow ({ " Acct1 " : rRec [ " AcctNo " ] , values(sendVal, recVal, amt, ’D’);
" Acct2 " : sRec [ " AcctNo " ] , update accounts
" Amount " : amt , " D / C " : " C " }) set balance =
success = success + 1 balance + amt
break where account_no = recVal;
f_ob j_Accoun t1 . seek (0) insert into
next ( f_ obj_Account1 ) ledger(sendAc, recAc, amnt, ttype)
for record in f_reader1 : values(recVal, sendVal, amt, ’C’);
if record [ " AcctNo " ] != temp [0][ " AcctNo " ] and commit;
record [ " AcctNo " ] != temp [1][ " AcctNo " ]: raise notice "Successful";
temp . append ( record ) end if;
except : end; $$
print ( " Wrong input entered !!! " )
Database Management Systems Partha Pratim Das 03.13
Bank Transaction: Python viz-a-viz SQL (6) PPD

Module 03 Python SQL


Partha Pratim
Das
#Writing back to the file

Objectives & f_obj_Account1.close()


Outline f_obj_Account2.close() // Handled implicitly by the DBMS
File Systems vs
f_obj_Ledger.close()
Databases
Python viz-a-viz SQL if success == 1:
Parameterized f_obj_Account = open(’Accounts.csv’, ’w+’, newline=’’)
Comparison
f_writer = csv.DictWriter(f_obj_Account,
Module Summary fieldnames=col_name_Account)
f_writer.writeheader()
for data in temp:
f_writer.writerow(data)

f_obj_Account.close()
print("Transaction is successful !!")

else:
print(’Transaction failed : Confirm Account details’)

Database Management Systems Partha Pratim Das 03.14


Comparison PPD

Module 03
Parameter File Handling via Python DBMS
Partha Pratim Scalability with re- Very difficult to handle insert, update and In-built features to provide high scalability for
Das spect to querying of records a large number of records
amount of data
Objectives &
Outline
Scalability with re- Extremely difficult to change the structure of Adding or removing attributes can be done
spect to changes records as in the case of adding or removing seamlessly using simple SQL queries
File Systems vs in structure attributes
Databases
Python viz-a-viz SQL
Time of execution In seconds In milliseconds
Parameterized
Persistence Data processed using temporary data struc- Data persistence is ensured via automatic, sys-
Comparison tures have to be manually updated to the file tem induced mechanisms
Module Summary Robustness Ensuring robustness of data has to be done Backup, recovery and restore need minimum
manually manual intervention
Security Difficult to implement in Python (Security at User-specific access at database level
OS level)
Programmer’s Most file access operations involve extensive Standard and simple built-in queries reduce the
productivity coding to ensure persistence, robustness and effort involved in coding thereby increasing a
security of data programmer’s throughput
Arithmetic opera- Easy to do arithmetic computations Limited set of arithmetic operations are avail-
tions able
Costs Low costs for hardware, software and human High costs for hardware, software and human
resources resources

Database Management Systems Partha Pratim Das 03.15


PPD

Module 03

Partha Pratim
Das

Objectives &
Outline

File Systems vs
Databases
Python viz-a-viz SQL
Parameterized
Comparison

Module Summary

Parameterized Comparison

Database Management Systems Partha Pratim Das 03.16


Scalability PPD

Module 03 File handling via Python DBMS


Partha Pratim
Das • Number of records: As the # of records • Number of records: Databases are built
increases, the efficiency of flat files to efficiently scale up when the # of
Objectives &
Outline
reduces: records increase drastically.
File Systems vs
Databases ◦ the time spent in searching for the ◦ In-built mechanisms, like indexing, for
Python viz-a-viz SQL
right records quick access of right data.
Parameterized
Comparison
◦ the limitations of the OS in handling • Structural Change: During adding an
Module Summary
huge files attribute, a default value can be defined
• Structural Change: To add an attribute, that holds for all existing records - the
initializing the new attribute of each new attribute gets initialized with the
record with a default value has to be done default value. During deletion, constraints
by program. It is very difficult to detect are used either not to allow the removal
and maintain relationships between or ensure its safe removal
entities if and when an attribute has to be
removed.
Database Management Systems Partha Pratim Das 03.17
Time and Efficiency PPD

Module 03

Partha Pratim
File handling via Python DBMS
Das
• The effort needed to implement a file • The effort to install and configure a DB
Objectives &
Outline handler is quite less in Python in a DB server is expensive & time
File Systems vs
• In order to process a 1GB file, a program consuming
Databases
Python viz-a-viz SQL in Python would typically take few • In order to process a 1GB file, an SQL
Parameterized
Comparison seconds. query would typically take few
Module Summary milliseconds.

• If the number of records is very small, the overhead in installing and configuring a
database will be much more than the time advantage obtained from executing the
queries.
• However, if the number of records is really large, then the time required in the
initialization process of a database will be negligible as compared to the time saved in
using SQL queries.

Database Management Systems Partha Pratim Das 03.18


Persistence, Robustness, Security PPD

Module 03 File handling via Python DBMS


Partha Pratim
Das • Persistence: Data processed using • Persistence: Data persistence is ensured
in-memory data structures stay in the via automatic, system mechanisms. The
Objectives &
Outline
memory during processing. After updates, programmer does not have to worry about
File Systems vs
Databases these are manually updated to the file on the data getting lost due to manual errors
Python viz-a-viz SQL
disk • Robustness: Backup, recovery & restore
Parameterized
Comparison
• Robustness: Ensuring consistency, need minimum manual intervention. The
Module Summary
reliability and sanity is manual via backup and recovery plan can be devised
multiple checks. On a system crash, a for automatic recovery on a crash
transaction may cause inconsistency or • Security: DBMS provides user-specific
loss of data. access at the database level with
• Security: Extremely difficult to restriction for to view only access
implement granular security in file
systems. Authentication is at the OS
level.
Database Management Systems Partha Pratim Das 03.19
Programmer’s Productivity PPD

Module 03 File handling via Python DBMS


Partha Pratim
Das • Building the file handler: Since the • Configuring the database: The
constraints within and across entities installation and configuration of a
Objectives &
Outline
have to be enforced manually, the effort database is specialized job of a DBA. A
File Systems vs
Databases involved in building a file handling programmer, on the other hand, is saved
Python viz-a-viz SQL
application is huge the trouble
Parameterized
Comparison
• Maintenance: To maintain the • Maintenance: DBMS has in-built
Module Summary
consistency of data, one must regularly mechanisms to ensure consistency and
check for sanity of data and the sanity of data being inserted, updated or
relationships between entities during deleted. The programmer does not need
inserts, updates and deletes to do such checks
• Handling huge data: As the data grows • Handling huge data: DBMS can handle
beyond the capacity of the file handler, even terabytes of data - Programmer does
more efforts are needed not have to worry

Database Management Systems Partha Pratim Das 03.20


Arithmetic Operations PPD

Module 03

Partha Pratim
File handling via Python DBMS
Das
• Extensive support for arithmetic and • Limited support for arithmetic and
Objectives &
Outline logical operations: Extensive arithmetic logical operations: SQL provides limited
File Systems vs and logical operations can be performed arithmetic and logical operations. Any
Databases
Python viz-a-viz SQL on data using Python. These include other complex computation has to be
Parameterized
Comparison complex numerical calculations and done outside the SQL.
Module Summary recursive computations.

Database Management Systems Partha Pratim Das 03.21


Costs and Complexity PPD

Module 03

Partha Pratim
File handling via Python DBMS
Das
• File systems are cheaper to install and • Large databases are served by dedicated
Objectives &
Outline use. No specialized hardware, software or database servers need large storage and
File Systems vs personnel are required to maintain processing power
Databases
Python viz-a-viz SQL filesystems. • DBMSs are expensive software that have
Parameterized
Comparison to be installed and regularly updated
Module Summary
• Databases are inherently complex and
need specialized people to work on it -
like DBA
• The above factors lead to huge costs in
implementing and maintaining database
management systems

Database Management Systems Partha Pratim Das 03.22


Module Summary PPD

Module 03

Partha Pratim • Elucidated the difference between File handling by Python viz-a-viz DBMS through an
Das
Bank Transaction example
Objectives &
Outline • Parameterized Comparison
File Systems vs
Databases
Python viz-a-viz SQL
Parameterized
Comparison

Module Summary

Slides used in this presentation are borrowed from http://db-book.com/ with kind
permission of the authors.
Edited and new slides are marked with “PPD”.

Database Management Systems Partha Pratim Das 03.23


Module 04

Partha Pratim
Das

Objectives &
Outline Database Management Systems
Levels of
Abstraction Module 04: Introduction to DBMS/1
Schema and
Instance

Data Models

DDL and DML Partha Pratim Das


SQL

Database Design Department of Computer Science and Engineering


Module Summary Indian Institute of Technology, Kharagpur

[email protected]

Database Management Systems Partha Pratim Das 04.1


Module Recap PPD

Module 04

Partha Pratim • Comparison of data management using Python & files and DBMS
Das
• Efficacy and Efficient DBMS highlighted
Objectives &
Outline

Levels of
Abstraction

Schema and
Instance

Data Models

DDL and DML

SQL

Database Design

Module Summary

Database Management Systems Partha Pratim Das 04.2


Module Objectives PPD

Module 04

Partha Pratim • To familiarize with the basic notions and terminology of database management systems
Das
• To understand the role of data models and languages
Objectives &
Outline • To understand the approaches to database design
Levels of
Abstraction

Schema and
Instance

Data Models

DDL and DML

SQL

Database Design

Module Summary

Database Management Systems Partha Pratim Das 04.3


Module Outline PPD

Module 04

Partha Pratim • Levels of Abstraction


Das
• Schema & Instance
Objectives &
Outline • Data Models
Levels of
Abstraction ◦ Relational Databases
Schema and
Instance
• DDL & DML
Data Models • SQL
DDL and DML
• Database Design
SQL

Database Design

Module Summary

Database Management Systems Partha Pratim Das 04.4


Levels of Abstraction PPD

Module 04

Partha Pratim
Das

Objectives &
Outline

Levels of
Abstraction

Schema and
Instance

Data Models

DDL and DML

SQL

Database Design

Module Summary
Levels of Abstraction

Database Management Systems Partha Pratim Das 04.5


Levels of Abstraction

Module 04

Partha Pratim • Physical level: describes how a record (for example, instructor) is stored
Das
• Logical level: describes data stored in database, and the relationships among the data
Objectives &
Outline fields
Levels of type instructor = record
Abstraction

Schema and
ID : string;
Instance name : string;
Data Models
dept name : string;
DDL and DML
salary : integer;
SQL
end;
Database Design

Module Summary • View level: application programs hide details of data types
◦ Views can also hide information (such as an employee’s salary) for security purposes

Database Management Systems Partha Pratim Das 04.6


View of Data

Module 04

Partha Pratim
An architecture for a database system
Das

Objectives &
Outline

Levels of
Abstraction

Schema and
Instance

Data Models

DDL and DML

SQL

Database Design

Module Summary

Database Management Systems Partha Pratim Das 04.7


Schema and Instance PPD

Module 04

Partha Pratim
Das

Objectives &
Outline

Levels of
Abstraction

Schema and
Instance

Data Models

DDL and DML

SQL

Database Design

Module Summary
Schema and Instance

Database Management Systems Partha Pratim Das 04.8


Schemas and Instances PPD

Module 04

Partha Pratim • Similar to type of a variable and value of the variable at run-time in programming
Das
languages
Objectives &
Outline • Schema
Levels of
Abstraction
◦ Logical Schema – the overall logical structure of the database
Schema and . Analogous to type information of a variable in a program
Instance
. Example: The database consists of information about a set of customers and
Data Models

DDL and DML


accounts in a bank and the relationship between them
SQL
. Customer Schema
Database Design

Module Summary . Account Schema


◦ Physical Schema– the overall physical structure of the database

Database Management Systems Partha Pratim Das 04.9


Schemas and Instances PPD

Module 04

Partha Pratim • Instance


Das
◦ The actual content of the database at a particular point in time
Objectives &
Outline ◦ Analogous to the value of a variable
Levels of
Abstraction

Schema and
Instance

Data Models

DDL and DML


◦ Customer Instance
SQL
◦ Account Instance
Database Design

Module Summary

Database Management Systems Partha Pratim Das 04.10


Schema and Instances

Module 04

Partha Pratim • Physical Data Independence – the ability to modify the physical schema without
Das
changing the logical schema
Objectives &
Outline ◦ Analogous to independence of Interface and Implementation in Object-Oriented
Levels of Systems
Abstraction
◦ Applications depend on the logical schema
Schema and
Instance ◦ In general, the interfaces between the various levels and components should be well
Data Models defined so that changes in some parts do not seriously influence others.
DDL and DML

SQL

Database Design

Module Summary

Database Management Systems Partha Pratim Das 04.11


Data Models PPD

Module 04

Partha Pratim
Das

Objectives &
Outline

Levels of
Abstraction

Schema and
Instance

Data Models

DDL and DML

SQL

Database Design

Module Summary
Data Models

Database Management Systems Partha Pratim Das 04.12


Data Models

Module 04 • A collection of tools for describing


Partha Pratim ◦ Data
Das
◦ Data relationships
Objectives &
Outline
◦ Data semantics
Levels of
◦ Data constraints
Abstraction
• Relational model (we focus in this course)
Schema and
Instance • Entity-Relationship data model (mainly for database design)
Data Models
• Object-based data models (Object-oriented and Object-relational)
DDL and DML

SQL
• Other older models
Database Design ◦ Network model
Module Summary ◦ Hierarchical model
• Recent models for Semi-structured or Unstructured data
◦ Converted to easily manageable formats
◦ Content Addressable Storage (CAS) with metadata descriptors
◦ XML format.
◦ RDBMS which supports BLOBs
Database Management Systems Partha Pratim Das 04.13
Data Models (2) PPD

Module 04

Partha Pratim
Das

Objectives &
Outline

Levels of
Abstraction

Schema and
Instance

Data Models

DDL and DML

SQL

Database Design

Module Summary

Database Management Systems Partha Pratim Das 04.14


Relational Model

Module 04

Partha Pratim • All the data is stored in various tables


Das
• Example of tabular data in the relational model
Objectives &
Outline

Levels of
Abstraction

Schema and
Instance

Data Models

DDL and DML

SQL

Database Design

Module Summary

Database Management Systems Partha Pratim Das 04.15


A Sample Relational Database

Module 04

Partha Pratim
Das

Objectives &
Outline

Levels of
Abstraction

Schema and
Instance

Data Models

DDL and DML

SQL

Database Design

Module Summary

Database Management Systems Partha Pratim Das 04.16


DDL and DML PPD

Module 04

Partha Pratim
Das

Objectives &
Outline

Levels of
Abstraction

Schema and
Instance

Data Models

DDL and DML

SQL

Database Design

Module Summary
DDL and DML

Database Management Systems Partha Pratim Das 04.17


Data Definition Language (DDL)

Module 04

Partha Pratim • Specification notation for defining the database schema


Das
◦ Example:
Objectives &
Outline create table instructor (
Levels of ID char(5),
Abstraction
name varchar(20),
Schema and
Instance dept name varchar(20),
Data Models salary numeric(8,2))
DDL and DML
• DDL compiler generates a set of table templates stored in a data dictionary
SQL

Database Design • Data dictionary contains metadata (that is, data about data)
Module Summary ◦ Database schema
◦ Integrity constraints
. Primary key (ID uniquely identifies instructors)
◦ Authorization
. Who can access what
Database Management Systems Partha Pratim Das 04.18
Data Manipulation Language (DML)

Module 04

Partha Pratim • Language for accessing and manipulating the data organized by the appropriate data
Das
model
Objectives &
Outline ◦ DML: also known as Query Language
Levels of
Abstraction
• Two classes of languages
Schema and ◦ Pure – used for proving properties about computational power and for optimization
Instance

Data Models
. Relational Algebra (we focus in this course)
DDL and DML . Tuple relational calculus
SQL . Domain relational calculus
Database Design ◦ Commercial – used in commercial systems
Module Summary
. SQL is the most widely used commercial language

Database Management Systems Partha Pratim Das 04.19


SQL PPD

Data Manipulation Language (DML) and its role in accessing and manipulating data within a database. Let's break it down:

Data Manipulation Language (DML)


Module 04 Definition: DML is a subset of SQL (Structured Query Language) used for accessing, inserting, updating, and deleting data in databases.
Purpose: It allows users to perform operations on the data itself, as opposed to defining the structure of the database (which is handled by DDL –
Partha Pratim
Data Definition Language).
Das
Types of DML:
Pure DML (Theoretical/Relational Model Languages):
Objectives &
Outline
Relational Algebra: A procedural query language that uses operators like selection, projection, union, set difference, and Cartesian product to
Levels of manipulate relations (tables).
Abstraction Tuple Relational Calculus: A non-procedural query language where you specify what data you want, not how to retrieve it. Uses tuples (rows) as
variables.
Schema and Domain Relational Calculus: Similar to tuple relational calculus, but uses domain variables (values of attributes) instead of entire tuples.
Instance
These languages are more theoretical and are used for understanding the fundamentals of query processing, query optimization, and the
Data Models computational power of databases.

DDL and DML Commercial DML (Practical, used in DBMS):


SQL
SQL (Structured Query Language): The most widely used commercial language for querying relational databases. It is a standard language used
Database Design for managing and manipulating relational databases in almost all modern DBMS (like MySQL, PostgreSQL, SQL Server, Oracle).
SQL - The Most Widely Used DML
Module Summary
SQL
SQL can be divided into various parts, but the DML portion includes commands like:
SELECT: Retrieves data from one or more tables.
INSERT: Adds new records to a table.
UPDATE: Modifies existing records.
DELETE: Removes records from a table.
In short, DML provides the tools to work with the data, while theoretical models like relational algebra and calculus help optimize and understand
how queries can be structured and processed in a database system.

Database Management Systems Partha Pratim Das 04.20


SQL

Module 04

Partha Pratim • The most widely used commercial language


Das
• SQL is NOT a Turing Machine equivalent language
Objectives &
Outline ◦ Cannot be used to solve all problems that a C program, for example, can solve
Levels of
Abstraction • To be able to compute complex functions, SQL is usually embedded in some
Schema and higher-level language
Instance

Data Models • Application programs generally access databases through one of


DDL and DML ◦ Language extensions to allow embedded SQL
SQL
◦ Application Programming Interface or API (for example, ODBC/JDBC) which allow
Database Design
SQL queries to be sent to a database
Module Summary

Database Management Systems Partha Pratim Das 04.21


Database Design PPD

Module 04

Partha Pratim
Das

Objectives &
Outline

Levels of
Abstraction

Schema and
Instance

Data Models

DDL and DML

SQL

Database Design

Module Summary
Database Design

Database Management Systems Partha Pratim Das 04.22


Database Design

Module 04

Partha Pratim
The process of designing the general structure of the database:
Das
• Logical Design – Deciding on the database schema. Database design requires that we
Objectives &
Outline
find a good collection of relation schema
Levels of ◦ Business decision
Abstraction

Schema and
. What attributes should we record in the database?
Instance
◦ Computer Science decision
Data Models

DDL and DML


. What relation schemas should we have and how should the attributes be
SQL distributed among the various relation schemas?
Database Design • Physical Design – Deciding on the physical layout of the database
Module Summary

Database Management Systems Partha Pratim Das 04.23


Database Design (2)

Module 04

Partha Pratim • Is there any problem with this relation?


Das

Objectives &
Outline

Levels of
Abstraction

Schema and
Instance

Data Models

DDL and DML

SQL

Database Design

Module Summary

Database Management Systems Partha Pratim Das 04.24


Module Summary PPD

Module 04

Partha Pratim • Familiarized with the basic notions and terminology of database management systems
Das
• Introduced the role of data models and languages
Objectives &
Outline • Introduced the approaches to database design
Levels of
Abstraction

Schema and
Instance

Data Models

DDL and DML

SQL

Database Design

Module Summary

Slides used in this presentation are borrowed from http://db-book.com/ with kind
permission of the authors.
Edited and new slides are marked with “PPD”.

Database Management Systems Partha Pratim Das 04.25


Module 05

Partha Pratim
Das

Objectives &
Outline Database Management Systems
Database Design
Object-Relational
Module 05: Introduction to DBMS/2
Data Models
XML: Extensible
Markup Language

Database Engine
Database System
Internals
Partha Pratim Das
Database Users
& Administrators
Department of Computer Science and Engineering
Module Summary Indian Institute of Technology, Kharagpur

[email protected]

Database Management Systems Partha Pratim Das 05.1


Module Recap PPD

Module 05

Partha Pratim • Basic notions and terminology of database management systems


Das
• Role of data models and languages
Objectives &
Outline • Approaches to database design
Database Design
Object-Relational
Data Models
XML: Extensible
Markup Language

Database Engine
Database System
Internals

Database Users
& Administrators

Module Summary

Database Management Systems Partha Pratim Das 05.2


Module Objectives PPD

Module 05

Partha Pratim • To understand models of database management systems


Das
• To familiarize with major components of a database engine
Objectives &
Outline • To familiarize with database internals and architecture
Database Design
Object-Relational • To understand the historical perspective
Data Models
XML: Extensible
Markup Language

Database Engine
Database System
Internals

Database Users
& Administrators

Module Summary

Database Management Systems Partha Pratim Das 05.3


Module Outline PPD

Module 05

Partha Pratim • Database Design


Das
• OO Relational Model
Objectives &
Outline • XML
Database Design
Object-Relational • Database Engine
Data Models
XML: Extensible
Markup Language
◦ Storage Management
Database Engine
◦ Query Processing
Database System
Internals
◦ Transaction Management
Database Users • Database Internals and Architecture
& Administrators

Module Summary • Database Users and Administrators

Database Management Systems Partha Pratim Das 05.4


Database Design PPD

Module 05

Partha Pratim
Das

Objectives &
Outline

Database Design
Object-Relational
Data Models
XML: Extensible
Markup Language

Database Engine
Database System
Internals

Database Users
& Administrators

Module Summary

Database Design

Database Management Systems Partha Pratim Das 05.5


Database Design

Module 05

Partha Pratim
The process of designing the general structure of the database:
Das
• Logical Design
Objectives &
Outline ◦ Deciding on the database schema. Database design requires that we find a good
Database Design collection of relation schema
Object-Relational
Data Models ◦ Business decision
XML: Extensible
Markup Language . What attributes should we record in the database?
Database Engine
Database System
◦ Computer Science decision
Internals
. What relation schemas should we have and how should the attributes be
Database Users
& Administrators distributed among the various relation schemas?
Module Summary
• Physical Design
◦ Deciding on the physical layout of the database

Database Management Systems Partha Pratim Das 05.6


Database Design (2) PPD

Module 05

Partha Pratim • Is there any problem with this relation?


Das

Objectives &
Outline

Database Design
Object-Relational
Data Models redundency
XML: Extensible
Markup Language

Database Engine
Database System same
Internals
data is
Database Users coming
& Administrators multiple
times
Module Summary

Database Management Systems Partha Pratim Das 05.7


Database Design (3) PPD

Module 05

Partha Pratim
Das

Objectives &
Outline

Database Design
Object-Relational
Data Models
XML: Extensible
Markup Language

Database Engine
Database System
Internals

Database Users
& Administrators

Module Summary

Database Management Systems Partha Pratim Das 05.8


Design Approaches

Module 05

Partha Pratim • Need to come up with a methodology to ensure that each relations in the database is
Das
good
Objectives &
Outline • Two ways of doing so:
Database Design ◦ Entity Relationship Model (Chapter 7)
Object-Relational
Data Models
XML: Extensible
. Models an enterprise as a collection of entities and relationships
Markup Language
. Represented diagrammatically by an entity-relationship diagram
Database Engine
Database System ◦ Normalization Theory (Chapter 8)
Internals

Database Users . Formalize what designs are bad, and test for them
& Administrators

Module Summary

Database Management Systems Partha Pratim Das 05.9


Object-Relational Data Models PPD

Module 05

Partha Pratim
Das

Objectives &
Outline

Database Design
Object-Relational
Data Models
XML: Extensible
Markup Language

Database Engine
Database System
Internals

Database Users
& Administrators

Module Summary

Object-Relational Data Models

Database Management Systems Partha Pratim Das 05.10


Object-Relational Data Models

Module 05

Partha Pratim • Relational model: flat, atomic values


Das
• Object Relational Data Models
Objectives &
Outline ◦ Extend the relational data model by including object orientation and constructs to
Database Design deal with added data types
Object-Relational
Data Models ◦ Allow attributes of tuples to have complex types, including non-atomic values such
XML: Extensible
Markup Language as nested relations
Database Engine
Database System
◦ Preserve relational foundations, in particular the declarative access to data, while
Internals
extending modeling power
Database Users
& Administrators ◦ Provide upward compatibility with existing relational languages
Module Summary

Database Management Systems Partha Pratim Das 05.11


XML: Extensible Markup Language PPD

Module 05

Partha Pratim
Das

Objectives &
Outline

Database Design
Object-Relational
Data Models
XML: Extensible
Markup Language

Database Engine
Database System
Internals

Database Users
& Administrators

Module Summary

XML: Extensible Markup Language

Database Management Systems Partha Pratim Das 05.12


XML: Extensible Markup Language

Module 05

Partha Pratim • Defined by the WWW Consortium (W3C)


Das
• Originally intended as a document markup language not a database language
Objectives &
Outline • The ability to specify new tags, and to create nested tag structures made XML a great
Database Design
Object-Relational
way to exchange data, not just documents
Data Models
XML: Extensible • XML has become the basis for all new generation data interchange formats
Markup Language

Database Engine • A wide variety of tools is available for parsing, browsing and querying XML
Database System
Internals
documents/data
Database Users
& Administrators

Module Summary

Database Management Systems Partha Pratim Das 05.13


Database Engine PPD

Module 05

Partha Pratim
Das

Objectives &
Outline

Database Design
Object-Relational
Data Models
XML: Extensible
Markup Language

Database Engine
Database System
Internals

Database Users
& Administrators

Module Summary

Database Engine

Database Management Systems Partha Pratim Das 05.14


Database Engine PPD

Module 05

Partha Pratim • Storage manager


Das
• Query processing
Objectives &
Outline • Transaction manager
Database Design
Object-Relational
Data Models
XML: Extensible
Markup Language

Database Engine
Database System
Internals

Database Users
& Administrators

Module Summary

Database Management Systems Partha Pratim Das 05.15


Storage Management

Module 05

Partha Pratim • Storage manager is a program module that provides the interface between the
Das
low-level data stored in the database and the application programs and queries
Objectives &
Outline
submitted to the system
Database Design • The storage manager is responsible to the following tasks:
Object-Relational
Data Models ◦ Interaction with the OS file manager
XML: Extensible
Markup Language ◦ Efficient storing, retrieving and updating of data
Database Engine
Database System • Issues:
Internals

Database Users
◦ Storage access
& Administrators
◦ File organization
Module Summary
◦ Indexing and hashing

Database Management Systems Partha Pratim Das 05.16


Query Processing

Module 05

Partha Pratim a) Parsing and translation


Das
b) Optimization
Objectives &
Outline c) Evaluation
Database Design
Object-Relational
Data Models
XML: Extensible
Markup Language

Database Engine
Database System
Internals

Database Users
& Administrators

Module Summary

Database Management Systems Partha Pratim Das 05.17


Query Processing (2)

Module 05

Partha Pratim • Alternative ways of evaluating a given query


Das
◦ Equivalent expressions
Objectives &
Outline ◦ Different algorithms for each operation
Database Design • Cost difference between a good and a bad way of evaluating a query can be enormous
Object-Relational
Data Models
XML: Extensible
• Need to estimate the cost of operations
Markup Language

Database Engine
◦ Depends critically on statistical information about relations which the database
Database System
Internals
must maintain
Database Users
◦ Need to estimate statistics for intermediate results to compute cost of complex
& Administrators
expressions
Module Summary

Database Management Systems Partha Pratim Das 05.18


Transaction Management

Module 05

Partha Pratim • What if the system fails?


Das
• What if more than one user is concurrently updating the same data?
Objectives &
Outline • A transaction is a collection of operations that performs a single logical function in a
Database Design
Object-Relational
database application
Data Models
XML: Extensible • Transaction-management component ensures that the database remains in a
Markup Language
consistent (correct) state despite system failures (e.g., power failures and operating
Database Engine
Database System system crashes) and transaction failures.
Internals

Database Users • Concurrency-control manager controls the interaction among the concurrent
& Administrators
transactions, to ensure the consistency of the database.
Module Summary

Database Management Systems Partha Pratim Das 05.19


Database System Internals PPD

Module 05

Partha Pratim
Das

Objectives &
Outline

Database Design
Object-Relational
Data Models
XML: Extensible
Markup Language

Database Engine
Database System
Internals

Database Users
& Administrators

Module Summary

Database System Internals

Database Management Systems Partha Pratim Das 05.20


Database System Internals

Module 05

Partha Pratim
Das

Objectives &
Outline

Database Design
Object-Relational
Data Models
XML: Extensible
Markup Language

Database Engine
Database System
Internals

Database Users
& Administrators

Module Summary

Database Management Systems Partha Pratim Das 05.21


Database Architecture

Module 05

Partha Pratim
The architecture of a database system is greatly influenced by the underlying computer
Das system on which the database is running:
Objectives &
Outline
• Centralized
Database Design • Client-server
Object-Relational
Data Models
XML: Extensible
• Parallel (multi-processor)
Markup Language
• Distributed
Database Engine
Database System
Internals
• Cloud
Database Users
& Administrators

Module Summary

Database Management Systems Partha Pratim Das 05.22


Database Architecture (2) PPD

Module 05

Partha Pratim
Das

Objectives &
Outline

Database Design
Object-Relational
Data Models
XML: Extensible
Markup Language

Database Engine
Database System
Internals

Database Users
& Administrators

Module Summary

Database Management Systems Partha Pratim Das 05.23


Database Users and Administrators PPD

Module 05

Partha Pratim
Das

Objectives &
Outline

Database Design
Object-Relational
Data Models
XML: Extensible
Markup Language

Database Engine
Database System
Internals

Database Users
& Administrators

Module Summary

Database Users and Administrators

Database Management Systems Partha Pratim Das 05.24


Database Users and Administrators

Module 05

Partha Pratim
Das

Objectives &
Outline

Database Design
Object-Relational
Data Models
XML: Extensible
Markup Language

Database Engine
Database System
Internals
Database
Database Users
& Administrators

Module Summary

Database Management Systems Partha Pratim Das 05.25


Module Summary PPD

Module 05

Partha Pratim • Introduced models of database management systems


Das
• Familiarized with major components of a database engine
Objectives &
Outline • Familiarized with database internals and architecture
Database Design
Object-Relational
Data Models
XML: Extensible
Markup Language

Database Engine
Database System
Internals

Database Users
& Administrators

Module Summary

Slides used in this presentation are borrowed from http://db-book.com/ with kind
permission of the authors.
Edited and new slides are marked with “PPD”.

Database Management Systems Partha Pratim Das 05.26


Module 06

Partha Pratim
Das

Week Recap

Objectives &
Database Management Systems
Outline
Module 06: Introduction to Relational Model/1
Example of a
Relation

Attributes

Schema and
Instance Partha Pratim Das
Keys

Relational Query Department of Computer Science and Engineering


Languages
Indian Institute of Technology, Kharagpur
Module Summary
[email protected]

Database Management Systems Partha Pratim Das 06.1


Week Recap PPD

Module 06

Partha Pratim • The proliferation of DBMS in wide range of applications provide motivation to study
Das
the subject
Week Recap
• Know Your Course provided information about prerequisites, outline and text book
Objectives &
Outline
• The specific need for a DBMS discussed in contrast to a file system based application
Example of a
Relation using a programming language like Python
Attributes
• Basic notions of a DBMS are introduced
Schema and
Instance

Keys

Relational Query
Languages

Module Summary

Database Management Systems Partha Pratim Das 06.2


Module Objectives PPD

Module 06

Partha Pratim • To understand attributes and their types


Das
• To understand the mathematical structure of relational model
Week Recap

Objectives &
◦ Schema
Outline ◦ Instance
Example of a
Relation
◦ Keys
Attributes • To familiarize with different types of relational query languages
Schema and
Instance

Keys

Relational Query
Languages

Module Summary

Database Management Systems Partha Pratim Das 06.3


Module Outline PPD

Module 06

Partha Pratim • Attribute Types


Das
• Relation Schema and Instance
Week Recap

Objectives &
• Keys
Outline

Example of a
• Relational Query Languages
Relation

Attributes

Schema and
Instance

Keys

Relational Query
Languages

Module Summary

Database Management Systems Partha Pratim Das 06.4


Example of a Relation

Module 06

Partha Pratim
Das

Week Recap

Objectives &
Outline

Example of a
Relation

Attributes

Schema and
Instance

Keys

Relational Query
Languages

Module Summary

Database Management Systems Partha Pratim Das 06.5


Attributes PPD

Module 06

Partha Pratim
Das

Week Recap

Objectives &
Outline

Example of a
Relation

Attributes

Schema and
Instance

Keys

Relational Query
Languages

Module Summary
Attributes

Database Management Systems Partha Pratim Das 06.6


Attribute Types PPD

Module 06

Partha Pratim • Consider


Das
Students = Roll#, First Name, Last Name, DoB, Passport#, Aadhaar #, Department
Week Recap relation
Objectives &
Outline • The set of allowed values for each attribute is called the domain of the attribute
Example of a
Relation
◦ Roll #: Alphanumeric string
Attributes ◦ First Name, Last Name: Alpha String
Schema and ◦ DoB: Date
Instance
◦ Passport #: String (Letter followed by 7 digits) – nullable (optional)
Keys

Relational Query
◦ Aadhaar #: 12-digit number
Languages ◦ Department: Alpha String
Module Summary
• Attribute values are (normally) required to be atomic; that is, indivisible
• The special value null is a member of every domain. Indicates that the value is unknown
• The null value may cause complications in the definition of many operations

Database Management Systems Partha Pratim Das 06.7


Attribute Types PPD

Module 06
• For
Partha Pratim
Das
Students = Roll#, First Name, Last Name, DoB, Passport#, Aadhaar #, Department
Week Recap
• And domain of the attributes as:
Objectives & ◦ Roll #: Alphanumeric string
Outline
◦ First Name, Last Name: Alpha String
Example of a
Relation ◦ DoB: Date nullable means that
can be empty, as we
Attributes ◦ Passport #: String (Letter followed by 7 digits) – nullable (optional) can not make any
Schema and
Instance
◦ Aadhaar #: 12-digit number attribute emtpty
e.g we can not let roll
Keys ◦ Department: Alpha String no be empty
Relational Query
Languages

Module Summary

Database Management Systems Partha Pratim Das 06.8


Schema and Instance PPD

Module 06

Partha Pratim
Das

Week Recap

Objectives &
Outline

Example of a
Relation

Attributes

Schema and
Instance

Keys

Relational Query
Languages

Module Summary
Schema and Instance

Database Management Systems Partha Pratim Das 06.9


Relation Schema and Instance

Module 06

Partha Pratim • A1 , A2 , · · · , An are attributes


Das
• R = (A1 , A2 , · · · , An ) is a relation schema
Week Recap
Example: instructor = (ID, name, dept name, salary )
Objectives &
Outline
• Formally, given sets D1 , D2 , · · · , Dn a relation r is a subset of
Example of a
Relation

Attributes D 1 × D2 × · · · × D n
Schema and
Instance
Thus, a relation is a set of n-tuples (a1 , a2 , · · · , an ) where each ai ∈ Di
Keys

Relational Query
• The current values (relation instance) of a relation are specified by a table
Languages
• An element t of r is a tuple, represented by a row in a table
Module Summary
• Example:
instructor ≡ (String (5) × String × String × Number +), where ID ∈ String (5),
name ∈ String , dept name ∈ String , and salary ∈ Number +

Database Management Systems Partha Pratim Das 06.10


Relations are Unordered with Unique Tuples

Module 06 • Order of tuples / rows is irrelevant (tuples may be stored in an arbitrary order)
Partha Pratim
Das • No two tuples / rows may be identical
Week Recap
• Example: instructor relation with unordered tuples
Objectives &
Outline

Example of a
Relation

Attributes

Schema and
Instance

Keys

Relational Query
Languages

Module Summary

Database Management Systems Partha Pratim Das 06.11


Keys PPD

Module 06

Partha Pratim
Das

Week Recap

Objectives &
Outline

Example of a
Relation

Attributes

Schema and
Instance

Keys

Relational Query
Languages

Module Summary
Keys

Database Management Systems Partha Pratim Das 06.12


Keys PPD

Module 06

Partha Pratim • Let K ⊆ R, where R is the set of attributes in the relation


Das
• K is a superkey of R if values for K are sufficient to identify a unique tuple of each
Week Recap
possible relation r (R)
Objectives &
Outline ◦ Example: {ID} and {ID, name} are both superkeys of instructor
Example of a
Relation • Superkey K is a candidate key if K is minimal
Attributes
◦ Example: {ID} is a candidate key for instructor
Schema and
Instance • One of the candidate keys is selected to be the primary key
Keys
◦ Which one?
Relational Query
Languages
• A surrogate key (or synthetic key) in a database is a unique identifier for either an
Module Summary
entity in the modeled world or an object in the database
◦ The surrogate key is not derived from application data, unlike a natural (or
business) key which is derived from application data
https://www.geeksforgeeks.org/surrogate-key-in-dbms/

Database Management Systems Partha Pratim Das 06.13


Keys PPD

Module 06
• Students = Roll#, First Name, Last Name, DoB, Passport#, Aadhaar #, Department
Partha Pratim
Das • Super Key: Roll #, {Roll #, DoB}
Week Recap • Candidate Keys: Roll #, {First Name, Last Name}, Aadhaar#
Objectives &
Outline
◦ Passport # cannot be a key. Why?
Example of a ◦ Null values are allowed for Passport # (a student may not have a passport)
Relation

Attributes
• Primary Key: Roll #
Schema and ◦ Can Aadhaar# be a key?
Instance
◦ It may suffice for unique identification. But Roll# may have additional useful
Keys

Relational Query
information. For example: 14CS92P01
Languages
. Read 14CS92P01 as 14-CS-92-P-01
Module Summary
. 14: Admission in 2014
. CS: Department = CS
. 92: Category of Student
. P: Type of admission: Project
. 01: Serial Number
Database Management Systems Partha Pratim Das 06.14
Keys PPD

Module 06
• Secondary / Alternate Key: {First Name, Last Name}, Aadhaar #
Partha Pratim
Das • Simple Key: Consists of a single attribute
Week Recap • Composite Key: {First Name, Last Name}
Objectives &
Outline
◦ Consists of more than one attribute to uniquely identify an entity occurrence
Example of a
◦ One or more of the attributes, which make up the key, are not simple keys in their
Relation
own right
Attributes

Schema and
Instance

Keys

Relational Query
Languages

Module Summary

Database Management Systems Partha Pratim Das 06.15


Keys PPD

Module 06
• Foreign key constraint: Value in one relation must appear in another
Partha Pratim
Das ◦ Referencing relation
Week Recap
. Enrolment: Foreign Keys – Roll #, Course #
Objectives & ◦ Referenced relation
Outline

Example of a
. Students, Courses
Relation
• A compound key consists of more than one attribute to uniquely identify an entity
Attributes
occurrence
Schema and
Instance ◦ Each attribute, which makes up the key, is a simple key in its own right
Keys
◦ {Roll #, Course #}
Relational Query
Languages

Module Summary

Database Management Systems Partha Pratim Das 06.16


Schema Diagram for University Database

Module 06

Partha Pratim
Das

Week Recap

Objectives &
Outline

Example of a
Relation

Attributes

Schema and
Instance

Keys

Relational Query
Languages

Module Summary

Database Management Systems Partha Pratim Das 06.17


Relational Query Languages PPD

Module 06

Partha Pratim
Das

Week Recap

Objectives &
Outline

Example of a
Relation

Attributes

Schema and
Instance

Keys

Relational Query
Languages

Module Summary
Relational Query Languages

Database Management Systems Partha Pratim Das 06.18


Relational Query Languages PPD

Module 06

Partha Pratim
Procedural viz-a-viz Non-procedural or Declarative Paradigms
Das
• Procedural programming requires that the programmer tell the computer what to do
Week Recap
◦ That is, how to get the output for the range of required inputs
Objectives &
Outline ◦ The programmer must know an appropriate algorithm
Example of a
Relation
• Declarative programming requires a more descriptive style
Attributes ◦ The programmer must know what relationships hold between various entities
Schema and
Instance

Keys

Relational Query
Languages

Module Summary

Database Management Systems Partha Pratim Das 06.19


Relational Query Languages (Cont..) PPD

Module 06

Partha Pratim
Procedural vs. Non-procedural or Declarative Paradigms
Das
• Example: Square root of n
Week Recap
◦ Procedural we know or find set of step to find but not know its proof or
Objectives & insides in detail is procedural
Outline a) Guess x0 (close to root of n)
when we know its proof its declarative
Example of a
Relation
b) i ← 0
Attributes c) xi+1 ← (xi + n/xi )/2
Schema and d) Repeat Step 2 if |xi+1 − xi | > delta
Instance

Keys
◦ Declarative
Relational Query . Root of n is m such that m2 = n
Languages

Module Summary

Database Management Systems Partha Pratim Das 06.20


Relational Query Languages PPD

Module 06

Partha Pratim • “Pure” languages:


Das
◦ Relational algebra
Week Recap
◦ Tuple relational calculus
Objectives &
Outline ◦ Domain relational calculus
Example of a
Relation
• The above 3 pure languages are equivalent in computing power
Attributes • We will concentrate on relational algebra
Schema and
Instance ◦ Not Turing-machine equivalent
Keys . Not all algorithms can be expressed in RA
Relational Query
Languages ◦ Consists of 6 basic operations
Module Summary

Database Management Systems Partha Pratim Das 06.21


Module Summary PPD

Module 06

Partha Pratim • Introduced the notion of attributes and their types


Das
• Taken an overview of the mathematical structure of relational model – schema and
Week Recap
instance
Objectives &
Outline
• Introduced the notion of keys – primary as well as foreign
Example of a
Relation

Attributes

Schema and
Instance

Keys

Relational Query
Languages

Module Summary

Slides used in this presentation are borrowed from http://db-book.com/ with kind
permission of the authors.
Edited and new slides are marked with “PPD”.
Database Management Systems Partha Pratim Das 06.22
Module 07

Partha Pratim
Das

Objectives &
Outline Database Management Systems
Relational
Operators Module 07: Introduction to Relational Model/2
Aggregation
Operators

Module Summary

Partha Pratim Das

Department of Computer Science and Engineering


Indian Institute of Technology, Kharagpur

[email protected]

Database Management Systems Partha Pratim Das 07.1


Module Recap PPD

Module 07

Partha Pratim • Basic notions of modeling introduced


Das
◦ Attributes and their Types
Objectives &
Outline ◦ Schema and Instance
Relational ◦ Keys and their Categorization
Operators

Aggregation
• Languages for Relation Model introduced
Operators

Module Summary

Database Management Systems Partha Pratim Das 07.2


Module Objectives PPD

Module 07

Partha Pratim • To understand relational algebra


Das
• To familiarize with the operators of relational algebra
Objectives &
Outline

Relational
Operators

Aggregation
Operators

Module Summary

Database Management Systems Partha Pratim Das 07.3


Module Outline PPD

Module 07

Partha Pratim • Operations


Das
◦ Select
Objectives &
Outline ◦ Project
Relational ◦ Union
Operators
◦ Difference
Aggregation
Operators ◦ Intersection
Module Summary ◦ Cartesian Product
◦ Natural Join
• Aggregate Operations

Database Management Systems Partha Pratim Das 07.4


Relational Operators PPD

Module 07

Partha Pratim
Das

Objectives &
Outline

Relational
Operators

Aggregation
Operators

Module Summary

Relational Operators

Database Management Systems Partha Pratim Das 07.5


Basic Properties of Relations

Module 07 • A relation is set. Hence,


Partha Pratim
Das • Ordering of rows / tuples is inconsequential
Objectives &
Outline A B A B
Relational
Operators
a1 b1 a1 b1
Aggregation a1 b2 is same as: a2 b1
Operators
a2 b1 a2 b2
Module Summary
a2 b2 a1 b2
• All rows / tuples must be distinct

A B
a1 b1 A B
a1 b2 is not valid a1 b1 is
a1 b2 a1 b2
a1 b1
Database Management Systems Partha Pratim Das 07.6
Select Operation – selection of rows (tuples)

Module 07

Partha Pratim • Relation r


Das

Objectives &
Outline

Relational
Operators

Aggregation
Operators

Module Summary

• σA=B∧D>5 (r )

Database Management Systems Partha Pratim Das 07.7


Project Operation – selection of columns (Attributes)

Module 07

Partha Pratim • Relation r


Das

Objectives &
Outline

Relational
Operators

Aggregation
Operators

Module Summary

• πA,C (r )

Database Management Systems Partha Pratim Das 07.8


Union of two relations

Module 07

Partha Pratim • Relation r , s


Das

Objectives &
Outline

Relational
Operators

Aggregation
Operators

Module Summary

• r ∪s

Database Management Systems Partha Pratim Das 07.9


Set difference of two relations

Module 07

Partha Pratim • Relation r , s


Das

Objectives &
Outline

Relational
Operators

Aggregation
Operators

Module Summary

• r −s

Database Management Systems Partha Pratim Das 07.10


Set intersection of two relations

Module 07

Partha Pratim • Relation r , s


Das

Objectives &
Outline

Relational
Operators

Aggregation
Operators

Module Summary

• r ∩s

Note: r ∩ s = r − (r − s)

Database Management Systems Partha Pratim Das 07.11


Joining two relations – Cartesian-product

Module 07

Partha Pratim • Relation r , s


Das

Objectives &
Outline

Relational
Operators

Aggregation
Operators

Module Summary

• r ×s

Database Management Systems Partha Pratim Das 07.12


Cartesian-product – naming issue

Module 07

Partha Pratim • Relation r , s


Das

Objectives &
Outline

Relational
Operators

Aggregation
Operators

Module Summary • r ×s

Database Management Systems Partha Pratim Das 07.13


Renaming a Table

Module 07

Partha Pratim • Allows us to refer to a relation, (say E ) by more than one name.
Das

Objectives & ρX (E )
Outline

Relational
Operators returns the expression E under the name X
Aggregation
Operators
• Relations r
Module Summary

• r × ρs (r )

Database Management Systems Partha Pratim Das 07.14


Composition of Operations

Module 07

Partha Pratim • Can build expressions using multiple operations


Das
• Example: σA=C (r × s)
Objectives &
Outline • r ×s
Relational
Operators

Aggregation
Operators

Module Summary

• σA=C (r × s)

Database Management Systems Partha Pratim Das 07.15


Joining two relations – Natural Join

Module 07

Partha Pratim • Let r and s be relations on schemas R and S respectively. Then, the “natural join” of
Das
relations R and S is a relation on schema R ∪ S obtained as follows:
Objectives &
Outline ◦ Consider each pair of tuples tr from r and ts from s.
Relational ◦ If tr and ts have the same value on each of the attributes in R ∩ S, add a tuple t to
Operators
the result, where
Aggregation
Operators . t has the same value as tr on r
Module Summary
. t has the same value as ts on s

Database Management Systems Partha Pratim Das 07.16


Natural Join Example

Module 07

Partha Pratim • Relations r , s :


Das

Objectives &
Outline

Relational
Operators

Aggregation
Operators

Module Summary
• Natural Join
◦ r ./ s

πA,r .B,C ,r .D,E (σr .B=s.B ∧ r .D=s.D (r × s))


Database Management Systems Partha Pratim Das 07.17
Aggregation Operators PPD

Module 07

Partha Pratim
Das

Objectives &
Outline

Relational
Operators

Aggregation
Operators

Module Summary

Aggregation Operators

Database Management Systems Partha Pratim Das 07.18


Aggregate Operators PPD

Module 07

Partha Pratim • Can we compute:


Das
◦ SUM
Objectives &
Outline ◦ AVG
Relational ◦ MAX
Operators
◦ MIN
Aggregation
Operators

Module Summary

Database Management Systems Partha Pratim Das 07.19


Notes about Relational Languages

Module 07

Partha Pratim • Each query input is a table (or set of tables)


Das
• Each query output is a table
Objectives &
Outline • All data in the output table appears in one of the input tables
Relational
Operators • Relational Algebra is not Turing complete
Aggregation
Operators

Module Summary

Database Management Systems Partha Pratim Das 07.20


Summary of Relational Algebra Operators

Module 07

Partha Pratim
Das

Objectives &
Outline

Relational
Operators

Aggregation
Operators

Module Summary

Database Management Systems Partha Pratim Das 07.21


Module Summary PPD

Module 07

Partha Pratim • Introduced relational algebra


Das
• Familiarized with the operators of relational algebra
Objectives &
Outline

Relational
Operators

Aggregation
Operators

Module Summary

Slides used in this presentation are borrowed from http://db-book.com/ with kind
permission of the authors.
Edited and new slides are marked with “PPD”.

Database Management Systems Partha Pratim Das 07.22


Module 08

Partha Pratim
Das

Objectives &
Outline Database Management Systems
Outline
Module 08: Introduction to SQL/1
History of SQL

Data Definition
Language (DDL)
Create Table
Integrity Constraints
Update Table
Partha Pratim Das
Data
Manipulation Department of Computer Science and Engineering
Language (DML):
Query Structure
Indian Institute of Technology, Kharagpur
Select Clause
Where Clause
[email protected]
From Clause

Module Summary

Database Management Systems Partha Pratim Das 08.1


Module Recap PPD

Module 08

Partha Pratim • Introduced relational algebra


Das
• Familiarized with the operators of relational algebra
Objectives &
Outline

Outline

History of SQL

Data Definition
Language (DDL)
Create Table
Integrity Constraints
Update Table

Data
Manipulation
Language (DML):
Query Structure
Select Clause
Where Clause
From Clause

Module Summary

Database Management Systems Partha Pratim Das 08.2


Module Objectives PPD

Module 08

Partha Pratim • To understand relational query language


Das
• To understand data definition and basic query structure
Objectives &
Outline

Outline

History of SQL

Data Definition
Language (DDL)
Create Table
Integrity Constraints
Update Table

Data
Manipulation
Language (DML):
Query Structure
Select Clause
Where Clause
From Clause

Module Summary

Database Management Systems Partha Pratim Das 08.3


Module Outline PPD

Module 08

Partha Pratim • History of SQL


Das
• Data Definition Language (DDL)
Objectives &
Outline • Data Manipulation Language (DML): Query Structure
Outline

History of SQL

Data Definition
Language (DDL)
Create Table
Integrity Constraints
Update Table

Data
Manipulation
Language (DML):
Query Structure
Select Clause
Where Clause
From Clause

Module Summary

Database Management Systems Partha Pratim Das 08.4


History of SQL PPD

Module 08

Partha Pratim
Das

Objectives &
Outline

Outline

History of SQL

Data Definition
Language (DDL)
Create Table
Integrity Constraints
Update Table

Data
Manipulation
Language (DML):
Query Structure
History of SQL
Select Clause
Where Clause
From Clause

Module Summary

Database Management Systems Partha Pratim Das 08.5


History of Query Language PPD

Module 08

Partha Pratim • IBM developed Structured English Query Language (SEQUEL) as part of System R
Das
project. Renamed Structured Query Language (SQL: pronounced still as SEQUEL)
Objectives &
Outline • ANSI and ISO standard SQL:
Outline
SQL-86 First formalized by ANSI
History of SQL SQL-89 + Integrity Constraints
Data Definition SQL-92 Major revision (ISO/IEC 9075 standard), De-facto Industry Standard
Language (DDL) SQL:1999 + Regular Expression Matching, Recursive Queries, Triggers, Support for Procedural and
Create Table
Control Flow Statements, Nonscalar types (Arrays), and Some OO features (structured
Integrity Constraints
types), Embedding SQL in Java (SQL/OLB), and Embedding Java in SQL (SQL/JRT)
Update Table
SQL:2003 + XML features (SQL/XML), Window Functions, Standardized Sequences, and Columns
Data with Auto-generated Values (identity columns)
Manipulation
Language (DML): SQL:2006 + Ways of importing and storing XML data in an SQL database, manipulating it within
Query Structure the database, and publishing both XML and conventional SQL-data in XML form
Select Clause SQL:2008 Legalizes ORDER BY outside Cursor Definitions
Where Clause + INSTEAD OF Triggers, TRUNCATE Statement, and FETCH Clause
From Clause
SQL:2011 + Temporal Data (PERIOD FOR)
Module Summary Enhancements for Window Functions and FETCH Clause
SQL:2016 + Row Pattern Matching, Polymorphic Table Functions, and JSON
SQL:2019 + Multidimensional Arrays (MDarray type and operators)

Database Management Systems Partha Pratim Das 08.6


History of Query Language (2): Compliance PPD

Module 08

Partha Pratim • SQL is the de facto industry standard today for relational or structred data systems
Das
• Commercial systems as well as open systems may be fully or partially compliant to one
Objectives &
Outline or more standards from SQL-92 onward
Outline
• Not all examples here may work on your particular system. Check your system’s SQL
History of SQL
documentation
Data Definition
Language (DDL)
Create Table
Integrity Constraints
Update Table

Data
Manipulation
Language (DML):
Query Structure
Select Clause
Where Clause
From Clause

Module Summary

Database Management Systems Partha Pratim Das 08.7


History of Query Language (3): Alternatives PPD

Module 08

Partha Pratim • There aren’t any alternatives to SQL for speaking to relational databases (that is, SQL
Das
as a protocol), but there are many alternatives to writing SQL in the applications
Objectives &
Outline • These alternatives have been implemented in the form of frontends for working with
Outline relational databases. Some examples of a frontend include (for a section of languages):
History of SQL
◦ SchemeQL and CLSQL, which are probably the most flexible, owing to their Lisp
Data Definition
Language (DDL) heritage, but they also look like a lot more like SQL than other frontends
Create Table
Integrity Constraints
◦ LINQ (in .Net)
Update Table ◦ ScalaQL and ScalaQuery (in Scala)
Data
Manipulation
◦ SqlStatement, ActiveRecord and many others in Ruby
Language (DML):
Query Structure
◦ HaskellDB
Select Clause ◦ ...the list goes on for many other languages.
Where Clause
From Clause

Module Summary

Source: What are good alternatives to SQL (the language)?

Database Management Systems Partha Pratim Das 08.8


History of Query Language (4): Derivatives PPD

Module 08

Partha Pratim • There are several query languages that are derived from or inspired by SQL. Of these,
Das
the most popular and effective is SPARQL.
Objectives &
Outline ◦ SPARQL (pronounced sparkle, a recursive acronym for SPARQL Protocol and RDF
Outline Query Language) is an RDF query language
History of SQL . A semantic query language for databases - able to retrieve and manipulate data
Data Definition
Language (DDL) stored in Resource Description Framework (RDF) format.
Create Table . It has been standardized by the W3C Consortium as key technology of the
Integrity Constraints
Update Table semantic web
Data . Versions:
Manipulation
Language (DML):
Query Structure
− SPARQL 1.0 (January 2008)
Select Clause − SPARQL 1.1 (March, 2013)
Where Clause
From Clause . Used as the query languages for several NoSQL systems - particularly the Graph
Module Summary Databases that use RDF as store

Database Management Systems Partha Pratim Das 08.9


Data Definition Language (DDL) PPD

Module 08

Partha Pratim
Das

Objectives &
Outline

Outline

History of SQL

Data Definition
Language (DDL)
Create Table
Integrity Constraints
Update Table

Data
Manipulation
Language (DML):
Query Structure
Data Definition Language (DDL)
Select Clause
Where Clause
From Clause

Module Summary

Database Management Systems Partha Pratim Das 08.10


Data Definition Language (DDL)

Module 08

Partha Pratim
The SQL data-definition language (DDL) allows the specification of information about
Das relations, including:
Objectives &
Outline
• The Schema for each Relation
Outline • The Domain of values associated with each Attribute
History of SQL
• Integrity Constraints
Data Definition
Language (DDL)
• And, as we will see later, also other information such as
Create Table
Integrity Constraints ◦ The set of Indices to be maintained for each relations
Update Table

Data
◦ Security and Authorization information for each relation
Manipulation
Language (DML):
◦ The Physical Storage Structure of each relation on disk
Query Structure
Select Clause
Where Clause
From Clause

Module Summary

Database Management Systems Partha Pratim Das 08.11


Domain Types in SQL

Module 08

Partha Pratim • char(n). Fixed length character string, with user-specified length n
Das
• varchar(n). Variable length character strings, with user-specified maximum length n
Objectives &
Outline • int. Integer (a finite subset of the integers that is machine-dependent)
Outline

History of SQL
• smallint(n). Small integer (a machine-dependent subset of the integer domain type)
Data Definition • numeric(p, d). Fixed point number, with user-specified precision of p digits, with d
Language (DDL)
Create Table
digits to the right of decimal point. (ex., numeric(3, 1), allows 44.5 to be stores
Integrity Constraints
Update Table
exactly, but not 444.5 or 0.32)
Data • real, double precision. Floating point and double-precision floating point numbers,
Manipulation
Language (DML): with machine-dependent precision
Query Structure
Select Clause • float(n). Floating point number, with user-specified precision of at least n digits
Where Clause
From Clause • More are covered in Chapter 4
Module Summary

Database Management Systems Partha Pratim Das 08.12


Schema Diagram for University Database PPD

Module 08

Partha Pratim
Das

Objectives &
Outline

Outline

History of SQL

Data Definition
Language (DDL)
Create Table
Integrity Constraints
Update Table

Data
Manipulation
Language (DML):
Query Structure
Select Clause
Where Clause
From Clause

Module Summary

Database Management Systems Partha Pratim Das 08.13


Create Table Construct PPD

Module 08
• An SQL relation is defined using the create table command:
Partha Pratim
Das create table r (A1 D1 , A2 D2 , . . . , An Dn ),
Objectives &
(integrity -constraint1 ),
Outline
...
Outline
(integrity -constraintk ));
History of SQL

Data Definition
◦ r is the name of the relation
Language (DDL) ◦ each Ai is an attribute name in the schema of relation r
Create Table
Integrity Constraints ◦ Di is the data type of values in the domain of attribute Ai
Update Table

Data
Manipulation
Language (DML):
Query Structure
Select Clause
Where Clause
From Clause

Module Summary

Database Management Systems Partha Pratim Das 08.14


Create Table Construct (2) PPD

Module 08

Partha Pratim
Das

Objectives &
Outline

Outline create table instructor (


History of SQL ID char(5),
Data Definition
Language (DDL) name varchar(20)
Create Table
Integrity Constraints dept name varchar(20)
Update Table

Data salary numeric(8, 2));


Manipulation
Language (DML):
Query Structure
Select Clause
Where Clause
From Clause
length of number
Module Summary upto decimal

Database Management Systems Partha Pratim Das 08.15


Create Table Construct (3): Integrity Constraints PPD

Module 08
• not null
Partha Pratim
Das • primary key (A1 , . . . , An )
Objectives &
Outline
• foreign key (Am , . . . , An ) references r
not null ensure that
Outline
attribute(name) should
create table instructor ( create table instructor ( not be left emtpty.
History of SQL

Data Definition ID char(5), ID char(5),


Language (DDL)
(dept_name) must exist in
Create Table name varchar(20) name varchar(20) not null, department table
Integrity Constraints

dept name varchar(20), i.e constrain which is used


Update Table
dept name varchar(20) here (in foreign key) must
Data be avilable in reference
Manipulation salary numeric(8, 2)); salary numeric(8, 2),
Language (DML):
Query Structure primary key (ID),
Select Clause
Where Clause
foreign key (dept name) references department));
From Clause

Module Summary
primary key declaration on an attribute automatically ensures not null

Database Management Systems Partha Pratim Das 08.16


University Schema PPD

Module 08

Partha Pratim
Das

Objectives &
Outline

Outline

History of SQL

Data Definition
Language (DDL)
Create Table
Integrity Constraints
Update Table

Data
Manipulation
Language (DML):
Query Structure
Select Clause
Where Clause
From Clause

Module Summary

Database Management Systems Partha Pratim Das 08.17


Create Table Construct (4): More Relations PPD

create table student ( create table takes (


Module 08
ID varchar(5), ID varchar(5),
Partha Pratim
Das
name varchar(20) not null, course id varchar(8), sec id varchar(8),
Objectives & dept name varchar(20), semester varchar(6), year numeric(4, 0),
Outline

Outline tot cred numeric(3, 0), grade varchar(2),


History of SQL primary key (ID), primary key (ID, course id, sec id, semester, year ),
Data Definition
Language (DDL)
foreign key (dept name) foreign key (ID) references student
Create Table references department);
Integrity Constraints
foreign key (course id, sec id, semester, year )
Update Table
create table course ( references section);
Data course id varchar(8), • Note: sec id can be dropped from primary key above,
Manipulation
Language (DML): title varchar(50), to ensure a student cannot be registered for two
Query Structure sections of the same course in the same semester
Select Clause dept name varchar(20),
Where Clause
From Clause credits numeric(2, 0),
Module Summary primary key (course id),
foreign key (dept name)
references department);
Database Management Systems Partha Pratim Das 08.18
Update Tables

Module 08 • Insert (DML command)


Partha Pratim
Das
◦ insert into instructor values (‘10211’, ‘Smith’, ‘Biology’, 66000);
Objectives &
• Delete (DML command)
Outline
◦ Remove all tuples from the student relation
Outline
delete from student
History of SQL

Data Definition
• Drop Table (DDL command)
Language (DDL)
Create Table
◦ drop table r
Integrity Constraints
Update Table
• Alter (DDL command)
Data ◦ alter table r add A D
Manipulation
Language (DML): . Where A is the name of the attribute to be added to relation r and D is the domain of A
Query Structure . All existing tuples in the relation are assigned null as the value for the new attribute
Select Clause
Where Clause ◦ alter table r drop A
From Clause
. Where A is the name of an attribute of relation r
Module Summary . Dropping of attributes not supported by many databases

Database Management Systems Partha Pratim Das 08.19


Data Manipulation Language (DML): Query Structure PPD

Module 08

Partha Pratim
Das

Objectives &
Outline

Outline

History of SQL

Data Definition
Language (DDL)
Create Table
Integrity Constraints
Update Table

Data
Manipulation
Language (DML):
Query Structure
Data Manipulation Language (DML):
Select Clause
Where Clause
From Clause
Query Structure
Module Summary

Database Management Systems Partha Pratim Das 08.20


Basic Query Structure

Module 08
• A typical SQL query has the form:
Partha Pratim
Das select A1 , A2 , . . . , An ,
Objectives &
from r1 , r2 , ..., rm
Outline where P
Outline
◦ Ai represents an attribute from ri ’s
History of SQL
◦ ri represents a relation
Data Definition
Language (DDL) ◦ P is a predicate
Create Table
Integrity Constraints • The result of an SQL query is a relation
Update Table

Data
Manipulation
Language (DML):
Query Structure
Select Clause
Where Clause
From Clause

Module Summary

Database Management Systems Partha Pratim Das 08.21


Select Clause

Module 08
• The select clause lists the attributes desired in the result of a query
Partha Pratim
Das ◦ Corresponds to the projection operation of the relational algebra
Objectives & • Example: find the names of all instructors:
Outline

Outline
select name,
History of SQL
from instructor
Data Definition • NOTE: SQL names are case insensitive (that is, you may use upper-case or lower-case
Language (DDL)
Create Table letters)
Integrity Constraints
Update Table ◦ Name ≡ NAME ≡ name
Data ◦ Some people use upper case wherever we use bold font
Manipulation
Language (DML):
Query Structure
Select Clause
Where Clause
From Clause

Module Summary

Database Management Systems Partha Pratim Das 08.22


Select Clause (2)

Module 08
• SQL allows duplicates in relations as well as in query results!!!
Partha Pratim
Das • To force the elimination of duplicates, insert the keyword distinct after select
Objectives &
Outline
• Find the department names of all instructors, and remove duplicates
Outline
select distinct dept name
History of SQL
from instructor
Data Definition
Language (DDL)
• The keyword all specifies that duplicates should not be removed
Create Table select all dept name
Integrity Constraints
Update Table
from instructor
Data
Manipulation
Language (DML):
Query Structure
Select Clause
Where Clause
From Clause

Module Summary

Database Management Systems Partha Pratim Das 08.23


Select Clause (3)

Module 08
• An asterisk in the select clause denotes all attributes
Partha Pratim
Das select *
Objectives &
from instructor
Outline
• An attribute can be a literal with no from clause
Outline
select ’437’
History of SQL

Data Definition
◦ Results is a table with one column and a single row with value ’437’
Language (DDL)
Create Table
◦ Can give the column a name using:
Integrity Constraints select ’437’ as FOO
Update Table

Data • An attribute can be a literal with from clause


Manipulation
Language (DML): select ’A’
Query Structure
Select Clause
from instructor
Where Clause
From Clause
◦ Result is a table with one column and N rows (number of tuples in the instructors
Module Summary
table), each row with value ’A’

Database Management Systems Partha Pratim Das 08.24


Select Clause (4)

Module 08 The select clause can contain arithmetic expressions involving the operation, +, –, *, and
Partha Pratim /, and operating on constants or attributes of tuples
Das

Objectives &
• The query:
Outline select ID, name, salary/12
Outline from instructor
History of SQL

Data Definition
• Would return a relation that is the same as the instructor relation, except that the
Language (DDL) value of the attribute salary is divided by 12
Create Table
Integrity Constraints • Can rename “salary /12” using the as clause:
Update Table

Data
select ID, name, salary/12 as monthly salary
Manipulation
Language (DML):
Query Structure
Select Clause
Where Clause
From Clause

Module Summary

Database Management Systems Partha Pratim Das 08.25


Where Clause

Module 08
• The where clause specifies conditions that the result must satisfy
Partha Pratim
Das ◦ Corresponds to the selection predicate of the relational algebra
Objectives & • To find all instructors in Comp. Sci. dept
Outline

Outline
select name
History of SQL
from instructor
Data Definition
where dept name = ’Comp. Sci.’
Language (DDL)
Create Table • Comparison results can be combined using the logical connectives and, or, and not
Integrity Constraints
Update Table ◦ To find all instructors in Comp. Sci. dept with salary > 80000
Data select name
Manipulation
Language (DML): from instructor
Query Structure
Select Clause
where dept name = ’Comp. Sci.’ and salary > 80000
Where Clause
From Clause • Comparisons can be applied to results of arithmetic expressions
Module Summary

Database Management Systems Partha Pratim Das 08.26


From Clause

Module 08
• The from clause lists the relations involved in the query
Partha Pratim
Das ◦ Corresponds to the Cartesian product operation of the relational algebra
Objectives & • Find the Cartesian product instructor X teaches
Outline

Outline
select *
History of SQL
from instructor , teaches
Data Definition ◦ Generates every possible instructor-teaches pair, with all attributes from both
Language (DDL)
Create Table
relations
Integrity Constraints
Update Table
◦ For common attributes (for example, ID), the attributes in the resulting table are
Data
renamed using the relation name (for example, instructor.ID)
Manipulation
Language (DML): • Cartesian product not very useful directly, but useful combined with where-clause
Query Structure
Select Clause
condition (selection operation in relational algebra)
Where Clause
From Clause

Module Summary

Database Management Systems Partha Pratim Das 08.27


Cartesian Product

Module 08

Partha Pratim
Das

Objectives &
Outline

Outline

History of SQL

Data Definition
Language (DDL)
Create Table
Integrity Constraints
Update Table
valid as inst.id=teches.id

Data
Manipulation
Language (DML):
Query Structure
Select Clause
Where Clause not valid as inst.id=teches.id
From Clause

Module Summary

Database Management Systems Partha Pratim Das 08.28


Module Summary PPD

Module 08

Partha Pratim • Introduced relational query language


Das
• Familiarized with data definition and basic query structure
Objectives &
Outline

Outline

History of SQL

Data Definition
Language (DDL)
Create Table
Integrity Constraints
Update Table

Data
Manipulation
Language (DML):
Query Structure Slides used in this presentation are borrowed from http://db-book.com/ with kind
Select Clause
Where Clause
permission of the authors.
From Clause Edited and new slides are marked with “PPD”.
Module Summary

Database Management Systems Partha Pratim Das 08.29


Module 09

Partha Pratim
Das

Objectives &
Outline Database Management Systems
Additional Basic
Operations Module 09: Introduction to SQL/2
Cartesian Product
Rename AS
Operation
String Values
Order By Clause
Select Top / Fetch
Partha Pratim Das
Clause
Where Clause
Predicates
Department of Computer Science and Engineering
Duplicates
Indian Institute of Technology, Kharagpur
Module Summary
[email protected]

Database Management Systems Partha Pratim Das 09.1


Module Recap PPD

Module 09

Partha Pratim • Introduced relational query language


Das
• Familiarized with data definition and basic query structure
Objectives &
Outline

Additional Basic
Operations
Cartesian Product
Rename AS
Operation
String Values
Order By Clause
Select Top / Fetch
Clause
Where Clause
Predicates
Duplicates

Module Summary

Database Management Systems Partha Pratim Das 09.2


Module Objectives PPD

Module 09

Partha Pratim • To complete the understanding of basic query structure


Das

Objectives &
Outline

Additional Basic
Operations
Cartesian Product
Rename AS
Operation
String Values
Order By Clause
Select Top / Fetch
Clause
Where Clause
Predicates
Duplicates

Module Summary

Database Management Systems Partha Pratim Das 09.3


Module Outline PPD

Module 09

Partha Pratim • Additional Basic Operations


Das
◦ Cartesian Product
Objectives &
Outline ◦ Rename AS Operation
Additional Basic ◦ String Values
Operations
Cartesian Product
◦ Order By
Rename AS
Operation
◦ Select Top / Fetch
String Values
◦ Where Clause Predicate
Order By Clause
Select Top / Fetch ◦ Duplicates
Clause
Where Clause
Predicates
Duplicates

Module Summary

Database Management Systems Partha Pratim Das 09.4


Additional Basic Operations PPD

Module 09

Partha Pratim
Das

Objectives &
Outline

Additional Basic
Operations
Cartesian Product
Rename AS
Operation
String Values
Order By Clause
Select Top / Fetch
Clause
Where Clause
Predicates
Duplicates Additional Basic Operations
Module Summary

Database Management Systems Partha Pratim Das 09.5


Cartesian Product

Module 09

Partha Pratim • Find the Cartesian product instructor X teaches


Das
select *
Objectives &
Outline
from instructor , teaches
Additional Basic ◦ generates every possible instructor-teaches pair, with all attributes from both
Operations
Cartesian Product
relations
Rename AS
Operation
◦ For common attributes (for example, ID), the attributes in the resulting table are
String Values
renamed using the relation name (for example, instructor.ID)
Order By Clause
Select Top / Fetch
Clause
• Cartesian product not very useful directly, but useful combined with where-clause
Where Clause
Predicates
condition (selection operation in relational algebra)
Duplicates

Module Summary

Database Management Systems Partha Pratim Das 09.6


Cartesian Product

Module 09

Partha Pratim
Das

Objectives &
Outline

Additional Basic
Operations
Cartesian Product
Rename AS
Operation
String Values
Order By Clause
Select Top / Fetch
Clause
Where Clause
Predicates
Duplicates

Module Summary

Database Management Systems Partha Pratim Das 09.7


Examples PPD

Module 09 • Find the names of all instructors who have taught some course and the course id
Partha Pratim
Das
select name, course id
from instructor , teaches
Objectives &
Outline where instructor .ID = teaches.ID
Additional Basic
Operations
◦ Equi-Join, Natural Join
Cartesian Product
Rename AS
Operation
String Values
Order By Clause
Select Top / Fetch
Clause
Where Clause
Predicates
Duplicates

Module Summary

here we can see that all invalid


tuples get eliminated.
using where

Database Management Systems Partha Pratim Das 09.8


Examples PPD

Module 09

Partha Pratim • Find the names of all instructors in the Art department who have taught some course
Das
and the course id
Objectives &
Outline
select name, course id
Additional Basic
from instructor , teaches
Operations
Cartesian Product
where instructor .ID = teaches.ID and instructor.dept name = ’Art’
Rename AS
Operation
String Values
Order By Clause
Select Top / Fetch
Clause
Where Clause
Predicates
Duplicates

Module Summary

Database Management Systems Partha Pratim Das 09.9


Rename AS Operation

Module 09

Partha Pratim • The SQL allows renaming relations and attributes using the as clause:
Das
old name as new name
Objectives &
Outline • Find the names of all instructors who have a higher salary than some instructor in
Additional Basic ’Comp. Sci’.
Operations
Cartesian Product select distinct T.name
Rename AS
Operation from instructor as T, instructor as S,
String Values
Order By Clause
where T .salary > S.salary and S.dept name = ’Comp. Sci’
Select Top / Fetch
Clause • Keyword as is optional and may be omitted
Where Clause
Predicates instructor as T ≡ instructor T
Duplicates
here instructor instructor happend
Module Summary

renamed one instructor table as T and other as S


and took there cartesian product.

Database Management Systems Partha Pratim Das 09.10


Cartesian Product Example

Module 09

Partha Pratim • Relation emp super


Das

Objectives &
Outline

Additional Basic
Operations
Cartesian Product
Rename AS
Operation
String Values
Order By Clause
• Find the supervisor of “Bob”
Select Top / Fetch
Clause • Find the supervisor of the supervisor of “Bob”
Where Clause
Predicates • Find ALL the supervisors (direct and indirect) of “Bob”
Duplicates

Module Summary

Database Management Systems Partha Pratim Das 09.11


String Operations

Module 09

Partha Pratim • SQL includes a string-matching operator for comparisons on character strings. The
Das
operator like uses patterns that are described using two special characters:
Objectives &
Outline ◦ percent ( % ). The % character matches any substring
Additional Basic ◦ underscore ( ). The character matches any character
Operations
Cartesian Product • Find the names of all instructors whose name includes the substring “dar”
Rename AS
Operation select name
String Values
Order By Clause
from instructor
Select Top / Fetch
Clause
where name like ’%dar %’
Where Clause
Predicates • Match the string “100%”
Duplicates
like ’100%’ escape ’\’
Module Summary
• in that above we use backslash (\) as the escape character

Database Management Systems Partha Pratim Das 09.12


String Operations (2)

Module 09

Partha Pratim • Patterns are case sensitive


Das
• Pattern matching examples:
Objectives &
Outline ◦ ’Intro%’ matches any string beginning with “Intro”
Additional Basic
Operations
◦ ’%Comp%’ matches any string containing “Comp” as a substring
Cartesian Product ◦ ’ ’ matches any string of exactly three characters
Rename AS
Operation ◦ ’ %’ matches any string of at least three characters
String Values
Order By Clause • SQL supports a variety of string operations such as
Select Top / Fetch
Clause
Where Clause
◦ concatenation (using “k”)
Predicates
◦ converting from upper to lower case (and vice versa)
Duplicates

Module Summary
◦ finding string length, extracting substrings, etc.

Database Management Systems Partha Pratim Das 09.13


Ordering the Display of Tuples

Module 09

Partha Pratim • List in alphabetic order the names of all instructors


Das
select distinct name
Objectives &
Outline
from instructor
Additional Basic
order by name
Operations
Cartesian Product • We may specify desc for descending order or asc for ascending order, for each
Rename AS
Operation attribute; ascending order is the default.
String Values
Order By Clause
◦ Example: order by name desc
Select Top / Fetch
Clause • Can sort on multiple attributes
Where Clause
Predicates
◦ Example: order by dept name, name
Duplicates

Module Summary

Database Management Systems Partha Pratim Das 09.14


Selecting Number of Tuples in Output

Module 09

Partha Pratim • The Select Top clause is used to specify the number of records to return
Das
• The Select Top clause is useful on large tables with thousands of records. Returning a
Objectives &
Outline large number of records can impact performance
Additional Basic select top 10 distinct name
Operations
Cartesian Product from instructor
Rename AS
Operation • Not all database systems support the SELECT TOP clause.
String Values
Order By Clause ◦ SQL Server & MS Access support select top
Select Top / Fetch
Clause ◦ MySQL supports the limit clause
Where Clause
Predicates ◦ Oracle uses fetch first n rows only and rownum
Duplicates

Module Summary
select distinct name
from instructor
order by name
fetch first 10 rows only

Database Management Systems Partha Pratim Das 09.15


Where Clause Predicates

Module 09

Partha Pratim • SQL includes a between comparison operator


Das
• Example: Find the names of all instructors with salary between $90, 000 and $100, 000
Objectives &
Outline (that is, ≥ $90, 000 and ≤ $100, 000)
Additional Basic select name
Operations
Cartesian Product from instructor
Rename AS
Operation where salary between 90000 and 100000
String Values
Order By Clause • Tuple comparison
Select Top / Fetch
Clause select name, course id
Where Clause
Predicates from instructor , teaches
Duplicates
where (instructor.ID, dept name) = (teaches.ID, ’Biology’);
Module Summary

Database Management Systems Partha Pratim Das 09.16


In Operator

Module 09

Partha Pratim • The in operator allows you to specify multiple values in a where clause
Das
• The in operator is a shorthand for multiple or conditions
Objectives &
Outline select name
Additional Basic from instructor
Operations
Cartesian Product where dept name in (’Comp. Sci.’, ’Biology’)
Rename AS
Operation
String Values
Order By Clause
Select Top / Fetch
Clause
Where Clause
Predicates
Duplicates

Module Summary

Database Management Systems Partha Pratim Das 09.17


Duplicates

Module 09

Partha Pratim • In relations with duplicates, SQL can define how many copies of tuples appear in the
Das
result
Objectives &
Outline • Multiset versions of some of the relational algebra operators – given multiset relations
Additional Basic r1 and r2 :
Operations
Cartesian Product a) σθ (r1 ): If there are c1 copies of tuple t1 in r1 , and t1 satisfies selections σθ , then
Rename AS
Operation there are c1 copies of t1 in σθ (r1 )
String Values
Order By Clause
b) ΠA (r ): For each copy of tuple t1 in r1 , there is a copy of tuple ΠA (t1 ) in ΠA (r1 )
Select Top / Fetch
Clause
where ΠA (t1 ) denotes the projection of the single tuple t1
Where Clause
Predicates
c) r1 x r2 : If there are c1 copies of tuple t1 in r1 and c2 copies of tuple t2 in r2 , there
Duplicates are c1 x c2 copies of the tuple t1 .t2 in r1 x r2
Module Summary

Database Management Systems Partha Pratim Das 09.18


Duplicates (2)

Module 09

Partha Pratim • Example: Suppose multiset relations r1 (A, B) and r2 (C ) are as follows:
Das
r1 = {(1, a)(2, a)} r 2 = {(2), (3), (3)}
Objectives &
Outline • Then ΠB (r1 ) would be {(a), (a)}, while ΠB (r1 ) x r2 would be
Additional Basic {(a, 2), (a, 2), (a, 3), (a, 3), (a, 3), (a, 3)}
Operations
Cartesian Product • SQL duplicate semantics:
Rename AS
Operation select A1 , A2 , . . . , An
String Values
Order By Clause from r1 , r2 , . . . , rm
Select Top / Fetch
Clause where P
Where Clause
Predicates is equivalent to the multiset version of the expression:
Duplicates

Module Summary ΠA1 ,A2 ,...,An (σP (r1 × r2 × . . . × rm ))

Database Management Systems Partha Pratim Das 09.19


Module Summary PPD

Module 09

Partha Pratim • Completed the understanding of basic query structure


Das

Objectives &
Outline

Additional Basic
Operations
Cartesian Product
Rename AS
Operation
String Values
Order By Clause
Select Top / Fetch
Clause
Where Clause
Predicates
Slides used in this presentation are borrowed from http://db-book.com/ with kind
Duplicates permission of the authors.
Module Summary Edited and new slides are marked with “PPD”.

Database Management Systems Partha Pratim Das 09.20


Module 10

Partha Pratim
Das

Objectives &
Outline Database Management Systems
Set Operations
Module 10: Introduction to SQL/3
Null Values
Three Valued Logic

Aggregate
Functions
Group By
Having
Partha Pratim Das
Null Values

Module Summary Department of Computer Science and Engineering


Indian Institute of Technology, Kharagpur

[email protected]

Database Management Systems Partha Pratim Das 10.1


Module Recap PPD

Module 10

Partha Pratim • Completed the understanding of basic query structure


Das

Objectives &
Outline

Set Operations

Null Values
Three Valued Logic

Aggregate
Functions
Group By
Having
Null Values

Module Summary

Database Management Systems Partha Pratim Das 10.2


Module Objectives PPD

Module 10

Partha Pratim • To familiarize with set operations, null values and aggregation
Das

Objectives &
Outline

Set Operations

Null Values
Three Valued Logic

Aggregate
Functions
Group By
Having
Null Values

Module Summary

Database Management Systems Partha Pratim Das 10.3


Module Outline PPD

Module 10

Partha Pratim • Set Operations: union, intersect, except


Das
• Null Values
Objectives &
Outline • Aggregate Functions: avg, min, max, sum, and count
Set Operations
◦ Group By
Null Values
Three Valued Logic ◦ Having
Aggregate ◦ Null Values
Functions
Group By
Having
Null Values

Module Summary

Database Management Systems Partha Pratim Das 10.4


Set Operations PPD

Module 10

Partha Pratim
Das

Objectives &
Outline

Set Operations

Null Values
Three Valued Logic

Aggregate
Functions
Group By
Having
Null Values

Module Summary
Set Operations

Database Management Systems Partha Pratim Das 10.5


Set Operations

Module 10

Partha Pratim • Find courses that ran in Fall 2009 or in Spring 2010
Das
(select course id from section where sem = ’Fall’ and year = 2009)
Objectives &
Outline
union
Set Operations
(select course id from section where sem = ’Spring’ and year = 2010)
Null Values • Find courses that ran in Fall 2009 and in Spring 2010
Three Valued Logic

Aggregate
(select course id from section where sem = ’Fall’ and year = 2009)
Functions intersect
Group By
Having (select course id from section where sem = ’Spring’ and year = 2010)
Null Values

Module Summary
• Find courses that ran in Fall 2009 but not in Spring 2010
(select course id from section where sem = ’Fall’ and year = 2009)
except
(select course id from section where sem = ’Spring’ and year = 2010)

Database Management Systems Partha Pratim Das 10.6


Set Operations (2)

Module 10

Partha Pratim • Find the salaries of all instructors that are less than the largest salary
Das
select distinct T .salary
Objectives &
Outline
from instructor as T, instructor as S
Set Operations
where T .salary < S.salary
Null Values • Find all the salaries of all instructors
Three Valued Logic

Aggregate
select distinct salary
Functions from instructor
Group By
Having • Find the largest salary of all instructors
Null Values

Module Summary
(select “second query” )
except
(select “first query”)

Database Management Systems Partha Pratim Das 10.7


Set Operations (3)

Module 10

Partha Pratim • Set operations union, intersect, and except


Das
◦ Each of the above operations automatically eliminates duplicates
Objectives &
Outline • To retain all duplicates use the corresponding multiset versions union all, intersect all,
Set Operations and except all.
Null Values
Three Valued Logic • Suppose a tuple occurs m times in r and n times in s, then, it occurs:
Aggregate
Functions
◦ m + n times in r union all s
Group By ◦ min(m, n) times in r intersect all s
Having
Null Values
◦ max(0, m − n) times in r except all s
Module Summary

Database Management Systems Partha Pratim Das 10.8


Null Values PPD

Module 10

Partha Pratim
Das

Objectives &
Outline

Set Operations

Null Values
Three Valued Logic

Aggregate
Functions
Group By
Having
Null Values

Module Summary
Null Values

Database Management Systems Partha Pratim Das 10.9


Null Values

Module 10

Partha Pratim • It is possible for tuples to have a null value, denoted by null, for some of their attributes
Das
• null signifies an unknown value or that a value does not exist
Objectives &
Outline • The result of any arithmetic expression involving null is null
Set Operations
◦ Example: 5 + null returns null
Null Values
Three Valued Logic • The predicate is null can be used to check for null values
Aggregate
Functions ◦ Example: Find all instructors whose salary is null
Group By
Having
select name
Null Values from instructor
Module Summary where salary is null
• It is not possible to test for null values with comparison operators, such as =, <, or <>
We need to use the is null and is not null operators instead

Database Management Systems Partha Pratim Das 10.10


Null Values (2): Three Valued Logic

Module 10

Partha Pratim • Three values – true, false, unknown


Das
• Any comparison with null returns unknown
Objectives &
Outline ◦ Example: 5 < null or null <> null or null = null
Set Operations
• Three-valued logic using the value unknown:
Null Values
Three Valued Logic ◦ OR: (unknown or true) = true,
Aggregate
Functions
(unknown or false) = unknown
Group By (unknown or unknown) = unknown
Having
Null Values
◦ AND: (true and unknown) = unknown,
Module Summary (false and unknown) = false,
(unknown and unknown) = unknown
◦ NOT: (not unknown) = unknown
◦ “P is unknown“ evaluates to true if predicate P evaluates to unknown
• Result of where clause predicate is treated as false if it evaluates to unknown

Database Management Systems Partha Pratim Das 10.11


Aggregate Functions PPD

Module 10

Partha Pratim
Das

Objectives &
Outline

Set Operations

Null Values
Three Valued Logic

Aggregate
Functions
Group By
Having
Null Values

Module Summary
Aggregate Functions

Database Management Systems Partha Pratim Das 10.12


Aggregate Functions

Module 10

Partha Pratim • These functions operate on the multiset of values of a column of a relation, and return
Das
a value
Objectives &
Outline avg: average value
Set Operations min: minimum value
Null Values max: maximum value
Three Valued Logic
sum: sum of values
Aggregate
Functions count: number of values
Group By
Having
Null Values

Module Summary

Database Management Systems Partha Pratim Das 10.13


Aggregate Functions (2)

Module 10

Partha Pratim • Find the average salary of instructors in the Computer Science department
Das
select avg (salary )
Objectives &
Outline
from instructor
Set Operations
where dept name = ’Comp. Sci’;
Null Values • Find the total number of instructors who teach a course in the Spring 2010 semester
Three Valued Logic

Aggregate
select count (distinct ID)
Functions from teaches
Group By
Having where semester = ’Spring’ and year = 2010;
Null Values

Module Summary
• Find the number of tuples in the course relation
select count (*)
from courses;

Database Management Systems Partha Pratim Das 10.14


Aggregate Functions (3): Group By

Module 10

Partha Pratim
• Find the average salary of instructors in each department
Das
select dept name, avg(salary ) as avg salary
Objectives & from instructor
Outline
group by dept name;
Set Operations

Null Values
Three Valued Logic

Aggregate
Functions
Group By
Having
Null Values

Module Summary

Database Management Systems Partha Pratim Das 10.15


Aggregate Functions (4): Group By

Module 10

Partha Pratim • Attributes in select clause outside of aggregate functions must appear in group by list
Das
/* erroneous query */
Objectives &
Outline
select dept name, ID, avg(salary )
Set Operations
from instructor
Null Values group by dept name;
Three Valued Logic

Aggregate
Functions
Group By
Having
Null Values

Module Summary

Database Management Systems Partha Pratim Das 10.16


Aggregate Functions (5): Having Clause

Module 10

Partha Pratim • Find the names and average salaries of all departments whose average salary is greater
Das
than 42000
Objectives &
Outline
select dept name, avg(salary )
Set Operations
from instructor
Null Values group by dept name
Three Valued Logic
having avg(salary ) > 42000;
Aggregate
Functions Note: predicates in the having clause are applied after the formation of groups whereas
Group By
Having predicates in the where clause are applied before forming groups
Null Values

Module Summary

Database Management Systems Partha Pratim Das 10.17


Null Values and Aggregates

Module 10

Partha Pratim • Total all salaries


Das
select sum (salary )
Objectives &
Outline
from instructor ;
Set Operations ◦ Above statement ignores null amounts
Null Values ◦ Result is null if there is no non-null amount
Three Valued Logic

Aggregate
• All aggregate operations except count(*) ignore tuples with null values on the
Functions
Group By
aggregated attributes
Having
Null Values
• What if collection has only null values?
Module Summary ◦ count returns 0
◦ all other aggregates return null

Database Management Systems Partha Pratim Das 10.18


Module Summary PPD

Module 10

Partha Pratim • Completed the understanding of set operations, null values, and aggregation
Das

Objectives &
Outline

Set Operations

Null Values
Three Valued Logic

Aggregate
Functions
Group By
Having
Null Values
Slides used in this presentation are borrowed from http://db-book.com/ with kind
Module Summary
permission of the authors.
Edited and new slides are marked with “PPD”.

Database Management Systems Partha Pratim Das 10.19


Module 11

Partha Pratim
Das

Week Recap

Objectives &
Database Management Systems
Outline
Module 11: SQL Examples
SQL Examples
SELECT
Cartesian Product /
AS
WHERE: AND / OR
String Partha Pratim Das
ORDER BY
IN
Set Department of Computer Science and Engineering
UNION Indian Institute of Technology, Kharagpur
INTERSECT
EXCEPT [email protected]
Aggregation
AVG
MIN
MAX
COUNT
SUM

Module Summary Database Management Systems Partha Pratim Das 11.1


Week Recap PPD

Module 11

Partha Pratim • Basic notions of Relational Database Models


Das
◦ Attributes and their types
Week Recap
◦ Mathematical structure of relational model
Objectives &
Outline ◦ Schema and Instance
SQL Examples ◦ Keys, primary as well as foreign
SELECT
Cartesian Product /
AS
• Relational algebra with operators
WHERE: AND / OR
String
• Relational query language
ORDER BY
IN
◦ DDL (Data Definition)
Set ◦ DML (Basic Query Structure)
UNION
INTERSECT • Detailed understanding of basic query structure
EXCEPT
Aggregation • Set operations, null values, and aggregation
AVG
MIN
MAX
COUNT
SUM

Module Summary Database Management Systems Partha Pratim Das 11.2


Module Objectives PPD

Module 11

Partha Pratim • To recap various basic SQL features through example workout
Das

Week Recap

Objectives &
Outline

SQL Examples
SELECT
Cartesian Product /
AS
WHERE: AND / OR
String
ORDER BY
IN
Set
UNION
INTERSECT
EXCEPT
Aggregation
AVG
MIN
MAX
COUNT
SUM

Module Summary Database Management Systems Partha Pratim Das 11.3


Module Outline PPD

Module 11

Partha Pratim • Examples of basic SQL


Das

Week Recap

Objectives &
Outline

SQL Examples
SELECT
Cartesian Product /
AS
WHERE: AND / OR
String
ORDER BY
IN
Set
UNION
INTERSECT
EXCEPT
Aggregation
AVG
MIN
MAX
COUNT
SUM

Module Summary Database Management Systems Partha Pratim Das 11.4


Select distinct PPD

Module 11

Partha Pratim • From the classroom relation in the figure, find the names of buildings in which every
Das
individual classroom has capacity less than 100 (removing the duplicates).
Week Recap

Objectives & ◦ Query:


Outline

SQL Examples
select distinct building
SELECT from classroom
Cartesian Product /
AS where capacity < 100;
WHERE: AND / OR
String ◦ Output :
ORDER BY
IN building
Set
Painter
UNION Figure: classroom relation
INTERSECT
Taylor
EXCEPT Watson
Aggregation
AVG
MIN
MAX
COUNT
SUM

Module Summary Database Management Systems Partha Pratim Das 11.5


Select all PPD

Module 11

Partha Pratim • From the classroom relation in the figure, find the names of buildings in which every
Das
individual classroom has capacity less than 100 (without removing the duplicates).
Week Recap

Objectives & ◦ Query:


Outline

SQL Examples
select all building
SELECT from classroom
Cartesian Product /
AS where capacity < 100;
WHERE: AND / OR
String ◦ Output:
ORDER BY
IN
building
Set Painter
UNION Figure: classroom relation Taylor
INTERSECT
Watson
EXCEPT
Watson
Aggregation
AVG
MIN • Note that duplicate retention is the default and hence it is a common practice to skip
MAX
COUNT
all immediately after select.
SUM

Module Summary Database Management Systems Partha Pratim Das 11.6


Cartesian Product PPD

Module 11
• Find the list of all students of departments which have a
Partha Pratim
Das budget < $0.1million
Week Recap
select name, budget
Objectives & from student, department name budget
Outline
where student.dept name = department.dept name and Brandt 50000.00
SQL Examples Peltier 70000.00
SELECT
budget < 100000;
Cartesian Product /
Levy 70000.00
AS • The above query first generates every possible student- Sanchez 80000.00
WHERE: AND / OR
String
department pair, which is the Cartesian product of stu- Snow 70000.00
ORDER BY
dent and department. Then, it filters all the rows with Aoi 85000.00
IN
Bourikas 85000.00
Set student.dept name = department.dept name and budget <
UNION
Tanaka 90000.00
INTERSECT
100000.
EXCEPT
Aggregation
• The common attribute dept name in the resulting table are
AVG
renamed using the relation name - student.dept name and
MIN
MAX department.dept name)
COUNT
SUM

Module Summary Database Management Systems Partha Pratim Das 11.7


Rename AS Operation PPD

Module 11
• The same query in the previous slide can be framed by
Partha Pratim
Das renaming the tables as shown below.
Week Recap
select S.name as studentname, budget as deptbud-
studentname deptbudget
Objectives & get
Outline Brandt 50000.00
from student as S, department as D Peltier 70000.00
SQL Examples
SELECT
where S.dept name = D.dept name and budget < Levy 70000.00
Cartesian Product /
AS
100000; Sanchez 80000.00
WHERE: AND / OR Snow 70000.00
String
• The above query renames the relation student as S and Aoi 85000.00
ORDER BY
IN
the relation department as D Bourikas 85000.00
Set
• It also displays the attribute name as StudentName and Tanaka 90000.00
UNION
INTERSECT budget as DeptBudget.
EXCEPT
Aggregation • Note that the budget attribute does not have any prefix
AVG
MIN
because it occurs only in the department relation.
MAX
COUNT
SUM

Module Summary Database Management Systems Partha Pratim Das 11.8


Where: AND and OR PPD

Module 11
• From the instructor and department relations in the figure, find out the names of all instructors whose
Partha Pratim department is Finance or whose department is in any of the following buildings: Watson, Taylor.
Das
instructor ◦ Query:
Week Recap
select name
Objectives & from instructor I, department D
Outline
where D.dept name = I.dept name
SQL Examples
SELECT and (I.dept name = ’Finance’
Cartesian Product /
AS
or building in (‘Watson’,‘Taylor’));
WHERE: AND / OR
String
◦ Output:
ORDER BY
name
IN
Set Srinivasan
UNION department Wu
INTERSECT Einstein
EXCEPT Gold
Aggregation
Katz
AVG
Singh
MIN
MAX
Crick
COUNT Brandt
SUM Kim
Module Summary Database Management Systems Partha Pratim Das 11.9
String Operations PPD

Module 11
• From the course relation in the figure, find the titles of all courses whose course id has
Partha Pratim
Das three alphabets indicating the department.
◦ Query:
Week Recap
select title
Objectives &
Outline
from course
where course id like ‘ -%’;
SQL Examples
SELECT ◦ Output:
Cartesian Product /
AS
title
WHERE: AND / OR
String Intro. to Biology
ORDER BY Genetics
IN Computational Biology
Set Investment Banking
UNION
World History
INTERSECT Figure: course relation Physical Principles
EXCEPT
Aggregation
AVG • The course id of each department has either 2 or 3 alphabets in the beginning, followed
MIN
MAX
by a hyphen and then followed by a 3-digit number. The above query returns the
COUNT names of those departments that have 3 alphabets in the beginning.
SUM

Module Summary Database Management Systems Partha Pratim Das 11.10


Order By PPD

Module 11
• From the student relation in the figure, obtain the list of all students in alphabetic order of departments
Partha Pratim and within each department, in decreasing order of total credits.
Das
◦ Query:
Week Recap
select name, dept name, tot cred
Objectives &
Outline from student
SQL Examples
order by dept name ASC, tot cred DESC;
SELECT
Cartesian Product /
◦ Output:
AS
name dept name tot cred
WHERE: AND / OR
Tanaka Biology 120
String
Zhang Comp. Sci. 102
ORDER BY
Brown Comp. Sci. 58
IN Williams Comp. Sci. 54
Set Shankar Comp. Sci. 32
UNION
Figure: student relation Bourikas Elec. Eng. 98
INTERSECT Aoi Elec. Eng. 60
Chavez Finance 110
EXCEPT
Aggregation
◦ The list is first sorted in alphabetic order Brandt History 80
Sanchez Music 38
AVG of dept name. Peltier Physics 56
MIN
MAX
◦ Within each dept, it is sorted in decreas- Levy
Snow
Physics
Physics
46
0
COUNT ing order of total credits.
SUM

Module Summary Database Management Systems Partha Pratim Das 11.11


In Operator PPD

Module 11

Partha Pratim • From the teaches relation in the figure, find the IDs of all courses taught in the Fall or
Das
Spring of 2018.
Week Recap ◦ Query:
Objectives &
Outline
select course id
SQL Examples from teaches
SELECT
Cartesian Product /
where semester in (‘Fall’,‘Spring’)
AS
WHERE: AND / OR
and year =2018;
String
ORDER BY
◦ Output:
IN course id
Set
UNION
CS-315
INTERSECT FIN-201
EXCEPT Figure: teaches relation MU-199
Aggregation HIS-351
AVG CS-101
MIN Note: We can use distinct to remove CS-319
MAX
COUNT duplicates. CS-319
SUM

Module Summary Database Management Systems Partha Pratim Das 11.12


Set Operations: union PPD

Module 11 • For the same question in the previous slide, we can find the solution using union
Partha Pratim operator as follows.
Das
◦ Query:
Week Recap select course id
Objectives & from teaches
Outline where semester=‘Fall’
SQL Examples and year =2018
SELECT
Cartesian Product / union
AS
select course id
WHERE: AND / OR
String
from teaches
ORDER BY where semester=‘Spring’
IN
Set
and year =2018
UNION ◦ Output:
INTERSECT
EXCEPT Figure: teaches relation course id
Aggregation CS-101
AVG
CS-315
MIN ◦ Note that union removes all duplicates. If we use union CS-319
MAX all instead of union, we get the same set of tuples as FIN-201
COUNT
SUM
in previous slide. HIS-351
Module Summary
MU-199
Database Management Systems Partha Pratim Das 11.13
Set Operations (2): intersect PPD

Module 11

Partha Pratim • From the instructor relation in the figure, find the names of all instructors who taught
Das
in either the Computer Science department or the Finance department and whose salary
Week Recap is < 80000.
Objectives & ◦ Query:
Outline
select name
SQL Examples from instructor
SELECT
Cartesian Product /
where dept name in (‘Comp. Sci.’,‘Finance’)
AS intersect
WHERE: AND / OR
String
select name
ORDER BY from instructor
IN where salary < 80000;
Set
UNION ◦ Output:
INTERSECT
name
EXCEPT Figure: instructor relation
Aggregation Srinivasan
AVG Katz
MIN
MAX • Note that the same can be achieved using the query:
COUNT
select name from instructor where dept name in(‘Comp. Sci.’, ‘Finance’) and salary < 80000;
SUM

Module Summary Database Management Systems Partha Pratim Das 11.14


Set Operations (3): except PPD

Module 11
• From the instructor relation in the figure, find the names of all instructors who taught
Partha Pratim
Das
in either the Computer Science department or the Finance department and whose salary
is either ≥ 90000 or ≤ 70000.
Week Recap

Objectives &
Outline
◦ Query:
SQL Examples select name
SELECT from instructor
Cartesian Product /
AS
where dept name in (‘Comp. Sci.’,‘Finance’)
WHERE: AND / OR except
String select name
ORDER BY
IN
from instructor
Set where salary < 90000 and salary > 70000;
Figure: instructor relation
UNION
INTERSECT
◦ Output:
name
EXCEPT
Aggregation
◦ Note that the same can be achieved using the
query given below: Srinivasan
AVG
MIN
Brandt
select name from instructor Wu
MAX
COUNT
where dept name in(‘Comp. Sci.’, ‘Finance’)
SUM and (salary >= 90000 or salary <= 70000);
Module Summary Database Management Systems Partha Pratim Das 11.15
Aggregate functions: avg PPD

Module 11

Partha Pratim • From the classroom relation given in the figure, find the names and the average
Das
capacity of each building whose average capacity is greater than 25.
Week Recap

Objectives & ◦ Query:


Outline
select building, avg (capacity )
SQL Examples
SELECT from classroom
Cartesian Product /
AS group by building
WHERE: AND / OR
String
having avg (capacity ) > 25;
ORDER BY
IN
◦ Output:
Set building avg
UNION
Figure: classroom relation
INTERSECT
Taylor 70.00
EXCEPT
Packard 500.00
Aggregation Watson 40.00
AVG
MIN
MAX
COUNT
SUM

Module Summary Database Management Systems Partha Pratim Das 11.16


Aggregate functions (2): min PPD

Module 11

Partha Pratim • From the instructor relation given in the figure, find the least salary drawn by any
Das
instructor among all the instructors.
Week Recap

Objectives &
Outline

SQL Examples
SELECT
◦ Query:
Cartesian Product /
AS select min(salary ) as least salary
WHERE: AND / OR
String
from instructor ;
ORDER BY
IN
◦ Output:
Set least salary
UNION 40000.00
INTERSECT
EXCEPT
Aggregation Figure: instructor relation
AVG
MIN
MAX
COUNT
SUM

Module Summary Database Management Systems Partha Pratim Das 11.17


Aggregate functions (3): max PPD

Module 11

Partha Pratim • From the student relation given in the figure, find the maximum credits obtained by
Das
any student among all the students.
Week Recap

Objectives &
Outline

SQL Examples
SELECT ◦ Query:
Cartesian Product /
AS select max(tot cred) as max credits
WHERE: AND / OR
String from student;
ORDER BY
IN ◦ Output:
Set
UNION
max credits
INTERSECT
120
EXCEPT
Aggregation
AVG Figure: student relation
MIN
MAX
COUNT
SUM

Module Summary Database Management Systems Partha Pratim Das 11.18


Aggregate functions (4): count PPD

Module 11

Partha Pratim • From the section relation given in the figure, find the number of courses run in each
Das
building.
Week Recap

Objectives &
Outline
◦ Query:
SQL Examples
select building,
SELECT
Cartesian Product / count(course id) as course count
AS
WHERE: AND / OR from section
String group by building ;
ORDER BY
IN ◦ Output:
Set
building course count
UNION
INTERSECT Taylor 5
EXCEPT Packard 4
Aggregation Painter 3
AVG
Watson 3
MIN
MAX
Figure: section relation
COUNT
SUM

Module Summary Database Management Systems Partha Pratim Das 11.19


Aggregate functions (5): sum PPD

Module 11

Partha Pratim • From the course relation given in the figure, find the total credits offered by each
Das
department.
Week Recap

Objectives &
◦ Query:
Outline select dept name,
SQL Examples
sum(credits) as sum credits
SELECT
Cartesian Product / from course
AS
WHERE: AND / OR
group by dept name;
String ◦ Output:
ORDER BY
IN dept name sum credits
Set Finance 3
UNION
History 3
INTERSECT
Physics 4
EXCEPT
Aggregation
Music 3
AVG Comp. Sci. 17
MIN Figure: course relation Biology 11
MAX Elec. Eng. 3
COUNT
SUM

Module Summary Database Management Systems Partha Pratim Das 11.20


Module Summary PPD

Module 11

Partha Pratim
• SQL Examples have been practiced for
Das
◦ Select
Week Recap ◦ Cartesian Product / as
Objectives & ◦ Where: and / or
Outline

SQL Examples
◦ String Matching
SELECT ◦ Order by
Cartesian Product /
AS ◦ in
WHERE: AND / OR
String
◦ Set Operations: union, intersect, except
ORDER BY ◦ Aggregate Functions: avg, min, max, count, sum
IN
Set
UNION
INTERSECT
EXCEPT
Aggregation
AVG
MIN
MAX
COUNT
SUM

Module Summary Database Management Systems Partha Pratim Das 11.21


Module 12

Partha Pratim
Das

Objectives &
Outline Database Management Systems
Nested
Subqueries Module 12: Intermediate SQL/1
Subqueries in the
Where Clause
Subqueries in the
From Clause
Subqueries in the
Select Clause
Partha Pratim Das
Modifications of
the Database

Module Summary
Department of Computer Science and Engineering
Indian Institute of Technology, Kharagpur

[email protected]

Database Management Systems Partha Pratim Das 12.1


Module Recap PPD

Module 12

Partha Pratim • SQL Examples Practiced


Das

Objectives &
Outline

Nested
Subqueries
Subqueries in the
Where Clause
Subqueries in the
From Clause
Subqueries in the
Select Clause

Modifications of
the Database

Module Summary

Database Management Systems Partha Pratim Das 12.2


Module Objectives PPD

Module 12

Partha Pratim • To understand nested subquery in SQL


Das
• To understand processes for data modification
Objectives &
Outline

Nested
Subqueries
Subqueries in the
Where Clause
Subqueries in the
From Clause
Subqueries in the
Select Clause

Modifications of
the Database

Module Summary

Database Management Systems Partha Pratim Das 12.3


Module Outline PPD

Module 12

Partha Pratim • Nested Subqueries


Das
• Modifications of the Database
Objectives &
Outline

Nested
Subqueries
Subqueries in the
Where Clause
Subqueries in the
From Clause
Subqueries in the
Select Clause

Modifications of
the Database

Module Summary

Database Management Systems Partha Pratim Das 12.4


Nested Subqueries PPD

Module 12

Partha Pratim
Das

Objectives &
Outline

Nested
Subqueries
Subqueries in the
Where Clause
Subqueries in the
From Clause
Subqueries in the
Select Clause

Modifications of
the Database

Module Summary Nested Subqueries

Database Management Systems Partha Pratim Das 12.5


Nested Subqueries

Module 12

Partha Pratim • SQL provides a mechanism for the nesting of subqueries


Das
• A subquery is a select-from-where expression that is nested within another query
Objectives &
Outline • The nesting can be done in the following SQL query
Nested
Subqueries
select A1 , A2 , . . . , An
Subqueries in the
Where Clause
from r1 , r2 , . . . , rm
Subqueries in the
From Clause
where P
Subqueries in the
Select Clause
as follows:
Modifications of ◦ Ai can be replaced by a subquery that generates a single value
the Database
◦ ri can be replaced by any valid subquery
Module Summary
◦ P can be replaced with an expression of the form:
B <operation> (subquery)
where B is an attribute and <operation> to be defined later

Database Management Systems Partha Pratim Das 12.6


Module 12

Partha Pratim
Das

Objectives &
Outline

Nested
Subqueries
Subqueries in the
Where Clause
Subqueries in the
From Clause
Subqueries in the
Select Clause

Modifications of
the Database

Module Summary Subqueries in the Where Clause

Database Management Systems Partha Pratim Das 12.7


Subqueries in the Where Clause

Module 12

Partha Pratim • Typical use of subqueries is to perform tests:


Das
◦ For set membership
Objectives &
Outline ◦ For set comparisons
Nested ◦ For set cardinality
Subqueries
Subqueries in the
Where Clause
Subqueries in the
From Clause
Subqueries in the
Select Clause

Modifications of
the Database

Module Summary

Database Management Systems Partha Pratim Das 12.8


Set Membership

Module 12

Partha Pratim • Find courses offered in Fall 2009 and in Spring 2010. (intersect example)
Das
select distinct course id
Objectives &
Outline
from section
Nested
where semester =’Fall’ and year = 2009 and
Subqueries
Subqueries in the
course id in (select course id
Where Clause
Subqueries in the
from section
From Clause
Subqueries in the
where semester =’Spring’ and year = 2010);
Select Clause
• Find courses offered in Fall 2009 but not in Spring 2010. (except example)
Modifications of
the Database select distinct course id
Module Summary from section
where semester =’Fall’ and year = 2009 and
course id not in (select course id
from section
where semester =’Spring’ and year = 2010);

Database Management Systems Partha Pratim Das 12.9


Set Membership (2)

Module 12

Partha Pratim • Find the total number of (distinct) students who have taken course sections taught by
Das
the instructor with ID 10101
Objectives &
Outline
select count (distinct ID)
Nested
from takes
Subqueries
Subqueries in the
where (course id, sec id, semester, year ) in
Where Clause
Subqueries in the
(select course id, sec id, semester, year
From Clause
Subqueries in the
from teaches
Select Clause
where teaches.ID = 10101);
Modifications of
the Database • Note: Above query can be written in simpler manner. The formulation above is simply
Module Summary to illustrate SQL features.

Database Management Systems Partha Pratim Das 12.10


Set Comparison – “some” Clause

Module 12

Partha Pratim • Find names of instructors with salary greater than that of some (at least one) instructor
Das
in the Biology department
Objectives &
Outline
select distinct T.name
Nested
from instructor as T, instructor as S
Subqueries
Subqueries in the
where T .salary > S.salary and S.dept name = ’Biology’;
Where Clause
Subqueries in the • Same query using some clause
From Clause
Subqueries in the select name
Select Clause
from instructor
Modifications of
the Database where salary > some (select salary
Module Summary from instructor
where dept name = ’Biology’);

Database Management Systems Partha Pratim Das 12.11


Definition of ”some” Clause

Module 12

Partha Pratim • F <comp> some r ⇔ ∃t ∈ r such that (F <comp> t )


Das
where <comp> can be: <, ≤, >, ≥, =, 6=
Objectives &
Outline • some represents existential quantification
Nested
Subqueries
Subqueries in the
Where Clause
Subqueries in the
From Clause
Subqueries in the
Select Clause

Modifications of
the Database

Module Summary

Database Management Systems Partha Pratim Das 12.12


Set Comparison – “all” Clause

Module 12

Partha Pratim • Find the names of all instructors whose salary is greater than the salary of all
Das
instructors in the Biology department
Objectives &
Outline
select name
Nested
from instructor
Subqueries
Subqueries in the
where salary > all (select salary
Where Clause
Subqueries in the
from instructor
From Clause
Subqueries in the
where dept name = ’Biology’);
Select Clause

Modifications of
the Database

Module Summary

Database Management Systems Partha Pratim Das 12.13


Definition of “all” Clause

Module 12

Partha Pratim • F <comp> all r ⇔ ∀t ∈ r such that (F <comp> t )


Das
Where <comp> can be: <, ≤, >, ≥, =, 6=
Objectives &
Outline • all represents universal quantification
Nested
Subqueries
Subqueries in the
Where Clause
Subqueries in the
From Clause
Subqueries in the
Select Clause

Modifications of
the Database

Module Summary

Database Management Systems Partha Pratim Das 12.14


Test for Empty Relations: “exists”

Module 12

Partha Pratim • The exists construct returns the value true if the argument subquery is nonempty
Das
◦ exists r ⇔ r 6= ∅
Objectives &
Outline ◦ not exists r ⇔ r = ∅
Nested
Subqueries
Subqueries in the
Where Clause
Subqueries in the
From Clause
Subqueries in the
Select Clause

Modifications of
the Database

Module Summary

Database Management Systems Partha Pratim Das 12.15


Use of “exists” Clause

Module 12

Partha Pratim • Yet another way of specifying the query “Find all courses taught in both the Fall 2009
Das
semester and in the Spring 2010 semester”
Objectives &
Outline
select course id
Nested
from section as S
Subqueries
Subqueries in the
where semester = ’Fall’ and year = 2009 and
Where Clause
Subqueries in the
exists (select *
From Clause
Subqueries in the
from section as T
Select Clause
where semester = ’Spring’ and year = 2010
Modifications of
the Database
and S.course id = T.course id);
Module Summary • Correlation name – variable S in the outer query
• Correlated subquery – the inner query

Database Management Systems Partha Pratim Das 12.16


Use of “not exists” Clause

Module 12

Partha Pratim • Find all students who have taken all courses offered in the Biology department.
Das
select distinct S.ID, S.name
Objectives &
Outline
from student as S
Nested
where not exists ( (select course id
Subqueries
Subqueries in the
from course
Where Clause
Subqueries in the
where dept name = ’Biology’)
From Clause
Subqueries in the
except
Select Clause
(select T.course id
Modifications of
the Database
from takes as T
Module Summary where S.ID = T.ID));
◦ First nested query lists all courses offered in Biology
◦ Second nested query lists all courses a particular student took
• Note: X − Y = ∅ ⇔ X ⊆ Y
• Note: Cannot write this query using = all and its variants
Database Management Systems Partha Pratim Das 12.17
Test for Absence of Duplicate Tuples: “unique”

Module 12

Partha Pratim • The unique construct tests whether a subquery has any duplicate tuples in its result
Das
• The unique construct evaluates to “true” if a given subquery contains no duplicates
Objectives &
Outline • Find all courses that were offered at most once in 2009
Nested
Subqueries
select T.course id
Subqueries in the
Where Clause
from course as T
Subqueries in the
From Clause
where unique (select R.course id
Subqueries in the
Select Clause
from section as R
Modifications of
where T.course id = R.course id
the Database
and R.year = 2009);
Module Summary

Database Management Systems Partha Pratim Das 12.18


Module 12

Partha Pratim
Das

Objectives &
Outline

Nested
Subqueries
Subqueries in the
Where Clause
Subqueries in the
From Clause
Subqueries in the
Select Clause

Modifications of
the Database

Module Summary
Subqueries in the From Clause

Database Management Systems Partha Pratim Das 12.19


Subqueries in the From Clause

Module 12
• SQL allows a subquery expression to be used in the from clause
Partha Pratim
Das • Find the average instructors’ salaries of those departments where the average salary is
Objectives &
greater than $42,000
Outline
select dept name, avg salary
Nested
Subqueries from (select dept name, avg(salary ) as avg salary
Subqueries in the
Where Clause
from instructor
Subqueries in the
From Clause
group by dept name)
Subqueries in the
Select Clause
where avg salary > 42000;
Modifications of • Note that we do not need to use the having clause
the Database

Module Summary • Another way to write above query


select dept name, avg salary
from (select dept name, avg (salary)
from instructor
group by dept name) as dept avg (dept name, avg salary )
where avg salary > 42000;
Database Management Systems Partha Pratim Das 12.20
With Clause

Module 12

Partha Pratim • The with clause provides a way of defining a temporary relation whose definition is
Das
available only to the query in which the with clause occurs
Objectives &
Outline • Find all departments with the maximum budget
Nested with max budget(value) as
Subqueries
Subqueries in the (select max(budget)
Where Clause
Subqueries in the from department)
From Clause
Subqueries in the select department.name
Select Clause
from department, max budget
Modifications of
the Database where department.budget=max budget.value;
Module Summary

Database Management Systems Partha Pratim Das 12.21


Complex Queries using With Clause

Module 12

Partha Pratim • Find all departments where the total salary is greater than the average of the total
Das
salary at all departments
Objectives &
Outline
with dept total (dept name, value) as
Nested
select dept name, sum(salary )
Subqueries
Subqueries in the
from instructor
Where Clause
Subqueries in the
group by dept name,
From Clause
Subqueries in the
dept total avg(value) as
Select Clause
(select avg(value)
Modifications of
the Database
from dept total)
Module Summary select dept name
from dept total, dept total avg
where dept total.value > dept total avg.value;

Database Management Systems Partha Pratim Das 12.22


Module 12

Partha Pratim
Das

Objectives &
Outline

Nested
Subqueries
Subqueries in the
Where Clause
Subqueries in the
From Clause
Subqueries in the
Select Clause

Modifications of
the Database

Module Summary Subqueries in the Select Clause

Database Management Systems Partha Pratim Das 12.23


Scalar Subquery

Module 12

Partha Pratim • Scalar subquery is one which is used where a single value is expected
Das
• List all departments along with the number of instructors in each department
Objectives &
Outline select dept name,
Nested (select count(*)
Subqueries
Subqueries in the from instructor
Where Clause
Subqueries in the where department.dept name = instructor.dept name)
From Clause
Subqueries in the as num instructors
Select Clause
from department;
Modifications of
the Database
• Runtime error if subquery returns more than one result tuple
Module Summary

Database Management Systems Partha Pratim Das 12.24


Modifications of the Database PPD

Module 12

Partha Pratim
Das

Objectives &
Outline

Nested
Subqueries
Subqueries in the
Where Clause
Subqueries in the
From Clause
Subqueries in the
Select Clause

Modifications of
the Database

Module Summary Modifications of the Database

Database Management Systems Partha Pratim Das 12.25


Modification of the Database

Module 12

Partha Pratim • Deletion of tuples from a given relation


Das
• Insertion of new tuples into a given relation
Objectives &
Outline • Updating of values in some tuples in a given relation
Nested
Subqueries
Subqueries in the
Where Clause
Subqueries in the
From Clause
Subqueries in the
Select Clause

Modifications of
the Database

Module Summary

Database Management Systems Partha Pratim Das 12.26


Deletion

Module 12

Partha Pratim • Delete all instructors


Das
delete from instructor
Objectives &
Outline • Delete all instructors from the Finance department
Nested delete from instructor
Subqueries
Subqueries in the where dept name= ’Finance’;
Where Clause
Subqueries in the
From Clause
• Delete all tuples in the instructor relation for those instructors associated with a
Subqueries in the
Select Clause
department located in the Watson building
Modifications of
delete from instructor
the Database
where dept name in (select dept name
Module Summary
from department
where building = ’Watson’);

Database Management Systems Partha Pratim Das 12.27


Deletion (2)

Module 12

Partha Pratim • Delete all instructors whose salary is less than the average salary of instructors
Das
delete from instructor
Objectives &
Outline
where salary < (select avg (salary )
Nested
from instructor );
Subqueries
Subqueries in the • Problem: as we delete tuples from deposit, the average salary changes
Where Clause
Subqueries in the
From Clause
• Solution used in SQL:
Subqueries in the
Select Clause a) First, compute avg (salary) and find all tuples to delete
Modifications of b) Next, delete all tuples found above (without recomputing avg or retesting the
the Database
tuples)
Module Summary

Database Management Systems Partha Pratim Das 12.28


Insertion

Module 12

Partha Pratim • Add a new tuple to course


Das
insert into course
Objectives &
Outline
values (’CS-437’, ’Database Systems’, ’Comp. Sci.’, 4);
Nested • or equivalently:
Subqueries
Subqueries in the insert into course (course id, title, dept name, credits)
Where Clause
Subqueries in the values (’CS-437’, ’Database Systems’, ’Comp. Sci.’, 4);
From Clause
Subqueries in the
Select Clause
• Add a new tuple to student with tot creds set to null
Modifications of
insert into student
the Database
values (’3003’, ’Green’, ’Finance’, null);
Module Summary

Database Management Systems Partha Pratim Das 12.29


Insertion (2)

Module 12

Partha Pratim • Add all instructors to the student relation with tot creds set to 0
Das
insert into student
Objectives &
Outline
select ID, name, dept name, 0
Nested
from instructor
Subqueries
Subqueries in the • The select from where statement is evaluated fully before any of its results are
Where Clause
Subqueries in the inserted into the relation
From Clause
Subqueries in the
Select Clause
• Otherwise queries like
Modifications of
insert into table1 select * from table1
the Database
would cause problem
Module Summary

Database Management Systems Partha Pratim Das 12.30


Updates

Module 12

Partha Pratim • Increase salaries of instructors whose salary is over $100,000 by 3%, and all others by a
Das
5%
Objectives &
Outline ◦ Write two update statements:
Nested
Subqueries
update instructor
Subqueries in the set salary = salary ∗ 1.03
Where Clause
Subqueries in the where salary > 100000;
From Clause
Subqueries in the update instructor
Select Clause

Modifications of
set salary = salary ∗ 1.05
the Database where salary <= 100000;
Module Summary
• The order is important
• Can be done better using the case statement (next slide)

Database Management Systems Partha Pratim Das 12.31


Case Statement for Conditional Updates

Module 12

Partha Pratim • Same query as before but with case statement


Das
update instructor
Objectives &
Outline
set salary = case
Nested
when salary <= 100000
Subqueries
Subqueries in the
then salary ∗ 1.05
Where Clause
Subqueries in the
else salary ∗ 1.03
From Clause
Subqueries in the
end
Select Clause

Modifications of
the Database

Module Summary

Database Management Systems Partha Pratim Das 12.32


Updates with Scalar Subqueries

Module 12

Partha Pratim • Recompute and update tot creds value for all students
Das
update student S
Objectives &
Outline
set tot creds = (select sum(credits)
Nested
from takes, course
Subqueries
Subqueries in the
where takes.course id = course.course id and
Where Clause
Subqueries in the
S.ID = takes.ID and
From Clause
Subqueries in the
takes.grade <> ’F’ and
Select Clause
takes.grade is not null);
Modifications of
the Database • Sets tot creds to null for students who have not taken any course
Module Summary
• Instead of sum(credits), use:
case
when sum(credits) is not null then sum(credits)
else 0
end

Database Management Systems Partha Pratim Das 12.33


Module Summary PPD

Module 12

Partha Pratim • Introduced nested subquery in SQL


Das
• Introduced data modification
Objectives &
Outline

Nested
Subqueries
Subqueries in the
Where Clause
Subqueries in the
From Clause
Subqueries in the
Select Clause

Modifications of
the Database

Module Summary
Slides used in this presentation are borrowed from http://db-book.com/ with kind
permission of the authors.
Edited and new slides are marked with “PPD”.

Database Management Systems Partha Pratim Das 12.34


Module 13

Partha Pratim
Das

Objectives &
Outline Database Management Systems
Join Expressions
Cross Join
Module 13: Intermediate SQL/2
Inner Join
Outer Join
Left Outer Join
Right Outer Join
Full Outer Join Partha Pratim Das
Views
View Expansion
View Update
Department of Computer Science and Engineering
Materialized Views Indian Institute of Technology, Kharagpur
Module Summary
[email protected]

Database Management Systems Partha Pratim Das 13.1


Module Recap PPD

Module 13

Partha Pratim • Nested subquery in SQL


Das
• Processes for data modification
Objectives &
Outline

Join Expressions
Cross Join
Inner Join
Outer Join
Left Outer Join
Right Outer Join
Full Outer Join

Views
View Expansion
View Update
Materialized Views

Module Summary

Database Management Systems Partha Pratim Das 13.2


Module Objectives PPD

Module 13

Partha Pratim • To learn SQL expressions for Join


Das
• To learn SQL expressions for Views
Objectives &
Outline

Join Expressions
Cross Join
Inner Join
Outer Join
Left Outer Join
Right Outer Join
Full Outer Join

Views
View Expansion
View Update
Materialized Views

Module Summary

Database Management Systems Partha Pratim Das 13.3


Module Outline PPD

Module 13

Partha Pratim • Join Expressions


Das
• Views
Objectives &
Outline

Join Expressions
Cross Join
Inner Join
Outer Join
Left Outer Join
Right Outer Join
Full Outer Join

Views
View Expansion
View Update
Materialized Views

Module Summary

Database Management Systems Partha Pratim Das 13.4


Join Expressions PPD

Module 13

Partha Pratim
Das

Objectives &
Outline

Join Expressions
Cross Join
Inner Join
Outer Join
Left Outer Join
Right Outer Join
Full Outer Join

Views
View Expansion
View Update
Materialized Views
Join Expressions
Module Summary

Database Management Systems Partha Pratim Das 13.5


Joined Relations

Module 13

Partha Pratim • Join operations take two relations and return as a result another relation
Das
• A join operation is a Cartesian product which requires that tuples in the two relations
Objectives &
Outline match (under some condition).
Join Expressions
Cross Join
• It also specifies the attributes that are present in the result of the join
Inner Join
Outer Join
• The join operations are typically used as subquery expressions in the from clause
Left Outer Join
Right Outer Join
Full Outer Join

Views
View Expansion
View Update
Materialized Views

Module Summary

Database Management Systems Partha Pratim Das 13.6


Types of Join between Relations PPD

Module 13

Partha Pratim • Cross join


Das
• Inner join
Objectives &
Outline ◦ Equi-join
Join Expressions
Cross Join
. Natural join
Inner Join
Outer Join
• Outer join
Left Outer Join
Right Outer Join
◦ Left outer join
Full Outer Join ◦ Right outer join
Views ◦ Full outer join
View Expansion
View Update
Materialized Views
• Self-join
Module Summary

Database Management Systems Partha Pratim Das 13.7


Cross Join PPD

Module 13

Partha Pratim • CROSS JOIN returns the Cartesian product of rows from tables in the join
Das
◦ Explicit
Objectives &
Outline select *
Join Expressions from employee cross join department;
Cross Join
Inner Join ◦ Implicit
Outer Join
Left Outer Join select *
Right Outer Join
Full Outer Join
from employee, department;
Views
View Expansion
View Update
Materialized Views

Module Summary

Database Management Systems Partha Pratim Das 13.8


Join operations – Example

Module 13

Partha Pratim • Relation course


Das

Objectives &
Outline

Join Expressions
Cross Join
Inner Join
Outer Join
Left Outer Join
Right Outer Join • Relation prereq
Full Outer Join

Views
View Expansion
View Update
Materialized Views

Module Summary

• Observe that
prereq information is missing for CS-315 and
course information is missing for CS-347
Database Management Systems Partha Pratim Das 13.9
Inner Join PPD

Module 13

Partha Pratim
• course inner join prereq
Das

Objectives &
Outline

Join Expressions
Cross Join
Inner Join
Outer Join • If specified as natural, the 2nd course id field is skipped
Left Outer Join
Right Outer Join
Full Outer Join

Views
View Expansion
View Update
Materialized Views

Module Summary

Database Management Systems Partha Pratim Das 13.10


Outer Join

Module 13

Partha Pratim • An extension of the join operation that avoids loss of information
Das
• Computes the join and then adds tuples from one relation that does not match tuples
Objectives &
Outline in the other relation to the result of the join
Join Expressions
Cross Join
• Uses null values
Inner Join
Outer Join
Left Outer Join
Right Outer Join
Full Outer Join

Views
View Expansion
View Update
Materialized Views

Module Summary

Database Management Systems Partha Pratim Das 13.11


Left Outer Join PPD

Module 13

Partha Pratim • course natural left outer join prereq


Das

Objectives &
Outline

Join Expressions
Cross Join
Inner Join
Outer Join
Left Outer Join
Right Outer Join
Full Outer Join

Views
View Expansion
View Update
Materialized Views

Module Summary

Database Management Systems Partha Pratim Das 13.12


Right Outer Join PPD

Module 13

Partha Pratim • course natural right outer join prereq


Das

Objectives &
Outline

Join Expressions
Cross Join
Inner Join
Outer Join
Left Outer Join
Right Outer Join
Full Outer Join

Views
View Expansion
View Update
Materialized Views

Module Summary

Database Management Systems Partha Pratim Das 13.13


Joined Relations

Module 13

Partha Pratim • Join operations take two relations and return as a result another relation
Das
• These additional operations are typically used as subquery expressions in the from
Objectives &
Outline clause
Join Expressions
Cross Join
• Join condition – defines which tuples in the two relations match, and what attributes
Inner Join are present in the result of the join
Outer Join
Left Outer Join • Join type – defines how tuples in each relation that do not match any tuple in the other
Right Outer Join
Full Outer Join relation (based on the join condition) are treated
Views
View Expansion
View Update
Materialized Views

Module Summary

Database Management Systems Partha Pratim Das 13.14


Full Outer Join PPD

Module 13

Partha Pratim
• course natural full outer join prereq
Das

Objectives &
Outline

Join Expressions
Cross Join
Inner Join
Outer Join
Left Outer Join
Right Outer Join
Full Outer Join

Views
View Expansion
View Update
Materialized Views

Module Summary

Database Management Systems Partha Pratim Das 13.15


Joined Relations - Examples

Module 13

Partha Pratim • course inner join prereq on


Das
course.course id = prereq.course id
Objectives &
Outline

Join Expressions
Cross Join
Inner Join
Outer Join
Left Outer Join
Right Outer Join • What is the difference between the above (equi join), and a natural join?
Full Outer Join

Views
• course left outer join prereq on
View Expansion course.course id = prereq.course id
View Update
Materialized Views

Module Summary

Database Management Systems Partha Pratim Das 13.16


Joined Relations - Examples

Module 13

Partha Pratim • course natural right outer join prereq


Das

Objectives &
Outline

Join Expressions
Cross Join
Inner Join
Outer Join
Left Outer Join
Right Outer Join
Full Outer Join
• course full outer join prereq using (course id)
Views
View Expansion
View Update
Materialized Views

Module Summary

Database Management Systems Partha Pratim Das 13.17


Views PPD

Module 13

Partha Pratim
Das

Objectives &
Outline

Join Expressions
Cross Join
Inner Join
Outer Join
Left Outer Join
Right Outer Join
Full Outer Join

Views
View Expansion
View Update
Materialized Views
Views
Module Summary

Database Management Systems Partha Pratim Das 13.18


Views

Module 13

Partha Pratim • In some cases, it is not desirable for all users to see the entire logical model (that is, all
Das
the actual relations stored in the database.)
Objectives &
Outline • Consider a person who needs to know an instructors name and department, but not the
Join Expressions salary. This person should see a relation described, in SQL, by
Cross Join
Inner Join
select ID, name, dept name
Outer Join
from instructor
Left Outer Join
Right Outer Join
Full Outer Join
• A view provides a mechanism to hide certain data from the view of certain users
Views • Any relation that is not of the conceptual model but is made visible to a user as a
View Expansion
View Update
“virtual relation” is called a view.
Materialized Views

Module Summary

Database Management Systems Partha Pratim Das 13.19


View Definition

Module 13

Partha Pratim • A view is defined using the create view statement which has the form
Das
create view v as < query expression >
Objectives &
Outline
where < query expression > is any legal SQL expression
Join Expressions • The view name is represented by v
Cross Join
Inner Join • Once a view is defined, the view name can be used to refer to the virtual relation that
Outer Join
Left Outer Join
the view generates
Right Outer Join
Full Outer Join • View definition is not the same as creating a new relation by evaluating the query
Views expression
View Expansion
View Update ◦ Rather, a view definition causes the saving of an expression; the expression is
Materialized Views
substituted into queries using the view
Module Summary

Database Management Systems Partha Pratim Das 13.20


Example Views

Module 13

Partha Pratim • A view of instructors without their salary


Das
create view faculty as
Objectives &
Outline
select ID, name, dept name
Join Expressions
from instructor
Cross Join
Inner Join
• Find all instructors in the Biology department
Outer Join
select name
Left Outer Join
Right Outer Join from faculty
Full Outer Join
where dept name = ’Biology’
Views
View Expansion • Create a view of department salary totals
View Update
Materialized Views create view departments total salary(dept name, total salary) as
Module Summary select dept name, sum (salary )
from instructor
group by dept name;

Database Management Systems Partha Pratim Das 13.21


Views Defined Using Other Views

Module 13

Partha Pratim • create view physics fall 2009 as


Das
select course.course id, sec id, building, room number
Objectives &
Outline
from course, section
Join Expressions
where course.course id = section.course id
Cross Join and course.dept name = ’Physics’
Inner Join
Outer Join and section.semester = ’Fall’
Left Outer Join
Right Outer Join
and section.year = ’2009’;
Full Outer Join
• create view physics fall 2009 watson as
Views
View Expansion
select course id, room number
View Update
Materialized Views
from physics fall 2009
Module Summary
where building = ’Watson’;

Database Management Systems Partha Pratim Das 13.22


View Expansion

Module 13

Partha Pratim • Expand use of a view in a query/another view


Das
create view physics fall 2009 watson as
Objectives &
Outline
(select course id, room number
Join Expressions
from (select course.course id, building, room number
Cross Join from course, section
Inner Join
Outer Join where course.course id = section.course id
Left Outer Join
Right Outer Join
and course.dept name = ’Physics’
Full Outer Join and section.semester = ’Fall’
Views and section.year = ’2009’)
View Expansion
View Update where building = ’Watson’);
Materialized Views

Module Summary

Database Management Systems Partha Pratim Das 13.23


Views Defined Using Other Views

Module 13

Partha Pratim • One view may be used in the expression defining another view
Das
• A view relation v1 is said to depend directly on a view relation v2 if v2 is used in the
Objectives &
Outline expression defining v1
Join Expressions
Cross Join
• A view relation v1 is said to depend on view relation v2 if either v1 depends directly on
Inner Join v2 or there is a path of dependencies from v1 to v2
Outer Join
Left Outer Join • A view relation v is said to be recursive if it depends on itself
Right Outer Join
Full Outer Join

Views
View Expansion
View Update
Materialized Views

Module Summary

Database Management Systems Partha Pratim Das 13.24


View Expansion*

Module 13

Partha Pratim • A way to define the meaning of views defined in terms of other views
Das
• Let view v1 be defined by an expression e1 that may itself contain uses of view relations
Objectives &
Outline • View expansion of an expression repeats the following replacement step:
Join Expressions
Cross Join
repeat
Inner Join Find any view relation vi in e1
Outer Join
Left Outer Join
Replace the view relation vi by the expression defining vi
Right Outer Join
Full Outer Join
until no more view relations are present in e1
Views • As long as the view definitions are not recursive, this loop will terminate
View Expansion
View Update
Materialized Views

Module Summary

Database Management Systems Partha Pratim Das 13.25


Update of a View

Module 13

Partha Pratim • Add a new tuple to faculty view which we defined earlier
Das
insert into faculty values (’30765’, ’Green’, ’Music’);
Objectives &
Outline • This insertion must be represented by the insertion of the tuple
Join Expressions (’30765’, ’Green’, ’Music’, null)
Cross Join
Inner Join
into the instructor relation
Outer Join
Left Outer Join
Right Outer Join
Full Outer Join

Views
View Expansion
View Update
Materialized Views

Module Summary

Database Management Systems Partha Pratim Das 13.26


Some Updates cannot be Translated Uniquely

Module 13

Partha Pratim • create view instructor info as


Das
select ID, name, building
Objectives &
Outline
from instructor, department
Join Expressions
where instructor.dept name= department.dept name;
Cross Join
Inner Join
• insert into instructor info values (’69987’, ’White’, ’Taylor’);
Outer Join
Left Outer Join
◦ which department, if multiple departments in Taylor?
Right Outer Join ◦ what if no department is in Taylor?
Full Outer Join

Views • Most SQL implementations allow updates only on simple views


View Expansion
View Update
◦ The from clause has only one database relation
Materialized Views
◦ The select clause contains only attribute names of the relation, and does not have
Module Summary
any expressions, aggregates, or distinct specification
◦ Any attribute not listed in the select clause can be set to null
◦ The query does not have a group by or having clause

Database Management Systems Partha Pratim Das 13.27


And Some Not at All

Module 13

Partha Pratim • create view history instructors as


Das
select *
Objectives &
Outline
from instructor
Join Expressions
where dept name= ’History’;
Cross Join
Inner Join
• What happens if we insert (’25566’, ’Brown’, ’Biology’, 100000) into
Outer Join
history instructors?
Left Outer Join
Right Outer Join
Full Outer Join

Views
View Expansion
View Update
Materialized Views

Module Summary

Database Management Systems Partha Pratim Das 13.28


Materialized Views

Module 13

Partha Pratim • Materializing a view: create a physical table containing all the tuples in the result of
Das
the query defining the view
Objectives &
Outline • If relations used in the query are updated, the materialized view result becomes out of
Join Expressions date
Cross Join
Inner Join ◦ Need to maintain the view, by updating the view whenever the underlying relations
Outer Join
Left Outer Join
are updated
Right Outer Join
Full Outer Join

Views
View Expansion
View Update
Materialized Views

Module Summary

Database Management Systems Partha Pratim Das 13.29


Module Summary PPD

Module 13

Partha Pratim • Learnt SQL expressions for Join and Views


Das

Objectives &
Outline

Join Expressions
Cross Join
Inner Join
Outer Join
Left Outer Join
Right Outer Join
Full Outer Join

Views
View Expansion Slides used in this presentation are borrowed from http://db-book.com/ with kind
View Update
Materialized Views
permission of the authors.
Module Summary Edited and new slides are marked with “PPD”.

Database Management Systems Partha Pratim Das 13.30


Module 14

Partha Pratim
Das

Objectives &
Outline Database Management Systems
Transactions
Module 14: Intermediate SQL/3
Integrity
Constraints
Referential Integrity

SQL Data Types


and Schemas
Built-in Types
Partha Pratim Das
Index
UDT
Domains
Department of Computer Science and Engineering
Large Object
Indian Institute of Technology, Kharagpur
Authorization
[email protected]
Privileges
Revocation
Roles

Module Summary

Database Management Systems Partha Pratim Das 14.1


Module Recap PPD

Module 14

Partha Pratim • SQL expressions for Join and Views


Das

Objectives &
Outline

Transactions

Integrity
Constraints
Referential Integrity

SQL Data Types


and Schemas
Built-in Types
Index
UDT
Domains
Large Object

Authorization
Privileges
Revocation
Roles

Module Summary

Database Management Systems Partha Pratim Das 14.2


Module Objectives PPD

Module 14

Partha Pratim • To understand Transactions


Das
• To learn SQL expressions for Integrity Constraints
Objectives &
Outline • To understand more Data Types in SQL
Transactions

Integrity
• To understand Authorization in SQL
Constraints
Referential Integrity

SQL Data Types


and Schemas
Built-in Types
Index
UDT
Domains
Large Object

Authorization
Privileges
Revocation
Roles

Module Summary

Database Management Systems Partha Pratim Das 14.3


Module Outline PPD

Module 14

Partha Pratim • Transactions


Das
• Integrity Constraints
Objectives &
Outline • SQL Data Types and Schemas
Transactions

Integrity
• Authorization
Constraints
Referential Integrity

SQL Data Types


and Schemas
Built-in Types
Index
UDT
Domains
Large Object

Authorization
Privileges
Revocation
Roles

Module Summary

Database Management Systems Partha Pratim Das 14.4


Transactions PPD

Module 14

Partha Pratim
Das

Objectives &
Outline

Transactions

Integrity
Constraints
Referential Integrity

SQL Data Types


and Schemas
Built-in Types
Index
UDT
Domains
Large Object
Transactions
Authorization
Privileges
Revocation
Roles

Module Summary

Database Management Systems Partha Pratim Das 14.5


Transactions

Module 14

Partha Pratim • Unit of work


Das
• Atomic transaction
Objectives &
Outline ◦ either fully executed or rolled back as if it never occurred
Transactions
• Isolation from concurrent transactions
Integrity
Constraints
Referential Integrity
• Transactions begin implicitly
SQL Data Types ◦ Ended by commit work or rollback work
and Schemas
Built-in Types • But default on most databases: each SQL statement commits automatically
Index
UDT ◦ Can turn off auto commit for a session (for example, using API)
Domains
Large Object
◦ In SQL:1999, can use: begin atomic ... end
Authorization . Not supported on most databases
Privileges
Revocation
Roles

Module Summary

Database Management Systems Partha Pratim Das 14.6


Integrity Constraints PPD

Module 14

Partha Pratim
Das

Objectives &
Outline

Transactions

Integrity
Constraints
Referential Integrity

SQL Data Types


and Schemas
Built-in Types
Index
UDT
Domains
Large Object
Integrity Constraints
Authorization
Privileges
Revocation
Roles

Module Summary

Database Management Systems Partha Pratim Das 14.7


Integrity Constraints

Module 14

Partha Pratim • Integrity constraints guard against accidental damage to the database, by ensuring that
Das
authorized changes to the database do not result in a loss of data consistency
Objectives &
Outline ◦ A checking account must have a balance greater than Rs. 10,000.00
Transactions ◦ A salary of a bank employee must be at least Rs. 250.00 an hour
Integrity ◦ A customer must have a (non-null) phone number
Constraints
Referential Integrity

SQL Data Types


and Schemas
Built-in Types
Index
UDT
Domains
Large Object

Authorization
Privileges
Revocation
Roles

Module Summary

Database Management Systems Partha Pratim Das 14.8


Integrity Constraints on a Single Relation

Module 14

Partha Pratim • not null


Das
• primary key
Objectives &
Outline • unique
Transactions

Integrity
• check(P), where P is a predicate
Constraints
Referential Integrity

SQL Data Types


and Schemas
Built-in Types
Index
UDT
Domains
Large Object

Authorization
Privileges
Revocation
Roles

Module Summary

Database Management Systems Partha Pratim Das 14.9


Not Null and Unique Constraints

Module 14

Partha Pratim • not null


Das
◦ Declare name and budget to be not null
Objectives &
Outline name varchar(20) not null
Transactions budget numeric(12,2) not null
Integrity
Constraints
• unique (A1 , A2 , . . . , Am )
Referential Integrity
◦ The unique specification states that the attributes A1 , A2 , . . . , Am form a candidate
SQL Data Types
and Schemas key
Built-in Types
Index
◦ Candidate keys are permitted to be null (in contrast to primary keys).
UDT
Domains
Large Object

Authorization
Privileges
Revocation
Roles

Module Summary

Database Management Systems Partha Pratim Das 14.10


The check clause

Module 14

Partha Pratim • check(P), where P is a predicate


Das
• Ensure that semester is one of fall, winter, spring or summer:
Objectives &
Outline

Transactions create table section (


Integrity
Constraints
course id varchar(8),
Referential Integrity sec id varchar(8),
SQL Data Types
and Schemas
semester varchar(6),
Built-in Types year numeric(4,0),
Index
UDT
building varchar(15),
Domains room number varchar(7),
Large Object

Authorization
time slot id varchar(4),
Privileges primary key (course id, sec id, semester, year ),
Revocation
Roles
check (semester in (’Fall’, ’Winter’, ’Spring’, ’Summer’))
Module Summary );

Database Management Systems Partha Pratim Das 14.11


Referential Integrity

Module 14

Partha Pratim • Ensures that a value that appears in one relation for a given set of attributes also
Das
appears for a certain set of attributes in another relation
Objectives &
Outline • Example: If “Biology” is a department name appearing in one of the tuples in the
Transactions instructor relation, then there exists a tuple in the department relation for “Biology”
Integrity
Constraints • Let A be a set of attributes. Let R and S be two relations that contain attributes A and
Referential Integrity
where A is the primary key of S. A is said to be a foreign key of R if for any values of
SQL Data Types
and Schemas A appearing in R these values also appear in S
Built-in Types
Index
UDT
Domains
Large Object

Authorization
Privileges
Revocation
Roles

Module Summary

Database Management Systems Partha Pratim Das 14.12


Cascading Actions in Referential Integrity PPD

Module 14 • With cascading, you can define the actions that the Database Engine takes when a user
Partha Pratim tries to delete or update a key to which existing foreign keys point
Das
• create table course (
Objectives &
Outline course id char(5) primary key,
Transactions title varchar(20),
Integrity dept name varchar(20) references department
Constraints
Referential Integrity )
SQL Data Types
and Schemas
• create table course (
Built-in Types ...
Index
UDT dept name varchar(20),
Domains
Large Object
foreign key (dept name) references department
Authorization on delete cascade
Privileges
on update cascade,
Revocation
Roles ...
Module Summary )
• Alternative actions to cascade: no action, set null, set default
Database Management Systems Partha Pratim Das 14.13
Integrity Constraint Violation During Transactions

Module 14

Partha Pratim • create table person (


Das
ID char(10),
Objectives &
Outline
name char(40),
Transactions
mother char(10),
Integrity father char(10),
Constraints
Referential Integrity
primary key ID,
SQL Data Types foreign key father references person,
and Schemas
Built-in Types
foreign key mother references person)
Index
UDT
• How to insert a tuple without causing constraint violation?
Domains
Large Object
◦ Insert father and mother of a person before inserting person
Authorization ◦ OR, Set father and mother to null initially, update after inserting all persons (not
Privileges
possible if father and mother attributes declared to be not null)
Revocation
Roles ◦ OR Defer constraint checking (will discuss later)
Module Summary

Database Management Systems Partha Pratim Das 14.14


SQL Data Types and Schemas PPD

Module 14

Partha Pratim
Das

Objectives &
Outline

Transactions

Integrity
Constraints
Referential Integrity

SQL Data Types


and Schemas
Built-in Types
Index
UDT
Domains
Large Object
SQL Data Types and Schemas
Authorization
Privileges
Revocation
Roles

Module Summary

Database Management Systems Partha Pratim Das 14.15


Built-in Data Types in SQL

Module 14

Partha Pratim • date: Dates, containing a (4 digit) year, month and date
Das
◦ Example: date ‘2005-7-27’
Objectives &
Outline • time: Time of day, in hours, minutes and seconds.
Transactions
◦ Example: time ‘09:00:30’ time ‘09:00:30.75’
Integrity
Constraints • timestamp: date plus time of day
Referential Integrity

SQL Data Types ◦ Example: timestamp ‘2005-7-27 09:00:30.75’


and Schemas
Built-in Types • interval: period of time
Index
UDT ◦ Example: interval ‘1’ day
Domains
Large Object
◦ Subtracting a date/time/timestamp value from another gives an interval value
Authorization ◦ Interval values can be added to date/time/timestamp values
Privileges
Revocation
Roles

Module Summary

Database Management Systems Partha Pratim Das 14.16


Index Creation

Module 14
• create table student
Partha Pratim
Das (ID varchar(5),
Objectives &
name varchar(20) not null,
Outline
dept name varchar(20),
Transactions
tot cred numeric (3,0) default 0,
Integrity
Constraints primary key (ID))
Referential Integrity

SQL Data Types


• create index studentID index on student(ID)
and Schemas
Built-in Types
• Indices are data structures used to speed up access to records with specified values for
Index index attributes
UDT
Domains select *
Large Object
from student
Authorization
Privileges
where ID = ‘12345’
Revocation
Roles
◦ Can be executed by using the index to find the required record, without looking at
Module Summary all records of student
◦ More on indices in Chapter 9
Database Management Systems Partha Pratim Das 14.17
User-Defined Types

Module 14

Partha Pratim • create type construct in SQL creates user-defined type (alias, like typedef in C)
Das
create type Dollars as numeric (12,2) final
Objectives &
Outline ◦ create table department (
Transactions dept name varchar (20),
Integrity building varchar (15),
Constraints
Referential Integrity budget Dollars);
SQL Data Types
and Schemas
Built-in Types
Index
UDT
Domains
Large Object

Authorization
Privileges
Revocation
Roles

Module Summary

Database Management Systems Partha Pratim Das 14.18


Domains

Module 14

Partha Pratim • create domain construct in SQL-92 creates user-defined domain types
Das
create domain person name char(20) not null
Objectives &
Outline • Types and domains are similar
Transactions
• Domains can have constraints, such as not null, specified on them
Integrity
Constraints create domain degree level varchar(10)
Referential Integrity
constraint degree level test
SQL Data Types
and Schemas check (value in (’Bachelors’, ’Masters’, ’Doctorate’));
Built-in Types
Index
UDT
Domains
Large Object

Authorization
Privileges
Revocation
Roles

Module Summary

Database Management Systems Partha Pratim Das 14.19


Large-Object Types

Module 14

Partha Pratim • Large objects (photos, videos, CAD files, etc.) are stored as a large object:
Das
◦ blob: binary large object – object is a large collection of uninterpreted binary data
Objectives &
Outline (whose interpretation is left to an application outside of the database system)
Transactions ◦ clob: character large object – object is a large collection of character data
Integrity ◦ When a query returns a large object, a pointer is returned rather than the large
Constraints
Referential Integrity object itself
SQL Data Types
and Schemas
Built-in Types
Index
UDT
Domains
Large Object

Authorization
Privileges
Revocation
Roles

Module Summary

Database Management Systems Partha Pratim Das 14.20


Authorization PPD

Module 14

Partha Pratim
Das

Objectives &
Outline

Transactions

Integrity
Constraints
Referential Integrity

SQL Data Types


and Schemas
Built-in Types
Index
UDT
Domains
Large Object
Authorization
Authorization
Privileges
Revocation
Roles

Module Summary

Database Management Systems Partha Pratim Das 14.21


Authorization

Module 14

Partha Pratim • Forms of authorization on parts of the database:


Das
◦ Read - allows reading, but not modification of data
Objectives &
Outline ◦ Insert - allows insertion of new data, but not modification of existing data
Transactions ◦ Update - allows modification, but not deletion of data
Integrity ◦ Delete - allows deletion of data
Constraints
Referential Integrity • Forms of authorization to modify the database schema
SQL Data Types
and Schemas ◦ Index - allows creation and deletion of indices
Built-in Types
Index
◦ Resources - allows creation of new relations
UDT ◦ Alteration - allows addition or deletion of attributes in a relation
Domains
Large Object ◦ Drop - allows deletion of relations
Authorization
Privileges
Revocation
Roles

Module Summary

Database Management Systems Partha Pratim Das 14.22


Authorization Specification in SQL

Module 14

Partha Pratim • The grant statement is used to confer authorization


Das
grant <privilege list>
Objectives &
Outline
on <relation name or view name> to <user list>
Transactions • <user list> is:
Integrity
Constraints
◦ a user-id
Referential Integrity ◦ public, which allows all valid users the privilege granted
SQL Data Types
and Schemas
◦ A role (more on this later)
Built-in Types
Index
• Granting a privilege on a view does not imply granting any privileges on the underlying
UDT relations
Domains
Large Object
• The grantor of the privilege must already hold the privilege on the specified item (or be
Authorization
Privileges
the database administrator)
Revocation
Roles

Module Summary

Database Management Systems Partha Pratim Das 14.23


Privileges in SQL

Module 14

Partha Pratim • select: allows read access to relation, or the ability to query using the view
Das
◦ Example: grant users U1 , U2 , and U3 select authorization on the instructor relation:
Objectives &
Outline grant select on instructor to U1 , U2 , U3
Transactions • insert: the ability to insert tuples
Integrity
Constraints • update: the ability to update using the SQL update statement
Referential Integrity

SQL Data Types


• delete: the ability to delete tuples.
and Schemas
Built-in Types • all privileges: used as a short form for all the allowable privileges
Index
UDT
Domains
Large Object

Authorization
Privileges
Revocation
Roles

Module Summary

Database Management Systems Partha Pratim Das 14.24


Revoking Authorization in SQL

Module 14

Partha Pratim • The revoke statement is used to revoke authorization


Das
revoke <privilege list>
Objectives &
Outline
on <relation name or view name> from <user list>
Transactions • Example:
Integrity
Constraints
revoke select on branch from U1 , U2 , U3
Referential Integrity
• <privilege-list> may be all to revoke all privileges the revokee may hold
SQL Data Types
and Schemas • If <revokee-list> includes public, all users lose the privilege except those granted it
Built-in Types
Index explicitly
UDT
Domains • If the same privilege was granted twice to the same user by different grantees, the user
Large Object
may retain the privilege after the revocation
Authorization
Privileges • All privileges that depend on the privilege being revoked are also revoked
Revocation
Roles

Module Summary

Database Management Systems Partha Pratim Das 14.25


Roles

Module 14

Partha Pratim • create role instructor ;


Das
grant instructor to Amit;
Objectives &
Outline • Privileges can be granted to roles:
Transactions grant select on takes to instructor ;
Integrity
Constraints • Roles can be granted to users, as well as to other roles
Referential Integrity
create role teaching assistant
SQL Data Types
and Schemas grant teaching assistant to instructor ;
Built-in Types
Index
◦ Instructor inherits all privileges of teaching assistant
UDT
Domains
• Chain of roles
Large Object
◦ create role dean;
Authorization
Privileges ◦ grant instructor to dean;
Revocation
Roles
◦ grant dean to Satoshi;
Module Summary

Database Management Systems Partha Pratim Das 14.26


Authorization on Views

Module 14

Partha Pratim • create view geo instructor as


Das
(select *
Objectives &
Outline
from instructor
Transactions
where dept name = ’Geology’);
Integrity grant select on geo instructor to geo staff
Constraints
Referential Integrity • Suppose that a geo staff member issues
SQL Data Types
and Schemas
select *
Built-in Types from geo instructor ;
Index
UDT • What if
Domains
Large Object ◦ geo staff does not have permissions on instructor ?
Authorization ◦ creator of view did not have some permissions on instructor ?
Privileges
Revocation
Roles

Module Summary

Database Management Systems Partha Pratim Das 14.27


Other Authorization Features

Module 14

Partha Pratim • references privilege to create foreign key


Das
grant reference (dept name) on department to Mariano;
Objectives &
Outline ◦ why is this required?
Transactions • Transfer of privileges
Integrity
Constraints ◦ grant select on department to Amit with grant option;
Referential Integrity
◦ revoke select on department from Amit, Satoshi cascade;
SQL Data Types
and Schemas ◦ revoke select on department from Amit, Satoshi restrict;
Built-in Types
Index
UDT
Domains
Large Object

Authorization
Privileges
Revocation
Roles

Module Summary

Database Management Systems Partha Pratim Das 14.28


Module Summary PPD

Module 14

Partha Pratim • Introduced transactions


Das
• Learnt SQL expressions for integrity constraints
Objectives &
Outline • Familiarized with more data types in SQL
Transactions

Integrity
• Discussed authorization in SQL
Constraints
Referential Integrity

SQL Data Types


and Schemas
Built-in Types
Index
UDT
Domains
Large Object

Authorization
Privileges
Revocation
Roles
Slides used in this presentation are borrowed from http://db-book.com/ with kind
Module Summary
permission of the authors.
Edited and new slides are marked with “PPD”.
Database Management Systems Partha Pratim Das 14.29
Module 15

Partha Pratim
Das

Objectives &
Outline Database Management Systems
Functions and
Procedural Module 15: Advanced SQL
Constructs

Triggers
Triggers :
Functionality vs
Performance
Partha Pratim Das
Module Summary

Department of Computer Science and Engineering


Indian Institute of Technology, Kharagpur

[email protected]

Database Management Systems Partha Pratim Das 15.1


Module Recap PPD

Module 15

Partha Pratim • Transactions


Das
• Integrity Constraints
Objectives &
Outline • More Data Types in SQL
Functions and
Procedural • Authorization in SQL
Constructs

Triggers
Triggers :
Functionality vs
Performance

Module Summary

Database Management Systems Partha Pratim Das 15.2


Module Objectives

Module 15

Partha Pratim • To familiarize with functions and procedures in SQL


Das
• To understand the triggers and their performance issues
Objectives &
Outline

Functions and
Procedural
Constructs

Triggers
Triggers :
Functionality vs
Performance

Module Summary

Database Management Systems Partha Pratim Das 15.3


Module Outline

Module 15

Partha Pratim • Functions and Procedural Constructs


Das
• Triggers
Objectives &
Outline ◦ Functionality vs Performance
Functions and
Procedural
Constructs

Triggers
Triggers :
Functionality vs
Performance

Module Summary

Database Management Systems Partha Pratim Das 15.4


Module 15

Partha Pratim
Das

Objectives &
Outline

Functions and
Procedural
Constructs

Triggers
Triggers :
Functionality vs
Performance

Module Summary

Functions and Procedural Constructs

Database Management Systems Partha Pratim Das 15.5


Native Language← →Query Language

Module 15

Partha Pratim
Das

Objectives &
Outline

Functions and
Procedural
Constructs

Triggers
Triggers :
Functionality vs
Performance

Module Summary

Database Management Systems Partha Pratim Das 15.6


Functions and Procedures

Module 15

Partha Pratim • Functions / Procedures and Control Flow Statements were added in SQL:1999
Das
◦ Functions/Procedures can be written in SQL itself, or in an external
Objectives &
Outline programming language (like C, Java)
Functions and ◦ Functions written in an external languages are particularly useful with specialized
Procedural
Constructs data types such as images and geometric objects
Triggers . Example: Functions to check if polygons overlap, or to compare images for
Triggers :
Functionality vs
Performance
similarity
Module Summary ◦ Some database systems support table-valued functions, which can return a
relation as a result
• SQL:1999 also supports a rich set of imperative constructs, including loops,
if-then-else, and assignment
• Many databases have proprietary procedural extensions to SQL that differ from
SQL:1999

Database Management Systems Partha Pratim Das 15.7


SQL Functions

Module 15 • Define a function that, given the name of a department, returns the count of the
Partha Pratim
Das
number of instructors in that department:
create function dept count (dept name varchar(20))
Objectives &
Outline returns integer
Functions and begin
Procedural
Constructs declare d count integer;
Triggers select count (*) into d count
Triggers :
Functionality vs from instructor
Performance

Module Summary
where instructor.dept name = dept name
return d cont;
end
• The function dept count can be used to find the department names and budget of all
departments with more that 12 instructors:
select dept name, budget
from department
where dept count (dept name ) > 12
Database Management Systems Partha Pratim Das 15.8
SQL functions (2)

Module 15

Partha Pratim • Compound statement: begin . . . end


Das
May contain multiple SQL statements between begin and end.
Objectives &
Outline • returns – indicates the variable-type that is returned (for example, integer)
Functions and
Procedural
• return – specifies the values that are to be returned as result of invoking the function
Constructs

Triggers
• SQL function are in fact parameterized views that generalize the regular notion of
Triggers : views by allowing parameters
Functionality vs
Performance

Module Summary

Database Management Systems Partha Pratim Das 15.9


Table Functions

Module 15
• Functions that return a relation as a result added in SQL:2003
Partha Pratim
Das • Return all instructors in a given department:
Objectives &
create function instructor of (dept name char(20))
Outline
returns table (
Functions and
Procedural ID varchar(5),
Constructs
name varchar(20),
Triggers
Triggers :
dept name varchar(20)
Functionality vs
Performance salary numeric(8, 2) )
Module Summary returns table
(select ID, name, dept name, salary
from instructor
where instructor.dept name = instructor of.dept name)
• Usage
select *
from table (instructor of (‘Music’))
Database Management Systems Partha Pratim Das 15.10
SQL Procedures

Module 15 • The dept count function could instead be written as procedure:


Partha Pratim
Das
create procedure dept count proc (
in dept name varchar (20), out d count integer)
Objectives &
Outline begin
Functions and select count(*) into d count
Procedural
Constructs from instructor
Triggers where instructor.dept name = dept count proc.dept name
Triggers :
Functionality vs end
Performance

Module Summary • Procedures can be invoked either from an SQL procedure or from embedded SQL,
using the call statement.
declare d count integer;
call dept count proc(‘Physics’, d count);
• Procedures and functions can be invoked also from dynamic SQL
• SQL:1999 allows overloading - more than one function/procedure of the same name as
long as the number of arguments and / or the types of the arguments differ
Database Management Systems Partha Pratim Das 15.11
Language Constructs for Procedures and Functions

Module 15

Partha Pratim • SQL supports constructs that gives it almost all the power of a general-purpose
Das
programming language.
Objectives &
Outline ◦ Warning: Most database systems implement their own variant of the
Functions and standard syntax
Procedural
Constructs • Compound statement: begin . . . end
Triggers
Triggers :
◦ May contain multiple SQL statements between begin and end.
Functionality vs
Performance ◦ Local variables can be declared within a compound statements
Module Summary

Database Management Systems Partha Pratim Das 15.12


Language Constructs (2): while and repeat

Module 15
• while loop:
Partha Pratim
Das while boolean expression do
Objectives &
sequence of statements;
Outline end while;
Functions and
Procedural • repeat loop:
Constructs
repeat
Triggers
Triggers : sequence of statements;
Functionality vs
Performance until boolean expression
Module Summary end repeat;

Database Management Systems Partha Pratim Das 15.13


Language Constructs (3): for

Module 15

Partha Pratim • for loop


Das
◦ Permits iteration over all results of a query
Objectives &
Outline • Find the budget of all departments:
Functions and
Procedural
declare n integer default 0;
Constructs for r as
Triggers
Triggers :
select budget from department
Functionality vs
Performance
do
Module Summary set n = n + r.budget
end for;

Database Management Systems Partha Pratim Das 15.14


Language Constructs (4): if-then-else

Module 15 • Conditional statements


Partha Pratim ◦ if-then-else
Das
◦ case
Objectives &
Outline • if-then-else statement
Functions and if boolean expression then
Procedural
Constructs sequence of statements;
Triggers elseif boolean expression then
Triggers :
Functionality vs
Performance
sequence of statements;
Module Summary ···
else
sequence of statements;
end if;
• The if statement supports the use of optional elseif clauses and a default else clause.
• Example procedure: registers student after ensuring classroom capacity is not exceeded
◦ Returns 0 on success and -1 if capacity is exceeded
◦ See book (page 177) for details
Database Management Systems Partha Pratim Das 15.15
Language Constructs (5): Simple case

Module 15

Partha Pratim • Simple case statement


Das
case variable
Objectives &
Outline
when value1 then
Functions and
sequence of statements;
Procedural
Constructs
when value2 then
Triggers sequence of statements;
Triggers :
Functionality vs
···
Performance
else
Module Summary
sequence of statements;
end case;
• The when clause of the case statement defines the value that when satisfied
determines the flow of control

Database Management Systems Partha Pratim Das 15.16


Language Constructs (6): Searched case

Module 15

Partha Pratim • Searched case statement


Das
case
Objectives &
Outline
when sql-expression = value1 then
Functions and
sequence of statements;
Procedural
Constructs
when sql-expression = value2 then
Triggers sequence of statements;
Triggers :
Functionality vs
···
Performance
else
Module Summary
sequence of statements;
end case;
• Any supported SQL expression can be used here. These expressions can contain
references to variables, parameters, special registers, and more.

Database Management Systems Partha Pratim Das 15.17


Language Constructs (7): Exception

Module 15

Partha Pratim • Signaling of exception conditions, and declaring handlers for exceptions
Das

Objectives &
Outline
declare out of classroom seats condition
Functions and
declare exit handler for out of classroom seats
Procedural
Constructs
begin
Triggers ...
Triggers :
Functionality vs
signal out of classroom seats
Performance
...
Module Summary
end
◦ The handler here is exit – causes enclosing begin . . . end to be terminate and exit
◦ Other actions possible on exception

Database Management Systems Partha Pratim Das 15.18


External Language Routines*

Module 15 • SQL:1999 allows the definition of functions / procedures in an imperative programming


Partha Pratim language, (Java, C#, C or C++) which can be invoked from SQL queries
Das
• Such functions can be more efficient than functions defined in SQL, and computations
Objectives &
Outline that cannot be carried out in SQL can be executed by these functions
Functions and
Procedural
• Declaring external language procedures and functions
Constructs

Triggers
Triggers :
create procedure dept count proc(
Functionality vs
Performance
in dept name varchar(20),
Module Summary out count integer)
language C
external name ’/usr/avi/bin/dept count proc’

create function dept count(dept name varchar(20))


returns integer
language C
external name ‘/usr/avi/bin/dept count’
Database Management Systems Partha Pratim Das 15.19
External Language Routines (2)*

Module 15

Partha Pratim • Benefits of external language functions/procedures:


Das
◦ More efficient for many operations, and more expressive power
Objectives &
Outline • Drawbacks
Functions and
Procedural ◦ Code to implement function may need to be loaded into database system and
Constructs
executed in the database system’s address space.
Triggers
Triggers : . Risk of accidental corruption of database structures
Functionality vs
Performance . Security risk, allowing users access to unauthorized data
Module Summary
◦ There are alternatives, which give good security at the cost of performance
◦ Direct execution in the database system’s space is used when efficiency is more
important than security

Database Management Systems Partha Pratim Das 15.20


External Language Routines (3)*: Security

Module 15

Partha Pratim • To deal with security problems, we can do one of the following:
Das
◦ Use sandbox techniques
Objectives &
Outline . That is, use a safe language like Java, which cannot be used to access/damage
Functions and
Procedural
other parts of the database code
Constructs
◦ Run external language functions/procedures in a separate process, with no access to
Triggers
Triggers :
the database process’ memory
Functionality vs
Performance . Parameters and results communicated via inter-process communication
Module Summary
• Both have performance overheads
• Many database systems support both above approaches as well as direct executing in
database system address space

Database Management Systems Partha Pratim Das 15.21


Module 15

Partha Pratim
Das

Objectives &
Outline

Functions and
Procedural
Constructs

Triggers
Triggers :
Functionality vs
Performance

Module Summary

Triggers

Database Management Systems Partha Pratim Das 15.22


Trigger

Module 15

Partha Pratim • A trigger defines a set of actions that are performed in response to an insert, update,
Das
or delete operation on a specified table
Objectives &
Outline ◦ When such an SQL operation is executed, the trigger is said to have been activated
Functions and ◦ Triggers are optional
Procedural
Constructs ◦ Triggers are defined using the create trigger statement
Triggers
Triggers :
• Triggers can be used
Functionality vs
Performance ◦ To enforce data integrity rules via referential constraints and check constraints
Module Summary ◦ To cause updates to other tables, automatically generate or transform values for
inserted or updated rows, or invoke functions to perform tasks such as issuing alerts
• To design a trigger mechanism, we must:
◦ Specify the events / (like update, insert, or delete) for the trigger to executed
◦ Specify the time (BEFORE or AFTER) of execution
◦ Specify the actions to be taken when the trigger executes
• Syntax of triggers may vary across systems
Database Management Systems Partha Pratim Das 15.23
Types of Triggers: BEFORE

Module 15

Partha Pratim • BEFORE triggers


Das
◦ Run before an update, or insert
Objectives &
Outline ◦ Values that are being updated or inserted can be modified before the database is
Functions and actually modified. You can use triggers that run before an update or insert to:
Procedural
Constructs . Check or modify values before they are actually updated or inserted in the
Triggers database
Triggers :
Functionality vs
Performance − Useful if user-view and internal database format differs
Module Summary . Run other non-database operations coded in user-defined functions
• BEFORE DELETE triggers
◦ Run before a delete
. Checks values (a raises an error, if necessary)

Database Management Systems Partha Pratim Das 15.24


Types of Triggers (2): AFTER

Module 15

Partha Pratim • AFTER triggers


Das
◦ Run after an update, insert, or delete
Objectives &
Outline ◦ You can use triggers that run after an update or insert to:
Functions and
Procedural
. Update data in other tables
Constructs
− Useful for maintain relationships between data or keep audit trail
Triggers
Triggers : . Check against other data in the table or in other tables
Functionality vs
Performance
− Useful to ensure data integrity when referential integrity constraints aren’t
Module Summary
appropriate, or
− when table check constraints limit checking to the current table only
. Run non-database operations coded in user-defined functions
− Useful when issuing alerts or to update information outside the database

Database Management Systems Partha Pratim Das 15.25


Row Level and Statement Level Triggers

Module 15

Partha Pratim
There are two types of triggers based on the level at which the triggers are applied:
Das
• Row level triggers are executed whenever a row is affected by the event on which the
Objectives &
Outline
trigger is defined.
Functions and ◦ Let Employee be a table with 100 rows. Suppose an update statement is executed
Procedural
Constructs to increase the salary of each employee by 10%. Any row level update trigger
Triggers configured on the table Employee will affect all the 100 rows in the table during this
Triggers :
Functionality vs update.
Performance

Module Summary • Statement level triggers perform a single action for all rows affected by a statement,
instead of executing a separate action for each affected row.
◦ Used for each statement instead of for each row
◦ Uses referencing old table or referencing new table to refer to temporary tables
called transition tables containing the affected rows
◦ Can be more efficient when dealing with SQL statements that update a large
number of rows

Database Management Systems Partha Pratim Das 15.26


Triggering Events and Actions in SQL

Module 15
• Triggering event can be an insert, delete or update
Partha Pratim
Das • Triggers on update can be restricted to specific attributes
Objectives & ◦ For example, after update of grade on takes
Outline

Functions and • Values of attributes before and after an update can be referenced
Procedural
Constructs ◦ referencing old row as : for deletes and updates
Triggers ◦ referencing new row as : for inserts and updates
Triggers :
Functionality vs
Performance • Triggers can be activated before an event, which can serve as extra constraints.
Module Summary For example, convert blank grades to null.
create trigger setnull trigger before update of takes
referencing new row as nrow
for each row
when (nrow.grade = ‘ ‘)
begin atomic
set nrow.grade = null;
end;
Database Management Systems Partha Pratim Das 15.27
Trigger to Maintain credits earned value

Module 15

Partha Pratim
create trigger credits earned after update of grade on (takes)
Das referencing new row as nrow
Objectives & referencing old row as orow
Outline
for each row
Functions and
Procedural when nrow.grade <>’F’ and nrow.grade is not null
Constructs

Triggers
and (orow.grade = ’F’ or orow.grade is null)
Triggers : begin atomic
Functionality vs
Performance update student
Module Summary
set tot cred= tot cred +
(select credits
from course
where course.course id=nrow.course id)
where student.id = nrow.id;
end;

Database Management Systems Partha Pratim Das 15.28


How to use triggers? PPD

Module 15

Partha Pratim • The optimal use of DML triggers is for short, simple, and easy to maintain write
Das
operations that act largely independent of an applications business logic.
Objectives &
Outline • Typical and recommended uses of triggers include:
Functions and
Procedural
◦ Logging changes to a history table
Constructs ◦ Auditing users and their actions against sensitive tables
Triggers
Triggers :
◦ Adding additional values to a table that may not be available to an application (due
Functionality vs
Performance
to security restrictions or other limitations), such as:
Module Summary . Login/user name
. Time an operation occurs
. Server/database name
◦ Simple validation
Source: SQL Server triggers: The good and the scary

Database Management Systems Partha Pratim Das 15.29


How not to use triggers? PPD

Module 15

Partha Pratim • Triggers are like Lays: Once you pop, you can’t stop
Das
• One of the greatest challenges for architects and developers is to ensure that
Objectives &
Outline ◦ triggers are used only as needed, and
Functions and
Procedural
◦ to not allow them to become a one-size-fits-all solution for any data needs that
Constructs happen to come along
Triggers
Triggers : • Adding triggers is often seen as faster and easier than adding code to an application,
Functionality vs
Performance but the cost of doing so is compounded over time with each added line of code
Module Summary
Source: SQL Server triggers: The good and the scary

Database Management Systems Partha Pratim Das 15.30


How to use triggers? (2) PPD

Module 15

Partha Pratim • Triggers can become dangerous when:


Das
◦ There are too many
Objectives &
Outline ◦ Trigger code becomes complex
Functions and ◦ Triggers go cross-server - across databases over network
Procedural
Constructs ◦ Triggers call triggers
Triggers ◦ Recursive triggers are set to ON. This database-level setting is set to off by default
Triggers :
Functionality vs ◦ Functions, stored procedures, or views are in triggers
Performance

Module Summary
◦ Iteration occurs
Source: SQL Server triggers: The good and the scary

Database Management Systems Partha Pratim Das 15.31


Module Summary PPD

Module 15

Partha Pratim • Familiarized with functions and procedures in SQL


Das
• Understood the triggers
Objectives &
Outline • Familiarized with some of the performance issues of triggers
Functions and
Procedural
Constructs

Triggers
Triggers :
Functionality vs
Performance

Module Summary

Slides used in this presentation are borrowed from http://db-book.com/ with kind
permission of the authors.
Edited and new slides are marked with “PPD”.

Database Management Systems Partha Pratim Das 15.32


Module 16

Partha Pratim
Das

Week Recap

Objectives &
Database Management Systems
Outline
Module 16: Formal Relational Query Languages/1
Relational
Algebra
Select
Project
Union
Difference Partha Pratim Das
Intersection
Cartesian Product
Rename Department of Computer Science and Engineering
Division Indian Institute of Technology, Kharagpur
Module Summary
[email protected]

Database Management Systems Partha Pratim Das 16.1


Week Recap PPD

Module 16

Partha Pratim • SQL Examples have been practiced for basic query structures
Das
• Nested Subquery in SQL
Week Recap

Objectives &
• Data Modification
Outline

Relational
• SQL expressions for Join and Views
Algebra
Select
• Transactions
Project
Union
• Integrity Constraints
Difference
Intersection
• More data types in SQL
Cartesian Product
Rename
• Authorization in SQL
Division
• Functions and Procedures in SQL
Module Summary
• Triggers

Database Management Systems Partha Pratim Das 16.2


Module Objectives PPD

Module 16

Partha Pratim • To understand formal query language through relational algebra


Das

Week Recap

Objectives &
Outline

Relational
Algebra
Select
Project
Union
Difference
Intersection
Cartesian Product
Rename
Division

Module Summary

Database Management Systems Partha Pratim Das 16.3


Module Outline PPD

Module 16

Partha Pratim • Relational Algebra


Das

Week Recap

Objectives &
Outline

Relational
Algebra
Select
Project
Union
Difference
Intersection
Cartesian Product
Rename
Division

Module Summary

Database Management Systems Partha Pratim Das 16.4


Formal Relational Query Language

Module 16

Partha Pratim • Relational Algebra


Das
◦ Procedural and Algebra based
Week Recap

Objectives &
• Tuple Relational Calculus
Outline
◦ Non-Procedural and Predicate Calculus based
Relational
Algebra • Domain Relational Calculus
Select
Project ◦ Non-Procedural and Predicate Calculus based
Union
Difference
Intersection
Cartesian Product
Rename
Division

Module Summary

Database Management Systems Partha Pratim Das 16.5


PPD

Module 16

Partha Pratim
Das

Week Recap

Objectives &
Outline

Relational
Algebra
Select
Project
Union
Difference
Intersection
Cartesian Product
Rename
Division
Relational Algebra
Module Summary

Database Management Systems Partha Pratim Das 16.6


Relational Algebra PPD

Module 16

Partha Pratim • Created by Edgar F Codd at IBM in 1970


Das
• Procedural language
Week Recap

Objectives &
• Six basic operators
Outline
◦ select: σ
Relational
Algebra ◦ project: Π
Select
Project
◦ union: ∪
Union
◦ set difference: −
Difference
Intersection ◦ Cartesian product: x
Cartesian Product
Rename
◦ rename: ρ
Division
• The operators take one or two relations as inputs and produce a new relation as a result
Module Summary

Database Management Systems Partha Pratim Das 16.7


Select Operation PPD

Module 16
• Notation: σp (r )
Partha Pratim
Das • p is called the selection predicate
Week Recap • Defined as:
Objectives &
Outline
σp (r ) = {t|t ∈ r and p(t)}
Relational
Algebra where p is a formula in propositional calculus consisting of
Select
terms connected by : ∧ (and), ∨ (or), ¬ (not)
Project
Union Each terms is one of:
Difference
Intersection
Cartesian Product
Rename < attribute > op < attribute > or < constant >
Division

Module Summary
where op is one of: =, 6=, >, ≥ . < . ≤

• Example of selection:
σdept name = ’Physics’ (instructor )
Database Management Systems Partha Pratim Das 16.8
Project Operation PPD

Module 16

Partha Pratim
• Notation: ΠA1 ,A2 ,...Ak (r)
Das where A1 , A2 are attribute names and r is a relation
Week Recap • The result is defined as the relation of k columns
Objectives &
Outline
obtained by erasing the columns that are not listed
Relational
Algebra
• Duplicate rows removed from result, since relations
Select are sets
Project
Union • Example: To eliminate the dept name attribute of
Difference
Intersection
instructor
Cartesian Product
Rename
ΠID,name,salary (instructor )
Division

Module Summary

Database Management Systems Partha Pratim Das 16.9


Union Operation PPD

Module 16
• Notation: r ∪ s
Partha Pratim
Das • Defined as: r ∪ s = {t|t ∈ r or t ∈ s}
Week Recap • For r ∪ s to be valid.
Objectives &
Outline a) r, s must have the same arity (same number of
Relational attributes)
Algebra
Select
b) The attribute domains must be compatible (ex-
Project
Union
ample: 2nd column of r deals with the same
Difference type of values as does the 2nd column of s)
Intersection
Cartesian Product
c) Example: to find all courses taught in the Fall
Rename 2009 semester, or in the Spring 2010 semester,
Division

Module Summary
or in both
Πcourse id (σsemester =“Fall”∧year =2009 (section)) ∪ Πcourse id (σsemester =“Spring ”∧year =2010 (section))

Database Management Systems Partha Pratim Das 16.10


Difference Operation PPD

Module 16
• Notation r − s
Partha Pratim
Das • Defined as: r − s = {t|t ∈ r and t ∈
/ s}
Week Recap • Set differences must be taken between compatible
Objectives &
Outline
relations
Relational ◦ r and s must have the same arity
Algebra
Select
◦ attribute domains of r and s must be compatible
Project
Union • Example: to find all courses taught in the Fall 2009
Difference
Intersection
semester, but not in the Spring 2010 semester
Cartesian Product
Rename
Division
Πcourse id (σsemester =“Fall”∧year =2009 (section))−
Module Summary Πcourse id (σsemester =“Spring ”∧year =2010 (section))

Database Management Systems Partha Pratim Das 16.11


Intersection Operation

Module 16

Partha Pratim
• Notation: r ∩ s
Das
• Defined as:
Week Recap

Objectives &
Outline
r ∩ s = {t|t ∈ r and t ∈ s}
Relational
Algebra • Assume:
Select
Project ◦ r, s have the same arity
Union
Difference
◦ attributes of r and s are compatible
Intersection
Cartesian Product
• Note: r ∩ s = r - (r - s)
Rename
Division

Module Summary

Database Management Systems Partha Pratim Das 16.12


Cartesian-Product Operation PPD

Module 16

Partha Pratim
Das
• Notation r × s
Week Recap

Objectives & • Defined as:


Outline

Relational
Algebra
r × s = {t q|t ∈ r and q ∈ s}
Select
Project
Union
• Assume that attributes of r (R) and s(S) are disjoint.
Difference (That is, R ∩ S = φ)
Intersection
Cartesian Product • If attributes of r(R) and s(S) are not disjoint, then
Rename
Division renaming must be used
Module Summary

Database Management Systems Partha Pratim Das 16.13


Rename Operation

Module 16

Partha Pratim • Allows us to name, and therefore to refer to, the results of relational-algebra expressions.
Das
• Allows us to refer to a relation by more than one name.
Week Recap

Objectives &
• Example:
Outline

Relational ρx (E )
Algebra
Select
returns the expression E under the name X
Project
Union • If a relational-algebra expression E has arity n, then
Difference
Intersection
Cartesian Product ρx(A1 ,A2 ,··· ,An ) (E )
Rename
Division

Module Summary
returns the result of expression E under the name X, and with the attributes renamed to

A1 , A2 , . . . , An

.
Database Management Systems Partha Pratim Das 16.14
Division Operation PPD

Module 16

Partha Pratim
• The division operation is applied to two relations
Das
• R(Z) ÷ S(X), where X subset Z. Let Y = Z - X (and hence Z = X ∪ Y); that is, let Y be the set of
Week Recap attributes of R that are not attributes of S
Objectives &
Outline
• The result of DIVISION is a relation T(Y) that includes a tuple t if tuples tR appear in R with tR [Y] =
t, and with
Relational
Algebra ◦ tR [X ] = ts for every tuple ts in S.
Select
Project • For a tuple t to appear in the result T of the DIVISION, the values in t must appear in R in
Union
combination with every tuple in S
Difference
Intersection • Division is a derived operation and can be expressed in terms of other operations
Cartesian Product
Rename
Division
• r ÷ s ≡ ΠR−S (r ) − ΠR−S ((ΠR−S (r ) × s) − ΠR−S,S (r ))
Module Summary

Database Management Systems Partha Pratim Das 16.15


Division Examples PPD

Module 16

Partha Pratim
Das

Week Recap

Objectives &
Outline

Relational
Algebra
Select
Project
Union
Difference
Intersection
Cartesian Product
Rename
Division

Module Summary

Database Management Systems Partha Pratim Das 16.16


Division Examples (2) PPD

Module 16

Partha Pratim
Das

Week Recap

Objectives &
Outline

Relational
Algebra
Select
Project
Union
Difference
Intersection
Cartesian Product
Rename
Division

Module Summary

Database Management Systems Partha Pratim Das 16.17


Division Examples (3) PPD

Module 16

Partha Pratim
Das

Week Recap

Objectives &
Outline

Relational
Algebra
Select
Project
Union
Difference
Intersection
Cartesian Product
Rename
Division

Module Summary

Database Management Systems Partha Pratim Das 16.18


Division Example (4) PPD

Module 16
• Relations r, s:
Partha Pratim
Das

Week Recap

Objectives &
Outline

Relational
Algebra
Select
Project
Union
Difference
Intersection
Cartesian Product
Rename
Division

Module Summary
e.g. A is customer name
B is branch-name
1 and 2 here show two specific branch-names
(Find customers who have an account in all
branches of the bank)
• r ÷ s:
Database Management Systems Partha Pratim Das 16.19
Division Example (5) PPD

Module 16
• Relations r, s:
Partha Pratim
Das

Week Recap

Objectives &
Outline

Relational
Algebra
Select
Project
Union
Difference
Intersection
Cartesian Product
Rename e.g. Students who have taken both “a” and “b”
Division courses, with instructor “1”
Module Summary
(Find students who have taken all courses given
• r ÷ s: by instructor 1)

Source: db.fcngroup.nl/silberslides/Divsion
Database Management Systems Partha Pratim Das 16.20
Module Summary

Module 16

Partha Pratim • Discussed relational algebra with examples


Das

Week Recap

Objectives &
Outline

Relational
Algebra
Select
Project
Union
Difference
Intersection
Cartesian Product Slides used in this presentation are borrowed from http://db-book.com/ with kind
Rename
Division
permission of the authors.
Module Summary Edited and new slides are marked with “PPD”.

Database Management Systems Partha Pratim Das 16.21


Module 17

Partha Pratim
Das

Objectives &
Outline Database Management Systems
Predicate Logic
Module 17: Formal Relational Query Languages/2
Tuple Relational
Calculus

Domain
Relational
Calculus

Equivalence of
Partha Pratim Das
Algebra and
Calculus
Department of Computer Science and Engineering
Module Summary Indian Institute of Technology, Kharagpur

[email protected]

Database Management Systems Partha Pratim Das 17.1


Module Recap PPD

Module 17

Partha Pratim • Relational Algebras and its Operations


Das

Objectives &
Outline

Predicate Logic

Tuple Relational
Calculus

Domain
Relational
Calculus

Equivalence of
Algebra and
Calculus

Module Summary

Database Management Systems Partha Pratim Das 17.2


Module Objectives PPD

Module 17

Partha Pratim • To understand formal calculus-based query language through relational algebra
Das

Objectives &
Outline

Predicate Logic

Tuple Relational
Calculus

Domain
Relational
Calculus

Equivalence of
Algebra and
Calculus

Module Summary

Database Management Systems Partha Pratim Das 17.3


Module Outline PPD

Module 17

Partha Pratim • Tuple Relational Calculus (Overview only)


Das
• Domain Relational Calculus (Overview only)
Objectives &
Outline • Equivalence of Algebra and Calculus
Predicate Logic

Tuple Relational
Calculus

Domain
Relational
Calculus

Equivalence of
Algebra and
Calculus

Module Summary

Database Management Systems Partha Pratim Das 17.4


Formal Relational Query Language

Module 17

Partha Pratim • Relational Algebra


Das
◦ Procedural and Algebra based
Objectives &
Outline • Tuple Relational Calculus
Predicate Logic
◦ Non-Procedural and Predicate Calculus based
Tuple Relational
Calculus • Domain Relational Calculus
Domain
Relational ◦ Non-Procedural and Predicate Calculus based
Calculus

Equivalence of
Algebra and
Calculus

Module Summary

Database Management Systems Partha Pratim Das 17.5


PPD

Module 17

Partha Pratim
Das

Objectives &
Outline

Predicate Logic

Tuple Relational
Calculus

Domain
Relational
Calculus

Equivalence of
Algebra and
Calculus

Module Summary Predicate Logic

Database Management Systems Partha Pratim Das 17.6


Predicate Logic

Module 17

Partha Pratim
Predicate Logic or Predicate Calculus is an extension of Propositional Logic or
Das Boolean Algebra.
Objectives &
Outline
It adds the concept of predicates and quantifiers to better capture the meaning of
Predicate Logic

Tuple Relational
statements that cannot be adequately expressed by propositional logic.
Calculus

Domain
Relational
Tuple Relational Calculus and Domain Relational Calculus are based on Predicate
Calculus
Calculus
Equivalence of
Algebra and
Calculus

Module Summary

Database Management Systems Partha Pratim Das 17.7


Predicate

Module 17

Partha Pratim • Consider the statement, “x is greater than 3”. It has two parts. The first part, the
Das
variable x, is the subject of the statement. The second part, “is greater than 3”, is the
Objectives &
Outline
predicate. It refers to a property that the subject of the statement can have.
Predicate Logic • The statement “x is greater than 3” can be denoted by P(x) where P denotes the
Tuple Relational
Calculus
predicate “is greater than 3” and x is the variable.
Domain • The predicate P can be considered as a function. It tells the truth value of the
Relational
Calculus statement P(x) at x. Once a value has been assigned to the variable x, the statement
Equivalence of P(x) becomes a proposition and has a truth or false value.
Algebra and
Calculus
• In general, a statement involving n variables x1 , x2 , x3 , · · · , xn can be denoted by
Module Summary
P(x1 , x2 , x3 , · · · , xn ). Here P is also referred to as n-place predicate or a n-ary predicate.

Database Management Systems Partha Pratim Das 17.8


Quantifiers

Module 17

Partha Pratim
In predicate logic, predicates are used alongside quantifiers to express the extent to which a
Das predicate is true over a range of elements. Using quantifiers to create such propositions is
Objectives & called quantification. There are two types of quantifiers:
Outline

Predicate Logic
• Universal Quantifier
Tuple Relational
Calculus
• Existential Quantifier
Domain
Relational
Calculus

Equivalence of
Algebra and
Calculus

Module Summary

Database Management Systems Partha Pratim Das 17.9


Universal Quantifier

Module 17

Partha Pratim
Universal Quantification: Mathematical statements sometimes assert that a property is
Das true for all the values of a variable in a particular domain, called the domain of discourse
Objectives &
Outline
• Such a statement is expressed using universal quantification.
Predicate Logic • The universal quantification of P(x) for a particular domain is the proposition that
Tuple Relational
Calculus
asserts that P(x) is true for all values of x in this domain
Domain • The domain is very important here since it decides the possible values of x
Relational
Calculus
• Formally, The universal quantification of P(x) is the statement “P(x) for all values of x
Equivalence of
Algebra and in the domain”.
Calculus

Module Summary
• The notation ∀P(x) denotes the universal quantification of P(x). Here ∀ is called the
universal quantifier. ∀P(x) is read as “for all x P(x)”.
• Example: Let P(x) be the statement “x + 2 > x“. What is the truth value of the
statement ∀x P(x)?
Solution: As x + 2 is greater than x for any real number, so P(x) ≡ T for all x or
∀x P(x) ≡ T
Database Management Systems Partha Pratim Das 17.10
Existential Quantifier

Module 17

Partha Pratim
Existential Quantification: Some mathematical statements assert that there is an
Das element with a certain property. Such statements are expressed by existential
Objectives & quantification. Existential quantification can be used to form a proposition that is true if
Outline
and only if P(x) is true for at least one value of x in the domain.
Predicate Logic

Tuple Relational • Formally, the existential quantification of P(x) is the statement ”There exists an
Calculus
element x in the domain such that P(x)”.
Domain
Relational
Calculus
• The notation ∃P(x) denotes the existential quantification of P(x). Here ∃ is called the
Equivalence of existential quantifier. ∃P(x) is read as “There is atleast one such x such that P(x)”
Algebra and
Calculus • Example: Let P(x) be the statement “x > 5”. What is the truth value of the
Module Summary statement ∃xP(x)?
Solution: P(x) is true for all real numbers greater than 5and false for all real numbers
less than 5. So ∃x P(x) ≡ T

Database Management Systems Partha Pratim Das 17.11


PPD

Module 17

Partha Pratim
Das

Objectives &
Outline

Predicate Logic

Tuple Relational
Calculus

Domain
Relational
Calculus

Equivalence of
Algebra and
Calculus

Module Summary Tuple Relational Calculus

Database Management Systems Partha Pratim Das 17.12


Tuple Relational Calculus

Module 17

Partha Pratim
TRC is a non-procedural query language, where each query is of the form
Das

Objectives & {t | P(t)}


Outline

Predicate Logic where t = resulting tuples,


Tuple Relational
Calculus
P(t) = known as predicate and these are the conditions that are used to fetch t.
Domain
P(t) may have various conditions logically combined with OR (∨), AND (∧), NOT(¬).
Relational
Calculus

Equivalence of
Algebra and It also uses quantifiers:
Calculus
∃t ∈ r (Q(t)) = “there exists” a tuple in t in relation r such that predicate Q(t) is true.
Module Summary
∀t ∈ r (Q(t)) = Q(t) is true “for all” tuples in relation r.

• {P | ∃S ∈ Students and (S.CGPA > 8 ∧ P.name = S.sname ∧ P.age = S.age)} :


returns the name and age of students with a CGPA above 8.

Database Management Systems Partha Pratim Das 17.13


Predicate Calculus Formula

Module 17

Partha Pratim a) Set of attributes and constants


Das
b) Set of comparison operators: (e.g ., <, ≤, =, 6=, >, ≥)
Objectives &
Outline c) Set of connectives: and (∧), or (∨), not (¬)
Predicate Logic

Tuple Relational
d) Implication (⇒) : x ⇒ y , if x if true, then y is true
Calculus x ⇒ y ≡ ¬x ∨ y
Domain
Relational
e) Set of quantifiers:
Calculus
• ∃t ∈ r (Q(t)) ≡ “there exists” a tuple in t in relation r such that predicate Q(t) is true
Equivalence of
Algebra and
• ∀t ∈ r (Q(t)) ≡ Q is true “for all” tuples t in relation r
Calculus

Module Summary

Database Management Systems Partha Pratim Das 17.14


TRC Example

Module 17
Solution:
Partha Pratim
Das
Student {t.Fname | Student(t) ∧ t.age > 21}
Fname Lname Age Course
Objectives & David Sharma 27 DBMS
Outline {t.Fname | t ∈ Student ∧ t.age > 21}
Aaron Lilly 17 JAVA
Predicate Logic
Sahil Khan 19 Python
Tuple Relational
Calculus Sachin Rao 20 DBMS
Varun George 23 JAVA {t | ∃s ∈ Student(s.age > 21 ∧ t.Fname = s.Fname)}
Domain
Relational Simi Verma 22 JAVA
Calculus Fname
Equivalence of David
Algebra and Q.1 Obtain the first name of students whose
Calculus
Varun
age is greater than 21. Simi
Module Summary

Database Management Systems Partha Pratim Das 17.15


TRC Example (2)

Module 17
Consider the relational schema
Partha Pratim
Das
student(rollNo, name, year , courseId)
course(courseId, cname, teacher )
Objectives &
Outline Q.2 Find out the names of all students who have taken the course name ‘DBMS’.
Predicate Logic

Tuple Relational • {t | ∃s ∈ student ∃c ∈ course(s.courseId = c.courseId ∧ c.cname = ‘DBMS’ ∧t.name = s.name)}


Calculus

Domain
Relational
• {s.name | s ∈ student ∧ ∃c ∈ course(s.courseId = c.courseId ∧ c.cname= ‘DBMS’ )}
Calculus

Equivalence of Q.3 Find out the names of all students and their rollNo who have taken the course name ‘DBMS’.
Algebra and
Calculus

Module Summary
• {s.name, s.rollNo | s ∈ student ∧ ∃c ∈ course(s.courseId = c.courseId ∧ c.cname = ‘DBMS’ )}
• {t | ∃s ∈ student ∃c ∈ course(s.courseId = c.courseId ∧ c.cname =‘DBMS’
∧t.name = s.name ∧ t.rollNo = s.rollNo)}

Database Management Systems Partha Pratim Das 17.16


TRC Example (3)

Module 17
Consider the following relations:
Partha Pratim
Das
Flights(flno, from, to, distance, departs, arrives)
Aircraft(aid, aname, cruisingrange)
Objectives & Certified(eid, aid)
Outline
Employees(eid, ename, salary)
Predicate Logic

Tuple Relational Q.4. Find the eids of pilots certified for Boeing aircraft.
Calculus

Domain RA
Relational
Calculus
Πeid (σaname=‘Boeing 0 (Aircraft n
o Certified))
Equivalence of
TRC
Algebra and
Calculus
• {C .eid | C ∈ Certified ∧ ∃A ∈ Aircraft(A.aid = C .aid ∧ A.aname = ‘Boeing’)}
Module Summary
• {T | ∃C ∈ Certified∃A ∈ Aircraft(A.aid = C .aid ∧ A.aname = ‘Boeing’
∧T .eid = C .eid)}

Database Management Systems Partha Pratim Das 17.17


TRC Example (4)

Module 17
Consider the following relations:
Partha Pratim
Das
Flights(flno, from, to, distance, departs, arrives)
Aircraft(aid, aname, cruisingrange)
Objectives & Certified(eid, aid)
Outline
Employees(eid, ename, salary)
Predicate Logic

Tuple Relational Q.5. Find the names and salaries of certified pilots working on Boeing aircrafts.
Calculus

Domain RA
Relational
Calculus
Πename,salary (σaname=‘Boeing ‘ (Aircraft n
o Certified n
o Employees))
Equivalence of
TRC
Algebra and {P | ∃E ∈ Employees ∃C ∈ Certified ∃A ∈ Aircraft(A.aid = C .aid ∧ A.aname=
Calculus
‘Boeing’∧E .eid = C .eid ∧ P.ename = E .ename ∧ P.salary = E .salary )}
Module Summary

Database Management Systems Partha Pratim Das 17.18


TRC Example (5)

Module 17
Consider the following relations:
Partha Pratim
Das
Flights(flno, from, to, distance, departs, arrives)
Aircraft(aid, aname, cruisingrange)
Objectives & Certified(eid, aid)
Outline
Employees(eid, ename, salary)
Predicate Logic

Tuple Relational Q.6 Identify the flights that can be piloted by every pilot whose salary is more than $100,000.
Calculus
(Hint: The pilot must be certified for at least one plane with a sufficiently large cruising range.)
Domain
Relational
Calculus

Equivalence of
• {F .flno | F ∈ Flights ∧ ∃A ∈ Aircraft∃C ∈ Certified∃E ∈ Employees(A.cruisingrange >
Algebra and F .distance ∧ A.aid = C .aid ∧ E .salary > 100, 000 ∧ E .eid = C .eid)}
Calculus

Module Summary

Database Management Systems Partha Pratim Das 17.19


Safety of Expressions

Module 17

Partha Pratim • It is possible to write tuple calculus expressions that generate infinite relations
Das
• For example,{t | ¬t ∈ r } results in an infinite relation if the domain of any attribute of
Objectives &
Outline relation r is infinite
Predicate Logic
• To guard against the problem, we restrict the set of allowable expressions to safe
Tuple Relational
Calculus expressions
Domain
Relational
• An expression {t | P(t)} in the tuple relational calculus is safe if every component of t
Calculus
appears in one of the relations, tuples, or constants that appear in P.
Equivalence of
Algebra and ◦ NOTE: this is more than just a syntax condition
Calculus
◦ E.g. {t | t[A] = 5 ∨ true} is not safe — it defines an infinite set with attribute
Module Summary
values that do not appear in any relation or tuples or constants in P

Database Management Systems Partha Pratim Das 17.20


PPD

Module 17

Partha Pratim
Das

Objectives &
Outline

Predicate Logic

Tuple Relational
Calculus

Domain
Relational
Calculus

Equivalence of
Algebra and
Calculus

Module Summary Domain Relational Calculus

Database Management Systems Partha Pratim Das 17.21


Domain Relational Calculus

Module 17

Partha Pratim • A non-procedural query language equivalent in power to the tuple relational calculus
Das
• Each query is an expression of the form:
Objectives &
Outline

Predicate Logic {< x1 , x2 , . . . , xn > |P(x1 , x2 , . . . , xn )}


Tuple Relational
Calculus
◦ x1 , x2 , . . . , xn represent domain variables
Domain
Relational ◦ P represents a formula similar to that of the predicate calculus
Calculus

Equivalence of
Algebra and
Calculus

Module Summary

Database Management Systems Partha Pratim Das 17.22


PPD

Module 17

Partha Pratim
Das

Objectives &
Outline

Predicate Logic

Tuple Relational
Calculus

Domain
Relational
Calculus

Equivalence of
Algebra and
Calculus

Module Summary Equivalence of Algebra and Calculus

Database Management Systems Partha Pratim Das 17.23


Equivalence of RA, TRC and DRC PPD

Module 17

Partha Pratim
Das

Objectives &
Outline

Predicate Logic

Tuple Relational
Calculus

Domain
Relational
Calculus

Equivalence of
Algebra and
Calculus

Module Summary

Source: http://www.cs.sfu.ca/CourseCentral/354/louie/Equiv Notations.pdf

Database Management Systems Partha Pratim Das 17.24


Equivalence of RA, TRC and DRC PPD

Module 17

Partha Pratim
Das

Objectives &
Outline

Predicate Logic

Tuple Relational
Calculus

Domain
Relational
Calculus

Equivalence of
Algebra and
Calculus

Module Summary

Source: http://www.cs.sfu.ca/CourseCentral/354/louie/Equiv Notations.pdf

Database Management Systems Partha Pratim Das 17.25


Equivalence of RA, TRC and DRC PPD

Module 17

Partha Pratim
Das

Objectives &
Outline

Predicate Logic

Tuple Relational
Calculus

Domain
Relational
Calculus

Equivalence of
Algebra and
Calculus

Module Summary

Source: http://www.cs.sfu.ca/CourseCentral/354/louie/Equiv Notations.pdf

Database Management Systems Partha Pratim Das 17.26


Equivalence of RA, TRC and DRC PPD

Module 17

Partha Pratim
Das

Objectives &
Outline

Predicate Logic

Tuple Relational
Calculus

Domain
Relational
Calculus

Equivalence of
Algebra and
Calculus

Module Summary

Source: http://www.cs.sfu.ca/CourseCentral/354/louie/Equiv Notations.pdf

Database Management Systems Partha Pratim Das 17.27


Equivalence of RA, TRC and DRC PPD

Module 17

Partha Pratim
Das

Objectives &
Outline

Predicate Logic

Tuple Relational
Calculus

Domain
Relational
Calculus

Equivalence of
Algebra and
Calculus

Module Summary

Source: http://www.cs.sfu.ca/CourseCentral/354/louie/Equiv Notations.pdf

Database Management Systems Partha Pratim Das 17.28


Equivalence of RA, TRC and DRC PPD

Module 17

Partha Pratim
Das

Objectives &
Outline

Predicate Logic

Tuple Relational
Calculus

Domain
Relational
Calculus

Equivalence of
Algebra and
Calculus

Module Summary

Source: http://www.cs.sfu.ca/CourseCentral/354/louie/Equiv Notations.pdf

Database Management Systems Partha Pratim Das 17.29


Equivalence of RA, TRC and DRC PPD

Module 17

Partha Pratim
Das

Objectives &
Outline

Predicate Logic

Tuple Relational
Calculus

Domain
Relational
Calculus

Equivalence of
Algebra and
Calculus

Module Summary

Source: http://www.cs.sfu.ca/CourseCentral/354/louie/Equiv Notations.pdf

Database Management Systems Partha Pratim Das 17.30


Equivalence of RA, TRC and DRC PPD

Module 17

Partha Pratim
Das

Objectives &
Outline

Predicate Logic

Tuple Relational
Calculus

Domain
Relational
Calculus

Equivalence of
Algebra and
Calculus

Module Summary

Source: http://www.cs.sfu.ca/CourseCentral/354/louie/Equiv Notations.pdf


Database Management Systems Partha Pratim Das 17.31
Equivalence of RA, TRC and DRC PPD

Module 17

Partha Pratim
Das

Objectives &
Outline

Predicate Logic

Tuple Relational
Calculus

Domain
Relational
Calculus

Equivalence of
Algebra and
Calculus

Module Summary

Source: http://www.cs.sfu.ca/CourseCentral/354/louie/Equiv Notations.pdf

Database Management Systems Partha Pratim Das 17.32


Module Summary

Module 17

Partha Pratim • Introduced tuple relational and domain relational calculus


Das
• Illustrated equivalence of algebra and calculus
Objectives &
Outline

Predicate Logic

Tuple Relational
Calculus

Domain
Relational
Calculus

Equivalence of
Algebra and
Calculus

Module Summary
Slides used in this presentation are borrowed from http://db-book.com/ with kind
permission of the authors.
Edited and new slides are marked with “PPD”.

Database Management Systems Partha Pratim Das 17.33


Module 18

Partha Pratim
Das

Objectives &
Outline Database Management Systems
Design Process
Abstraction
Module 18: Entity-Relationship Model/1
Models
Design Approach

ER Model
Attributes
Entity Sets
Partha Pratim Das
Relationship
Cardinality
Constraints Department of Computer Science and Engineering
Weak Entity Sets Indian Institute of Technology, Kharagpur
Module Summary
[email protected]

Database Management Systems Partha Pratim Das 18.1


Module Recap PPD

Module 18

Partha Pratim • Predicate Calculus


Das
• Tuple Relational and Domain Relational Calculus
Objectives &
Outline • Equivalence of Relational Algebra and Relational Calculus
Design Process
Abstraction
Models
Design Approach

ER Model
Attributes
Entity Sets
Relationship
Cardinality
Constraints
Weak Entity Sets

Module Summary

Database Management Systems Partha Pratim Das 18.2


Module Objectives PPD

Module 18

Partha Pratim • To understand the Design Process for Database Systems


Das
• To study the E-R Model for real world representation
Objectives &
Outline

Design Process
Abstraction
Models
Design Approach

ER Model
Attributes
Entity Sets
Relationship
Cardinality
Constraints
Weak Entity Sets

Module Summary

Database Management Systems Partha Pratim Das 18.3


Module Outline PPD

Module 18

Partha Pratim • Design Process


Das
• E-R Model
Objectives &
Outline ◦ Entity and Entity Set
Design Process ◦ Relationship
Abstraction
Models . Cardinality
Design Approach

ER Model
◦ Attributes
Attributes ◦ Weak Entity Sets
Entity Sets
Relationship
Cardinality
Constraints
Weak Entity Sets

Module Summary

Database Management Systems Partha Pratim Das 18.4


PPD

Module 18

Partha Pratim
Das

Objectives &
Outline

Design Process
Abstraction
Models
Design Approach

ER Model
Attributes
Entity Sets
Relationship
Cardinality
Constraints
Weak Entity Sets
Design Process
Module Summary

Database Management Systems Partha Pratim Das 18.5


What is Design?

Module 18

Partha Pratim
A Design:
Das
• Satisfies a given (perhaps informal) functional specification
Objectives &
Outline • Conforms to limitations of the target medium
Design Process
Abstraction
• Meets implicit or explicit requirements on performance and resource usage
Models
Design Approach
• Satisfies implicit or explicit design criteria on the form of the artifact
ER Model • Satisfies restrictions on the design process itself, such as its length or cost, or the tools
Attributes
Entity Sets
available for doing the design
Relationship
Cardinality
Constraints
Weak Entity Sets

Module Summary

Database Management Systems Partha Pratim Das 18.6


Role of Abstraction

Module 18 • Disorganized Complexity results from


Partha Pratim
Das
◦ Storage (STM) limitations of human brain – an individual can simultaneously
comprehend of the order of seven, plus or minus two chunks of information
Objectives &
Outline ◦ Speed limitations of human brain – it takes the mind about five seconds to accept a
Design Process new chunk of information
Abstraction
Models • Abstraction provides the major tool to handle Disorganized Complexity by chunking
Design Approach
information
ER Model
Attributes • Ignore inessential details, deal only with the generalized, idealized model of the world
Entity Sets
Relationship
Cardinality Consider: A binary number 110010101001
Constraints
Weak Entity Sets

Module Summary Hard to remembers. Right?

Try the octal form: (110)(010)(101)(001) ⇒ 6251

Or the hex form: (1100)(1010)(1001) ⇒ CA9


Database Management Systems Partha Pratim Das 18.7
Model Building

Module 18
• Physics • Electrical Circuits
Partha Pratim
Das
◦ Time-Distance Equation ◦ Kirchoff’s Loop Equations
Objectives & ◦ Quantum Mechanics ◦ Time Series Signals and FFT
Outline
◦ Transistor Models
Design Process • Chemistry
◦ Schematic Diagram
Abstraction
Models
◦ Valency-Bond Structures ◦ Interconnect Routing
Design Approach
• Geography • Building & Bridges
ER Model
Attributes ◦ Maps ◦ Drawings – Plan, Elevation, Side view
Entity Sets
Relationship
◦ Projections ◦ Finite Element Models
Cardinality
Constraints
Weak Entity Sets • Models are common in all engineering disciplines
Module Summary • Model building follows principles of decomposition, abstraction, and hierarchy
• Each model describes a specific aspect of the system
• Build new models upon old proven models

Database Management Systems Partha Pratim Das 18.8


Design Approach

Module 18 • Requirement Analysis: Analyse the data needs of the prospective database users
Partha Pratim
Das
◦ Planning
◦ System Definition
Objectives &
Outline • Database Designing: Use a modeling framework to create abstraction of the real world
Design Process
◦ Logical Model
Abstraction
Models ◦ Physical Model
Design Approach
• Implementation
ER Model
Attributes ◦ Data Conversion and Loading
Entity Sets
Relationship
◦ Testing
Cardinality
Constraints
Weak Entity Sets

Module Summary

Database Management Systems Partha Pratim Das 18.9


Design Approach (2): Database Designing

Module 18 • Logical Model: Deciding on a good database schema


Partha Pratim
Das
◦ Business Decision: What attributes should we record in the database?
◦ Computer Science Decision: What relation schema should we have and how should
Objectives &
Outline the attributes be distributed among the various relation schema?
Design Process • Physical Model: Deciding on the physical layout of the database
Abstraction
Models
Design Approach

ER Model
Attributes
Entity Sets
Relationship
Cardinality
Constraints
Weak Entity Sets

Module Summary

Database Management Systems Partha Pratim Das 18.10


Design Approach (3): Database Designing: Logical Model

Module 18

Partha Pratim • Entity Relationship Model


Das
◦ Models an enterprise as a collection of entities and relationships
Objectives &
Outline . Entity: A distinguishable “thing” or “object” in the enterprise
Design Process
Abstraction
− Described by a set of attributes
Models
Design Approach
. Relationship: An association among multiple entities
ER Model ◦ Represented by an Entity-Relationship or ER Diagram
Attributes
Entity Sets • Database Normalization (Chapter 8)
Relationship
Cardinality
◦ Formalize what designs are bad, and test for them
Constraints
Weak Entity Sets

Module Summary

Database Management Systems Partha Pratim Das 18.11


PPD

Module 18

Partha Pratim
Das

Objectives &
Outline

Design Process
Abstraction
Models
Design Approach

ER Model
Attributes
Entity Sets
Relationship
Cardinality
Constraints
Weak Entity Sets Entity Relationship (ER) Model
Module Summary

Database Management Systems Partha Pratim Das 18.12


ER Model: Database Modeling

Module 18

Partha Pratim • The ER data model was developed to facilitate database design by allowing specification
Das
of an enterprise schema that represents the overall logical structure of a database
Objectives &
Outline • The ER model is useful in mapping the meanings and interactions of real-world
Design Process enterprises onto a conceptual schema
Abstraction
Models • The ER data model employs three basic concepts:
Design Approach

ER Model
◦ Attributes
Attributes ◦ Entity sets
Entity Sets
Relationship ◦ Relationship sets
Cardinality
Constraints • The ER model also has an associated diagrammatic representation, the ER diagram,
Weak Entity Sets
which can express the overall logical structure of a database graphically
Module Summary

Database Management Systems Partha Pratim Das 18.13


Attributes

Module 18

Partha Pratim • An Attribute is a property associated with and entity / entity set. Based on the values
Das
of certain attributes, an entity can be identified uniquely
Objectives &
Outline • Attribute types:
Design Process ◦ Simple and Composite attributes
Abstraction
Models ◦ Single-valued and Multivalued attributes
Design Approach

ER Model
. Example: Multivalued attribute: phone numbers
Attributes ◦ Derived attributes
Entity Sets
Relationship . Can be computed from other attributes
Cardinality
Constraints . Example: age, given date of birth
Weak Entity Sets

Module Summary • Domain: Set of permitted values for each attribute

Database Management Systems Partha Pratim Das 18.14


Attributes (2): Composite

Module 18

Partha Pratim
Das

Objectives &
Outline

Design Process
Abstraction
Models
Design Approach

ER Model
Attributes
Entity Sets
Relationship
Cardinality
Constraints
Weak Entity Sets

Module Summary

Database Management Systems Partha Pratim Das 18.15


Entity Sets

Module 18

Partha Pratim • An entity is an object that exists and is distinguishable from other objects.
Das
◦ Example: specific person, company, event, plant
Objectives &
Outline • An entity set is a set of entities of the same type that share the same properties.
Design Process
Abstraction
◦ Example: set of all persons, companies, trees, holidays
Models
Design Approach
• An entity is represented by a set of attributes; i.e., descriptive properties possessed by
ER Model all members of an entity set.
Attributes
Entity Sets
◦ Example:
Relationship
instructor = (ID, name, street, city, salary )
Cardinality
Constraints course= (course id, title, credits)
Weak Entity Sets

Module Summary • A subset of the attributes form a primary key of the entity set; that is, uniquely
identifying each member of the set.
◦ Primary key of an entity set is represented by underlining it

Database Management Systems Partha Pratim Das 18.16


Entity Sets – instructor and student

Module 18

Partha Pratim
Das

Objectives &
Outline

Design Process
Abstraction
Models
Design Approach

ER Model
Attributes
Entity Sets
Relationship
Cardinality
Constraints
Weak Entity Sets

Module Summary

Database Management Systems Partha Pratim Das 18.17


Relationship Sets

Module 18

Partha Pratim • A relationship is an association among several entities


Das
Example:
Objectives &
Outline
44553 (Peltier) advisor 22222 (Einstein)
Design Process
student entity relationship set instructor entity
Abstraction
Models
• A relationship set is a mathematical relation among n ≥ 2 entities, each taken from
Design Approach
entity sets
ER Model
Attributes
{(e1 , e2 , . . . en ) | e1 ∈ E1 , e2 ∈ E2 , . . . , en ∈ En }
Entity Sets
Relationship where (e1 , e2 , . . . en ) is a relationship.
Cardinality
Constraints ◦ Example: (44553, 22222) ∈ advisor
Weak Entity Sets

Module Summary

Database Management Systems Partha Pratim Das 18.18


Relationship Set (2) advisor

Module 18

Partha Pratim
Das

Objectives &
Outline

Design Process
Abstraction
Models
Design Approach

ER Model
Attributes
Entity Sets
Relationship
Cardinality
Constraints
Weak Entity Sets

Module Summary

Database Management Systems Partha Pratim Das 18.19


Relationship Sets (3)

Module 18

Partha Pratim • An attribute can also be associated with a relationship set.


Das
• For instance, the advisor relationship set between entity sets instructor and student
Objectives &
Outline may have the attribute date which tracks when the student started being associated
Design Process with the advisor
Abstraction
Models
Design Approach

ER Model
Attributes
Entity Sets
Relationship
Cardinality
Constraints
Weak Entity Sets

Module Summary

Database Management Systems Partha Pratim Das 18.20


Relationship Set (4): Degree

Module 18

Partha Pratim • Binary relationship


Das
◦ involves two entity sets (or degree two).
Objectives &
Outline ◦ most relationship sets in a database system are binary.
Design Process • Relationships between more than two entity sets are rare. Most relationships are binary
Abstraction
Models ◦ Example: students work on research projects under the guidance of an instructor.
Design Approach

ER Model
◦ relationship proj guide is a ternary relationship between instructor, student, and
Attributes project
Entity Sets
Relationship
Cardinality
Constraints
Weak Entity Sets

Module Summary

Database Management Systems Partha Pratim Das 18.21


Attributes (3): Redundant

Module 18

Partha Pratim • Suppose we have entity sets:


Das
◦ instructor, with attributes: ID, name, dept name, salary
Objectives &
Outline ◦ department, with attributes: dept name, building, budget
Design Process • We model the fact that each instructor has an associated department using a
Abstraction
Models relationship set inst dept
Design Approach

ER Model
• The attribute dept name appears in both entity sets. Since it is the primary key for the
Attributes entity set department, it replicates information present in the relationship and is
Entity Sets
Relationship therefore redundant in the entity set instructor and needs to be removed
Cardinality
Constraints • BUT: When converting back to tables, in some cases the attribute gets reintroduced, as
Weak Entity Sets
we will see later
Module Summary

Database Management Systems Partha Pratim Das 18.22


Mapping Cardinality Constraints

Module 18

Partha Pratim • Express the number of entities to which another entity can be associated via a
Das
relationship set.
Objectives &
Outline • Most useful in describing binary relationship sets.
Design Process
Abstraction
• For a binary relationship set the mapping cardinality must be one of the following types:
Models
Design Approach
◦ One to one
ER Model
◦ One to many
Attributes ◦ Many to one
Entity Sets
Relationship ◦ Many to many
Cardinality
Constraints
Weak Entity Sets

Module Summary

Database Management Systems Partha Pratim Das 18.23


Mapping Cardinalities

Module 18

Partha Pratim
Das

Objectives &
Outline

Design Process
Abstraction
Models
Design Approach

ER Model
Attributes
Entity Sets
Relationship
Cardinality
Constraints
Weak Entity Sets

Module Summary

Note: Some elements in A and B may not be mapped to any elements in the other set
Database Management Systems Partha Pratim Das 18.24
Mapping Cardinalities

Module 18

Partha Pratim
Das

Objectives &
Outline

Design Process
Abstraction
Models
Design Approach

ER Model
Attributes
Entity Sets
Relationship
Cardinality
Constraints
Weak Entity Sets

Module Summary

Note: Some elements in A and B may not be mapped to any elements in the other set

Database Management Systems Partha Pratim Das 18.25


Weak Entity Sets

Module 18

Partha Pratim
An entity set may be of two types:
Das
• Strong entity set
Objectives &
Outline ◦ A strong entity set is an entity set that contains sufficient attributes to uniquely
Design Process identify all its entities
Abstraction
Models
◦ In other words, a primary key exists for a strong entity set
Design Approach ◦ Primary key of a strong entity set is represented by underlining it
ER Model
Attributes
• Weak entity set
Entity Sets
Relationship
◦ A weak entity set is an entity set that does not contain sufficient attributes to
Cardinality
Constraints
uniquely identify its entities
Weak Entity Sets ◦ In other words, a primary key does not exist for a weak entity set
Module Summary
◦ However, it contains a partial key called as a discriminator
◦ Discriminator can identify a group of entities from the entity set
◦ Discriminator is represented by underlining with a dashed line

Database Management Systems Partha Pratim Das 18.26


Weak Entity Sets (2)

Module 18

Partha Pratim • Since a weak entity set does not have primary key, it cannot independently exist in the
Das
ER Model
Objectives &
Outline • It features in the model in relationship with a strong entity set. This is called the
Design Process identifying relationship
Abstraction
Models • Primary Key of Weak Entity Set
Design Approach

ER Model
◦ The combination of discriminator and primary key of the strong entity set makes it
Attributes possible to uniquely identify all entities of the weak entity set
Entity Sets
Relationship ◦ Thus, this combination serves as a primary key for the weak entity set.
Cardinality
Constraints
◦ Clearly, this primary key is not formed by the weak entity set completely.
Weak Entity Sets
◦ Primary Key of Weak Entity Set = Its own discriminator + Primary Key of
Module Summary
Strong Entity Set
• Weak entity set must have total participation in the identifying relationship. That is
all its entities must feature in the relationship

Database Management Systems Partha Pratim Das 18.27


Weak Entity Sets (3): Example

Module 18
• Strong Entity Set: Building(building no, building name, address). building no is its
Partha Pratim
Das
primary key
Objectives &
• Weak Entity Set: Apartment(door no, floor). door no is its discriminator as door no
Outline
alone can not identify an apartment uniquely. There may be several other buildings
Design Process
Abstraction
having the same door number
Models
Design Approach
• Relationship: BA between Building and Apartment
ER Model • By total participation in BA, each apartment must be present in at least one building
Attributes
Entity Sets • In contrast, Building has partial participation in BA only as there might exist some
Relationship
Cardinality buildings which has no apartment
Constraints
Weak Entity Sets • Primary Key: To uniquely identify any apartment
Module Summary
◦ First, building no is required to identify the particular building
◦ Second, door no of the apartment is required to uniquely identify the apartment
• Primary key of Apartment = Primary key of Building + Its own discriminator
= building no + door no
Database Management Systems Partha Pratim Das 18.28
Weak Entity Sets (4): Example

Module 18

Partha Pratim
Das • Consider a section entity,
which is uniquely identified
Objectives &
Outline
by a course id, semester,
year, and sec id.
Design Process
Abstraction • Clearly, section entities are
Models
Design Approach
related to course entities.
Suppose we create a rela-
ER Model
Attributes
tionship set sec course be-
Entity Sets tween entity sets section and
Relationship course.
Cardinality
Constraints
Weak Entity Sets
• Note that the information
in sec course is redundant,
Module Summary
since section already has an
attribute course id, which
identifies the course with
which the section is related.

Database Management Systems Partha Pratim Das 18.29


Module Summary

Module 18

Partha Pratim • Introduced the Design Process for Database Systems


Das
• Elucidated the E-R Model for real world representation with entities, entity sets,
Objectives &
Outline attributes, and relationships
Design Process
Abstraction
Models
Design Approach

ER Model
Attributes
Entity Sets
Relationship
Cardinality
Constraints
Weak Entity Sets

Module Summary
Slides used in this presentation are borrowed from http://db-book.com/ with kind
permission of the authors.
Edited and new slides are marked with “PPD”.

Database Management Systems Partha Pratim Das 18.30


Module 19

Partha Pratim
Das

Objectives &
Outline Database Management Systems
ER Diagram
Entity Sets
Module 19: Entity-Relationship Model/2
Relationship Sets
Cardinality
Constraints
Participation
Bounds
Partha Pratim Das
ER Model to
Relational
Schema
Department of Computer Science and Engineering
Entity Sets
Indian Institute of Technology, Kharagpur
Relationship
Composite Attributes
Multivalued
[email protected]
Attributes
Redundancy

Module Summary

Database Management Systems Partha Pratim Das 19.1


Module Recap PPD

Module 19

Partha Pratim • Design Process for Database Systems


Das
• ER Model for real world representation with entities, entity sets, attributes, and
Objectives &
Outline relationships
ER Diagram
Entity Sets
Relationship Sets
Cardinality
Constraints
Participation
Bounds

ER Model to
Relational
Schema
Entity Sets
Relationship
Composite Attributes
Multivalued
Attributes
Redundancy

Module Summary

Database Management Systems Partha Pratim Das 19.2


Module Objectives PPD

Module 19

Partha Pratim • To illustrate ER Diagram notation for ER Models


Das
• To explore translation of ER Models to Relational Schemas
Objectives &
Outline

ER Diagram
Entity Sets
Relationship Sets
Cardinality
Constraints
Participation
Bounds

ER Model to
Relational
Schema
Entity Sets
Relationship
Composite Attributes
Multivalued
Attributes
Redundancy

Module Summary

Database Management Systems Partha Pratim Das 19.3


Module Outline PPD

Module 19

Partha Pratim • ER Diagram


Das
• ER Model to Relational Schema
Objectives &
Outline

ER Diagram
Entity Sets
Relationship Sets
Cardinality
Constraints
Participation
Bounds

ER Model to
Relational
Schema
Entity Sets
Relationship
Composite Attributes
Multivalued
Attributes
Redundancy

Module Summary

Database Management Systems Partha Pratim Das 19.4


PPD

Module 19

Partha Pratim
Das

Objectives &
Outline

ER Diagram
Entity Sets
Relationship Sets
Cardinality
Constraints
Participation
Bounds

ER Model to
Relational
Schema
Entity Sets
Relationship
ER Diagram
Composite Attributes
Multivalued
Attributes
Redundancy

Module Summary

Database Management Systems Partha Pratim Das 19.5


Entity Sets

Module 19

Partha Pratim • Entities can be represented graphically as follows:


Das
◦ Rectangles represent entity sets.
Objectives &
Outline ◦ Attributes are listed inside entity rectangle.
ER Diagram ◦ Underline indicates primary key attributes.
Entity Sets
Relationship Sets
Cardinality
Constraints
Participation
Bounds

ER Model to
Relational
Schema
Entity Sets
Relationship
Composite Attributes
Multivalued
Attributes
Redundancy

Module Summary

Database Management Systems Partha Pratim Das 19.6


Relationship Sets

Module 19

Partha Pratim • Diamonds represent relationship sets.


Das

Objectives &
Outline

ER Diagram
Entity Sets
Relationship Sets
Cardinality
Constraints
Participation
Bounds

ER Model to
Relational
Schema
Entity Sets
Relationship
Composite Attributes
Multivalued
Attributes
Redundancy

Module Summary

Database Management Systems Partha Pratim Das 19.7


Relationship Sets with Attributes

Module 19

Partha Pratim
Das

Objectives &
Outline

ER Diagram
Entity Sets
Relationship Sets
Cardinality
Constraints
Participation
Bounds

ER Model to
Relational
Schema
Entity Sets
Relationship
Composite Attributes
Multivalued
Attributes
Redundancy

Module Summary

Database Management Systems Partha Pratim Das 19.8


Roles

Module 19

Partha Pratim • Entity sets of a relationship need not be distinct Each occurrence of an entity set plays
Das
a “role” in the relationship
Objectives &
Outline • The labels “course id” and “prereq id” are called roles.
ER Diagram
Entity Sets
Relationship Sets
Cardinality
Constraints
Participation
Bounds

ER Model to
Relational
Schema
Entity Sets
Relationship
Composite Attributes
Multivalued
Attributes
Redundancy

Module Summary

Database Management Systems Partha Pratim Das 19.9


Cardinality Constraints

Module 19

Partha Pratim • We express cardinality constraints by drawing either a directed line (→), signifying
Das
“one,” or an undirected line (—), signifying “many,” between the relationship set and
Objectives &
Outline
the entity set.
ER Diagram • One-to-one relationship between an instructor and a student :
Entity Sets
Relationship Sets ◦ A student is associated with at most one instructor via the relationship advisor
Cardinality
Constraints ◦ An instructor is associated with at most one student via the relationship advisor
Participation
Bounds

ER Model to
Relational
Schema
Entity Sets
Relationship
Composite Attributes
Multivalued
Attributes
Redundancy

Module Summary

Database Management Systems Partha Pratim Das 19.10


One-to-Many Relationship

Module 19

Partha Pratim • one-to-many relationship between an instructor and a student


Das
◦ an instructor is associated with several (including 0) students via advisor
Objectives &
Outline ◦ a student is associated with at most one instructor via advisor
ER Diagram
Entity Sets
Relationship Sets
Cardinality
Constraints
Participation
Bounds

ER Model to
Relational
Schema
Entity Sets
Relationship
Composite Attributes
Multivalued
Attributes
Redundancy

Module Summary

Database Management Systems Partha Pratim Das 19.11


Many-to-One Relationships

Module 19

Partha Pratim • many-to-one relationship between a student and an instructor,


Das
◦ an instructor is associated with at most one student via advisor,
Objectives &
Outline ◦ and a student is associated with several (including 0) instructors via advisor
ER Diagram
Entity Sets
Relationship Sets
Cardinality
Constraints
Participation
Bounds

ER Model to
Relational
Schema
Entity Sets
Relationship
Composite Attributes
Multivalued
Attributes
Redundancy

Module Summary

Database Management Systems Partha Pratim Das 19.12


Many-to-Many Relationship

Module 19

Partha Pratim • An instructor is associated with several (possibly 0) students via advisor
Das
• A student is associated with several (possibly 0) instructors via advisor
Objectives &
Outline

ER Diagram
Entity Sets
Relationship Sets
Cardinality
Constraints
Participation
Bounds

ER Model to
Relational
Schema
Entity Sets
Relationship
Composite Attributes
Multivalued
Attributes
Redundancy

Module Summary

Database Management Systems Partha Pratim Das 19.13


Total and Partial Participation

Module 19

Partha Pratim • Total participation (indicated by double line): every entity in the entity set participates
Das
in at least one relationship in the relationship set
Objectives &
Outline

ER Diagram
Entity Sets
Relationship Sets
Cardinality
Constraints
Participation
Bounds

ER Model to
Relational ◦ participation of student in advisor relation is total
Schema
Entity Sets . every student must have an associated instructor
Relationship
Composite Attributes • Partial participation: some entities may not participate in any relationship in the
Multivalued
Attributes relationship set
Redundancy

Module Summary
◦ Example: participation of instructor in advisor is partial

Database Management Systems Partha Pratim Das 19.14


Notation for Expressing More Complex Constraints

Module 19

Partha Pratim • A line may have an associated minimum and maximum cardinality, shown in the form
Das
l..h, where l is the minimum and h the maximum cardinality
Objectives &
Outline ◦ A minimum value of 1 indicates total participation.
ER Diagram ◦ A maximum value of 1 indicates that the entity participates in at most one
Entity Sets
Relationship Sets
relationship
Cardinality
Constraints
◦ A maximum value of * indicates no limit.
Participation
Bounds

ER Model to
Relational
Schema
Entity Sets
Relationship
Composite Attributes
Multivalued
Attributes
Redundancy
Instructor can advise 0 or more students.
Module Summary
A student must have 1 advisor; cannot have multiple advisors

Database Management Systems Partha Pratim Das 19.15


Notation to Express Entity with Complex Attributes

Module 19

Partha Pratim
Das

Objectives &
Outline

ER Diagram
Entity Sets
Relationship Sets
Cardinality
Constraints
Participation
Bounds

ER Model to
Relational
Schema
Entity Sets
Relationship
Composite Attributes
Multivalued
Attributes
Redundancy

Module Summary

Database Management Systems Partha Pratim Das 19.16


Expressing Weak Entity Sets

Module 19

Partha Pratim • In ER diagrams, a weak entity set is depicted via a double rectangle
Das
• We underline the discriminator of a weak entity set with a dashed line
Objectives &
Outline • The relationship set connecting the weak entity set to the identifying strong entity set
ER Diagram
Entity Sets
is depicted by a double diamond
Relationship Sets
Cardinality
• Primary key for section – (course id, sec id, semester, year)
Constraints
Participation
Bounds

ER Model to
Relational
Schema
Entity Sets
Relationship
Composite Attributes
Multivalued
Attributes
Redundancy

Module Summary

Database Management Systems Partha Pratim Das 19.17


ER Diagram for a University Enterprise

Module 19

Partha Pratim
Das

Objectives &
Outline

ER Diagram
Entity Sets
Relationship Sets
Cardinality
Constraints
Participation
Bounds

ER Model to
Relational
Schema
Entity Sets
Relationship
Composite Attributes
Multivalued
Attributes
Redundancy

Module Summary

Database Management Systems Partha Pratim Das 19.18


PPD

Module 19

Partha Pratim
Das

Objectives &
Outline

ER Diagram
Entity Sets
Relationship Sets
Cardinality
Constraints
Participation
Bounds

ER Model to
Relational
Schema
Entity Sets
Relationship
ER Model to Relational Schema
Composite Attributes
Multivalued
Attributes
Redundancy

Module Summary

Database Management Systems Partha Pratim Das 19.19


Reduction to Relation Schema

Module 19

Partha Pratim • Entity sets and relationship sets can be expressed uniformly as relation schemas that
Das
represent the contents of the database
Objectives &
Outline • A database which conforms to an ER diagram can be represented by a collection of
ER Diagram schemas
Entity Sets
Relationship Sets • For each entity set and relationship set there is a unique schema that is assigned the
Cardinality
Constraints name of the corresponding entity set or relationship set
Participation
Bounds • Each schema has a number of columns (generally corresponding to attributes), which
ER Model to
Relational
have unique names
Schema
Entity Sets
Relationship
Composite Attributes
Multivalued
Attributes
Redundancy

Module Summary

Database Management Systems Partha Pratim Das 19.20


Representing Entity Sets

Module 19

Partha Pratim • A strong entity set reduces to a schema with the same attributes
Das

Objectives &
student(ID, name, tot cred)
Outline

ER Diagram
• A weak entity set becomes a table that includes a column for the primary key of the
Entity Sets
Relationship Sets identifying strong entity set
Cardinality
Constraints
Participation section (course id, sec id, sem, year )
Bounds

ER Model to
Relational
Schema
Entity Sets
Relationship
Composite Attributes
Multivalued
Attributes
Redundancy

Module Summary

Database Management Systems Partha Pratim Das 19.21


Representing Relationship Sets

Module 19

Partha Pratim • A many-to-many relationship set is represented as a schema with attributes for the
Das
primary keys of the two participating entity sets, and any descriptive attributes of the
Objectives &
Outline
relationship set.
ER Diagram • Example: schema for relationship set advisor
Entity Sets
Relationship Sets
Cardinality
advisor = (s id, i id)
Constraints
Participation
Bounds

ER Model to
Relational
Schema
Entity Sets
Relationship
Composite Attributes
Multivalued
Attributes
Redundancy

Module Summary

Database Management Systems Partha Pratim Das 19.22


Representation of Entity Sets with Composite Attributes

Module 19
• Composite attributes are flattened out by creating a separate attribute
Partha Pratim
Das for each component attribute
Objectives &
◦ Example: given entity set instructor with composite attribute
Outline
name with component attributes first name and last name
ER Diagram
Entity Sets
the schema corresponding to the entity set has two attributes
Relationship Sets name first name and name last name
Cardinality
Constraints
Participation
. Prefix omitted if there is no ambiguity (name first name could
Bounds be first name)
ER Model to
Relational • Ignoring multivalued attributes, extended instructor schema is
Schema
Entity Sets ◦ instructor(ID, first name, middle initial, last name,
Relationship
Composite Attributes
street number, street name, apt number, city, state,
Multivalued
Attributes
zip code, date of birth)
Redundancy

Module Summary

Database Management Systems Partha Pratim Das 19.23


Representation of Entity Sets with Multivalued Attributes

Module 19

Partha Pratim • A multivalued attribute M of an entity E is represented by a separate schema EM


Das
• Schema EM has attributes corresponding to the primary key of E and an attribute
Objectives &
Outline corresponding to multivalued attribute M
ER Diagram
Entity Sets
• Example: Multivalued attribute phone number of instructor is represented by a schema:
Relationship Sets inst phone= ( ID, phone number)
Cardinality
Constraints
Participation
• Each value of the multivalued attribute maps to a separate tuple of the relation on
Bounds schema EM
ER Model to
Relational ◦ For example, an instructor entity with primary key 22222 and phone numbers
Schema
Entity Sets
456-7890 and 123-4567 maps to two tuples: (22222, 456-7890) and (22222,
Relationship 123-4567)
Composite Attributes
Multivalued
Attributes
Redundancy

Module Summary

Database Management Systems Partha Pratim Das 19.24


Redundancy of Schema

Module 19

Partha Pratim • Many-to-one and one-to-many relationship sets that are total on the many-side can be
Das
represented by adding an extra attribute to the “many” side, containing the primary key
Objectives &
Outline
of the “one” side
ER Diagram • Example: Instead of creating a schema for relationship set inst dept, add an attribute
Entity Sets
Relationship Sets
dept name to the schema arising from entity set instructor
Cardinality
Constraints
Participation
Bounds

ER Model to
Relational
Schema
Entity Sets
Relationship
Composite Attributes
Multivalued
Attributes
Redundancy

Module Summary

Database Management Systems Partha Pratim Das 19.25


Redundancy of Schema (2)

Module 19

Partha Pratim • For one-to-one relationship sets, either side can be chosen to act as the “many” side
Das
◦ That is, an extra attribute can be added to either of the tables corresponding to the
Objectives &
Outline two entity sets
ER Diagram • If participation is partial on the “many” side, replacing a schema by an extra attribute
Entity Sets
Relationship Sets in the schema corresponding to the “many” side could result in null values
Cardinality
Constraints
Participation
Bounds

ER Model to
Relational
Schema
Entity Sets
Relationship
Composite Attributes
Multivalued
Attributes
Redundancy

Module Summary

Database Management Systems Partha Pratim Das 19.26


Redundancy of Schema (3)

Module 19

Partha Pratim • The schema corresponding to a relationship set linking a weak entity set to its
Das
identifying strong entity set is redundant.
Objectives &
Outline • Example: The section schema already contains the attributes that would appear in the
ER Diagram sec course schema
Entity Sets
Relationship Sets
Cardinality
Constraints
Participation
Bounds

ER Model to
Relational
Schema
Entity Sets
Relationship
Composite Attributes
Multivalued
Attributes
Redundancy

Module Summary

Database Management Systems Partha Pratim Das 19.27


Module Summary

Module 19

Partha Pratim • Illustrated ER Diagram notation for ER Models


Das
• Discussed translation of ER Models to Relational Schema
Objectives &
Outline

ER Diagram
Entity Sets
Relationship Sets
Cardinality
Constraints
Participation
Bounds

ER Model to
Relational
Schema
Entity Sets
Relationship
Slides used in this presentation are borrowed from http://db-book.com/ with kind
Composite Attributes permission of the authors.
Multivalued
Attributes Edited and new slides are marked with “PPD”.
Redundancy

Module Summary

Database Management Systems Partha Pratim Das 19.28


Module 20

Partha Pratim
Das

Objectives &
Outline Database Management Systems
ER Features
Non-binary
Module 20: Entity-Relationship Model/3
Relationship
Specialization
Specialization as
Schema
Generalization
Aggregation Partha Pratim Das
Design Issues
Entities vs Attributes
Department of Computer Science and Engineering
Entities vs
Relationship Indian Institute of Technology, Kharagpur
Binary vs Non-Binary
Design Decisions [email protected]
ER Notation

Module Summary

Database Management Systems Partha Pratim Das 20.1


Module Recap PPD

Module 20

Partha Pratim • ER Diagram for ER Models


Das
• Translation of ER Models to Relational Schema
Objectives &
Outline

ER Features
Non-binary
Relationship
Specialization
Specialization as
Schema
Generalization
Aggregation

Design Issues
Entities vs Attributes
Entities vs
Relationship
Binary vs Non-Binary
Design Decisions
ER Notation

Module Summary

Database Management Systems Partha Pratim Das 20.2


Module Objectives PPD

Module 20

Partha Pratim • To understand extended features of ER Model


Das
• To discuss various design issues
Objectives &
Outline

ER Features
Non-binary
Relationship
Specialization
Specialization as
Schema
Generalization
Aggregation

Design Issues
Entities vs Attributes
Entities vs
Relationship
Binary vs Non-Binary
Design Decisions
ER Notation

Module Summary

Database Management Systems Partha Pratim Das 20.3


Module Outline PPD

Module 20

Partha Pratim • Extended ER Features


Das
• Design Issues
Objectives &
Outline

ER Features
Non-binary
Relationship
Specialization
Specialization as
Schema
Generalization
Aggregation

Design Issues
Entities vs Attributes
Entities vs
Relationship
Binary vs Non-Binary
Design Decisions
ER Notation

Module Summary

Database Management Systems Partha Pratim Das 20.4


Extended ER Features PPD

Module 20

Partha Pratim
Das

Objectives &
Outline

ER Features
Non-binary
Relationship
Specialization
Specialization as
Schema
Generalization
Aggregation

Design Issues
Entities vs Attributes
Entities vs
Relationship
Extended ER Features
Binary vs Non-Binary
Design Decisions
ER Notation

Module Summary

Database Management Systems Partha Pratim Das 20.5


Non-binary Relationship Sets

Module 20

Partha Pratim • Most relationship sets are binary


Das
• There are occasions when it is more convenient to represent relationships as non-binary
Objectives &
Outline • ER Diagram with a Ternary Relationship
ER Features
Non-binary
Relationship
Specialization
Specialization as
Schema
Generalization
Aggregation

Design Issues
Entities vs Attributes
Entities vs
Relationship
Binary vs Non-Binary
Design Decisions
ER Notation

Module Summary

Database Management Systems Partha Pratim Das 20.6


Cardinality Constraints on Ternary Relationship

Module 20

Partha Pratim • We allow at most one arrow out of a ternary (or greater degree) relationship to indicate
Das
a cardinality constraint
Objectives &
Outline • For example, an arrow from proj guide to instructor indicates each student has at most
ER Features one guide for a project
Non-binary
Relationship
Specialization
• If there is more than one arrow, there are two ways of defining the meaning.
Specialization as
Schema
◦ For example, a ternary relationship R between A, B and C with arrows to B and C
Generalization could mean
Aggregation

Design Issues a) Each A entity is associated with a unique entity from B and C or
Entities vs Attributes
b) Each pair of entities from (A, B) is associated with a unique C entity, and each
Entities vs
Relationship pair (A ,C ) is associated with a unique B
Binary vs Non-Binary
Design Decisions ◦ Each alternative has been used in different formalisms
ER Notation

Module Summary
◦ To avoid confusion we outlaw more than one arrow

Database Management Systems Partha Pratim Das 20.7


Specialization: ISA

Module 20

Partha Pratim • Top-down design process: We designate sub-groupings within an entity set that are
Das
distinctive from other entities in the set
Objectives &
Outline • These sub-groupings become lower-level entity sets that have attributes or participate
ER Features in relationships that do not apply to the higher-level entity set
Non-binary
Relationship
Specialization
• Depicted by a triangle component labeled ISA (e.g., instructor “is a” person)
Specialization as
Schema • Attribute inheritance: A lower-level entity set inherits all the attributes and
Generalization
Aggregation
relationship participation of the higher-level entity set to which it is linked
Design Issues
Entities vs Attributes
Entities vs
Relationship
Binary vs Non-Binary
Design Decisions
ER Notation

Module Summary

Database Management Systems Partha Pratim Das 20.8


Specialization: ISA (2)

Module 20

Partha Pratim • Overlapping: employee and student


Das
• Disjoint: instructor and secretary
Objectives &
Outline • Total and Partial
ER Features
Non-binary
Relationship
Specialization
Specialization as
Schema
Generalization
Aggregation

Design Issues
Entities vs Attributes
Entities vs
Relationship
Binary vs Non-Binary
Design Decisions
ER Notation

Module Summary

Database Management Systems Partha Pratim Das 20.9


Representing Specialization via Schema

Module 20

Partha Pratim • Method 1:


Das
◦ Form a schema for the higher-level entity
Objectives &
Outline ◦ Form a schema for each lower-level entity set, include primary key of higher-level
ER Features entity set and local attributes
Non-binary
Relationship
Specialization
Specialization as
Schema
Generalization
Aggregation

Design Issues
Entities vs Attributes ◦ Drawback: Getting information about, an employee requires accessing two relations,
Entities vs
Relationship the one corresponding to the low-level schema and the one corresponding to the
Binary vs Non-Binary
Design Decisions high-level schema
ER Notation

Module Summary

Database Management Systems Partha Pratim Das 20.10


Representing Specialization as Schema (2)

Module 20

Partha Pratim • Method 2:


Das
◦ Form a schema for each entity set with all local and inherited attributes
Objectives &
Outline

ER Features
Non-binary
Relationship
Specialization
Specialization as
Schema
Generalization ◦ Drawback: name, street and city may be stored redundantly for people who are
Aggregation
both students and employees
Design Issues
Entities vs Attributes
Entities vs
Relationship
Binary vs Non-Binary
Design Decisions
ER Notation

Module Summary

Database Management Systems Partha Pratim Das 20.11


Generalization

Module 20

Partha Pratim • Bottom-up design process: Combine a number of entity sets that share the same
Das
features into a higher-level entity set
Objectives &
Outline • Specialization and generalization are simple inversions of each other; they are
ER Features represented in an ER diagram in the same way
Non-binary
Relationship
Specialization
• The terms specialization and generalization are used interchangeably
Specialization as
Schema
Generalization
Aggregation

Design Issues
Entities vs Attributes
Entities vs
Relationship
Binary vs Non-Binary
Design Decisions
ER Notation

Module Summary

Database Management Systems Partha Pratim Das 20.12


Design Constraints on a Specialization / Generalization

Module 20

Partha Pratim
• Completeness constraint: Specifies whether or not an entity in the higher-level entity
Das
set must belong to at least one of the lower-level entity sets within a generalization
Objectives & ◦ total: an entity must belong to one of the lower-level entity sets
Outline
◦ partial: an entity need not belong to one of the lower-level entity sets
ER Features
Non-binary • Partial generalization is the default. We can specify total generalization in an ER
Relationship
Specialization diagram by adding the keyword total in the diagram and drawing a dashed line from
Specialization as
Schema the keyword to the corresponding hollow arrow-head to which it applies (for a total
Generalization
Aggregation
generalization), or to the set of hollow arrow-heads to which it applies (for an
Design Issues
overlapping generalization).
Entities vs Attributes
Entities vs
Relationship
• The student generalization is total. All student entities must
Binary vs Non-Binary be either graduate or undergraduate. Because the higher-
Design Decisions
ER Notation level entity set arrived at through generalization is generally
Module Summary composed of only those entities in the lower-level entity sets,
the completeness constraint for a generalized higher-level en-
tity set is usually total.
Database Management Systems Partha Pratim Das 20.13
Aggregation

Module 20 • Consider the ternary relationship proj guide, which we saw earlier
Partha Pratim
Das
• Suppose we want to record evaluations of a student by a guide on a project
Objectives &
Outline

ER Features
Non-binary
Relationship
Specialization
Specialization as
Schema
Generalization
Aggregation

Design Issues
Entities vs Attributes
Entities vs
Relationship
Binary vs Non-Binary
Design Decisions
ER Notation

Module Summary

Database Management Systems Partha Pratim Das 20.14


Aggregation (2)

Module 20
• Relationship sets eval for and proj guide represent overlapping information
Partha Pratim
Das ◦ Every eval for relationship corresponds to a proj guide relationship
Objectives &
◦ However, some proj guide relationships may not correspond to any eval for
Outline
relationships
ER Features
Non-binary . So we cannot discard the proj guide relationship
Relationship
Specialization • Eliminate this redundancy via aggregation
Specialization as
Schema
Generalization
◦ Treat relationship as an abstract entity
Aggregation ◦ Allows relationships between relationships
Design Issues ◦ Abstraction of relationship into new entity
Entities vs Attributes
Entities vs
Relationship
Binary vs Non-Binary
Design Decisions
ER Notation

Module Summary

Database Management Systems Partha Pratim Das 20.15


Aggregation (3)

Module 20
• Eliminate this redundancy via aggregation without introducing redundancy, the
Partha Pratim
Das
following diagram represents:
◦ A student is guided by a particular instructor on a particular project
Objectives &
Outline ◦ A student, instructor, project combination may have an associated evaluation
ER Features
Non-binary
Relationship
Specialization
Specialization as
Schema
Generalization
Aggregation

Design Issues
Entities vs Attributes
Entities vs
Relationship
Binary vs Non-Binary
Design Decisions
ER Notation

Module Summary

Database Management Systems Partha Pratim Das 20.16


Representing Aggregation via Schema

Module 20

Partha Pratim • To represent aggregation, create a schema containing


Das
◦ Primary key of the aggregated relationship,
Objectives &
Outline ◦ The primary key of the associated entity set
ER Features ◦ Any descriptive attributes
Non-binary
Relationship • In our example:
Specialization
Specialization as
Schema
◦ The schema
Generalization textiteval for is:
Aggregation
eval for (s ID, project id, i ID, evaluation id)
Design Issues
Entities vs Attributes ◦ The schema proj guide is redundant
Entities vs
Relationship
Binary vs Non-Binary
Design Decisions
ER Notation

Module Summary

Database Management Systems Partha Pratim Das 20.17


Design Issues PPD

Module 20

Partha Pratim
Das

Objectives &
Outline

ER Features
Non-binary
Relationship
Specialization
Specialization as
Schema
Generalization
Aggregation

Design Issues
Entities vs Attributes
Entities vs
Relationship
Design Issues
Binary vs Non-Binary
Design Decisions
ER Notation

Module Summary

Database Management Systems Partha Pratim Das 20.18


Entities vs. Attributes

Module 20
• Use of entity sets vs. attributes
Partha Pratim
Das

Objectives &
Outline

ER Features
Non-binary
Relationship
Specialization
Specialization as
Schema
Generalization • Use of phone as an entity allows extra information about phone numbers (plus multiple
Aggregation
phone numbers)
Design Issues
Entities vs Attributes
Entities vs
Relationship
Binary vs Non-Binary
Design Decisions
ER Notation

Module Summary

Database Management Systems Partha Pratim Das 20.19


Entities vs Relationship Sets

Module 20
• Use of entity sets vs. relationship sets
Partha Pratim
Das Possible guideline is to designate a relationship set to describe an action that occurs
Objectives &
between entities
Outline

ER Features
Non-binary
Relationship
Specialization
Specialization as
Schema
Generalization
Aggregation

Design Issues
Entities vs Attributes
Entities vs
Relationship
Binary vs Non-Binary
Design Decisions
• Placement of relationship attributes
ER Notation For example, attribute date as attribute of advisor or as attribute of student
Module Summary

Database Management Systems Partha Pratim Das 20.20


Binary vs Non-Binary Relationships

Module 20
• Although it is possible to replace any non-binary (n-ary, for n > 2) relationship set by a
Partha Pratim
Das number of distinct binary relationship sets, a n-ary relationship set shows more clearly
Objectives &
that several entities participate in a single relationship
Outline
• Some relationships that appear to be non-binary may be better represented using binary
ER Features
Non-binary
relationships
Relationship
Specialization ◦ For example, a ternary relationship parents, relating a child to his/her father and
Specialization as
Schema mother, is best replaced by two binary relationships, father and mother
Generalization
Aggregation . Using two binary relationships allows partial information (e.g., only mother being
Design Issues known)
Entities vs Attributes
Entities vs ◦ But there are some relationships that are naturally non-binary
Relationship
Binary vs Non-Binary . Example: proj guide
Design Decisions
ER Notation

Module Summary

Database Management Systems Partha Pratim Das 20.21


Binary vs Non-Binary Relationships (2): Conversion

Module 20 • In general, any non-binary relationship can be represented using binary relationships by
Partha Pratim
Das
creating an artificial entity set.
◦ Replace R between entity sets A, B and C by an entity set E, and three relationship
Objectives &
Outline sets:
ER Features 1. RA , relating E and A
Non-binary
Relationship 2. RB , relating E and B
Specialization
Specialization as
3. RC , relating E and C
Schema
Generalization
◦ Create an identifying attribute for E and add any attributes of R to E
Aggregation ◦ For each relationship (ai , bi , ci ) in R, create
Design Issues a) a new entity ei in the entity set E
Entities vs Attributes
Entities vs b) add (ei , ai ) to RA
Relationship
Binary vs Non-Binary c) add (ei , bi ) to RB
Design Decisions
ER Notation
d) add (ei , ci ) to RC
Module Summary

Database Management Systems Partha Pratim Das 20.22


Binary vs Non-Binary Relationships (3): Conversion

Module 20
• Also need to translate constraints
Partha Pratim
Das ◦ Translating all constraints may not be possible
Objectives &
◦ There may be instances in the translated schema that cannot correspond to any
Outline
instance of R.
ER Features
Non-binary . Exercise: add constraints to the relationships RA , RB and RC to ensure that a
Relationship
Specialization newly created entity corresponds to exactly one entity in each of entity sets —A,
Specialization as
Schema
B and C
Generalization
Aggregation
◦ We can avoid creating an identifying attribute by making E, a weak entity set
Design Issues (described shortly) identified by the three relationship sets
Entities vs Attributes
Entities vs
Relationship
Binary vs Non-Binary
Design Decisions
ER Notation

Module Summary

Database Management Systems Partha Pratim Das 20.23


ER Design Decisions

Module 20
• The use of an attribute or entity set to represent an object
Partha Pratim
Das • Whether a real-world concept is best expressed by an entity set or a relationship set
Objectives &
Outline
• The use of a ternary relationship versus a pair of binary relationships
ER Features • The use of a strong or weak entity set
Non-binary
Relationship • The use of specialization/generalization – contributes to modularity in the design
Specialization
Specialization as
Schema
• The use of aggregation – can treat the aggregate entity set as a single unit without
Generalization concern for the details of its internal structure
Aggregation

Design Issues
Entities vs Attributes
Entities vs
Relationship
Binary vs Non-Binary
Design Decisions
ER Notation

Module Summary

Database Management Systems Partha Pratim Das 20.24


Symbols Used in ER Notation

Module 20

Partha Pratim
Das

Objectives &
Outline

ER Features
Non-binary
Relationship
Specialization
Specialization as
Schema
Generalization
Aggregation

Design Issues
Entities vs Attributes
Entities vs
Relationship
Binary vs Non-Binary
Design Decisions
ER Notation

Module Summary

Database Management Systems Partha Pratim Das 20.25


Symbols Used in ER Notation (2)

Module 20

Partha Pratim
Das

Objectives &
Outline

ER Features
Non-binary
Relationship
Specialization
Specialization as
Schema
Generalization
Aggregation

Design Issues
Entities vs Attributes
Entities vs
Relationship
Binary vs Non-Binary
Design Decisions
ER Notation

Module Summary

Database Management Systems Partha Pratim Das 20.26


Symbols Used in ER Notation (3): Alternate

Module 20

Partha Pratim • Chen, IDE1FX,. . .


Das

Objectives &
Outline

ER Features
Non-binary
Relationship
Specialization
Specialization as
Schema
Generalization
Aggregation

Design Issues
Entities vs Attributes
Entities vs
Relationship
Binary vs Non-Binary
Design Decisions
ER Notation

Module Summary

Database Management Systems Partha Pratim Das 20.27


Symbols Used in ER Notation (4): Alternates

Module 20
Chen IDE1FX (Crows feet notation)
Partha Pratim
Das

Objectives &
Outline

ER Features
Non-binary
Relationship
Specialization
Specialization as
Schema
Generalization
Aggregation

Design Issues
Entities vs Attributes
Entities vs
Relationship
Binary vs Non-Binary
Design Decisions
ER Notation

Module Summary

Database Management Systems Partha Pratim Das 20.28


Module Summary

Module 20

Partha Pratim • Discussed the extended features of ER Model


Das
• Deliberated on various design issues
Objectives &
Outline

ER Features
Non-binary
Relationship
Specialization
Specialization as
Schema
Generalization
Aggregation

Design Issues
Entities vs Attributes
Entities vs
Relationship
Binary vs Non-Binary
Design Decisions
ER Notation

Module Summary

Database Management Systems Partha Pratim Das 20.29

You might also like