The document discusses various concepts related to relational databases including:
- Primary keys uniquely identify rows in a table. Foreign keys match values in other tables' primary keys.
- Relational databases represent data using relations which have a schema and instances consisting of tuples.
- Relational algebra operations like selection, projection, join, etc. allow querying relational data.
2. Structure of relational databases
Primary key: Each row of a table is uniquely identified by a primary key
composed of one or more columns.
Candidate key: A column, or group of columns, that uniquely identifies a
row in a table is called a candidate key.
Foreign key: A column, or combination of columns, that matches the
primary key of another table is called a foreign key.
3. For each attribute there is a set of permitted values called Domain.
In other words we can say that A Domain D is a set of atomic
values.
Ex:
For our account table if we have 3 attributes as branch name,
account number and balance. Then for attribute branch name,
domain is set of all branch names.
Attributes & Domains
4. Let D1 denotes the set of all account numbers.
D2 denotes the set of all branch names.
D3 denotes the set of all balances.
So any row of account of table must consist of 3 tuple (V1, V2,V3)
Where,
V1 is an account number in the domain D1.
V2 is branch name in the domain D2.
V3 is a balance in the domain D3.
Therefore we can say account is a subset of D1×D2×D3.
5. To generalize we can say that,a table of n attributes must be
subset of D1×D2×D3…×Dn-1×Dn
We can say that a relation to be a subset of Cartesian product of a
list of domain which corresponds to our definition of table.
The main construct for representing data in the relation model is
a relation.
A relation consists of a relation schema and a relation instance.
The relation instance is a table and relation schema describes the
column heads for the table.
The schema specifies the relation name,the name of each field
and the domain of each field.
6. An instance of a relation is a set of tuples can also be referred as
records, in which each tuple has the same number of fields as the
relation schema.
Let us take an example of student relation.
The student table has following attributes
a) Sid: student id
b) Name: name of the student
c) Age: age of student
7. sid name age
S101 Rohit 20
S102 Parita 22
S103 sanjana 23
Attributes
Field
names
Tulpes
Hence, A relational database is a collection of relations with
distinct relation names. The relational database schema is
the collection of schemas for the relations in the database.
8. Domain Constraint
In the schema, every attribute is declared to have a type integer, float,
date, Boolean, string, etc.
An insertion request can violate the domain constraint.
DBMS can check if insertion violates domain constraint and reject the
insertion.
9. Key Constraint and Constraint on Null Values
A key constraint is a statement that a certain minimal subset as the fields
of a relation is a unique identifier for a tuple.
A set of fields that uniquely identifies a tuple according to a key
constraint is called a candidate key for the relation.
Another constraint on attribute specifies whether null values are or are
not permitted.
E.g. if every student tuple must have a valid, non-null value for the name
attribute, the name of student is constrained to be NOT NULL.
10. Entity Integrity Constraint
A primary key must not contain a null value, else it may not be
possible to identify some tuples.
• For Example, if more than one tuple has a null value in its primary
key, we may not be able to distinguish them .
11. Referential Integrity and Foreign Keys
The condition for a foreign key given below, specify a referential integrity
constraint between two relation schemas R1 and R2.
A set of attributes Fk in relation schema R1 is a foreign key of R1 that
references relation R2 if it satisfies following 2 rules.
1. The attributes in Fk have the same domain, as the primary key attribute
Pk of R2, the attributes Fk are said to reference to the relation R2.
2. A value of Fk in a tuple t1 in the current state r1(R1) either occur as a
value of Pk for some tuple t2 in the r2(R2) or is null.
12. In this definition R1 is called the referencing relation and R2 is
the referenced relation.
Sid Name Age
S101 Swati 20
S102 Ruchita 20
S103 Ushma 19
S104 Jones 25
Cid Grade Studid
His.111 A S101
Maths121 B S102
Topology1 B S103
Sci.11 C S104
Foreign Key Primary Key
Student
(Referenced relation)
Enrolled
(Referencing Relation)
13. Relational algebra
It is one of the two formal query languages associated with the relational
model.
Six basic operators are as below:
1. Selection
2. Projection
3. Union
4. Intersection
5. Set difference
6. Cross product
7. Renaming
The operators take one or two relations as inputs and produce a new relation
as a result.
15. Selection
The select operation selects tuples that satisfy a given predicate.
Notation: σc(r)
c is called the selection predicate.
c = <attribute> op <attribute> or <constant>
where, op is one of: <,<=,=,!=,>,>=.
Ex:
σ rating > 8(S2)
16. The selection criteria is rating > 8.
The result is shown in the below table.
Sid Sname rating Age
28 Swati 9 35
58 Uttam 10 35
17. Projection
The project operation is a unary operation that returns its argument
relation, with certain attributes left out.
Notation: П A1,A2…Ak (r)
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
the columns that are not listed.
Duplicate rows removed from result.
Ex:
П sname,rating(S2)
18. The result is shown in below table:
Sname rating
Swati 9
Sonal 8
Maulik 5
Uttam 10
19. Now, using selection &projection we can compute the names
and rating of highly rated sailor as:
П sname,rating(σ rating > 8(S2))
The result is obtained by applying the selection to S2 and then
applying the projection, which is as shown in below table.
Sname rating
Swati 9
Uttam 10
20. Union
In order to perform the union operation, both operand relations must be
union-compatible i.e. they must have same number of columns drawn
from the same domain.
R U S returns a relation instance containing all tuples that occur in either
relation instance R or relation instance S(or both).
Ex:
S1 U S2.
21. The union of S1 & S2. i.e.S1 U S2 would result in:
Sid Sname rating Age
22 Ritesh 7 45
31 Ritesh 8 55
58 Uttam 10 30
28 Swati 9 35
44 Maulik 5 35
22. Intersection
R ∩ S returns a relation instance containing all tuples that occur both
in R and S.
The relation R and S must be union compatible and the schema of
the result is defined to be identical to the schema of R.
Ex:
S1 ∩ S2.
Sid Sname rating Age
31 Ritesh 8 55
58 Uttam 10 30
23. Set difference
R – S returns a relation instance containing all tuples that occur in R
but not in S.
Ex:
S1 – S2
The set difference S1 – S2 is as shown in table.
Sid Sname rating Age
22 Ritesh 7 45
24. Cross product
R × S returns a relation instance whose schema contains all the field of
R followed by all the fields of S.
The cross product operation is also known as the Cartesian product.
Ex:
The cross product between two instance S1 and R1 which are as
shown in the next table:
26. R1 and S1 both have a field named sid .The cross product between
them is as shown in below table.
Sid Sname Rating Age Sid Bid Day
22 Ritesh 7 45 22 101 10/10/96
22 Ritesh 7 45 58 103 11/12/96
31 Sonal 8 55 22 101 10/10/96
31 Sonal 8 55 58 103 11/12/96
58 Uttam 10 35 22 101 10/10/96
58 Uttam 10 35 58 103 11/12/96
27. Renaming
The results of relational-algebra expressions do not have a
name that we can use to refer to them. Renaming is useful to
be able to give them names.
The rename operator is denoted by the lowercase Greek letter
rho (ρ).
Given a relational-algebra expression E, the expression ρx(E)
returns the result of expression E under the name x.
28. Join
Join operation is used to combine information from two or more
relations.
A join can be defined as a cross product followed by selections
and projections. The result of a cross product is much larger than
the result of a join.
There are several kind of joins:
• Condition join
• Equijoin
• Natural join
29. Defined as a cross-product followed by a selection:
R ⋈c S = σc(R S) where c is the condition.
Example:
Given the sample relational instances S1 and R1
The condition join S1 ⋈S1.sid<R1.sid R1 yields
Sid Sname Rating Age
22 Dustin 7 45.0
31 Lubber 8 55.5
58 Rusty 10 35.0
Sid Bid Day
22 101 10/10/96
58 103 11/12/96
Condition join
(sid) Sname Rating Age (sid) Bid Day
22 Dustin 7 45.0 58 103 11/12/96
31 Lubber 8 55.5 58 103 11/12/96
30. Equijoin
Special case of the condition join where the join condition consists solely
of equalities between two fields in R and S connected by the logical AND
operator (∧).
Example: Given the two sample relational instances S1 and R1
Sid Sname Rating Age
22 Dustin 7 45.0
31 Lubber 8 55.5
58 Rusty 10 35.0
Sid Bid Day
22 101 10/10/96
58 103 11/12/96
32. Natural join
Special case of equijoin where equalities are implicitly specified on
all fields having the same name in R and S.
The condition c is now left out, so that the “bow tie” operator by
itself signifies a natural join.
N. B. If the two relations have no attributes in common, the natural
join is simply the cross-product.
33. Division
The division operator is used for queries which involve the ‘all’ qualifier.
such as “Find the names of sailors who have reserved all boats”..
Consider two relation instances A and B in which A has two fields x and y
and B has just one field y , with the same domain as in A.
The division operator A/B is defined as the set of all x values that for every
y value in B, there is a tuple <x,y> in A.
An analogy with integer division may also help to understand division. For
integers A and B, A/B is the largest integer Q such that Q * B ≤ A.
For relation instances A and B , A/B in the largest relation instance Q such
that Q × B ⊆ A.
34. S# P#
S1 P1
S1 P2
S1 P3
S1 P4
S2 P1
S2 P2
S3 P2
S4 P2
S4 P4
P#
P2
P#
P2
P4
P#
P1
P2
P4
S#
S1
S2
S3
S4
S#
S1
S4
S#
S1
The relation A can be thought of as a relation listing parts supplied by
suppliers and of the B relations as listing parts. A/Bi computes
suppliers who supply all parts listed in relation instance Bi.
35. Generalized projection
The generalized projection extends the projection operation by allowing
arithmetic functions to be used in the projection list.
The generalized projection has the form
Where, E is any relational algebra expression. each of F1,
F2,…, Fn is an arithmetic expression involving constants and
attributes in the schema of E.
1 2, ,..., ( )nF F F E
36. Customer name Limit Credit balance
Swati 2000 1750
Ruchita 1500 1500
Manthan 6000 700
Ushma 2000 400
Customer name Result
Swati 250
Ruchita 5300
Manthan 1600
Ushma 0
Let us consider the following relation
Suppose if we want to find out how much each person can spend,
we can write: ,lim ( inf)customer name it creditbalance credit
37. Aggregate function
Aggregate function takes a collection of values and returns a single value
as a result.
Aggregate functions are sum, avg, count, min and max.
To understand aggregate function , lets us consider the pt-works relation
for part time employees.
Employee-name Branch-name Salary
Swati Pune 1500
Ruchita Pune 1300
Manthan Mumbai 5300
Ushma Mumbai 1500
Arpita Delhi 2500
Sameer Delhi 1300
38. Branch name Salary
Delhi 3800
Mumbai 6800
Pune 2800
To find out the total sum of salaries of all the part
employees in the bank. The expression would be
39. Outer join
This operation is an extension of the join operation of deal with missing
information.
These are three types of out joins:
a) Left outer join, denoted by
b) Right outer join, denoted by
c) Full outer join, denoted by
40. Consider the employee and ft-works relations.
Employee name Street City
Swati Salisbury park Pune
Ruchita Kothrud Mumbai
Ushma Sion Delhi
Arpit Mint street Chennai
Employee name Branch name Salary
Swati ABC 1500
Ruchita XYZ 1300
Ushma PQR 5300
Arpit WBC 1500
41. Employee ⋈ ft-works
Employee-name Street City Branch-name Salary
Swati Salisbury park Pune ABC 1500
Ruchita Kothrud Mumbai XYZ 1300
Ushma Sion Delhi PQR 5300
42. Employee-name Street City Branch-name Salary
Swati Salisbury park Pune ABC 1500
Ruchita Kothrud Mumbai XYZ 1300
Ushma Sion Delhi PQR 5300
Arpit Mint street Chennai Null Null
Left outer join takes all tuples in the left relation that didn’t match
with any tuple in right relation, pads the tuples with null values for all
other attributes from the right relation.
43. The right outer join is symmetric with left outer join. It pads
tuples from the right relation that did not match any from left
relation with nulls and adds them to the result of natural join.
Employee-name Street City Branch-name Salary
Swati Salisbury park Pune ABC 1500
Ruchita Kothrud Mumbai XYZ 1300
Ushma Sion Delhi PQR 5300
Arpita Null Null WBC 1500
44. Employee-name Street City Branch-name Salary
Swati Salisbury park Pune ABC 1500
Ruchita Kothrud Mumbai XYZ 1300
Ushma Sion Delhi PQR 5300
Arpit Mint street Chennai Null Null
Arpita Null Null WBC 1500
The full outer join does both of these operations, padding tuples
from the left relation that didn’t match any from the relation, as
well as tuples from the right relation that didn’t match any from the
left relation.
45. Example
sid Sname Rating Age
22 Swati 7 45
29 Ushma 1 33
31 Ruchita 8 55.5
32 Manthan 8 25.5
58 Amit 10 35
64 Arpit 3 63.5
Sid Bid Day
22 101 10/10/98
22 102 10/10/98
22 103 10/8/98
31 101 11/10/98
31 102 11/6/98
31 103 11/2/98
64 101 9/5/98
64 102 9/8/98
64 103 9/8/98
Bid bname Color
101 Marine Red
102 Clipper Green
103 Interlake Blue
46. a) Find the names of sailors who have reserved boats 103.
sname((σbid=103 Reserves) ⋈ Sailors)
b) Find the names of sailors who have reserved a red boat.
sname((σcolor=‘red’Boats) ⋈ Reserves ⋈ Sailors)
c) Find the names of sailors who have reserved at least one boat.
sname(Sailors ⋈ Reserves)
47. d) Find the names of sailors who have reserved a red or green boat.
(Tempboats, (σcolor=‘red’Boats) ∪ (σcolor=‘green’Boats))
sname(Tempboats ⋈ Reserves ⋈ Sailors)
e) Find the names of sailors who have reserved all boats called
Interlake.
(Tempsids, (sid,bidReserves)/(bid(σbname=‘Interlake’Boats)))
sname(Tempsids ⋈ Sailors)
48. f) Find the sids of sailors with age over 20 who have not
reserved a red boat.
sid(σage>20Sailors) - sid((σcolor=‘red’Boats) ⋈ Reserves ⋈ Sailors)
g) Find the colors of boats reserved by Lubber.
color((σsname=‘Lubber’Sailors)Sailors ⋈ Reserves ⋈ Boats)