Download as pdf or txt
Download as pdf or txt
You are on page 1of 42

Database Design

Database Management Systems


Agenda
 DB Design
 Data Model
 Types of Data Model
 Relational Model
 Relation
 Relational Algebra
 Functional Dependencies

29 March 2020 Saleem Raza 2


Database Design
 DB Design
 DB Design represents the logical structure of Database
 Process of creating a logical representation of structure of the Database
 Database must mirror the real world to properly answer the real-world
questions(Queries).
 Goal is the identify the facts to be stored in Database
 DB Modeling involves users and analysts.
 DB design is created by using some Data Model.
 DB Design has 3 phases
 Conceptual Design Prepared by using SDM
 Logical Design Prepared by using Relational Model
 Physical Design Implemented by using DBMS

29 March 2020 Saleem Raza 3


Data Model
 Data Model
 Collections of concepts that can be used to describe the structure of DB.
 Set of tools and constructs that is used to construct/develop DB Design.
 Date Model provides guidelines about what to do and what to avoid.
 Components of Data Model
 Structure
✓ Data Containers and the way to store the data
 Data Manipulation Language
✓ Constructs to Define (Create, Alter etc.) and Manipulate (Select, Insert, Update, Delete etc.) Data
 Constraints
✓ Rules that govern the completeness, correctness, validation and verification of data, Or in simple
words deal with the Legality of the data.

29 March 2020 Saleem Raza 4


Types of Data Model
 Semantic Data Model ~ SDM
 SDM is method of structuring data in order to represent data in
specific/logical way.
 SDM is conceptual Data Model that includes Semantic Information that
adds a basic meaning to the data and relationships that lie between them.
 ER Model, OO Model
 Record Base Data Models
 Models based upon user level of data and application
 Model the system considering logical structure of objects in database
 Hierarchal Data Model
 A data model that treats all entities as hierarchal.
 Network Data Model
 A Data Model that permits a Network of entities by allowing multiple parents.
 Relational Model
 A Data Model purely deals with records and ensures a clear isolation between logical and
physical structure.

29 March 2020 Saleem Raza 5


Relational Model
 Model of structuring Data using Relations
 Most Popular Data Model
 Strong Mathematical Foundation(Set Topology)
 Simplicity (Only One Structure is used to define everything)
 Clearly isolates Conceptual Schema and Physical Implementation of
Conceptual Schema
 Components
 Structure Relation
 DML Relational Algebra
 Constraints Domain and Integrity Constraints

29 March 2020 Saleem Raza 6


…Relational Model ~ Structure
 Structure
 Relation
 Relation is subset of valid tuples from cartesian product of attributes of entity.
 Relation is the only structure to model complete DB Design, Real World
Data and its Interactions
 Both entities and their relationships are modeled using relation
 Table is Physical Representation of Relation
 Column(s) are attributes
 Rows are Records/Tuples
 Relation/Table Properties
 Cell (Intersection of Column and Row) contains Atomic Value Only
 Each Column has a Distinct Name. Number of Columns in a Relation is called Degree of Relation
 Each Row is Distinct. Number of Rows in a Relation is called Order of Relation
 Values of a Column belongs to same set, called domain.
 Column as well as Row order is immaterial.

29 March 2020 Saleem Raza 7


…Relational Model ~ Structure
 Relation is presented in 2 ways.
 Set: Tabular Form
✓ STUDENTS={RegNo:Text(8), Name:Text(30), Cell:Text(11), DoB:Date, City:Text(30), CGPA:Float(3.2)}
✓ Relation Name is given in CAPITAL
✓ Primary Key Attribute is underlined
✓ Data Type and Data Size is written with Attribute Name after Colon ‘:’

 Pictorial Form

STUDENTS RegNo Name Cell DoB City CGPA

✓ Relation Name is given in CAPITAL


✓ Primary Key Attribute is underlined

29 March 2020 Saleem Raza 8


…Relational Model ~ Structure
 Relationship
 Association/Interaction of Relations with each other is called Relationship
 Type of Relationship
✓ Unary Relationship
▪ An interaction that involves ONLY one relation; Managers to Employees, CR to Students etc.
✓ Binary Relationship
▪ An interaction that involves exactly Two relations; Departments are Administered by HoDs, Students get admission
in Degrees, Courses are offered in Degree etc.
✓ Ternary Relationship
▪ An interaction that involves exactly Three relations; Students registered in courses in each Term,
Faculty teaches Courses in each Term, etc.
✓ N-ary Relationship
▪ An interaction that involves more than Three relations.
 Cardinality
 Degree of Relationship, Number of Rows/Tuple/Instances involve of participating relations.
✓ One ~to~ One
▪ When for one row in Right relations maps on exactly one row on Left relations; e.g. Country ~ President etc.
✓ One ~to~ Many
▪ When for one row in Right relations maps on more than one row on Left relations; e.g. Department~ Employees etc.
✓ Many ~to~ Many
▪ When for one row in Right relations maps on more than one row on Left relations; Students ~ Courses, etc.
29 March 2020 Saleem Raza 9
…Relational Model ~ Structure
 ….Relationship
 Occurrence
✓ Unary Relationship has only One ~to~ Many Cardinality
✓ Binary Relationship may have any of ALL the Three Cardinalities.
✓ Ternary and N~ary Relationships have only Many ~to~ Many Cardinality.
 Existential Dependency
✓ If to have an instance in Relation A, there must exist an instance in Relation B, this means A is
Existentially Dependent on B, its presented with an Arrow with Arrow Head towards Existentially
Independent Relation, i.e. A  B
 Implementation
✓ Unary Relationship
▪ PK is introduced as FK in the same relation with a different column name.

29 March 2020 Saleem Raza 10


