Week 3 Lecture Material
Week 3 Lecture Material
SWAYAM: NPTEL-NOC MOOCs Instructor: Prof. P P Das, IIT Kharagpur. Jan-Apr, 2018
E
Module 11: Advanced SQL
P T
Partha Pratim Das
Department of Computer Science and Engineering
Indian Institute of Technology, Kharagpur
Srijoni Majumdar
Himadri B G S Bhuyan
Gurunath Reddy M
Week 02 Recap
SWAYAM: NPTEL-NOC MOOCs Instructor: Prof. P P Das, IIT Kharagpur. Jan-Apr, 2018
L
Module 06: Introduction to SQL/1 Module 09: Intermediate SQL/1
History of SQL Join Expressions
E
Data Definition Language (DDL) Views
Basic Query Structure (DML) Transactions
Module 07: Introduction to SQL/2 Module 10: Intermediate SQL/2
T
Additional Basic Operations Integrity Constraints
Set Operations SQL Data Types and Schemas
P
Null Values Authorization
Aggregate Functions
N
Module 08: Introduction to SQL/3
Nested Subqueries
Modification of the Database
Database System Concepts - 6th Edition 11.2 ©Silberschatz, Korth and Sudarshan
PPD
Module Objectives
SWAYAM: NPTEL-NOC MOOCs Instructor: Prof. P P Das, IIT Kharagpur. Jan-Apr, 2018
L
To understand how to use SQL from a programming language
To familiarize with functions and procedures in SQL
E
To understand the triggers
P T
N
Database System Concepts - 6th Edition 11.3 ©Silberschatz, Korth and Sudarshan
PPD
Module Outline
SWAYAM: NPTEL-NOC MOOCs Instructor: Prof. P P Das, IIT Kharagpur. Jan-Apr, 2018
L
Accessing SQL From a Programming Language
Functions and Procedural Constructs
E
Triggers
P T
N
Database System Concepts - 6th Edition 11.4 ©Silberschatz, Korth and Sudarshan
PPD
L
Constructs
Triggers
T E
N P
ACCESSING SQL FROM A
PROGRAMMING LANGUAGE
Database System Concepts - 6th Edition 11.5 ©Silberschatz, Korth and Sudarshan
PPD
E L
T
Functions Table Names
Variables Attributes Datadase
Schema
Tables
N
Connection
Library
P Query
Processor
Database System Concepts - 6th Edition 11.6 ©Silberschatz, Korth and Sudarshan
Accessing SQL From a Programming Language
SWAYAM: NPTEL-NOC MOOCs Instructor: Prof. P P Das, IIT Kharagpur. Jan-Apr, 2018
L
API (application-program interface) for a program to interact with a
database server
E
Application makes calls to
Connect with the database server
T
Send SQL commands to the database server
Fetch tuples of result one-by-one into program variables
P
Various tools:
JDBC (Java Database Connectivity) works with Java
N
ODBC (Open Database Connectivity) works with C, C++, C#,
Visual Basic, and Python
Other API’s such as ADO.NET sit on top of ODBC
Embedded SQL
Database System Concepts - 6th Edition 11.7 ©Silberschatz, Korth and Sudarshan
JDBC
SWAYAM: NPTEL-NOC MOOCs Instructor: Prof. P P Das, IIT Kharagpur. Jan-Apr, 2018
L
JDBC is a Java API for communicating with database systems
supporting SQL
E
JDBC supports a variety of features for querying and updating data,
and for retrieving query results.
T
JDBC also supports metadata retrieval, such as querying about
relations present in the database and the names and types of
relation attributes
P
Model for communicating with the database:
Open a connection
N
Create a “statement” object
Execute queries using the Statement object to send queries and
fetch results
Exception mechanism to handle errors
Database System Concepts - 6th Edition 11.8 ©Silberschatz, Korth and Sudarshan
ODBC
SWAYAM: NPTEL-NOC MOOCs Instructor: Prof. P P Das, IIT Kharagpur. Jan-Apr, 2018
L
Open DataBase Connectivity (ODBC) standard
standard for application program to communicate with a
E
database server
application program interface (API) to
T
open a connection with a database,
send queries and updates,
P
get back results
Applications such as GUI, spreadsheets, etc. can use ODBC
N
Database System Concepts - 6th Edition 11.9 ©Silberschatz, Korth and Sudarshan
PPD
L
The code uses a
data source
named “SQLS”
E
from the odbc.ini
file to connect and
T
issue a query.
It creates a table,
P
inserts data using
literal and
parameterized
N
statements and
fetches the data
Source: https://dzone.com/articles/tutorial-connecting-to-odbc-data-sources-with-pyth
Database System Concepts - 6th Edition 11.10 ©Silberschatz, Korth and Sudarshan
PPD
L
C Program
Functions
E
Variables
SQL Query
T
Table Names Datadase
Attributes Schema
Tables
N P Query
Processor
Database System Concepts - 6th Edition 11.11 ©Silberschatz, Korth and Sudarshan
Embedded SQL
SWAYAM: NPTEL-NOC MOOCs Instructor: Prof. P P Das, IIT Kharagpur. Jan-Apr, 2018
L
The SQL standard defines embeddings of SQL in a variety of
programming languages such as C, C++, Java, Fortran, and PL/1
E
A language to which SQL queries are embedded is referred to as a
host language, and the SQL structures permitted in the host
T
language comprise embedded SQL
The basic form of these languages follows that of the System R
P
embedding of SQL into PL/1
EXEC SQL statement is used to identify embedded SQL request to
the preprocessor
N
EXEC SQL <embedded SQL statement >;
Note: this varies by language:
In some languages, like COBOL, the semicolon is replaced with
END-EXEC
In Java embedding uses # SQL { …. };
Database System Concepts - 6th Edition 11.12 ©Silberschatz, Korth and Sudarshan
Embedded SQL (Cont.)
SWAYAM: NPTEL-NOC MOOCs Instructor: Prof. P P Das, IIT Kharagpur. Jan-Apr, 2018
L
Before executing any SQL statements, the program must first connect
to the database. This is done using:
E
EXEC-SQL connect to server user user-name using password;
Here, server identifies the server to which a connection is to be
T
established
Variables of the host language can be used within embedded SQL
P
statements. They are preceded by a colon (:) to distinguish from
SQL variables (e.g., :credit_amount )
Variables used as above must be declared within DECLARE section,
N
as illustrated below. The syntax for declaring the variables, however,
follows the usual host language syntax
EXEC-SQL BEGIN DECLARE SECTION
int credit-amount ;
EXEC-SQL END DECLARE SECTION;
Database System Concepts - 6th Edition 11.13 ©Silberschatz, Korth and Sudarshan
Embedded SQL (Cont.)
SWAYAM: NPTEL-NOC MOOCs Instructor: Prof. P P Das, IIT Kharagpur. Jan-Apr, 2018
L
To write an embedded SQL query, we use the
declare c cursor for <SQL query>
E
statement. The variable c is used to identify the query
Example:
T
From within a host language, find the ID and name of
students who have completed more than the number of
P
credits stored in variable credit_amount in the host langue
Specify the query in SQL as follows:
N
EXEC SQL
declare c cursor for
select ID, name
from student
where tot_cred > :credit_amount
END_EXEC
Database System Concepts - 6th Edition 11.14 ©Silberschatz, Korth and Sudarshan
Embedded SQL (Cont.)
SWAYAM: NPTEL-NOC MOOCs Instructor: Prof. P P Das, IIT Kharagpur. Jan-Apr, 2018
L
Example:
From within a host language, find the ID and name of
E
students who have completed more than the number of
credits stored in variable credit_amount in the host langue
T
Specify the query in SQL as follows:
EXEC SQL
P
declare c cursor for
select ID, name
N
from student
where tot_cred > :credit_amount
END_EXEC
The variable c (used in the cursor declaration) is used to
identify the query
Database System Concepts - 6th Edition 11.15 ©Silberschatz, Korth and Sudarshan
Embedded SQL (Cont.)
SWAYAM: NPTEL-NOC MOOCs Instructor: Prof. P P Das, IIT Kharagpur. Jan-Apr, 2018
L
The open statement for our example is as follows:
EXEC SQL open c ;
E
This statement causes the database system to execute the query
and to save the results within a temporary relation. The query uses
T
the value of the host-language variable credit-amount at the time the
open statement is executed.
P
The fetch statement causes the values of one tuple in the query
result to be placed on host language variables.
N
EXEC SQL fetch c into :si, :sn END_EXEC
Database System Concepts - 6th Edition 11.16 ©Silberschatz, Korth and Sudarshan
Embedded SQL (Cont.)
SWAYAM: NPTEL-NOC MOOCs Instructor: Prof. P P Das, IIT Kharagpur. Jan-Apr, 2018
L
A variable called SQLSTATE in the SQL communication area
(SQLCA) gets set to ‘02000’ to indicate no more data is available
E
The close statement causes the database system to delete the
temporary relation that holds the result of the query.
T
EXEC SQL close c ;
Note: above details vary with language. For example, the Java
P
embedding defines Java iterators to step through result tuples.
N
Database System Concepts - 6th Edition 11.17 ©Silberschatz, Korth and Sudarshan
PPD
L
The program prompts the user for an order number, retrieves the
customer number, salesperson, and status of the order, and displays
the retrieved information on the screen
T E
N P
The statement used to return the data is a singleton SELECT
statement; that is, it returns only a single row of data. Therefore, the
code example does not declare or use cursors
Source: https://docs.microsoft.com/en-us/sql/odbc/reference/embedded-sql-example
Database System Concepts - 6th Edition 11.18 ©Silberschatz, Korth and Sudarshan
Updates Through Embedded SQL
SWAYAM: NPTEL-NOC MOOCs Instructor: Prof. P P Das, IIT Kharagpur. Jan-Apr, 2018
L
Embedded SQL expressions for database modification (update, insert,
and delete)
E
Can update tuples fetched by cursor by declaring that the cursor is for
update
T
EXEC SQL
declare c cursor for
P
select *
from instructor
where dept_name = ‘Music’
N
for update
We then iterate through the tuples by performing fetch operations on
the cursor (as illustrated earlier), and after fetching each tuple we
execute the following code:
update instructor
set salary = salary + 1000
where current of c
Database System Concepts - 6th Edition 11.19 ©Silberschatz, Korth and Sudarshan
PPD
L
Constructs
Triggers
T E
N P
FUNCTIONS AND PROCEDURAL
CONSTRUCTS
Database System Concepts - 6th Edition 11.20 ©Silberschatz, Korth and Sudarshan
PPD
L
C Program SQL Query
Functions Tables
Attributes Datadase
Variables
Function
E
Schema
Procedure
Tables
CL QP
C Program
Functions, Variables
P T
N
SQL Query
Table Names
Attributes Datadase
Function Schema
Procedure Tables
QP
Database System Concepts - 6th Edition 11.21 ©Silberschatz, Korth and Sudarshan
Functions and Procedures
SWAYAM: NPTEL-NOC MOOCs Instructor: Prof. P P Das, IIT Kharagpur. Jan-Apr, 2018
L
SQL:1999 supports functions and procedures
Functions/procedures can be written in SQL itself, or in an external
E
programming language (e.g., C, Java)
Functions written in an external languages are particularly useful
T
with specialized data types such as images and geometric objects.
Example: functions to check if polygons overlap, or to compare
P
images for similarity
Some database systems support table-valued functions, which
can return a relation as a result
N
SQL:1999 also supports a rich set of imperative constructs, including
Loops, if-then-else, assignment
Many databases have proprietary procedural extensions to SQL that
differ from SQL:1999
Database System Concepts - 6th Edition 11.22 ©Silberschatz, Korth and Sudarshan
SQL Functions
SWAYAM: NPTEL-NOC MOOCs Instructor: Prof. P P Das, IIT Kharagpur. Jan-Apr, 2018
L
Define a function that, given the name of a department, returns the
count of the number of instructors in that department.
E
create function dept_count (dept_name varchar(20))
returns integer
begin
T
declare d_count integer;
select count (* ) into d_count
P
from instructor
where instructor.dept_name = dept_name
return d_count;
N
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 System Concepts - 6th Edition 11.23 ©Silberschatz, Korth and Sudarshan
SQL functions (Cont.)
SWAYAM: NPTEL-NOC MOOCs Instructor: Prof. P P Das, IIT Kharagpur. Jan-Apr, 2018
L
Compound statement: begin … end
May contain multiple SQL statements between begin and
E
end.
returns – indicates the variable-type that is returned (e.g.,
T
integer)
return – specifies the values that are to be returned as result
P
of invoking the function
SQL function are in fact parameterized views that generalize
N
the regular notion of views by allowing parameters
Database System Concepts - 6th Edition 11.24 ©Silberschatz, Korth and Sudarshan
Table Functions
SWAYAM: NPTEL-NOC MOOCs Instructor: Prof. P P Das, IIT Kharagpur. Jan-Apr, 2018
L
SQL:2003 added functions that return a relation as a result
Example: Return all instructors in a given department
E
create function instructor_of (dept_name char(20))
returns table (
T
ID varchar(5),
name varchar(20),
P
dept_name varchar(20),
salary numeric(8,2))
N
return 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 System Concepts - 6th Edition 11.25 ©Silberschatz, Korth and Sudarshan
SQL Procedures
SWAYAM: NPTEL-NOC MOOCs Instructor: Prof. P P Das, IIT Kharagpur. Jan-Apr, 2018
L
The dept_count function could instead be written as procedure:
create procedure dept_count_proc (
E
in dept_name varchar(20),
out d_count integer)
T
begin
select count(*) into d_count
P
from instructor
where instructor.dept_name = dept_count_proc.dept_name
end
N
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 more than one function/procedure of the same name
(called name overloading), as long as the number of
arguments differ, or at least the types of the arguments differ
Database System Concepts - 6th Edition 11.26 ©Silberschatz, Korth and Sudarshan
Language Constructs for Procedures & Functions
SWAYAM: NPTEL-NOC MOOCs Instructor: Prof. P P Das, IIT Kharagpur. Jan-Apr, 2018
L
SQL supports constructs that gives it almost all the power of a general-
purpose programming language.
Warning: most database systems implement their own variant of the
E
standard syntax below.
Compound statement: begin … end,
T
May contain multiple SQL statements between begin and end.
P
Local variables can be declared within a compound statements
While and repeat statements:
N
while boolean expression do
sequence of statements ;
end while
repeat
sequence of statements ;
until boolean expression
end repeat
Database System Concepts - 6th Edition 11.27 ©Silberschatz, Korth and Sudarshan
Language Constructs (Cont.)
SWAYAM: NPTEL-NOC MOOCs Instructor: Prof. P P Das, IIT Kharagpur. Jan-Apr, 2018
L
For loop
Permits iteration over all results of a query
E
Example: Find the budget of all departments
T
declare n integer default 0;
for r as
select budget from department
P
do
set n = n + r.budget
end for
N
Database System Concepts - 6th Edition 11.28 ©Silberschatz, Korth and Sudarshan
Language Constructs (Cont.)
SWAYAM: NPTEL-NOC MOOCs Instructor: Prof. P P Das, IIT Kharagpur. Jan-Apr, 2018
L
Conditional statements (if-then-else)
SQL:1999 also supports a case statement similar to C case statement
E
Example procedure: registers student after ensuring classroom capacity
is not exceeded
T
Returns 0 on success and -1 if capacity is exceeded
See book (page 177) for details
P
Signaling of exception conditions, and declaring handlers for exceptions
declare out_of_classroom_seats condition
N
declare exit handler for out_of_classroom_seats
begin
…
.. signal out_of_classroom_seats
end
The handler here is exit -- causes enclosing begin..end to be exited
Other actions possible on exception
Database System Concepts - 6th Edition 11.29 ©Silberschatz, Korth and Sudarshan
External Language Routines*
SWAYAM: NPTEL-NOC MOOCs Instructor: Prof. P P Das, IIT Kharagpur. Jan-Apr, 2018
L
SQL:1999 permits the use of functions and procedures written in other
E
languages such as C or C++
Declaring external language procedures and functions
T
create procedure dept_count_proc(in dept_name varchar(20),
P
out count integer)
language C
external name ’ /usr/avi/bin/dept_count_proc’
N
create function dept_count(dept_name varchar(20))
returns integer
language C
external name ‘/usr/avi/bin/dept_count’
Database System Concepts - 6th Edition 11.30 ©Silberschatz, Korth and Sudarshan
External Language Routines (Contd.)*
SWAYAM: NPTEL-NOC MOOCs Instructor: Prof. P P Das, IIT Kharagpur. Jan-Apr, 2018
L
SQL:1999 allows the definition of procedures in an imperative programming
language, (Java, C#, C or C++) which can be invoked from SQL queries.
E
Functions defined in this fashion can be more efficient than functions defined
in SQL, and computations that cannot be carried out in SQL can be
T
executed by these functions.
Declaring external language procedures and functions
P
create procedure dept_count_proc(in dept_name varchar(20),
out count integer)
N
language C
external name ’ /usr/avi/bin/dept_count_proc’
Database System Concepts - 6th Edition 11.31 ©Silberschatz, Korth and Sudarshan
External Language Routines (Cont.)*
SWAYAM: NPTEL-NOC MOOCs Instructor: Prof. P P Das, IIT Kharagpur. Jan-Apr, 2018
L
Benefits of external language functions/procedures:
more efficient for many operations, and more expressive power
E
Drawbacks
Code to implement function may need to be loaded into database
T
system and executed in the database system’s address space.
risk of accidental corruption of database structures
P
security risk, allowing users access to unauthorized data
There are alternatives, which give good security at the cost of
N
potentially worse performance
Direct execution in the database system’s space is used when
efficiency is more important than security
Database System Concepts - 6th Edition 11.32 ©Silberschatz, Korth and Sudarshan
Security with External Language Routines*
SWAYAM: NPTEL-NOC MOOCs Instructor: Prof. P P Das, IIT Kharagpur. Jan-Apr, 2018
L
To deal with security problems, we can do on of the following:
Use sandbox techniques
E
That is, use a safe language like Java, which cannot be used
to access/damage other parts of the database code.
T
Run external language functions/procedures in a separate
process, with no access to the database process’ memory.
P
Parameters and results communicated via inter-process
communication
N
Both have performance overheads
Many database systems support both above approaches as well as
direct executing in database system address space.
Database System Concepts - 6th Edition 11.33 ©Silberschatz, Korth and Sudarshan
PPD
L
Constructs
Triggers
T E
TRIGGERS
NP
Database System Concepts - 6th Edition 11.34 ©Silberschatz, Korth and Sudarshan
Triggers
SWAYAM: NPTEL-NOC MOOCs Instructor: Prof. P P Das, IIT Kharagpur. Jan-Apr, 2018
L
A trigger is a statement that is executed automatically by the
system as a side effect of a modification to the database
E
To design a trigger mechanism, we must:
Specify the conditions under which the trigger is to be
T
executed.
Specify the actions to be taken when the trigger executes.
P
Triggers introduced to SQL standard in SQL:1999, but
supported even earlier using non-standard syntax by most
databases.
N
Syntax illustrated here may not work exactly on your
database system; check the system manuals
Database System Concepts - 6th Edition 11.35 ©Silberschatz, Korth and Sudarshan
Triggering Events and Actions in SQL
SWAYAM: NPTEL-NOC MOOCs Instructor: Prof. P P Das, IIT Kharagpur. Jan-Apr, 2018
L
Triggering event can be insert, delete or update
Triggers on update can be restricted to specific attributes
E
For example, after update of takes on grade
Values of attributes before and after an update can be referenced
T
referencing old row as : for deletes and updates
referencing new row as : for inserts and updates
P
Triggers can be activated before an event, which can serve as
extra constraints. For example, convert blank grades to null.
N
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 System Concepts - 6th Edition 11.36 ©Silberschatz, Korth and Sudarshan
Trigger to Maintain credits_earned value
SWAYAM: NPTEL-NOC MOOCs Instructor: Prof. P P Das, IIT Kharagpur. Jan-Apr, 2018
L
create trigger credits_earned after update of takes on (grade)
referencing new row as nrow
referencing old row as orow
E
for each row
when nrow.grade <> ’F’ and nrow.grade is not null
T
and (orow.grade = ’F’ or orow.grade is null)
begin atomic
P
update student
set tot_cred= tot_cred +
(select credits
N
from course
where course.course_id= nrow.course_id)
where student.id = nrow.id;
end;
Database System Concepts - 6th Edition 11.37 ©Silberschatz, Korth and Sudarshan
Statement Level Triggers
SWAYAM: NPTEL-NOC MOOCs Instructor: Prof. P P Das, IIT Kharagpur. Jan-Apr, 2018
L
Instead of executing a separate action for each affected row, a
single action can be executed for all rows affected by a transaction
E
Use for each statement instead of for each row
Use referencing old table or referencing new table to
T
refer to temporary tables (called transition tables) containing
the affected rows
P
Can be more efficient when dealing with SQL statements that
update a large number of rows
N
Database System Concepts - 6th Edition 11.38 ©Silberschatz, Korth and Sudarshan
When Not To Use Triggers
SWAYAM: NPTEL-NOC MOOCs Instructor: Prof. P P Das, IIT Kharagpur. Jan-Apr, 2018
L
Triggers were used earlier for tasks such as
Maintaining summary data (e.g., total salary of each
department)
E
Replicating databases by recording changes to special
relations (called change or delta relations) and having a
T
separate process that applies the changes over to a replica
There are better ways of doing these now:
P
Databases today provide built in materialized view facilities
to maintain summary data
Databases provide built-in support for replication
N
Encapsulation facilities can be used instead of triggers in many
cases
Define methods to update fields
Carry out actions as part of the update methods instead of
through a trigger
Database System Concepts - 6th Edition 11.39 ©Silberschatz, Korth and Sudarshan
When Not To Use Triggers (Cont.)
SWAYAM: NPTEL-NOC MOOCs Instructor: Prof. P P Das, IIT Kharagpur. Jan-Apr, 2018
L
Risk of unintended execution of triggers, for example, when
Loading data from a backup copy
E
Replicating updates at a remote site
Trigger execution can be disabled before such actions.
T
Other risks with triggers:
Error leading to failure of critical transactions that set off
the trigger
P
Cascading execution
N
Database System Concepts - 6th Edition 11.40 ©Silberschatz, Korth and Sudarshan
Module Summary
SWAYAM: NPTEL-NOC MOOCs Instructor: Prof. P P Das, IIT Kharagpur. Jan-Apr, 2018
L
Introduced the use of SQL from a programming language
Familiarized with functions and procedures in SQL
E
Understood the triggers
P T
N
Database System Concepts - 6th Edition 11.41 ©Silberschatz, Korth and Sudarshan
PPD
L
Name Mail Mobile
Partha Pratim Das, Instructor [email protected] 9830030880
E
Srijoni Majumdar, TA [email protected] 9674474267
Himadri B G S Bhuyan, TA [email protected] 9438911655
T
Gurunath Reddy M [email protected] 9434137638
N P
Slides used in this presentation are borrowed from http://db-book.com/
with kind permission of the authors.
Database System Concepts - 6th Edition 11.42 ©Silberschatz, Korth and Sudarshan
L
SWAYAM: NPTEL-NOC MOOCs Instructor: Prof. P P Das, IIT Kharagpur. Jan-Apr, 2018
E
Module 12: Formal Relational Query Languages
P T
Partha Pratim Das
Department of Computer Science and Engineering
Indian Institute of Technology, Kharagpur
Srijoni Majumdar
Himadri B G S Bhuyan
Gurunath Reddy M
Module Recap
SWAYAM: NPTEL-NOC MOOCs Instructor: Prof. P P Das, IIT Kharagpur. Jan-Apr, 2018
L
Accessing SQL From a Programming Language
Functions and Procedural Constructs
E
Triggers
P T
N
Database System Concepts - 6th Edition 12.2 ©Silberschatz, Korth and Sudarshan
PPD
Module Objectives
SWAYAM: NPTEL-NOC MOOCs Instructor: Prof. P P Das, IIT Kharagpur. Jan-Apr, 2018
L
To understand formal query language through relational algebra
T E
N P
Database System Concepts - 6th Edition 12.3 ©Silberschatz, Korth and Sudarshan
PPD
Module Outline
SWAYAM: NPTEL-NOC MOOCs Instructor: Prof. P P Das, IIT Kharagpur. Jan-Apr, 2018
L
Relational Algebra
Tuple Relational Calculus (Overview only)
E
Domain Relational Calculus (Overview only)
Equivalence of Algebra and Calculus
P T
N
Database System Concepts - 6th Edition 12.4 ©Silberschatz, Korth and Sudarshan
Formal Relational Query Language
SWAYAM: NPTEL-NOC MOOCs Instructor: Prof. P P Das, IIT Kharagpur. Jan-Apr, 2018
L
Relational Algebra
Procedural and Algebra based
E
Tuple Relational Calculus
Non-Procedural and Predicate Calculus based
T
Domain Relational Calculus
Non-Procedural and Predicate Calculus based
N P
Database System Concepts - 6th Edition 12.5 ©Silberschatz, Korth and Sudarshan
PPD
Relational Algebra
Tuple Relational
Calculus
SWAYAM: NPTEL-NOC MOOCs Instructor: Prof. P P Das, IIT Kharagpur. Jan-Apr, 2018
L
Domain Relational
Calculus
E
Equivalence of Algebra
and Calculus
P T
N
RELATIONAL ALGEBRA
Database System Concepts - 6th Edition 12.6 ©Silberschatz, Korth and Sudarshan
PPD
Relational Algebra
SWAYAM: NPTEL-NOC MOOCs Instructor: Prof. P P Das, IIT Kharagpur. Jan-Apr, 2018
L
Created by Edgar F Codd at IBM in 1970
Procedural language
E
Six basic operators
select:
T
project:
union:
P
set difference: –
N
Cartesian product: x
rename:
The operators take one or two relations as inputs and produce a new
relation as a result
Database System Concepts - 6th Edition 12.7 ©Silberschatz, Korth and Sudarshan
PPD
Select Operation
SWAYAM: NPTEL-NOC MOOCs Instructor: Prof. P P Das, IIT Kharagpur. Jan-Apr, 2018
Notation: p(r)
L
p is called the selection predicate
Defined as:
E
p(r) = {t | t r and p(t)}
T
where p is a formula in propositional calculus consisting of terms
connected by : (and), (or), (not)
P
Each term is one of:
<attribute> op <attribute> or <constant>
N
where op is one of: =, , >, . <.
Example of selection:
dept_name=“Physics”(instructor)
A=B ^ D > 5 (r)
Database System Concepts - 6th Edition 12.8 ©Silberschatz, Korth and Sudarshan
PPD
Project Operation
SWAYAM: NPTEL-NOC MOOCs Instructor: Prof. P P Das, IIT Kharagpur. Jan-Apr, 2018
L
Notation:
A1 , A2 ,, Ak (r )
E
where A1, A2 are attribute names and r is a relation name
The result is defined as the relation of k columns obtained by erasing
T
the columns that are not listed
Duplicate rows removed from result, since relations are sets
P
Example: To eliminate the dept_name attribute of instructor
N
ID, name, salary (instructor)
A,C (r)
Database System Concepts - 6th Edition 12.9 ©Silberschatz, Korth and Sudarshan
PPD
Union Operation
SWAYAM: NPTEL-NOC MOOCs Instructor: Prof. P P Das, IIT Kharagpur. Jan-Apr, 2018
L
Notation: r s
Defined as:
E
r s = {t | t r or t s}
For r s to be valid.
T
1. r, s must have the same arity (same number of attributes)
2. The attribute domains must be compatible (example: 2nd column
P
of r deals with the same type of values as does the 2nd
column of s)
N
Example: to find all courses taught in the Fall 2009 semester, or in the
Spring 2010 semester, or in both
Database System Concepts - 6th Edition 12.10 ©Silberschatz, Korth and Sudarshan
PPD
L
Notation r – s
Defined as:
r – s = {t | t r and t s}
T E
Set differences must be taken between compatible relations
r and s must have the same arity
P
attribute domains of r and s must be compatible
Example: to find all courses taught in the Fall 2009 semester, but
N
not in the Spring 2010 semester
course_id ( semester=“Fall” Λ year=2009 (section)) −
course_id ( semester=“Spring” Λ year=2010 (section)) r –s
Database System Concepts - 6th Edition 12.11 ©Silberschatz, Korth and Sudarshan
Set-Intersection Operation
SWAYAM: NPTEL-NOC MOOCs Instructor: Prof. P P Das, IIT Kharagpur. Jan-Apr, 2018
L
Notation: r s
Defined as:
E
r s = { t | t r and t s }
Assume:
T
r, s have the same arity
attributes of r and s are compatible
P
Note: r s = r – (r – s)
N rs
Database System Concepts - 6th Edition 12.12 ©Silberschatz, Korth and Sudarshan
PPD
Cartesian-Product Operation
SWAYAM: NPTEL-NOC MOOCs Instructor: Prof. P P Das, IIT Kharagpur. Jan-Apr, 2018
L
Notation r x s
Defined as:
E
r x s = {t q | t r and q s}
T
Assume that attributes of r(R) and s(S) are
disjoint. (That is, R S = )
P
If attributes of r(R) and s(S) are not disjoint, then
renaming must be used
N rxs
Database System Concepts - 6th Edition 12.13 ©Silberschatz, Korth and Sudarshan
Rename Operation
SWAYAM: NPTEL-NOC MOOCs Instructor: Prof. P P Das, IIT Kharagpur. Jan-Apr, 2018
L
Allows us to name, and therefore to refer to, the results of relational-
algebra expressions.
Allows us to refer to a relation by more than one name.
E
Example:
T
x (E)
P
returns the expression E under the name X
If a relational-algebra expression E has arity n, then
N
x ( A1 , A 2 ,..., A n ) ( E )
returns the result of expression E under the name X, and with the
attributes renamed to A1 , A2 , …., An .
Database System Concepts - 6th Edition 12.14 ©Silberschatz, Korth and Sudarshan
PPD
Division Operation
SWAYAM: NPTEL-NOC MOOCs Instructor: Prof. P P Das, IIT Kharagpur. Jan-Apr, 2018
L
The division operation is applied to two relations
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 attributes of R that are not attributes of S
E
The result of DIVISION is a relation T(Y) that includes a tuple t if tuples
T
tR appear in R with tR [Y] = t, and with
tR [X] = ts for every tuple ts in S.
P
For a tuple t to appear in the result T of the DIVISION, the values in t
must appear in R in combination with every tuple in S
Division is a derived operation and can be expressed in terms of other
N
oeprations
Database System Concepts - 6th Edition 12.15 ©Silberschatz, Korth and Sudarshan
PPD
L
Relations r, s:
A B B
E
1 1
2
2
T
3
1 s
1
P
1
3
4
N
6 e.g.
1
A is customer name
2
r s: A r
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)
Source: db.fcngroup.nl/silberslides/Divsion%20-%20Slides%20-%20relational%20algebra.pptx
Database System Concepts - 6th Edition 12.16 ©Silberschatz, Korth and Sudarshan
Another Division Example
SWAYAM: NPTEL-NOC MOOCs Instructor: Prof. P P Das, IIT Kharagpur. Jan-Apr, 2018
L
Relations r, s:
A B C D E D E
E
a a 1 a 1
a a 1 b 1
T
a b 1 s
a a 1
P
a b 3
a a 1
a b 1
N
a b 1
r
e.g.
r s: Students who have taken both "a” and
A B C “b” courses, with instructor “1”
Source: db.fcngroup.nl/silberslides/Divsion%20-%20Slides%20-%20relational%20algebra.pptx
Database System Concepts - 6th Edition 12.17 ©Silberschatz, Korth and Sudarshan
Formal Definition
SWAYAM: NPTEL-NOC MOOCs Instructor: Prof. P P Das, IIT Kharagpur. Jan-Apr, 2018
L
A basic expression in the relational algebra consists of either one of the
following:
E
A relation in the database
A constant relation
T
Let E1 and E2 be relational-algebra expressions; the following are all
relational-algebra expressions:
P
E1 E2
N
E1 – E2
E1 x E2
Database System Concepts - 6th Edition 12.18 ©Silberschatz, Korth and Sudarshan
PPD
Relational Algebra
Tuple Relational
Calculus
SWAYAM: NPTEL-NOC MOOCs Instructor: Prof. P P Das, IIT Kharagpur. Jan-Apr, 2018
L
Domain Relational
Calculus
E
Equivalence of Algebra
and Calculus
P T
N
TUPLE RELATIONAL CALCULUS
Database System Concepts - 6th Edition 12.19 ©Silberschatz, Korth and Sudarshan
Tuple Relational Calculus
SWAYAM: NPTEL-NOC MOOCs Instructor: Prof. P P Das, IIT Kharagpur. Jan-Apr, 2018
L
A nonprocedural query language, where each query is of the form
{t | P (t ) }
E
It is the set of all tuples t such that predicate P is true for t
t is a tuple variable, t [A ] denotes the value of tuple t on attribute A
T
t r denotes that tuple t is in relation r
P is a formula similar to that of the predicate calculus
P
N
Database System Concepts - 6th Edition 12.20 ©Silberschatz, Korth and Sudarshan
Predicate Calculus Formula
SWAYAM: NPTEL-NOC MOOCs Instructor: Prof. P P Das, IIT Kharagpur. Jan-Apr, 2018
L
1. Set of attributes and constants
2. Set of comparison operators: (e.g., , , , , , )
E
3. Set of connectives: and (), or (v)‚ not ()
4. Implication (): x y, if x if true, then y is true
T
x y x v y
5. Set of quantifiers:
P
t r (Q (t )) ”there exists” a tuple in t in relation r
such that predicate Q (t ) is true
N
t r (Q (t )) Q is true “for all” tuples t in relation r
Database System Concepts - 6th Edition 12.21 ©Silberschatz, Korth and Sudarshan
Safety of Expressions
SWAYAM: NPTEL-NOC MOOCs Instructor: Prof. P P Das, IIT Kharagpur. Jan-Apr, 2018
L
It is possible to write tuple calculus expressions that generate infinite relations
For example, { t | t r } results in an infinite relation if the domain of any attribute of
E
relation r is infinite
To guard against the problem, we restrict the set of allowable expressions to safe
T
expressions
An expression {t | P (t )} in the tuple relational calculus is safe if every component of t
P
appears in one of the relations, tuples, or constants that appear in P
NOTE: this is more than just a syntax condition
N
E.g. { t | t [A] = 5 true } is not safe --- it defines an infinite set with attribute values
that do not appear in any relation or tuples or constants in P
Database System Concepts - 6th Edition 12.22 ©Silberschatz, Korth and Sudarshan
PPD
Relational Algebra
Tuple Relational
Calculus
SWAYAM: NPTEL-NOC MOOCs Instructor: Prof. P P Das, IIT Kharagpur. Jan-Apr, 2018
L
Domain Relational
Calculus
E
Equivalence of Algebra
and Calculus
P T
N
DOMAIN RELATIONAL CALCULUS
Database System Concepts - 6th Edition 12.23 ©Silberschatz, Korth and Sudarshan
Domain Relational Calculus
SWAYAM: NPTEL-NOC MOOCs Instructor: Prof. P P Das, IIT Kharagpur. Jan-Apr, 2018
L
A nonprocedural query language equivalent in power to the tuple
relational calculus
E
Each query is an expression of the form:
T
{ x1, x2, …, xn | P (x1, x2, …, xn)}
P
x1, x2, …, xn represent domain variables
P represents a formula similar to that of the predicate calculus
N
Database System Concepts - 6th Edition 12.24 ©Silberschatz, Korth and Sudarshan
PPD
Relational Algebra
Tuple Relational
Calculus
SWAYAM: NPTEL-NOC MOOCs Instructor: Prof. P P Das, IIT Kharagpur. Jan-Apr, 2018
L
Domain Relational
Calculus
E
Equivalence of Algebra
and Calculus
P T
N
EQUIVALENCE OF ALGEBRA AND
CALCULUS
Database System Concepts - 6th Edition 12.25 ©Silberschatz, Korth and Sudarshan
PPD
E L
P T
N
Source: http://www.cs.sfu.ca/CourseCentral/354/louie/Equiv_Notations.pdf
Database System Concepts - 6th Edition 12.26 ©Silberschatz, Korth and Sudarshan
PPD
E L
P T
N
Source: http://www.cs.sfu.ca/CourseCentral/354/louie/Equiv_Notations.pdf
Database System Concepts - 6th Edition 12.27 ©Silberschatz, Korth and Sudarshan
PPD
E L
P T
N
Source: http://www.cs.sfu.ca/CourseCentral/354/louie/Equiv_Notations.pdf
Database System Concepts - 6th Edition 12.28 ©Silberschatz, Korth and Sudarshan
PPD
E L
P T
N
Source: http://www.cs.sfu.ca/CourseCentral/354/louie/Equiv_Notations.pdf
Database System Concepts - 6th Edition 12.29 ©Silberschatz, Korth and Sudarshan
PPD
E L
P T
N
Source: http://www.cs.sfu.ca/CourseCentral/354/louie/Equiv_Notations.pdf
Database System Concepts - 6th Edition 12.30 ©Silberschatz, Korth and Sudarshan
PPD
E L
P T
N
Source: http://www.cs.sfu.ca/CourseCentral/354/louie/Equiv_Notations.pdf
Database System Concepts - 6th Edition 12.31 ©Silberschatz, Korth and Sudarshan
PPD
E L
P T
N
Source: http://www.cs.sfu.ca/CourseCentral/354/louie/Equiv_Notations.pdf
Database System Concepts - 6th Edition 12.32 ©Silberschatz, Korth and Sudarshan
PPD
E L
P T
N
Source: http://www.cs.sfu.ca/CourseCentral/354/louie/Equiv_Notations.pdf
Database System Concepts - 6th Edition 12.33 ©Silberschatz, Korth and Sudarshan
PPD
E L
P T
N
Source: http://www.cs.sfu.ca/CourseCentral/354/louie/Equiv_Notations.pdf
Database System Concepts - 6th Edition 12.34 ©Silberschatz, Korth and Sudarshan
Module Summary
SWAYAM: NPTEL-NOC MOOCs Instructor: Prof. P P Das, IIT Kharagpur. Jan-Apr, 2018
L
Discussed relational algebra with examples
Introduced tuple relational and domain relational calculus
E
Illustrated equivalence of algebra and calculus
P T
N
Database System Concepts - 6th Edition 12.35 ©Silberschatz, Korth and Sudarshan
PPD
L
Name Mail Mobile
Partha Pratim Das, Instructor [email protected] 9830030880
E
Srijoni Majumdar, TA [email protected] 9674474267
Himadri B G S Bhuyan, TA [email protected] 9438911655
T
Gurunath Reddy M [email protected] 9434137638
N P
Slides used in this presentation are borrowed from http://db-book.com/
with kind permission of the authors.
Database System Concepts - 6th Edition 12.36 ©Silberschatz, Korth and Sudarshan
L
SWAYAM: NPTEL-NOC MOOCs Instructor: Prof. P P Das, IIT Kharagpur. Jan-Apr, 2018
E
Module 13: Entity-Relationship Model/1
P T
Partha Pratim Das
Department of Computer Science and Engineering
Indian Institute of Technology, Kharagpur
Srijoni Majumdar
Himadri B G S Bhuyan
Gurunath Reddy M
Module Recap
SWAYAM: NPTEL-NOC MOOCs Instructor: Prof. P P Das, IIT Kharagpur. Jan-Apr, 2018
L
Relational Algebra
Tuple Relational Calculus (Overview only)
E
Domain Relational Calculus (Overview only)
Equivalence of Algebra and Calculus
P T
N
Database System Concepts 13.2 ©Silberschatz, Korth and Sudarshan
PPD
Module Objectives
SWAYAM: NPTEL-NOC MOOCs Instructor: Prof. P P Das, IIT Kharagpur. Jan-Apr, 2018
L
To understand the Design Process for Database Systems
To study the E-R Model for real world representation
T E
N P
Database System Concepts 13.3 ©Silberschatz, Korth and Sudarshan
PPD
Module Outline
SWAYAM: NPTEL-NOC MOOCs Instructor: Prof. P P Das, IIT Kharagpur. Jan-Apr, 2018
L
Design Process
E-R Model
E
Entity and Entity Set
Relationship
T
Cardinality
P
Attributes
Weak Entity Sets
N
Database System Concepts 13.4 ©Silberschatz, Korth and Sudarshan
PPD
Design Process
E-R Model
SWAYAM: NPTEL-NOC MOOCs Instructor: Prof. P P Das, IIT Kharagpur. Jan-Apr, 2018
E L
P T
N
DESIGN PROCESS
L
The initial phase of database design is to characterize fully the
data needs of the prospective database users
E
Next, the designer chooses a data model and, by applying the
concepts of the chosen data model, translates these
T
requirements into a conceptual schema of the database
A fully developed conceptual schema also indicates the
P
functional requirements of the enterprise. In a “specification of
functional requirements”, users describe the kinds of operations
(or transactions) that will be performed on the data
N
Database System Concepts 13.6 ©Silberschatz, Korth and Sudarshan
Design Phases (Cont.)
SWAYAM: NPTEL-NOC MOOCs Instructor: Prof. P P Das, IIT Kharagpur. Jan-Apr, 2018
L
The process of moving from an abstract data model to the
implementation of the database proceeds in two final design
E
phases.
T
Database design requires that we find a “good” collection
of relation schemas.
P
Business decision – What attributes should we record
in the database?
Computer Science decision – What relation schemas
N
should we have and how should the attributes be
distributed among the various relation schemas?
Physical Design – Deciding on the physical layout of the
database
L
Entity Relationship Model (covered in this chapter)
Models an enterprise as a collection of entities and relationships
E
Entity: a “thing” or “object” in the enterprise that is distinguishable from other objects
Described by a set of attributes
T
Relationship: an association among several entities
Represented diagrammatically by an entity-relationship diagram:
P
Normalization Theory (Chapter 8)
Formalize what designs are bad, and test for them
N
Database System Concepts 13.8 ©Silberschatz, Korth and Sudarshan
PPD
Design Process
E-R Model
SWAYAM: NPTEL-NOC MOOCs Instructor: Prof. P P Das, IIT Kharagpur. Jan-Apr, 2018
E L
P T
E-R MODEL
N
Database System Concepts 13.9 ©Silberschatz, Korth and Sudarshan
ER model – Database Modeling
SWAYAM: NPTEL-NOC MOOCs Instructor: Prof. P P Das, IIT Kharagpur. Jan-Apr, 2018
L
The ER data model was developed to facilitate database design by
allowing specification of an enterprise schema that represents the
E
overall logical structure of a database
The ER model is very useful in mapping the meanings and
T
interactions of real-world enterprises onto a conceptual schema.
Because of this usefulness, many database-design tools draw on
concepts from the ER model
P
The ER data model employs three basic concepts:
entity sets
N
relationship sets
attributes
The ER model also has an associated diagrammatic
representation, the ER diagram, which can express the overall
logical structure of a database graphically
L
An entity is an object that exists and is distinguishable from
other objects.
E
Example: specific person, company, event, plant
An entity set is a set of entities of the same type that share
T
the same properties.
Example: set of all persons, companies, trees, holidays
P
An entity is represented by a set of attributes; i.e., descriptive
properties possessed by all members of an entity set.
N
Example:
instructor = (ID, name, street, city, salary )
course= (course_id, title, credits)
A subset of the attributes form a primary key of the entity
set; i.e., uniquely identifying each member of the set.
L
instructor_ID instructor_name student-ID student_name
T E
N P
Database System Concepts 13.12 ©Silberschatz, Korth and Sudarshan
Relationship Sets
SWAYAM: NPTEL-NOC MOOCs Instructor: Prof. P P Das, IIT Kharagpur. Jan-Apr, 2018
L
A relationship is an association among several entities
Example:
E
44553 (Peltier) advisor 22222 (Einstein)
student entity relationship set instructor entity
T
A relationship set is a mathematical relation among n 2 entities, each
taken from entity sets
P
{(e1, e2, … en) | e1 E1, e2 E2, …, en En}
N
Example:
(44553,22222) advisor
E L
P T
N
Database System Concepts 13.14 ©Silberschatz, Korth and Sudarshan
Relationship Sets (Cont.)
SWAYAM: NPTEL-NOC MOOCs Instructor: Prof. P P Das, IIT Kharagpur. Jan-Apr, 2018
L
An attribute can also be associated with a relationship set.
For instance, the advisor relationship set between entity sets
instructor and student may have the attribute date which tracks
E
when the student started being associated with the advisor
P T
N
Database System Concepts 13.15 ©Silberschatz, Korth and Sudarshan
Degree of a Relationship Set
SWAYAM: NPTEL-NOC MOOCs Instructor: Prof. P P Das, IIT Kharagpur. Jan-Apr, 2018
L
Binary relationship
involve two entity sets (or degree two).
E
most relationship sets in a database system are binary.
Relationships between more than two entity sets are rare. Most
T
relationships are binary. (More on this later.)
Example: students work on research projects under the
P
guidance of an instructor.
relationship proj_guide is a ternary relationship between
N
instructor, student, and project
L
Express the number of entities to which another entity can be
associated via a relationship set.
E
Most useful in describing binary relationship sets.
For a binary relationship set the mapping cardinality must be one of
T
the following types:
One to one
P
One to many
Many to one
N
Many to many
E L
P T
N
One to one One to many
E L
P T
one
N
Many to Many to many
L
Attribute types:
Simple and composite attributes.
E
Single-valued and multivalued attributes
T
Example: multivalued attribute: phone_numbers
Derived attributes
P
Can be computed from other attributes
Example: age, given date_of_birth
N
Domain – the set of permitted values for each attribute
E L
P T
N
Database System Concepts 13.21 ©Silberschatz, Korth and Sudarshan
Redundant Attributes
SWAYAM: NPTEL-NOC MOOCs Instructor: Prof. P P Das, IIT Kharagpur. Jan-Apr, 2018
L
Suppose we have entity sets:
instructor, with attributes: ID, name, dept_name, salary
E
department, with attributes: dept_name, building, budget
We model the fact that each instructor has an associated
T
department using a relationship set inst_dept
The attribute dept_name appears in both entity sets. Since
P
it is the primary key for the entity set department, it
replicates information present in the relationship and is
therefore redundant in the entity set instructor and needs to
N
be removed
BUT: when converting back to tables, in some cases the
attribute gets reintroduced, as we will see later
L
Consider a section entity, which is uniquely
identified by a course_id, semester, year,
E
and sec_id.
Clearly, section entities are related to course
T
entities. Suppose we create a relationship
set sec_course between entity sets section
and course.
P
Note that the information in sec_course is
redundant, since section already has an
N
attribute course_id, which identifies the
course with which the section is related.
One option to deal with this redundancy is to
get rid of the relationship sec_course;
however, by doing so the relationship
between section and course becomes
implicit in an attribute, which is not desirable.
L
An alternative way to deal with this redundancy is to not store the
attribute course_id in the section entity and to only store the
E
remaining attributes section_id, year, and semester. However, the
entity set section then does not have enough attributes to identify a
T
particular section entity uniquely; although each section entity is
distinct, sections for different courses may share the same
section_id, year, and semester.
P
To deal with this problem, we treat the relationship sec_course as a
special relationship that provides extra information, in this case, the
N
course_id, required to identify section entities uniquely.
The notion of weak entity set formalizes the above intuition. A weak
entity set is one whose existence is dependent on another entity,
called its identifying entity; instead of associating a primary key
with a weak entity, we use the identifying entity, along with extra
attributes called discriminator to uniquely identify a weak entity. An
entity set that is not a weak entity set is termed a strong entity set.
L
Every weak entity must be associated with an identifying
entity; that is, the weak entity set is said to be existence
E
dependent on the identifying entity set. The identifying entity
set is said to own the weak entity set that it identifies. The
T
relationship associating the weak entity set with the
identifying entity set is called the identifying relationship.
Note that the relational schema we eventually create from the
P
entity set section does have the attribute course_id, for
reasons that will become clear later, even though we have
N
dropped the attribute course_id from the entity set section.
L
Introduced the Design Process for Database Systems
Elucidated the E-R Model for real world representation with
E
entities, entity sets, relationships, etc.
P T
N
Database System Concepts 13.26 ©Silberschatz, Korth and Sudarshan
PPD
L
Name Mail Mobile
Partha Pratim Das, Instructor [email protected] 9830030880
E
Srijoni Majumdar, TA [email protected] 9674474267
Himadri B G S Bhuyan, TA [email protected] 9438911655
T
Gurunath Reddy M [email protected] 9434137638
N P
Slides used in this presentation are borrowed from http://db-book.com/
with kind permission of the authors.
E
Module 14: Entity-Relationship Model/2
P T
Partha Pratim Das
Department of Computer Science and Engineering
Indian Institute of Technology, Kharagpur
Srijoni Majumdar
Himadri B G S Bhuyan
Gurunath Reddy M
Module Recap
SWAYAM: NPTEL-NOC MOOCs Instructor: Prof. P P Das, IIT Kharagpur. Jan-Apr, 2018
L
Design Process
E-R Model
E
Entity and Entity Set
Relationship
T
Cardinality
P
Attributes
Weak Entity Sets
N
Database System Concepts 14.2 ©Silberschatz, Korth and Sudarshan
PPD
Module Objectives
SWAYAM: NPTEL-NOC MOOCs Instructor: Prof. P P Das, IIT Kharagpur. Jan-Apr, 2018
L
To illustrate E-R Diagram notation for E-R Models
To explore translation of E-R Models to Relational Schemas
T E
N P
Database System Concepts 14.3 ©Silberschatz, Korth and Sudarshan
PPD
Module Outline
SWAYAM: NPTEL-NOC MOOCs Instructor: Prof. P P Das, IIT Kharagpur. Jan-Apr, 2018
L
E-R Diagram
E-R Model to Relational Schema
T E
N P
Database System Concepts 14.4 ©Silberschatz, Korth and Sudarshan
PPD
E-R Diagram
E-R Model to Relational
Schema
SWAYAM: NPTEL-NOC MOOCs Instructor: Prof. P P Das, IIT Kharagpur. Jan-Apr, 2018
E L
P T
N
E-R DIAGRAM
L
Entities can be represented graphically as follows:
• Rectangles represent entity sets.
E
• Attributes listed inside entity rectangle
• Underline indicates primary key attributes
P T
N
Database System Concepts 14.6 ©Silberschatz, Korth and Sudarshan
Relationship Sets
SWAYAM: NPTEL-NOC MOOCs Instructor: Prof. P P Das, IIT Kharagpur. Jan-Apr, 2018
L
Diamonds represent relationship sets.
T E
N P
Database System Concepts 14.7 ©Silberschatz, Korth and Sudarshan
Relationship Sets with Attributes
SWAYAM: NPTEL-NOC MOOCs Instructor: Prof. P P Das, IIT Kharagpur. Jan-Apr, 2018
E L
P T
N
Database System Concepts 14.8 ©Silberschatz, Korth and Sudarshan
Roles
SWAYAM: NPTEL-NOC MOOCs Instructor: Prof. P P Das, IIT Kharagpur. Jan-Apr, 2018
L
Entity sets of a relationship need not be distinct
Each occurrence of an entity set plays a “role” in the relationship
E
The labels “course_id” and “prereq_id” are called roles.
P T
N
Database System Concepts 14.9 ©Silberschatz, Korth and Sudarshan
Cardinality Constraints
SWAYAM: NPTEL-NOC MOOCs Instructor: Prof. P P Das, IIT Kharagpur. Jan-Apr, 2018
L
We express cardinality constraints by drawing either a directed line
(), signifying “one,” or an undirected line (—), signifying “many,”
E
between the relationship set and the entity set.
T
One-to-one relationship between an instructor and a student :
A student is associated with at most one instructor via the
relationship advisor
P
A student is associated with at most one department via
stud_dept
N
Database System Concepts 14.10 ©Silberschatz, Korth and Sudarshan
One-to-Many Relationship
SWAYAM: NPTEL-NOC MOOCs Instructor: Prof. P P Das, IIT Kharagpur. Jan-Apr, 2018
L
one-to-many relationship between an instructor and a student
an instructor is associated with several (including 0) students
E
via advisor
a student is associated with at most one instructor via advisor,
P T
N
Database System Concepts 14.11 ©Silberschatz, Korth and Sudarshan
Many-to-One Relationships
SWAYAM: NPTEL-NOC MOOCs Instructor: Prof. P P Das, IIT Kharagpur. Jan-Apr, 2018
L
many-to-one relationship between a student and a instructor,
an instructor is associated with at most one student via
E
advisor,
and a student is associated with several (including 0)
T
instructors via advisor
N P
Database System Concepts 14.12 ©Silberschatz, Korth and Sudarshan
Many-to-Many Relationship
SWAYAM: NPTEL-NOC MOOCs Instructor: Prof. P P Das, IIT Kharagpur. Jan-Apr, 2018
L
An instructor is associated with several (possibly 0) students via
advisor
E
A student is associated with several (possibly 0) instructors via
advisor
P T
N
Database System Concepts 14.13 ©Silberschatz, Korth and Sudarshan
Total and Partial Participation
Total participation (indicated by double line): every entity in the
SWAYAM: NPTEL-NOC MOOCs Instructor: Prof. P P Das, IIT Kharagpur. Jan-Apr, 2018
L
entity set participates in at least one relationship in the relationship
set
T E
P
participation of student in advisor relation is total
N
every student must have an associated instructor
Partial participation: some entities may not participate in any
relationship in the relationship set
Example: participation of instructor in advisor is partial
L
A line may have an associated minimum and maximum cardinality,
shown in the form l..h, where l is the minimum and h the maximum
cardinality
E
A minimum value of 1 indicates total participation.
T
A maximum value of 1 indicates that the entity participates in
at most one relationship
P
A maximum value of * indicates no limit.
N
Instructor can advise 0 or more students. A student must have
1 advisor; cannot have multiple advisors
E L
P T
N
Database System Concepts 14.16 ©Silberschatz, Korth and Sudarshan
Expressing Weak Entity Sets
SWAYAM: NPTEL-NOC MOOCs Instructor: Prof. P P Das, IIT Kharagpur. Jan-Apr, 2018
L
In E-R diagrams, a weak entity set is depicted via a double rectangle
We underline the discriminator of a weak entity set with a dashed
E
line
The relationship set connecting the weak entity set to the identifying
T
strong entity set is depicted by a double diamond
Primary key for section – (course_id, sec_id, semester, year)
N P
Database System Concepts 14.17 ©Silberschatz, Korth and Sudarshan
E-R Diagram for a University Enterprise
SWAYAM: NPTEL-NOC MOOCs Instructor: Prof. P P Das, IIT Kharagpur. Jan-Apr, 2018
E L
P T
N
Database System Concepts 14.18 ©Silberschatz, Korth and Sudarshan
PPD
E-R Diagram
E-R Model to Relational
Schema
SWAYAM: NPTEL-NOC MOOCs Instructor: Prof. P P Das, IIT Kharagpur. Jan-Apr, 2018
E L
P T
N
E-R MODEL TO RELATIONAL SCHEMA
L
Entity sets and relationship sets can be expressed uniformly as
relation schemas that represent the contents of the database
E
A database which conforms to an E-R diagram can be represented by
a collection of schemas
T
For each entity set and relationship set there is a unique schema that
is assigned the name of the corresponding entity set or relationship
set
P
Each schema has a number of columns (generally corresponding to
attributes), which have unique names
N
Database System Concepts 14.20 ©Silberschatz, Korth and Sudarshan
Representing Entity Sets
SWAYAM: NPTEL-NOC MOOCs Instructor: Prof. P P Das, IIT Kharagpur. Jan-Apr, 2018
L
A strong entity set reduces to a schema with the same attributes
T E
A weak entity set becomes a table that includes a column for the
primary key of the identifying strong entity set
P
section ( course_id, sec_id, sem, year )
N
Database System Concepts 14.21 ©Silberschatz, Korth and Sudarshan
Representing Relationship Sets
SWAYAM: NPTEL-NOC MOOCs Instructor: Prof. P P Das, IIT Kharagpur. Jan-Apr, 2018
L
A many-to-many relationship set is represented as a schema with
attributes for the primary keys of the two participating entity sets,
E
and any descriptive attributes of the relationship set.
Example: schema for relationship set advisor
T
advisor = (s_id, i_id)
N P
Database System Concepts 14.22 ©Silberschatz, Korth and Sudarshan
Representation of Entity Sets with Composite Attributes
SWAYAM: NPTEL-NOC MOOCs Instructor: Prof. P P Das, IIT Kharagpur. Jan-Apr, 2018
L
Composite attributes are flattened out by creating a
separate attribute for each component attribute
E
Example: given entity set instructor with
composite attribute name with component
attributes first_name and last_name the schema
T
corresponding to the entity set has two attributes
name_first_name and name_last_name
P
Prefix omitted if there is no ambiguity
(name_first_name could be first_name)
N
Ignoring multivalued attributes, extended instructor
schema is
instructor(ID,
first_name, middle_initial, last_name,
street_number, street_name,
apt_number, city, state, zip_code,
date_of_birth)
L
A multivalued attribute M of an entity E is represented by a
separate schema EM
E
Schema EM has attributes corresponding to the primary key of E
and an attribute corresponding to multivalued attribute M
T
Example: Multivalued attribute phone_number of instructor is
represented by a schema:
P
inst_phone= ( ID, phone_number)
Each value of the multivalued attribute maps to a separate tuple of
the relation on schema EM
N
For example, an instructor entity with primary key 22222 and
phone numbers 456-7890 and 123-4567 maps to two tuples:
(22222, 456-7890) and (22222, 123-4567)
L
Many-to-one and one-to-many relationship sets that are total on the
many-side can be represented by adding an extra attribute to the
“many” side, containing the primary key of the “one” side
E
Example: Instead of creating a schema for relationship set inst_dept,
add an attribute dept_name to the schema arising from entity set
T
instructor
N P
Database System Concepts 14.25 ©Silberschatz, Korth and Sudarshan
Redundancy of Schemas (Cont.)
SWAYAM: NPTEL-NOC MOOCs Instructor: Prof. P P Das, IIT Kharagpur. Jan-Apr, 2018
L
For one-to-one relationship sets, either side can be chosen
to act as the “many” side
E
That is, an extra attribute can be added to either of the
tables corresponding to the two entity sets
T
If participation is partial on the “many” side, replacing a
schema by an extra attribute in the schema corresponding
to the “many” side could result in null values
N P
Database System Concepts 14.26 ©Silberschatz, Korth and Sudarshan
Redundancy of Schemas (Cont.)
SWAYAM: NPTEL-NOC MOOCs Instructor: Prof. P P Das, IIT Kharagpur. Jan-Apr, 2018
L
The schema corresponding to a relationship set linking a weak
entity set to its identifying strong entity set is redundant.
E
Example: The section schema already contains the attributes that
T
would appear in the sec_course schema
N P
Database System Concepts 14.27 ©Silberschatz, Korth and Sudarshan
Module Summary
SWAYAM: NPTEL-NOC MOOCs Instructor: Prof. P P Das, IIT Kharagpur. Jan-Apr, 2018
L
Illustrated E-R Diagram notation for E-R Models
Discussed translation of E-R Models to Relational Schemas
T E
N P
Database System Concepts 14.28 ©Silberschatz, Korth and Sudarshan
PPD
L
Name Mail Mobile
Partha Pratim Das, Instructor [email protected] 9830030880
E
Srijoni Majumdar, TA [email protected] 9674474267
Himadri B G S Bhuyan, TA [email protected] 9438911655
T
Gurunath Reddy M [email protected] 9434137638
N P
Slides used in this presentation are borrowed from http://db-book.com/
with kind permission of the authors.
E
Module 15: Entity-Relationship Model/3
P T
Partha Pratim Das
Department of Computer Science and Engineering
Indian Institute of Technology, Kharagpur
Srijoni Majumdar
Himadri B G S Bhuyan
Gurunath Reddy M
Module Recap
SWAYAM: NPTEL-NOC MOOCs Instructor: Prof. P P Das, IIT Kharagpur. Jan-Apr, 2018
L
E-R Diagram
E-R Model to Relational Schema
T E
N P
Database System Concepts 15.2 ©Silberschatz, Korth and Sudarshan
PPD
Module Objectives
SWAYAM: NPTEL-NOC MOOCs Instructor: Prof. P P Das, IIT Kharagpur. Jan-Apr, 2018
L
To understand extended features of E-R Model
To discuss various design issues
T E
N P
Database System Concepts 15.3 ©Silberschatz, Korth and Sudarshan
PPD
Module Outline
SWAYAM: NPTEL-NOC MOOCs Instructor: Prof. P P Das, IIT Kharagpur. Jan-Apr, 2018
L
Extended E-R Features
Design Issues
T E
N P
Database System Concepts 15.4 ©Silberschatz, Korth and Sudarshan
PPD
E L
P T
N
EXTENDED E-R FEATURES
L
Most relationship sets are binary
There are occasions when it is more convenient to
E
represent relationships as non-binary.
E-R Diagram with a Ternary Relationship
P T
N
Database System Concepts 15.6 ©Silberschatz, Korth and Sudarshan
Cardinality Constraints on Ternary Relationship
SWAYAM: NPTEL-NOC MOOCs Instructor: Prof. P P Das, IIT Kharagpur. Jan-Apr, 2018
L
We allow at most one arrow out of a ternary (or greater degree)
relationship to indicate a cardinality constraint
E
For example, an arrow from proj_guide to instructor indicates
each student has at most one guide for a project
T
If there is more than one arrow, there are two ways of defining the
meaning.
P
For example, a ternary relationship R between A, B and C
with arrows to B and C could mean
Each A entity is associated with a unique entity from B
N
1.
and C or
2. Each pair of entities from (A, B) is associated with a
unique C entity, and each pair (A, C) is associated with a
unique B
Each alternative has been used in different formalisms
To avoid confusion we outlaw more than one arrow
L
Top-down design process; we designate sub-groupings within
an entity set that are distinctive from other entities in the set
E
These sub-groupings become lower-level entity sets that have
attributes or participate in relationships that do not apply to the
T
higher-level entity set
Depicted by a triangle component labeled ISA (e.g., instructor
P
“is a” person)
Attribute inheritance – a lower-level entity set inherits all the
attributes and relationship participation of the higher-level
N
entity set to which it is linked
L
Disjoint – instructor and secretary
E
Total and partial
P T
N
Database System Concepts 15.9 ©Silberschatz, Korth and Sudarshan
Representing Specialization via Schemas
SWAYAM: NPTEL-NOC MOOCs Instructor: Prof. P P Das, IIT Kharagpur. Jan-Apr, 2018
L
Method 1:
Form a schema for the higher-level entity
E
Form a schema for each lower-level entity set, include primary
key of higher-level entity set and local attributes
T
schema attributes
P
person ID, name, street, city
student ID, tot_cred
employee ID, salary
N
Drawback: Getting information about, an employee requires
accessing two relations, the one corresponding to the low-level
schema and the one corresponding to the high-level schema
L
Method 2:
Form a schema for each entity set with all local and inherited
E
attributes
T
schema attributes
person ID, name, street, city
student ID, name, street, city, tot_cred
P
employee ID, name, street, city, salary
N
for people who are both students and employees
L
A bottom-up design process – combine a number of entity
sets that share the same features into a higher-level entity set.
E
Specialization and generalization are simple inversions of each
other; they are represented in an E-R diagram in the same way
T
The terms specialization and generalization are used
interchangeably
N P
Database System Concepts 15.12 ©Silberschatz, Korth and Sudarshan
Design Constraints on a Specialization/Generalization
SWAYAM: NPTEL-NOC MOOCs Instructor: Prof. P P Das, IIT Kharagpur. Jan-Apr, 2018
L
Completeness constraint -- specifies whether or not an entity in
the higher-level entity set must belong to at least one of the lower-
E
level entity sets within a generalization
total: an entity must belong to one of the lower-level entity
T
sets
partial: an entity need not belong to one of the lower-level
P
entity sets
Partial generalization is the default. We can specify total generalization in
an ER diagram by adding the keyword total in the diagram and drawing a
N
dashed line from the keyword to the corresponding hollow arrow-head to
which it applies (for a total generalization), or to the set of hollow arrow-
heads to which it applies (for an overlapping generalization).
The student generalization is total: All student entities must be either
graduate or undergraduate. Because the higher-level entity set arrived at
through generalization is generally composed of only those entities in the
lower-level entity sets, the completeness constraint for a generalized
higher-level entity set is usually total
L
Consider the ternary relationship proj_guide, which we saw earlier
Suppose we want to record evaluations of a student by a guide
E
on a project
P T
N
Database System Concepts 15.14 ©Silberschatz, Korth and Sudarshan
Aggregation (Cont.)
SWAYAM: NPTEL-NOC MOOCs Instructor: Prof. P P Das, IIT Kharagpur. Jan-Apr, 2018
L
Relationship sets eval_for and proj_guide represent overlapping
information
E
Every eval_for relationship corresponds to a proj_guide
relationship
T
However, some proj_guide relationships may not correspond
to any eval_for relationships
P
So we cannot discard the proj_guide relationship
Eliminate this redundancy via aggregation
N
Treat relationship as an abstract entity
Allows relationships between relationships
Abstraction of relationship into new entity
L
Eliminate this redundancy via aggregation without introducing
redundancy, the following diagram represents:
E
A student is guided by a particular instructor on a particular
project
T
A student, instructor, project combination may have an
associated evaluation
N P
Database System Concepts 15.16 ©Silberschatz, Korth and Sudarshan
Representing Aggregation via Schemas
SWAYAM: NPTEL-NOC MOOCs Instructor: Prof. P P Das, IIT Kharagpur. Jan-Apr, 2018
L
To represent aggregation, create a schema containing
Primary key of the aggregated relationship,
E
The primary key of the associated entity set
T
Any descriptive attributes
In our example:
P
The schema eval_for is:
eval_for (s_ID, project_id, i_ID, evaluation_id)
N
The schema proj_guide is redundant
E L
P T
N
DESIGN ISSUES
L
Use of entity sets vs. attributes
T E
N P
Use of phone as an entity allows extra information about phone numbers
(plus multiple phone numbers)
L
Use of entity sets vs. relationship sets
Possible guideline is to designate a relationship set to
E
describe an action that occurs between entities
P T
N
Placement of relationship attributes
L
Although it is possible to replace any non-binary (n-ary, for n > 2)
relationship set by a number of distinct binary relationship sets, a
E
n-ary relationship set shows more clearly that several entities
participate in a single relationship
T
Some relationships that appear to be non-binary may be better
represented using binary relationships
P
For example, a ternary relationship parents, relating a child to
his/her father and mother, is best replaced by two binary
relationships, father and mother
N
Using two binary relationships allows partial information
(e.g., only mother being known)
But there are some relationships that are naturally non-binary
Example: proj_guide
L
In general, any non-binary relationship can be represented using binary
relationships by creating an artificial entity set.
Replace R between entity sets A, B and C by an entity set E, and
E
three relationship sets:
1. RA, relating E and A 2. RB, relating E and B
T
3. RC, relating E and C
Create an identifying attribute for E and add any attributes of R to E
P
For each relationship (ai , bi , ci) in R, create
1. a new entity ei in the entity set E 2. add (ei , ai ) to RA
N
3. add (ei , bi ) to RB 4. add (ei , ci ) to RC
L
Also need to translate constraints
Translating all constraints may not be possible
E
There may be instances in the translated schema that
cannot correspond to any instance of R
T
Exercise: add constraints to the relationships RA, RB and
RC to ensure that a newly created entity corresponds to
P
exactly one entity in each of entity sets A, B and C
We can avoid creating an identifying attribute by making E a
N
weak entity set (described shortly) identified by the three
relationship sets
L
The use of an attribute or entity set to represent an object
Whether a real-world concept is best expressed by an entity set or
E
a relationship set
The use of a ternary relationship versus a pair of binary
T
relationships
The use of a strong or weak entity set
P
The use of specialization/generalization – contributes to modularity
in the design
N
The use of aggregation – can treat the aggregate entity set as a
single unit without concern for the details of its internal structure
E L
P T
N
Database System Concepts 15.25 ©Silberschatz, Korth and Sudarshan
Symbols Used in E-R Notation (Cont.)
SWAYAM: NPTEL-NOC MOOCs Instructor: Prof. P P Das, IIT Kharagpur. Jan-Apr, 2018
E L
P T
N
Database System Concepts 15.26 ©Silberschatz, Korth and Sudarshan
Alternative ER Notations
SWAYAM: NPTEL-NOC MOOCs Instructor: Prof. P P Das, IIT Kharagpur. Jan-Apr, 2018
L
Chen, IDE1FX, …
T E
N P
Database System Concepts 15.27 ©Silberschatz, Korth and Sudarshan
Alternative ER Notations
SWAYAM: NPTEL-NOC MOOCs Instructor: Prof. P P Das, IIT Kharagpur. Jan-Apr, 2018
L
Chen IDE1FX (Crows feet notation)
T E
N P
Database System Concepts 15.28 ©Silberschatz, Korth and Sudarshan
Module Summary
SWAYAM: NPTEL-NOC MOOCs Instructor: Prof. P P Das, IIT Kharagpur. Jan-Apr, 2018
L
Discussed the extended features of E-R Model
Deliberated on various design issues
T E
N P
Database System Concepts 15.29 ©Silberschatz, Korth and Sudarshan
PPD
L
Name Mail Mobile
Partha Pratim Das, Instructor [email protected] 9830030880
E
Srijoni Majumdar, TA [email protected] 9674474267
Himadri B G S Bhuyan, TA [email protected] 9438911655
T
Gurunath Reddy M [email protected] 9434137638
N P
Slides used in this presentation are borrowed from http://db-book.com/
with kind permission of the authors.