DBMS Unit Iii
DBMS Unit Iii
DBMS Unit Iii
Introduction
relational model which can be implemented using any RDBMS languages like
Oracle SQL, MySQL etc.
It was proposed by E.F. Codd in 1970 to model data in the form of relations or
tables since then it was the most widely used data model and in fact, the only used
which are formally called attributes and tuples respectively. Each tuple in relation is
a real-world entity.
The basic structure of a relational model is tables. So, the tables are also
called relations in the relational model.
The figure below will help you identify the relation, attributes, and tuples in a
relational model.
properties rows and columns. Rows represent records and columns represent
attributes.
― Relation Schema: It represents the name of the relation with its attributes.
― Relation key: Every row has one, two or multiple attributes, which is called
relation key.
Characteristics
― Each tuple must have exactly one data value for an attribute.
― No ordering is required for tuples, similarly for attributes also.
Advantages
model.
― Scalable: This model can be easily scaled as we can add as many rows and
columns we want.
― Structural Independence: We can make changes in database structure
without changing the way to access the data. When we can make changes to
the database structure without affecting the capability to DBMS to access the
data we can say that structural independence has been achieved.
― Query capability: It makes possible for a high-level query language like SQL
to avoid complex database navigation.
Disadvantages
― Few relational databases have limits on field lengths which can't be exceeded.
― Relational databases can sometimes become complex as the amount of data
grows, and the relations between pieces of data become more complicated.
― Complex relational database systems may lead to isolated databases where
1. Domain Constraints
2. Entity Integrity Constraints
3. Key Constraints
4. Referential Integrity Constraints
1. Domain Constraint
― Domain constraint defines the domain or set of possible values for an
attribute.
― It specifies that the value taken by the attribute must be from its domain only.
― Example: Consider the following Student table
S001 Akshay 20
S013 Krishna 23
S056 Vivek Z
S054 Raghu 21
― Here, value ‘Z’ is not allowed since only positive integer values can be taken
by the age attribute.
Entity integrity constraint ensures that the primary key attribute in a relation,
should not accept a null value. This is because the primary key attribute value
S001 Akshay 20
S013 Krishna 23
S056 Vivek 20
Raghu 21
relationship/link between them. They are implemented in the name of foreign keys.
It states that if a foreign key exists in a relation, then either the foreign key value
must match a primary key value of some tuple in its parent relation or its value
must be null.
The rules are:
1. You can't delete a record from a primary table if matching records exist in a
child table.
3. You can't enter a value in the foreign key field of the child table that doesn't
exist in the primary key of the parent table.
4. However, you can enter a Null value in the foreign key, specifying that the
records are unrelated.
Example
Consider 2 relations “Dept" and “Emp" where “D_No" is the primary key in the
“Dept" relation and foreign key in the “Emp" relation.
In this, if a foreign key in Emp table refers to the Primary Key of Dept table, then
every value of the Foreign Key in Emp table must be null or be available in Dept
Table.
4. Key Constraints
1. Super Key
2. Candidate key.
3. Primary key
4. Foreign key.
Relation: STUDENT
STUD_NO NAME AGE MOBILE_NO
Relation: STUDENT-COURSE
512 Akshara 20 9876723452
STUD_NO COURSE_NO COURSE_NAME
513 Krishna 23 9991165674
512 C1 DBMS
546 Vivek 20 7898756543
546 C2 PYTHON
557 Raghu 21 8987867898
512 C2 PYTHON
595 Krishna 22 9876543210
1. Candidate Key
― The minimal set of attributes which can uniquely identify a tuple is
― A candidate key can never be NULL or empty and its value should be
unique.
― There can be more than one candidate keys for table. For example,
STUD_NO, as well as MOBILE_NO both, are candidate keys for relation
STUDENT.
― The candidate key can be simple or composite. For Example, {STUD_NO,
― The set of attributes which can uniquely identify a tuple is known as Super
Key. For Example, STUD_NO, (STUD_NO, STUD_NAME), MOBILE_NO, etc.
There can be more than one candidate key in a relation out of which one can
be chosen as a primary key.
For Example, STUD_NO as well as MOBILE_NO both are candidate keys for
relation STUDENT but STUD_NO can be chosen as primary key.
Ch. Vijayananda Ratnam @Dept. of CSE 6
Database Management Systems UNIT-III
4. Secondary or Alternative key
The candidate key other than the primary key is called an alternate key / Secondary
key.
For Example, STUD_NO, as well as MOBILE_NO both, are candidate keys for relation
5. Foreign Key
If an attribute can only take the values which are present as values of some
other attribute, it will be a foreign key to the attribute to which it refers.
The relation which is being referenced is called referenced relation and the
corresponding attribute is called referenced attribute and the relation which refers
The referenced attribute of the referenced relation should be the primary key
STUDENT relation.
Foreign key can be NULL as well as may contain duplicate tuples i.e., it need not
Let us convert the College Management System E-R Model into Relations by
following the above rules.
Relational Algebra
3.5 What is query language?
(or)
It is a specialized language for asking questions, or queries, that involve the data in a
database.
execution plans.
― Example: Relational Algebra - conceptual procedural query language used
on relational model.
Ch. Vijayananda Ratnam @Dept. of CSE 9
Database Management Systems UNIT-III
Non - procedural Query language:
― It describes the desired answer without specifying how the answer is to be
computed.
― Non - operational, declarative.
telling him to make a tea and not telling the process then it is a non-procedural
language, however if you are telling the step-by-step process like switch on the
stove, boil the water, add milk etc. then it is a procedural language.
various operations such as insert, update, delete on the data. When I say that
relational algebra is a procedural query language, it means that it tells what data to
be retrieved and how to be retrieved.
― Basic Operations
1. Select (σ)
2. Project (∏)
3. Union (∪)
4. Set Difference (-)
― Derived Operations
1. Natural Join (⋈)
4. Division (÷)
It is denoted by sigma (σ), and it is used to find the tuples (or rows) in a relation (or
Where
Example:
attributes) from a table (or relation). It is like the Select statement in SQL and syntax
is:
Example: We have a relation STUDENT with four columns, we want to fetch only
two columns of the table, which we can do with the help of projection operator ∏.
Query:
3. Union (∪)
It is used to select all the rows (tuples) from two tables (relations). It is denoted by
symbol ∪. Its syntax is:
R1 ∪ R2
For example, in r1 ∪ r2, the union of two relations r1 and r2 produces an output
relation that contains all the tuples of r1, or r2, or both r1 and r2, duplicate tuples
being eliminated.
For a union operation to be valid, the following conditions must hold on to both
relations –
Example 1: Consider the two relations R and S, each with two attributes A and B.
Ch. Vijayananda Ratnam @Dept. of CSE 12
Database Management Systems UNIT-III
Output: It gives the customer_name from both relation Depositor and Borrower by
eliminating duplication.
It is denoted by ― symbol. Let us say, we have two relation R and S, then the
set difference operation R – S, produces a relation consisting of the tuples that are in
relation R, but not in S. Both the relations R and S must be union compatible. The
syntax is:
Relation_name1 ― Relation_name2
Example 1: Consider the two relations R and S, each with two attributes A and B.
Example 2: To find only depositors who are not taking any loan.
in relation Borrower.
of the two relations (R X S) would combine each tuple of first relation R with every
tuple of second relation S and its syntax is:
Relation_name1 X Relation_name2
Note: The result set contains attributes of both the relations.
Example 1: Consider the two relations R and S, each with one attribute A and B
respectively.
relation.
For the above result set,
σ condition (R ✕ S)
The above query gives meaningful results.
Example 2:
σ city = ‘Kolkata’ (Depositor Χ Borrower)
Output – It selects all tuples from both relations Depositor and Borrower where city
is Kolkata.
we want to select all those tuples (rows) that are present in both the relations, then in
that case we can apply intersection operation on these two relations R ∩ S.
Note: Only those rows that are present in both the relations will appear in the result
set.
Syntax:
Relation_name1 ∩ Relation_name2
For an intersection operation to be valid, the following conditions must hold on both
relations –
Example 1: Consider the two relations R and S, each with two attributes A and B.
Output: It gives the customer_name which are present in both the relations.
7. Rename (ρ)
The results of relational algebra operations are always the relations, but they
are without any name. The rename operation allows user to rename the output
relation. It is denoted by using ρ (rho) and its syntax is:
ρ x (E)
Where E is the expression with different relational algebra operations and x is the
name given to their result.
Example:
Let’s say we have a relation CUSTOMER, we are fetching customer names and
8. Division operation ( / )
Consider two relation instances R and S in which R has (exactly) two fields x
and y and S has just one field y, with the same domain as in R.
We define the division operation R / S as the set of all x values such that for
every y value in S, there is a tuple ‹x, y › in R.
One of the most useful operations in relational algebra. This is the most
common way to combine information from two or more relations. It defined as a
There are mainly two types of joins which are further divided as follows:
1. Inner Join
a. Natural join.
b. Theta join.
c. Equi join.
2. Outer join
In an inner join, only those tuples that satisfy the matching criteria are included, while
the rest are excluded.
Ch. Vijayananda Ratnam @Dept. of CSE 17
Database Management Systems UNIT-III
1. Natural Join (⋈)
Natural join can only be performed if there is a common attribute
(column) between the relations. The name and type of the attribute must be same,
and its syntax is:
R⋈S
Where R an S are the two relations, joined using natural join.
Example: We have two relations of S (num, square) and C (num, cube). Now, we
will perform natural join on both the relations i.e.,
Example: We have two relations of Student (sid, sname, std) and Course (class, subject).
Now, we will perform theta join on both the relations i.e.,
Output: The result of theta join is the set of tuples of all combinations in Student
3. EQUI join
Output:
OUTER JOIN
In outer join, along with tuples that satisfy the matching criteria, we also
include some or all tuples that do not match the criteria.
right relation. However, if there is no matching tuple is found in right relation, then
the attributes of right relation in the join result are filled with NULL values.
Syntax is:
R1 ⟕ R2
S⟕C
Output:
This join returns all the tuples of the right relation and matching tuples for the
left relation. However, if there is no matching tuple is found in left relation, then the
attributes of left relation in the join result are filled with NULL values.
Syntax is:
R1 ⟖ R2
where R1 and R2 are relations.
In a full outer join, all tuples from both relations are included in the result,
irrespective of the matching condition. The rows for which there is no matching, the
Example: By considering the previous relations, we obtain the following result when
we apply the full join on both relations. i.e.,
S⟗C
Output:
retrieved but doesn’t tell how to retrieve it. It only focusses on what to do, and not
on how to do it. It exists in two forms:
It is used for selecting those tuples that satisfy the given condition. In Tuple Calculus,
a query is expressed as:
{t | P(t)}
Where
t = resulting tuples
P(t) = known as Predicate or is the condition used to fetch t.
― P(t) may have various conditions logically combined with OR (∨), AND (∧),
NOT(¬).
For example:
{ T.name | Student (T) AND T.address = 'Guntur' }
Output:
This query selects the tuples from the STUDENT relation. It returns a tuple with
In domain relational calculus, filtering is done based on the domain of the attributes
and not based on the tuple values. In Domain Calculus, a query is expressed as:
{ c1, c2, c3, ..., cn | P (c1, c2, c3, ... ,cn) }
Where
For example,
{< name, address > | ∈ Student ∧ address = 'guntur’}
This query selects the tuples from the STUDENT relation. It returns attributes name ,
address from Student whose address is 'guntur'.
Relation: Boats
bid bname color
101 Interlake blue
102 Interlake red
103 Clipper green
104 Marine red