…Relational Model ~ Structure
 ….Relationship
✓ Binary Relationship
▪ One ~to~ One Cardinality
• AB PK of B is introduced as FK in A, and FK is constrained on Referred PK

• A—B PK of either side relation can be introduced as FK in other relation with constrained on Referred PK

▪ One ~to~ Many Cardinality


• PK of Ones side relation is introduced as FK in Many side relation and FK is constrained on PK

▪ Many ~to~ Many Cardinality


• An intermediary relation will be formed comprised of PKs of participating relations and any other relationship attribute(s),
this intermediary will have the composite PK having all the PKs of participating relations.

29 March 2020 Saleem Raza 11


…Relational Model ~ Structure
 ….Relationship
✓ Ternary Relationship (N~ary Relationship)
▪ Many ~to~ Many Cardinality
• An intermediary relation will be formed comprised of PKs of participating relations and any other relationship attribute(s),
this intermediary will have the composite PK having all the PKs of participating relations.

29 March 2020 Saleem Raza 12


…Relational Model ~ Example
Conceptual Schema
A(a1, a2, a3, a4) B(b1, b2, b3, b4) C(c1, c2, c3) D(d1, d2, d3, d4) E(e1, e2, e3, e4, e5)
F(f1, f2, f3) G(g1, g2, g3, g4) H(h1, h2, h3)
Important: X―Y denotes a simple relationship where X←Y denotes Y is existentially dependent on X
r1 and r2 as well as k1, k2 and k3 are the attributes of relationship

Relations Cardinality FK New/Modified Relation Index Duplicates Null


A 1―M ta1 A(a1, a2, a3, a4, ta1) Yes Yes Yes

B←C 1―1 b1 C(c1, c2, c3, b1) Yes No No

C―D 1―M c1 D(d1, d2, d3, d4, c1) Yes Yes Yes

A←B 1―M a1 B(b1, b2, b3, b4, a1) Yes Yes No


E―G
M―M e1, g1 EG(e1, g1, r1, r2) Yes Yes No
r1, r2
E―F―H e1, f1,
M―M EFH(e1, f1, h1, k1, k2, k3) Yes Yes No
k1, k2, k3 h1
c1 E(e1, e2, e3, e4, e5, c1)
C―E 1―1 Yes No Yes
e1 C(c1, c2, c3, b1, e1)

29 March 2020 Saleem Raza 13


…Relational Model ~ …Example

A a1 a2 a3 a4 ta1
C c1 c2 c3 b1
B b1 b2 b3 b4 a1
D d1 d2 d3 d4 c1
G g1 g2 g3 g4

E e1 e2 e3 e4 e5 c1 EG g1 e1 r1 r2

F f1 f2 f3 EFH e1 f1 h1 k1 k2 k3 H h1 h2 h3

29 March 2020 Saleem Raza 14


…Relational Model ~ …Real World Example

29 March 2020 Saleem Raza 15


…Relational Model ~ Logical Design

29 March 2020 Saleem Raza 16


…Relational Model ~ Constraints
 Constraints
 Domain Constraints
✓ Values of a column can only belong to same set; which is called domain of the column.
 Integrity Constraints
✓ Entity Integrity Constraints
▪ NO Part of PK can be NULL
✓ Referential Integrity Constraints
▪ FK value can either be NULL or must have value that belongs to its parent domain; the referral column in the
parent relation, i.e. FK is constrained on referred PK.

29 March 2020 Saleem Raza 17


…Relational Model ~ DML
 Relational Query Language
 Relational Databases are described through SQL and Relational Algebra
 Structured Query Language ~ SQL
 SQL is Declarative and Predominant Application Query Language
 Relational Algebra
 Intermediate Query Language used with DBMS
 Procedural: Relational Expressions specifies query by describing an algorithm to get result.
 Language based upon operators and domain of values; operator maps different domains.
Hence, an expression involving operators and domains produces a value.
 Operations
 Select 𝜎 Role of Relational Algebra
 Project 𝜋
 Intersection ∩
 Union ∪ Query
RA Executable
SQL Execution
 Difference − Expression Code
Plan
 Cartesian Product ×
 Join ⋈
 Division ÷
Parser Query Optimizer Code Generator
29 March 2020 Saleem Raza 18
…Relational Model ~ DML ~ Select
 Relational Algebra ~ SQL
 Select Statement 𝜎
 Retrieves the rows/tuples from the input relation based upon criteria
 Output is also a relation
✓ Degree of Output relation is exactly same as that of input relation
✓ Order of Output relation is less than or equal to the order of input relation
▪ NO Row will be retrieved if it doesn't fulfill the criteria i.e. Order of Output Relation may be Zero;
▪ ALL Rows will be retrieved if NO criteria is given i.e. Order of Output Relation will be same as that of the
input relation
 Syntax
✓ 𝑂𝑢𝑡𝑝𝑢𝑡𝑅𝑒𝑙𝑎𝑡𝑖𝑜𝑛 = 𝜎𝑐𝑟𝑖𝑡𝑒𝑟𝑖𝑎 𝐼𝑛𝑝𝑢𝑡𝑅𝑒𝑙𝑎𝑡𝑖𝑜𝑛
✓ Input relation must have the criteria attribute/column
✓ Criteria can have both conjunctions and disjunctions but conjunction on equality can’t be applied on
same column whereas conjunction on inequality can be applied on same column.
✓ NO Row will be retrieved if it doesn't fulfill the criteria
 Select is commutative
✓ 𝜎𝐶1 ∧ 𝐶2 𝜎𝐶3 ∧ 𝑐4 𝑅 = 𝜎𝐶3 ∧ 𝐶4 𝜎𝐶1 ∧ 𝑐2 𝑅

29 March 2020 Saleem Raza 19


