BCS304
Database Management System
chintan Patel
chintan.patel@rru.ac.in
Relational Model, Relational Algebra, SQL
In this Unit
Example of a Instructor Relation
Tuples (or rows)
Attributes (or columns)
Domain of the attribute : Set of Allowed Values
NULL Attribute: value is “unknown”
Database Schema
Keys in Relational Model
Discussion
Candidate Key: Minimal set of attributes that can uniquely identify a tuple
For Example, ID in INSTRUCTOR relation.
Primary Key : ONLY ONE KEY CHOOSEN FROM SET OF CANDIDATE KEYS.
Alternate Keys : NOT CHOOSEN SET OF CANDIDATE KEYS:
Example: ID, Phone No, and University_Email are candidate keys but ID can be chosen as the primary key
ANY RELATION CAN/MUST HAVE ONLY ONE PRIMARY KEY
Primary keys are not necessarily to be a single column; more than one the column can also be a primary key for a table.
Foreign Key : Column that creates a relationship between two tables.
Example: Primary key and Foreign Keys
P
F
P
F
F
Quiz: Decide Primary key and Foreign Keys
University Database
Quiz: Decide Primary key and Foreign Keys
Quiz : Keys
Example-1 : Let a Relation R have attributes {a1,a2,a3} and a1 is the candidate key. Then how many super keys are possible?
Here, any superset of a1 is the super key. �Super keys are = {a1, a1 a2, a1 a3, a1 a2 a3} �Thus we see that 4 Super keys are possible in this case.
In general, if we have ‘N’ attributes with one candidate key then the number of possible superkeys is 2(N – 1).
Example-2 : Let a Relation R have attributes {a1, a2, a3,…,an}. Find Maximum Super key of R.
Maximum Super keys = 2n – 1. �If each attribute of relation is candidate key.
Example-3: Let a Relation R have attributes {a1, a2, a3,…, an} and the candidate key is “a1 a2 a3” then the possible number of super keys?
Following the above formula, we have 3 attributes instead of one. So, here the number of possible super keys is 2(N-3).
Examples of Procedural languages: FORTRAN, COBOL, ALGOL, BASIC, C and Pascal.
Examples of Non-Procedural languages: SQL, PROLOG, LISP.
Relational Query Languages
Relational algebra
(Procedural Language)
Tuple relational calculus
Domain relational calculus
Relational Algebra
Select Operation
σ dept_name=“Physics” (instructor)
Select Operation (Cont.)
=, ≠, >, ≥. <. ≤
∧ (and), ∨ (or), ¬ (not)
� σ dept_name=“Physics” ∧ salary > 90,000 (instructor)
σ dept_name=building (department)
∏ A1,A2,A3 ….Ak (r)
where A1, A2 are attribute names and r is a relation name.
Project Operation
� ∏name(σ dept_name =“Physics” (instructor))
Cartesian-Product Operation
instructor X teaches
��
Instructor |
ID |
name |
dept_name |
salary |
teaches |
ID |
course_id |
sec_id |
semester |
year |
X
=
Most of the resulting rows have information about instructors who did NOT teach a particular course.
And Write relational algebra query
σ instructor.id = teaches.id (instructor x teaches ))
Select
operation
Cartesian Product
operation
Select
operation
Cartesian Product
operation
Theta Join
operation
=
=
Union Operation
1. r, s must have the same arity (same number of attributes)
2. The attribute domains must be compatible (example: 2nd column � of r deals with the same type of values as does the 2nd � column of s)
Set Intersection Operation
∏course_id (σ semester=“Fall” Λ year=2017 (section)) ∩ � ∏course_id (σ semester=“Spring” Λ year=2018 (section))
�
Set Difference Operation
����
Assignment Operation
Music ← σ dept_name=“Music” (instructor)
Physics ∪ Music
�
Rename Operation
ρx (E)
returns the result of expression E under the name x
ρx(A1,A2, .. An) (E)
�
Equivalent Queries
σ dept_name=“Physics” ∧ salary > 90,000 (instructor)
σ dept_name=“Physics” (σ salary > 90.000 (instructor))
��
Practice : Assignment 1 [20 Marks]
�Schema :
Gracing Assignment - 1
�1. Scheme:
[GATE 2000]
�1. Given the relations employee (name, salary, deptno), and� department (deptno, deptname, address)
��
[GATE 2003]
�2. Consider the following SQL query select distinct al, a2,........., an
from r1, r2,........, rm where P
For an arbitrary predicate P, this query is equivalent to which of the following
relational algebra expressions ?
�������Answer : A
Option B is doing natural join meaning only those tuples having same value for common attribute(s) in both the relations are selected for output.
�
[GATE CSE 2006]
�Answer = 2000
[GATE CSE 2006]
�(A) II and III only�(B) I and II only�(C) I and III only�(D) I, II and III
�Answer : D
Also Refer :
https://www.cs.purdue.edu/homes/bb/cs448_Fall2017/lpdf/Chapter08.pdf