1-4 DBMS
1-4 DBMS
Partha Pratim
Das
Objectives &
Outline Database Management Systems
Why Databases?
Module 01: Course Overview
Know Your
Course
Course Outline
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
Module Summary
Module 01
Module Summary
Module 01
Partha Pratim
Das
Objectives &
Outline
Why Databases?
Know Your
Course
Course Outline
Module Summary
Why Databases?
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
Course Outline • In the early days, database applications were built directly on top of file systems
Course Text Book
Module Summary
Module 01
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
Module 01
Partha Pratim
Das
Objectives &
Outline
Why Databases?
Know Your
Course
Course Outline
Module Summary
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
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
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
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
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/
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
Module Summary
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/
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
Module Summary
Module 01
Partha Pratim
Das
Objectives &
Outline
Why Databases?
Know Your
Course
Course Outline
Module Summary
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
Website: http://db-book.com/
7th Edition will also do
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
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”.
Partha Pratim
Das
Objectives &
Outline Database Management Systems
Evolution of Data
Management Module 02: Why DBMS?/1
History
Module Summary
Module 02
Partha Pratim • To understand the need for a DBMS from historical perspective
Das
Objectives &
Outline
Evolution of Data
Management
History
Module Summary
Module 02
Evolution of Data
Management
History
Module Summary
Module 02
Partha Pratim
Das
Objectives &
Outline
Evolution of Data
Management
History
Module Summary
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.
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 Summary
• Ease of Use
• Consistency
• Efficiency
• Cost
• ...
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.
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
Module 02
Partha Pratim
Das
Objectives &
Outline
Evolution of Data
Management
History
Module Summary
History of DBMS
Module 02
Module 02
Module 02
Partha Pratim
Das
Objectives &
Outline
Evolution of Data
Management
History
Module Summary
Module 02
Partha Pratim
Das
Objectives &
Outline
Evolution of Data
Management
History
Module Summary
Module 02
Partha Pratim
Das
Objectives &
Outline
Evolution of Data
Management
History
Module Summary
Module 02
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”.
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
Module 03
File Systems vs
Databases
Python viz-a-viz SQL
Parameterized
Comparison
Module Summary
Module 03
Objectives &
Outline
File Systems vs
Databases
Python viz-a-viz SQL
Parameterized
Comparison
Module Summary
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
Module 03
Partha Pratim
Das
Objectives &
Outline
File Systems vs
Databases
Python viz-a-viz SQL
Parameterized
Comparison
Module Summary
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.
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.
Module 03
Partha Pratim
Das
Objectives &
Outline
File Systems vs
Databases
Python viz-a-viz SQL
Parameterized
Comparison
Module Summary
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
f_obj_Account.close()
print("Transaction is successful !!")
else:
print(’Transaction failed : Confirm Account details’)
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
Module 03
Partha Pratim
Das
Objectives &
Outline
File Systems vs
Databases
Python viz-a-viz SQL
Parameterized
Comparison
Module Summary
Parameterized Comparison
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.
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.
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
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”.
Partha Pratim
Das
Objectives &
Outline Database Management Systems
Levels of
Abstraction Module 04: Introduction to DBMS/1
Schema and
Instance
Data Models
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
SQL
Database Design
Module Summary
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
SQL
Database Design
Module Summary
Module 04
Database Design
Module Summary
Module 04
Partha Pratim
Das
Objectives &
Outline
Levels of
Abstraction
Schema and
Instance
Data Models
SQL
Database Design
Module Summary
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
Module 04
Partha Pratim
An architecture for a database system
Das
Objectives &
Outline
Levels of
Abstraction
Schema and
Instance
Data Models
SQL
Database Design
Module Summary
Module 04
Partha Pratim
Das
Objectives &
Outline
Levels of
Abstraction
Schema and
Instance
Data Models
SQL
Database Design
Module Summary
Schema and Instance
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
Module 04
Schema and
Instance
Data Models
Module Summary
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
Module 04
Partha Pratim
Das
Objectives &
Outline
Levels of
Abstraction
Schema and
Instance
Data Models
SQL
Database Design
Module Summary
Data Models
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
SQL
Database Design
Module Summary
Module 04
Levels of
Abstraction
Schema and
Instance
Data Models
SQL
Database Design
Module Summary
Module 04
Partha Pratim
Das
Objectives &
Outline
Levels of
Abstraction
Schema and
Instance
Data Models
SQL
Database Design
Module Summary
Module 04
Partha Pratim
Das
Objectives &
Outline
Levels of
Abstraction
Schema and
Instance
Data Models
SQL
Database Design
Module Summary
DDL and DML
Module 04
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
Data Manipulation Language (DML) and its role in accessing and manipulating data within a database. Let's break it down:
Module 04
Module 04
Partha Pratim
Das
Objectives &
Outline
Levels of
Abstraction
Schema and
Instance
Data Models
SQL
Database Design
Module Summary
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
Module 04
Objectives &
Outline
Levels of
Abstraction
Schema and
Instance
Data Models
SQL
Database Design
Module Summary
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
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”.
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
Module 05
Database Engine
Database System
Internals
Database Users
& Administrators
Module Summary
Module 05
Database Engine
Database System
Internals
Database Users
& Administrators
Module Summary
Module 05
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
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
Module 05
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
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
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
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
Module 05
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
Module 05
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
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
Module 05
Database Engine
Database System
Internals
Database Users
& Administrators
Module Summary
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
Module 05
Database Engine
Database System
Internals
Database Users
& Administrators
Module Summary
Module 05
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
Module 05
Database Users • Concurrency-control manager controls the interaction among the concurrent
& Administrators
transactions, to ensure the consistency of the database.
Module Summary
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
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
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
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
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
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
Module 05
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”.
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
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
Module 06
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
Module 06
Objectives &
• Keys
Outline
Example of a
• Relational Query Languages
Relation
Attributes
Schema and
Instance
Keys
Relational Query
Languages
Module Summary
Module 06
Partha Pratim
Das
Week Recap
Objectives &
Outline
Example of a
Relation
Attributes
Schema and
Instance
Keys
Relational Query
Languages
Module Summary
Module 06
Partha Pratim
Das
Week Recap
Objectives &
Outline
Example of a
Relation
Attributes
Schema and
Instance
Keys
Relational Query
Languages
Module Summary
Attributes
Module 06
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
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
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
Module 06
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 +
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
Module 06
Partha Pratim
Das
Week Recap
Objectives &
Outline
Example of a
Relation
Attributes
Schema and
Instance
Keys
Relational Query
Languages
Module Summary
Keys
Module 06
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
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
Module 06
Partha Pratim
Das
Week Recap
Objectives &
Outline
Example of a
Relation
Attributes
Schema and
Instance
Keys
Relational Query
Languages
Module Summary
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
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
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
Module 06
Module 06
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
Module 07
Aggregation
• Languages for Relation Model introduced
Operators
Module Summary
Module 07
Relational
Operators
Aggregation
Operators
Module Summary
Module 07
Module 07
Partha Pratim
Das
Objectives &
Outline
Relational
Operators
Aggregation
Operators
Module Summary
Relational Operators
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
Objectives &
Outline
Relational
Operators
Aggregation
Operators
Module Summary
• σA=B∧D>5 (r )
Module 07
Objectives &
Outline
Relational
Operators
Aggregation
Operators
Module Summary
• πA,C (r )
Module 07
Objectives &
Outline
Relational
Operators
Aggregation
Operators
Module Summary
• r ∪s
Module 07
Objectives &
Outline
Relational
Operators
Aggregation
Operators
Module Summary
• r −s
Module 07
Objectives &
Outline
Relational
Operators
Aggregation
Operators
Module Summary
• r ∩s
Note: r ∩ s = r − (r − s)
Module 07
Objectives &
Outline
Relational
Operators
Aggregation
Operators
Module Summary
• r ×s
Module 07
Objectives &
Outline
Relational
Operators
Aggregation
Operators
Module Summary • r ×s
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 )
Module 07
Aggregation
Operators
Module Summary
• σA=C (r × s)
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
Module 07
Objectives &
Outline
Relational
Operators
Aggregation
Operators
Module Summary
• Natural Join
◦ r ./ s
Module 07
Partha Pratim
Das
Objectives &
Outline
Relational
Operators
Aggregation
Operators
Module Summary
Aggregation Operators
Module 07
Module Summary
Module 07
Module Summary
Module 07
Partha Pratim
Das
Objectives &
Outline
Relational
Operators
Aggregation
Operators
Module Summary
Module 07
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”.
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
Module 08
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
Module 08
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
Module 08
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
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
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)
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
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
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
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
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
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
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
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
Module 08
Partha Pratim
Das
Objectives &
Outline
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
Module Summary
primary key declaration on an attribute automatically ensures not null
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
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
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
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
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
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
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
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
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
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
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
Module 08
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
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]
Module 09
Additional Basic
Operations
Cartesian Product
Rename AS
Operation
String Values
Order By Clause
Select Top / Fetch
Clause
Where Clause
Predicates
Duplicates
Module Summary
Module 09
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
Module 09
Module Summary
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
Module 09
Module Summary
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
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
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
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
Module 09
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
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
Module 09
Module Summary
◦ finding string length, extracting substrings, etc.
Module 09
Module Summary
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
Module 09
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
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
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 09
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”.
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 10
Objectives &
Outline
Set Operations
Null Values
Three Valued Logic
Aggregate
Functions
Group By
Having
Null Values
Module Summary
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
Module 10
Module Summary
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
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)
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”)
Module 10
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
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
Module 10
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
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
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;
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
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
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
Module 10
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
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”.
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 11
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 11
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 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
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 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
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 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 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 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 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 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 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 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
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 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 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 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 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
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
Module 12
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
Module 12
Nested
Subqueries
Subqueries in the
Where Clause
Subqueries in the
From Clause
Subqueries in the
Select Clause
Modifications of
the Database
Module Summary
Module 12
Nested
Subqueries
Subqueries in the
Where Clause
Subqueries in the
From Clause
Subqueries in the
Select Clause
Modifications of
the Database
Module Summary
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 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 12
Modifications of
the Database
Module Summary
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);
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.
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’);
Module 12
Modifications of
the Database
Module Summary
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
Module 12
Modifications of
the Database
Module Summary
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
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
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
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
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 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
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;
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 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
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 12
Modifications of
the Database
Module Summary
Module 12
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
Module 12
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
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)
Module 12
Modifications of
the Database
Module Summary
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
Module 12
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”.
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]
Module 13
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
Module 13
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
Module 13
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
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
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
Module 13
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
Module 13
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
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
Module 13
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
Module 13
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
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
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
Module 13
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
Module 13
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
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
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
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
Module 13
Module 13
Module 13
Module Summary
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
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
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
Module 13
Module 13
Views
View Expansion
View Update
Materialized Views
Module Summary
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
Module 13
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”.
Partha Pratim
Das
Objectives &
Outline Database Management Systems
Transactions
Module 14: Intermediate SQL/3
Integrity
Constraints
Referential Integrity
Module Summary
Module 14
Objectives &
Outline
Transactions
Integrity
Constraints
Referential Integrity
Authorization
Privileges
Revocation
Roles
Module Summary
Module 14
Integrity
• To understand Authorization in SQL
Constraints
Referential Integrity
Authorization
Privileges
Revocation
Roles
Module Summary
Module 14
Integrity
• Authorization
Constraints
Referential Integrity
Authorization
Privileges
Revocation
Roles
Module Summary
Module 14
Partha Pratim
Das
Objectives &
Outline
Transactions
Integrity
Constraints
Referential Integrity
Module Summary
Module 14
Module Summary
Module 14
Partha Pratim
Das
Objectives &
Outline
Transactions
Integrity
Constraints
Referential Integrity
Module Summary
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
Authorization
Privileges
Revocation
Roles
Module Summary
Module 14
Integrity
• check(P), where P is a predicate
Constraints
Referential Integrity
Authorization
Privileges
Revocation
Roles
Module Summary
Module 14
Authorization
Privileges
Revocation
Roles
Module Summary
Module 14
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 );
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
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
Module 14
Partha Pratim
Das
Objectives &
Outline
Transactions
Integrity
Constraints
Referential Integrity
Module Summary
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
Module Summary
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
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
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
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
Module 14
Partha Pratim
Das
Objectives &
Outline
Transactions
Integrity
Constraints
Referential Integrity
Module Summary
Module 14
Module Summary
Module 14
Module Summary
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
Authorization
Privileges
Revocation
Roles
Module Summary
Module 14
Module Summary
Module 14
Module 14
Module Summary
Module 14
Authorization
Privileges
Revocation
Roles
Module Summary
Module 14
Integrity
• Discussed authorization in SQL
Constraints
Referential Integrity
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
Module 15
Triggers
Triggers :
Functionality vs
Performance
Module Summary
Module 15
Functions and
Procedural
Constructs
Triggers
Triggers :
Functionality vs
Performance
Module Summary
Module 15
Triggers
Triggers :
Functionality vs
Performance
Module Summary
Partha Pratim
Das
Objectives &
Outline
Functions and
Procedural
Constructs
Triggers
Triggers :
Functionality vs
Performance
Module Summary
Module 15
Partha Pratim
Das
Objectives &
Outline
Functions and
Procedural
Constructs
Triggers
Triggers :
Functionality vs
Performance
Module Summary
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
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
Triggers
• SQL function are in fact parameterized views that generalize the regular notion of
Triggers : views by allowing parameters
Functionality vs
Performance
Module Summary
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 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
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;
Module 15
Module 15
Module 15
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
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’
Module 15
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
Partha Pratim
Das
Objectives &
Outline
Functions and
Procedural
Constructs
Triggers
Triggers :
Functionality vs
Performance
Module Summary
Triggers
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
Module 15
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
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;
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
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
Module 15
Module Summary
◦ Iteration occurs
Source: SQL Server triggers: The good and the scary
Module 15
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”.
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]
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
Module 16
Week Recap
Objectives &
Outline
Relational
Algebra
Select
Project
Union
Difference
Intersection
Cartesian Product
Rename
Division
Module Summary
Module 16
Week Recap
Objectives &
Outline
Relational
Algebra
Select
Project
Union
Difference
Intersection
Cartesian Product
Rename
Division
Module Summary
Module 16
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
Module 16
Partha Pratim
Das
Week Recap
Objectives &
Outline
Relational
Algebra
Select
Project
Union
Difference
Intersection
Cartesian Product
Rename
Division
Relational Algebra
Module Summary
Module 16
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
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
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))
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))
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
Module 16
Partha Pratim
Das
• Notation r × s
Week Recap
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
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
Module 16
Partha Pratim
Das
Week Recap
Objectives &
Outline
Relational
Algebra
Select
Project
Union
Difference
Intersection
Cartesian Product
Rename
Division
Module Summary
Module 16
Partha Pratim
Das
Week Recap
Objectives &
Outline
Relational
Algebra
Select
Project
Union
Difference
Intersection
Cartesian Product
Rename
Division
Module Summary
Module 16
Partha Pratim
Das
Week Recap
Objectives &
Outline
Relational
Algebra
Select
Project
Union
Difference
Intersection
Cartesian Product
Rename
Division
Module Summary
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
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”.
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
Module 17
Objectives &
Outline
Predicate Logic
Tuple Relational
Calculus
Domain
Relational
Calculus
Equivalence of
Algebra and
Calculus
Module Summary
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
Module 17
Tuple Relational
Calculus
Domain
Relational
Calculus
Equivalence of
Algebra and
Calculus
Module Summary
Module 17
Equivalence of
Algebra and
Calculus
Module Summary
Module 17
Partha Pratim
Das
Objectives &
Outline
Predicate Logic
Tuple Relational
Calculus
Domain
Relational
Calculus
Equivalence of
Algebra and
Calculus
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
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.
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
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
Module 17
Partha Pratim
Das
Objectives &
Outline
Predicate Logic
Tuple Relational
Calculus
Domain
Relational
Calculus
Equivalence of
Algebra and
Calculus
Module 17
Partha Pratim
TRC is a non-procedural query language, where each query is of the form
Das
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.
Module 17
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
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
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
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)}
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)}
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
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
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
Module 17
Partha Pratim
Das
Objectives &
Outline
Predicate Logic
Tuple Relational
Calculus
Domain
Relational
Calculus
Equivalence of
Algebra and
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
Equivalence of
Algebra and
Calculus
Module Summary
Module 17
Partha Pratim
Das
Objectives &
Outline
Predicate Logic
Tuple Relational
Calculus
Domain
Relational
Calculus
Equivalence of
Algebra and
Calculus
Module 17
Partha Pratim
Das
Objectives &
Outline
Predicate Logic
Tuple Relational
Calculus
Domain
Relational
Calculus
Equivalence of
Algebra and
Calculus
Module Summary
Module 17
Partha Pratim
Das
Objectives &
Outline
Predicate Logic
Tuple Relational
Calculus
Domain
Relational
Calculus
Equivalence of
Algebra and
Calculus
Module Summary
Module 17
Partha Pratim
Das
Objectives &
Outline
Predicate Logic
Tuple Relational
Calculus
Domain
Relational
Calculus
Equivalence of
Algebra and
Calculus
Module Summary
Module 17
Partha Pratim
Das
Objectives &
Outline
Predicate Logic
Tuple Relational
Calculus
Domain
Relational
Calculus
Equivalence of
Algebra and
Calculus
Module Summary
Module 17
Partha Pratim
Das
Objectives &
Outline
Predicate Logic
Tuple Relational
Calculus
Domain
Relational
Calculus
Equivalence of
Algebra and
Calculus
Module Summary
Module 17
Partha Pratim
Das
Objectives &
Outline
Predicate Logic
Tuple Relational
Calculus
Domain
Relational
Calculus
Equivalence of
Algebra and
Calculus
Module Summary
Module 17
Partha Pratim
Das
Objectives &
Outline
Predicate Logic
Tuple Relational
Calculus
Domain
Relational
Calculus
Equivalence of
Algebra and
Calculus
Module Summary
Module 17
Partha Pratim
Das
Objectives &
Outline
Predicate Logic
Tuple Relational
Calculus
Domain
Relational
Calculus
Equivalence of
Algebra and
Calculus
Module Summary
Module 17
Partha Pratim
Das
Objectives &
Outline
Predicate Logic
Tuple Relational
Calculus
Domain
Relational
Calculus
Equivalence of
Algebra and
Calculus
Module Summary
Module 17
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”.
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]
Module 18
ER Model
Attributes
Entity Sets
Relationship
Cardinality
Constraints
Weak Entity Sets
Module Summary
Module 18
Design Process
Abstraction
Models
Design Approach
ER Model
Attributes
Entity Sets
Relationship
Cardinality
Constraints
Weak Entity Sets
Module Summary
Module 18
ER Model
◦ Attributes
Attributes ◦ Weak Entity Sets
Entity Sets
Relationship
Cardinality
Constraints
Weak Entity Sets
Module Summary
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
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
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
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
ER Model
Attributes
Entity Sets
Relationship
Cardinality
Constraints
Weak Entity Sets
Module Summary
Module 18
Module Summary
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
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
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 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
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
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
Module 18
Module Summary
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
Module 18
ER Model
Attributes
Entity Sets
Relationship
Cardinality
Constraints
Weak Entity Sets
Module Summary
Module 18
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
Module 18
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
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
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
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
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
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.
Module 18
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”.
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
Module 19
ER Model to
Relational
Schema
Entity Sets
Relationship
Composite Attributes
Multivalued
Attributes
Redundancy
Module Summary
Module 19
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
Module 19
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
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
Module 19
ER Model to
Relational
Schema
Entity Sets
Relationship
Composite Attributes
Multivalued
Attributes
Redundancy
Module Summary
Module 19
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
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
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
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
Module 19
ER Model to
Relational
Schema
Entity Sets
Relationship
Composite Attributes
Multivalued
Attributes
Redundancy
Module Summary
Module 19
ER Model to
Relational
Schema
Entity Sets
Relationship
Composite Attributes
Multivalued
Attributes
Redundancy
Module Summary
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
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
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
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
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
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
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
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
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
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
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
Module 19
Module Summary
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
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
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
Module 19
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
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
Module 20
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
Module 20
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
Module 20
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
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
Module 20
Design Issues
Entities vs Attributes
Entities vs
Relationship
Binary vs Non-Binary
Design Decisions
ER Notation
Module Summary
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
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
Module 20
Design Issues
Entities vs Attributes
Entities vs
Relationship
Binary vs Non-Binary
Design Decisions
ER Notation
Module Summary
Module 20
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
Module 20
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
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
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
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
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
Module 20
Module Summary
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
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
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
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
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
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
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
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
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
Module 20
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
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
Module 20
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