…Relational Model ~ DML
Relational Schema
Degree(DCode, Title, DCH, Sem, Seats)
Students(RegNo, Name, Gender, DoB, City, CGPA, CR, DCode)
Courses(CCode, CTitle, CH, Sem, DCode)
Faculty(FCode, Name, Designation, eMail, AoS, HoD, DPCode) //AoS is Area of Specialization
Term(TCode, TTitle, MidWeek, FinalWeek)
Departments(DPCode, DTitle, Director, Location, ContactInfo)
CRS(RegNo, CCode, TCode, OM, Grade)
Teach(FCode, CCode, TCode, Section, NoS, FeedBack) //NoS is Number of Students

29 March 2020 Saleem Raza 20


…Relational Model ~ DML ~ Select Example
 List Students who lives in Lahore
 𝑅1 = 𝜎𝐶𝑖𝑡𝑦=′𝐿𝑎ℎ𝑜𝑟𝑒′ 𝑆𝑡𝑢𝑑𝑒𝑛𝑡𝑠
SELECT *
FROM Students
Where City = ‘Lahore’;
 List Students who has CGPA greater than 3.30
 𝑅2 = 𝜎𝐶𝐺𝑃𝐴>3.30 𝑆𝑡𝑢𝑑𝑒𝑛𝑡𝑠
SELECT *
FROM Students
Where CGPA > ‘Lahore’;
 List Students who lives in Lahore and have CGPA great than 3.30
𝑅3 = 𝜎𝐶𝐺𝑃𝐴>3.30 𝑅1 or 𝑅3 = 𝜎𝐶𝑖𝑡𝑦=′ 𝐿𝑎ℎ𝑜𝑟𝑒′ 𝑅2 or 𝑅3 = 𝜎𝐶𝑖𝑡𝑦=′ 𝐿𝑎ℎ𝑜𝑟𝑒 ′ ∧ 𝐶𝐺𝑃𝐴>3.30 𝑆𝑡𝑢𝑑𝑒𝑛𝑡𝑠
SELECT *
FROM Students
Where City = ‘Lahore’ and CGPA>3.30;

29 March 2020 Saleem Raza 21


…Relational Model ~ DML ~ Project
 Project Statement 𝜋
 Retrieves the Columns from the input relation based upon list of columns
 Output is also a relation
✓ Degree of Output relation is less than or equal to that of input relation
▪ Degree of output is equal to the number of column provided in the Comma Separated List (CSL)
✓ Order of Output relation is exactly equal to the order of input relation
 Syntax
✓ 𝑂𝑢𝑡𝑝𝑢𝑡𝑅𝑒𝑙𝑎𝑡𝑖𝑜𝑛 = 𝜋𝑐𝑜𝑙1, 𝑐𝑜𝑙2,𝑐𝑜𝑙3, 𝑐𝑜𝑙4 𝐼𝑛𝑝𝑢𝑡𝑅𝑒𝑙𝑎𝑡𝑖𝑜𝑛
✓ Column List can have columns which are present in the input relation
✓ Project will crash if the CSL contains any column which is not in the input relation.
 Project is NOT commutative, in general
✓ 𝜋𝑐1,𝑐2,𝑐3 𝜋𝑐1,𝑐2,𝑐3,𝑐4,𝑐5 𝑅 ≠ 𝜋𝑐1,𝑐2,𝑐3,𝑐4,𝑐5 𝜋𝑐1,𝑐2,𝑐3 𝑅
 In SQL, Project is implemented through CSL in Select Statement.

29 March 2020 Saleem Raza 22


…Relational Model ~ DML ~ Project Example
 List Names and City of Students who lives in Lahore and have CGPA
greater than and equal to 3.30
 𝑅 = 𝜋𝑁𝑎𝑚𝑒,𝐶𝑖𝑡𝑦 𝜎𝐶𝑖𝑡𝑦=′ 𝐿𝑎ℎ𝑜𝑟𝑒 ′ ∧ 𝐶𝐺𝑃𝐴≥3.0 𝑆𝑡𝑢𝑑𝑒𝑛𝑡𝑠
SELECT Name, City
FROM Students
WHERE City = ‘Lahore’;
 List Course Codes and Titles of BBIT sorted on the basis of Semester
 𝑅 = 𝜋𝐶𝐶𝑜𝑑𝑒,𝐶𝑇𝑖𝑡𝑙𝑒 𝜎𝐷𝐶𝑜𝑑𝑒=′𝐵𝐵𝐼𝑇′ 𝐶𝑜𝑢𝑟𝑠𝑒𝑠
SELECT Ccode, CTitle
FROM Courses
WHERE Dcode=‘BBIT’
ORDER BY Sem;

29 March 2020 Saleem Raza 23


…Relational Model ~ DML
 Set Operations
 Two Relations can be operated with set operations if they are union compatible
 Union Compatibility
✓ Degree of both relations must be same
✓ Domain of corresponding columns must be same
 Only Intersection, Union and Difference require Union Compatibility whereas Cartesian Product
does not require Union Compatibility.
 Intersection
✓ 𝑅 = 𝑅1 ∩ 𝑅2
Union
✓ 𝑅 = 𝑅1 ∪ 𝑅2
Difference
✓ 𝑅 = 𝑅1 − 𝑅2
Cartesian Product
✓ 𝑅 = 𝑅1 × 𝑅2
✓ 𝑅(𝑎1, 𝑎2, 𝑎3, … . , 𝑎𝑛, 𝑏1, 𝑏2, … . , 𝑏𝑛) = 𝑅1(𝑎1, 𝑎2, 𝑎3, … . , 𝑎𝑛) × 𝑅2(𝑏1, 𝑏2, … . , 𝑏𝑛)
29 March 2020 Saleem Raza 24
…Relational Model ~ DML ~ Intersection
 List ‘RegNo’ of Students who are enrolled in ‘IT-261’ and ‘Fin-240’
 𝑹 = 𝝅𝑹𝒆𝒈𝑵𝒐 𝝈𝑪𝑪𝒐𝒅𝒆=′ 𝑰𝑻−𝟐𝟔𝟏′ 𝑪𝑹𝑺 ∩ 𝝈𝑪𝑪𝒐𝒅𝒆=′ 𝑭𝒊𝒏−𝟐𝟒𝟎′ 𝑪𝑹𝑺
SELECT RegNo
FROM CRS
WHERE CCode=‘IT-261’ AND RegNO IN
SELECT RegNo
FROM CRS
WHERE CCode=‘Fin-240’ ;
 List ‘FCode’ who have taught ‘IT-261’ in term ‘S19’ and have taught ‘IT-366’ in term ‘F18’
 𝑹 = 𝝅𝑭𝑪𝒐𝒅𝒆 𝝈𝑪𝑪𝒐𝒅𝒆=′ 𝑰𝑻−𝟐𝟔𝟏′ ∧ 𝑻𝒆𝒓𝒎𝑰𝑫=′ 𝑺𝟏𝟗′ 𝑻𝒆𝒂𝒄𝒉 ∩ 𝝈𝑪𝑪𝒐𝒅𝒆=′ 𝑰𝑻−𝟑𝟔𝟔′ ∧ 𝑻𝒆𝒓𝒎𝑰𝑫=′ 𝑭𝟏𝟖′ 𝑻𝒆𝒂𝒄𝒉
SELECT FCode
FROM Teach
WHERE CCode=‘IT-261’ AND TermID=‘S19’ AND FCode IN
SELECT FCode
FROM Teach
WHERE CCode=‘IT-366’ AND TermID=‘F18’ ;

29 March 2020 Saleem Raza 25


…Relational Model ~ DML ~ Difference
 List ‘RegNo’ of Students who are enrolled in ‘IT-261’ but Not in ‘Fin-240’
 𝑹 = 𝝅𝑹𝒆𝒈𝑵𝒐 𝝈𝑪𝑪𝒐𝒅𝒆=′ 𝑰𝑻−𝟐𝟔𝟏′ 𝑪𝑹𝑺 − 𝝈𝑪𝑪𝒐𝒅𝒆=′ 𝑭𝒊𝒏−𝟐𝟒𝟎′ 𝑪𝑹𝑺
SELECT RegNo
FROM CRS
WHERE CCode=‘IT-261’ AND RegNO NOT IN
SELECT RegNo
FROM CRS
WHERE CCode=‘Fin-240’ ;
 List ‘FCode’ who have taught ‘IT-261’ in term ‘S19’ and have not taught ‘IT-366’ in term ‘F18’
 𝑹 = 𝝅𝑭𝑪𝒐𝒅𝒆 𝝈𝑪𝑪𝒐𝒅𝒆=′ 𝑰𝑻−𝟐𝟔𝟏′ ∧ 𝑻𝒆𝒓𝒎𝑰𝑫=′ 𝑺𝟏𝟗′ 𝑻𝒆𝒂𝒄𝒉 − 𝝈𝑪𝑪𝒐𝒅𝒆=′ 𝑰𝑻−𝟑𝟔𝟔′ ∧ 𝑻𝒆𝒓𝒎𝑰𝑫=′ 𝑭𝟏𝟖′ 𝑻𝒆𝒂𝒄𝒉
SELECT FCode
FROM Teach
WHERE CCode=‘IT-261’ AND TermID=‘S19’ AND FCode NOT IN
SELECT FCode
FROM Teach
WHERE CCode=‘IT-366’ AND TermID=‘F18’ ;

29 March 2020 Saleem Raza 26


…Relational Model ~ DML ~ Difference
 List ‘RegNo’ of Students who are NOT enrolled in any Course in term ‘S19’
 𝑹 = 𝝅𝑹𝒆𝒈𝑵𝒐 𝑺𝒕𝒖𝒅𝒆𝒏𝒕𝒔 − 𝝅𝑹𝒆𝒈𝑵𝒐 𝝈𝑻𝒆𝒓𝒎𝑰𝑫=′𝑺𝟏𝟗′ 𝑪𝑹𝑺
SELECT RegNo
FROM Studetns
WHERE RegNO NOT IN
SELECT RegNo
FROM CRS
WHERE TermID=‘S19’ ;
 List ‘CCode’ of Courses which are NOT enrolled by any Student in term ‘S19’
 𝑹 = 𝝅𝑪𝑪𝒐𝒅𝒆 𝑪𝒐𝒖𝒓𝒔𝒆𝒔 − 𝝅𝑪𝒐𝒖𝒓𝒔𝒆𝒔 𝝈𝑻𝒆𝒓𝒎𝑰𝑫=′𝑺𝟏𝟗′ 𝑪𝑹𝑺
SELECT CCode
FROM Courses
WHERE CCode NOT IN
SELECT CCode
FROM CRS
WHERE TermID=‘S19’ ;

29 March 2020 Saleem Raza 27


…Relational Model ~ DML ~ Union
 List ‘RegNo’ of Students who are enrolled in ‘IT-261’ OR ‘Fin-240’
 𝑹 = 𝝅𝑹𝒆𝒈𝑵𝒐 𝝈𝑪𝑪𝒐𝒅𝒆=′𝑰𝑻−𝟐𝟔𝟏′ 𝑪𝑹𝑺 ∪ 𝝈𝑪𝑪𝒐𝒅𝒆=′𝑭𝒊𝒏−𝟐𝟒𝟎′ 𝑪𝑹𝑺
 𝑹 = 𝝅𝑹𝒆𝒈𝑵𝒐 𝝈𝑪𝑪𝒐𝒅𝒆=′𝑰𝑻−𝟐𝟔𝟏′ ∨𝑪𝑪𝒐𝒅𝒆=′ 𝑭𝒊𝒏−𝟐𝟒𝟎′ 𝑪𝑹𝑺
SELECT RegNo
FROM CRS
WHERE CCode=‘IT-261’ OR CCode=‘Fin-240’ ;

 List ‘FCode’ who have taught ‘IT-261’ in term ‘S19’ OR have taught ‘IT-366’ in term ‘F18’
 𝑹 = 𝝅𝑭𝑪𝒐𝒅𝒆 𝝈𝑪𝑪𝒐𝒅𝒆=′𝑰𝑻−𝟐𝟔𝟏′ ∧ 𝑻𝒆𝒓𝒎𝑰𝑫=′ 𝑺𝟏𝟗′ 𝑻𝒆𝒂𝒄𝒉 ∪ 𝝈𝑪𝑪𝒐𝒅𝒆=′𝑰𝑻−𝟑𝟔𝟔′ ∧ 𝑻𝒆𝒓𝒎𝑰𝑫=′ 𝑭𝟏𝟖′ 𝑻𝒆𝒂𝒄𝒉

 𝑹 = 𝝅𝑭𝑪𝒐𝒅𝒆 𝝈 𝑪𝑪𝒐𝒅𝒆=′ 𝑰𝑻−𝟐𝟔𝟏′ ∧ 𝑻𝒆𝒓𝒎𝑰𝑫=′ 𝑺𝟏𝟗′ ∨ 𝑪𝑪𝒐𝒅𝒆=′ 𝑰𝑻−𝟑𝟔𝟔′ ∧ 𝑻𝒆𝒓𝒎𝑰𝑫=′ 𝑭𝟏𝟖′ 𝑻𝒆𝒂𝒄𝒉
SELECT FCode
FROM Teach
WHERE (CCode=‘IT-261’ AND TermID=‘S19’ ) OR (CCode=‘IT-366’ AND TermID=‘F18’ );

29 March 2020 Saleem Raza 28


…Relational Model ~ DML ~ Cartesian Product
 Cartesian Product ×
 In Cartesian Product of Two relations; each row/tuple of first relation is concatenated with the
each row/tuple of second relation.
✓ 𝑅 = 𝑅1 × 𝑅2
✓ 𝑅(𝑎1, 𝑎2, 𝑎3, … . , 𝑎𝑛, 𝑏1, 𝑏2, … . , 𝑏𝑛) = 𝑅1(𝑎1, 𝑎2, 𝑎3, … . , 𝑎𝑛) × 𝑅2(𝑏1, 𝑏2, … . , 𝑏𝑛)
 NO Common Column is required
 Columns with same name are proceeded by access specifier, the relation name i.e. 𝑅 ⋅ 𝑎
 Degree of resultant relation ‘R’ is sum of degrees of input relations i.e.
deg 𝑅 = deg 𝑅1 + deg(𝑅2)
 Order of resultant relation ‘R’ is product of orders of input relations i.e.
𝑜𝑟𝑑𝑒𝑟 𝑅 = order 𝑅1 + order(𝑅2)
 Resultant ‘R’ has many invalid tuples.
 Valid tuples can be selected, only if input relations have a common column, through
𝑅 = 𝜎𝑅1.𝐶=𝑅2.𝐶 𝑅1 × 𝑅2
which is definition of Join Operation

29 March 2020 Saleem Raza 29


…Relational Model ~ DML ~ Join
 Join Operation ⋈
 Join operation is extension of Cartesian Product.
 Join operations is selection of valid tuples from cartesian products of input relations based
upon some criteria.
 Join operation can be applied only if input relations have common key column.
𝑅 = 𝑅1 ⋈𝑐𝑟𝑖𝑡𝑒𝑟𝑖𝑎 𝑅2
𝑅(𝑎1, 𝑎2, 𝑎3, … . , 𝑎𝑛, 𝑏1, 𝑏2, … . , 𝑏𝑛) = 𝑅1(𝑎1, 𝑎2, 𝑎3, … . , 𝑎𝑛) ⋈𝒄𝒓𝒊𝒕𝒆𝒓𝒊𝒂 𝑅2(𝑏1, 𝑏2, … . , 𝑏𝑛)
✓ Criteria is conjunction/disjunction of attributes of ‘R1’ and ‘R2’ i.e. 𝒂𝒊 𝒐𝒑𝒆𝒓𝒂𝒕𝒐𝒓 𝒃𝒋
Operator may be any from =, >, <, ≥, ≤, ≠
 If join is made through non-key field, it will produce Spurious Tuples; tuples which does not exit
in any of input relation primarily.
 Columns with same name are proceeded by access specifier, the relation name i.e. 𝑅 ⋅ 𝑎
 Degree of resultant relation ‘R’ is sum of degrees of input relations i.e.
𝐝𝐞𝐠 𝑹 = 𝐝𝐞𝐠 𝑹𝟏 + 𝐝𝐞𝐠(𝑹𝟐)
 Order of resultant relation ‘R’ is number of tuples fulfilling the criteria; less than or equal to the
order of largely populated relation. i.e. 𝒐𝒓𝒅𝒆𝒓 𝑹 ≤ 𝐦𝐚𝐱(𝐨𝐫𝐝𝐞𝐫 𝑹𝟏 , 𝐨𝐫𝐝𝐞𝐫(𝑹𝟐))
 Join is one of the Most Commonly Used (MCU) operation in Relational Algebra

29 March 2020 Saleem Raza 30


…Relational Model ~ DML ~ Join Types
 Join Types
 Inner Join (Equi Join)
✓ In this kind of a Join, ALL records that match the condition in both the Relations, and records in both
the Relations that do not match are not reported.
✓ In other words, INNER JOIN is based on the single fact that ONLY the matching entries in BOTH the
tables SHOULD be listed.
✓ Join criteria is conjunctions/disjunctions of equalities.
𝑅 = 𝑅1 ⋈𝑹𝟏.𝒄=𝑹𝟐.𝒄 𝑅2
𝑅(𝑎1, 𝑎2, … , 𝑎𝑛, 𝑏1, 𝑏2, … , 𝑏𝑛) = 𝑅1(𝑎1, 𝑎2, … , 𝑎𝑛) ⋈𝑹𝟏.𝒂𝟏=𝑹𝟐.𝒃𝟏 𝑅2(𝑏1, 𝑏2, … , 𝑏𝑛)
✓ MCU as it joins the related data in different relations.

29 March 2020 Saleem Raza 31


…Relational Model ~ DML ~ Join Types
 Inner Join (Equi Join)
OrdID OrdDate Amount CustID
CustID FName LName City State Zip
1 07/04/1776 $234.56 1
1 George Washington Mount Vernon VA 22121
2 03/14/1760 $78.50 3
2 John Adams Quincy MA 02169
3 05/23/1784 $124.00 2
3 Thomas Jefferson Charlottesville VA 22902
4 09/03/1790 $65.50 3
4 James Madison Orange VA 22960
5 07/21/1795 $25.50 10
5 James Monroe Charlottesville VA 22902
6 11/27/1787 $14.40 9

𝝈 𝑪𝒖𝒔𝒕𝒐𝒎𝒆𝒓𝒔 ⋈𝑪𝒖𝒔𝒕𝑰𝑫 𝑶𝒓𝒅𝒆𝒓𝒔


SELECT *
FROM Customers C JOIN Orders O ON C.CustID=O.CustID;

C.CustID FName LName City State Zip OrdID OrdDate Amount O.CustID

1 George Washington Mount Vernon VA 22121 1 07/04/1776 $234.56 1

2 John Adams Quincy MA 02169 3 05/23/1784 $124.00 2

3 Thomas Jefferson Charlottesville VA 22902 2 03/14/1760 $78.50 3

3 Thomas Jefferson Charlottesville VA 22902 4 09/03/1790 $65.50 3

29 March 2020 Saleem Raza 32


…Relational Model ~ DML ~ Join Types
 Join Types
 Outer Join
✓ OUTER JOIN retrieves
Either, the matched rows from one table and all rows in the other table Or, all rows in all tables.
(it doesn't matter whether or not there is a match).
✓ There are three kinds of Outer Join:
✓ Left Outer Join ⋉
▪ This join returns all the rows from the left table in conjunction with the matching rows from the right table. If
there are no columns matching in the right table, it returns NULL values.
𝝈 𝑪𝒖𝒔𝒕𝒐𝒎𝒆𝒓𝒔 ⋉𝑪𝒖𝒔𝒕𝑰𝑫 𝑶𝒓𝒅𝒆𝒓𝒔
SELECT *
FROM Customers C LEFT JOIN Orders O ON C.CustID=O.CustID;
CustID FName LName City State Zip OrdID OrdDate Amount O.CustID
1 George Washington Mount Vernon VA 22121 1 07/04/1776 $234.56 1
2 John Adams Quincy MA 02169 3 05/23/1784 $124.00 2
3 Thomas Jefferson Charlottesville VA 22902 2 03/14/1760 $78.50 3
3 Thomas Jefferson Charlottesville VA 22902 4 09/03/1790 $65.50 3
4 James Madison Orange VA 22960

5 James Monroe Charlottesville VA 22902

29 March 2020 Saleem Raza 33


…Relational Model ~ DML ~ Join Types
 Outer Join
✓ Right Outer Join ⋊
▪ This join returns all the rows from the left table in conjunction with the matching rows from the right table. If
there are no columns matching in the right table, it returns NULL values.
𝝈 𝑪𝒖𝒔𝒕𝒐𝒎𝒆𝒓𝒔 ⋊𝑪𝒖𝒔𝒕𝑰𝑫 𝑶𝒓𝒅𝒆𝒓𝒔
SELECT *
FROM Customers C Right JOIN Orders O ON C.CustID=O.CustID;

C.CustID FName LName City State Zip OrdID OrdDate Amount O.CustID
1 George Washington Mount Vernon VA 22121 1 07/04/1776 $234.56 1
2 John Adams Quincy MA 02169 3 05/23/1784 $124.00 2
3 Thomas Jefferson Charlottesville VA 22902 2 03/14/1760 $78.50 3
3 Thomas Jefferson Charlottesville VA 22902 4 09/03/1790 $65.50 3
5 07/21/1795 $25.50 10

6 11/27/1787 $14.40 9

29 March 2020 Saleem Raza 34


…Relational Model ~ DML ~ Join Types
 Outer Join
✓ Full Outer Join ⟗
▪ This JOIN combines LEFT OUTER JOIN and RIGHT OUTER JOIN. It returns row from either table when the
conditions are met and returns NULL value when there is no match.
𝝈 𝑪𝒖𝒔𝒕𝒐𝒎𝒆𝒓𝒔 ⟗𝑪𝒖𝒔𝒕𝑰𝑫 𝑶𝒓𝒅𝒆𝒓𝒔
SELECT *
FROM Customers C FULL JOIN Orders O ON C.CustID=O.CustID;

C.CustID FName LName City State Zip OrdID OrdDate Amount O.CustID
1 George Washington Mount Vernon VA 22121 1 07/04/1776 $234.56 1
2 John Adams Quincy MA 02169 3 05/23/1784 $124.00 2
3 Thomas Jefferson Charlottesville VA 22902 2 03/14/1760 $78.50 3
3 Thomas Jefferson Charlottesville VA 22902 4 09/03/1790 $65.50 3
5 07/21/1795 $25.50 10

6 11/27/1787 $14.40 9
4 James Madison Orange VA 22960
5 James Monroe Charlottesville VA 22902

29 March 2020 Saleem Raza 35


…Relational Model ~ DML ~ Join Types
 Join Types
 Self Join
✓ It is not a different form of JOIN, rather it is a JOIN (INNER, OUTER, etc.) of a Relation to itself.
𝑅 = 𝑅1 ⋈𝑪 𝑅1
𝑅(𝑎1, 𝑎2, … , 𝑎𝑛, 𝑡𝑎1, 𝑡𝑎2, … , 𝑡𝑎𝑛) = 𝑅1(𝑎1, 𝑎2, … , 𝑎𝑛) ⋈𝑹𝟏.𝒂𝟏=𝑹𝟏.𝒂𝟐 𝑅1(𝑎1, 𝑎2, … , 𝑎𝑛)
✓ Domain of columns/attributes used in criteria must be same.
✓ NO Special Word for Self Join
✓ It’s basically used when there is any relationship between rows stored in the same Relation. (Unary
Relationship)
 List Student Names with their CR Names
SELECT S1.sName AS ‘Student Name’, S2.sName AS ‘CR’
FROM Students S1 Join Students S2 ON S1.RegNo=S2.CR
Order By S2.CR Desc
 Natural Join
✓ It is based on the two conditions:
▪ A~ the JOIN is made on all the columns with the same name for equality.
▪ B~ Removes duplicate columns from the result.
✓ This seems to be more of theoretical in nature and as a result (probably) most DBMS don't even
bother supporting this.

29 March 2020 Saleem Raza 36


…Relational Model ~ DML ~ Join Types
 Join Types based on Operators
 Theta Join ⋈𝜃
✓ In this kind of a Join, ALL records that match the condition ‘Ѳ’ in both the Relations, and records in
both the Relations that do not match are not reported.
✓ Join criteria is conjunctions/disjunctions of Inequalities.
𝑅 = 𝑅1 ⋈𝜽 𝑅2
𝑅(𝑎1, 𝑎2, … , 𝑎𝑛, 𝑏1, 𝑏2, … , 𝑏𝑛) = 𝑅1(𝑎1, 𝑎2, … , 𝑎𝑛) ⋈𝑹𝟏.𝒂𝟏 𝒐𝒑𝒓 𝑹𝟐.𝒃𝟏 𝑅2(𝑏1, 𝑏2, … , 𝑏𝑛)
✓ Criteria is conjunction/disjunction of attributes of ‘R1’ and ‘R2’ i.e. 𝒂𝒊 𝒐𝒑𝒆𝒓𝒂𝒕𝒐𝒓 𝒃𝒋
Operator may be any from >, <, ≤, ≥, ≠

𝑹 = 𝝅𝑭𝑪𝒐𝒅𝒆,𝑭𝑵𝒂𝒎𝒆 𝑭𝒂𝒄𝒖𝒍𝒕𝒚 ⋈𝑭𝒂𝒄𝒖𝒍𝒕𝒚.𝑫𝑷𝑪𝒐𝒅𝒆>𝑫𝒆𝒑𝒂𝒓𝒕𝒎𝒆𝒏𝒕𝒔.𝑫𝑷𝑪𝒐𝒅𝒆 𝑫𝒆𝒑𝒂𝒓𝒕𝒎𝒆𝒏𝒕𝒔

SELECT FCode, Fname AS Names


FROM Faculty F Join Departments D ON F.DPCode > D.DPCode

29 March 2020 Saleem Raza 37


…Relational Model ~ DML ~ Join Types
 Join Types based on Operators
 Equi Join ⋈𝑐
✓ In this kind of a Join, ALL records that match the condition ‘c’ in both the Relations, and records in
both the Relations that do not match are not reported.
✓ Join criteria is conjunctions/disjunctions of equalities ONLY.
𝑅 = 𝑅1 ⋈𝜽 𝑅2
𝑅(𝑎1, 𝑎2, … , 𝑎𝑛, 𝑏1, 𝑏2, … , 𝑏𝑛) = 𝑅1(𝑎1, 𝑎2, … , 𝑎𝑛) ⋈𝑹𝟏.𝒂𝟏 𝒐𝒑𝒓 𝑹𝟐.𝒃𝟏 𝑅2(𝑏1, 𝑏2, … , 𝑏𝑛)
✓ Criteria is conjunction/disjunction of attributes of ‘R1’ and ‘R2’ i.e. 𝒂𝒊 𝒐𝒑𝒆𝒓𝒂𝒕𝒐𝒓 𝒃𝒋
Operator may be any from >, <, ≤, ≥, ≠

29 March 2020 Saleem Raza 38


…Relational Model ~ DML ~ Examples
 List Students Names and Course Titles of which are enrolled in ‘IT-261’
 𝑹 = 𝝅𝒔𝑵𝒂𝒎𝒆,𝑪𝑻𝒊𝒕𝒍𝒆 𝝈𝑪𝑪𝒐𝒅𝒆=′ 𝑰𝑻−𝟐𝟔𝟏′ 𝑪𝑹𝑺 ⋈𝑹𝒆𝒈𝑵𝒐 𝑺𝒕𝒖𝒅𝒆𝒏𝒕𝒔 ⋈𝑪𝑪𝒐𝒅𝒆 𝑪𝒐𝒖𝒓𝒔𝒆𝒔
SELECT sName AS ‘Student Name’ , CTitle AS ‘Course Titles’
FROM (SELCT *
FROM CRS C Join Students S ON C.RegNo=S.RegNo
WHERE Ccode= ‘IT-261’) Join Courses R ON C.Code=R.Ccode) ;
Another (Better) Approach 
SELECT sName AS ‘Student Name’ , CTitle AS ‘Course Titles’
FROM SELCT *
FROM (CRS C Join Students S ON C.RegNo=S.RegNo) Join Courses R ON C.Code=R.Ccode
WHERE Ccode= ‘IT-261’;

29 March 2020 Saleem Raza 39


…Relational Model ~ DML ~ Examples
 List Faculty Name, Designation, Course Title and Term Title for faculty who have taught ‘IT-261’
in term ‘S19’ and have taught ‘IT-366’ in term ‘F18’
 𝝅𝑭𝑵𝒂𝒎𝒆,𝑫𝒆𝒔𝒊𝒈𝒏𝒂𝒕𝒊𝒐𝒏, 𝑪𝑻𝒊𝒕𝒍𝒆,𝑻𝑻𝒊𝒍𝒆 ቆ൬ቀ൫𝝈𝑪𝑪𝒐𝒅𝒆=′𝑰𝑻−𝟐𝟔𝟏′ ∧𝑻𝒆𝒓𝒎𝑰𝑫=′𝑺𝟏𝟗′ 𝑻𝒆𝒂𝒄𝒉 ∩

𝝅𝑪𝑪𝒐𝒅𝒆=′𝑰𝑻−𝟑𝟔𝟔′ ∧ 𝑻𝒆𝒓𝒎𝑰𝑫=′𝑭𝟏𝟖′ 𝑻𝒆𝒂𝒄𝒉 ൯ቁ ⋈𝑭𝑪𝒐𝒅𝒆 𝑭𝒂𝒄𝒖𝒍𝒕𝒚൰ ⋈𝑪𝑪𝒐𝒅𝒆 𝑪𝒐𝒖𝒓𝒔𝒆𝒔ቇ


SELECT FName AS ‘Faculty Name’, Designation, CTitle AS ‘Course Title’, Ttitle AS ‘Term Title’
FROM ((SELECT *
FROM Teach T
WHERE CCode=‘IT-216’ AND TermID=‘S19’ AND FCode IN
SELECT *
FROM Teach T
WHERE CCode=‘IT-366’ AND TermID=‘F18’)
Join Faculty F ON F.FCode=T.FCode)
Join Courses C ON C.Code=T.Code
Another (Better) Approach 
SELECT FName AS ‘Faculty Name’, Designation, CTitle AS ‘Course Title’, Ttitle AS ‘Term Title’
FROM (Teach T Join Faculty F ON F.FCode=T.FCode) Join Courses C ON C.Code=T.Code
WHERE CCode=‘IT-216’ AND TermID=‘S19’ AND FCode IN
SELECT *
FROM Teach T
WHERE CCode=‘IT-366’ AND TermID=‘F18’;
29 March 2020 Saleem Raza 40
…Relational Model ~ DML ~ Examples
 List Faculty Name, Designation who is two ranks below ‘Dr. Arif Butt’
 𝑹𝟏 = 𝝅𝑭𝑵𝒂𝒎𝒆,𝑫𝒆𝒔𝒊𝒈𝒏𝒂𝒕𝒊𝒐𝒏 𝑹𝟐 ⋈𝑭𝑪𝒐𝒅𝒆 𝑭𝒂𝒄𝒖𝒍𝒕𝒚
 𝑹𝟐 = 𝝅𝑭𝑪𝒐𝒅𝒆 𝝈𝑯𝒐𝑫=𝑹𝟑.𝑭𝑪𝒐𝒅𝒆 𝑹𝟑
 𝑹𝟑 = 𝝅𝑭𝑪𝒐𝒅𝒆 𝝈𝑯𝒐𝑫=𝑹𝟒.𝑭𝑪𝒐𝒅𝒆 𝑹𝟒
 𝑹𝟒 = 𝝅𝑭𝑪𝒐𝒅𝒆 𝝈𝑭𝑵𝒂𝒎𝒆=′ 𝑫𝒓.𝑨𝒓𝒊𝒇 𝑩𝒖𝒕𝒕′ 𝑭𝒂𝒄𝒖𝒍𝒕𝒚

SELECT FName AS ‘Faculty Name’, Designation


FROM SELECT *
FROM Faculty
WHERE HoD IN
SELECT FCode
FROM Faculty
WHERE HoD IN
SELECT FCode
FROM Faculty
WHERE FName=‘Dr. Arif Butt’;
29 March 2020 Saleem Raza 41
…Relational Model ~ DML ~ Examples
 List Name of Students who are NOT enrolled in any Course in term ‘S19’
 𝑹 = 𝝅𝒔𝑵𝒂𝒎𝒆 𝝅𝑹𝒆𝒈𝑵𝒐 𝑺𝒕𝒖𝒅𝒆𝒏𝒕𝒔 − 𝝅𝑹𝒆𝒈𝑵𝒐 𝝈𝑻𝒆𝒓𝒎𝑰𝑫=′ 𝑺𝟏𝟗′ 𝑪𝑹𝑺 ⋈𝑹𝒆𝒈𝑵𝒐 𝑺𝒕𝒖𝒅𝒆𝒏𝒕𝒔
SELECT sName AS ‘Names’
FROM (SELECT RegNo
FROM Studetns S
WHERE RegNO NOT IN
SELECT RegNo
FROM CRS C
WHERE TermID=‘S19’ ) Join Students S ON C.RegNO=S.RegNO;

29 March 2020 Saleem Raza 42

You might also like