UNIT2_RDBMS
UNIT2_RDBMS
Relational Model
Relational Model was proposed by E.F. Codd to model data in the form of relations or tables.
Various commercial relational DBMS products were developed during the 1980s such
as DB2, SQL/DS, and Oracle.
In a relational data model the data are stored in the form of tables.
Relational Model represents how data is stored in Relational Databases.
A relational database stores data in the form of relations (tables).
Important Terminologies
Relation
A relation is a table containing rows and columns.
Example:
STUDENT
Roll Nam Departme
No e nt
101 Arun CSE
102 Priya IT
Kum
103 ECE
ar
The above table is called a relation.
Attribute
An attribute is a column in a relation.
Example:
● RollNo
● Name
● Department
Tuple
A tuple is a row in a relation.
Example:
(101, Arun, CSE)
Domain
A domain is the set of valid values that an attribute can take.
Example:
Gender = {Male, Female}
Marks = {0 – 100}
Degree
Number of attributes in a relation.
Example:
STUDENT(RollNo, Name, Department)
Degree = 3
Cardinality
Number of tuples in a relation.
Example:
If STUDENT table contains 100 rows,
Cardinality = 100
Column
Column represents the set of values for a particular attribute. The column ROLL_NO is extracted from
relation STUDENT.
NULL Values
The value which is not known or unavailable is called NULL value.
CODD’s Rule:
Every database has tables, and constraints cannot be referred to as a rational database system. And if
any database has only relational data model, it cannot be a Relational Database System (RDBMS).
So, some rules define a database to be the correct RDBMS.
These rules were developed by Dr. Edgar F. Codd (E.F. Codd) in 1985, who has vast research
knowledge on the Relational Model of database Systems.
Codd presents his 13 rules for a database to test the concept of DBMS against his relational model,
and if a database follows the rule, it is called a true relational database (RDBMS). These 13 rules
are popular in RDBMS, known as Codd's 12 rules.
Rule 0: The Foundation Rule
The database must be in relational form. So that the system can handle the database through its
relational capabilities.
A database contains various information, and this information must be stored in each cell of a table in
the form of rows and columns.
Every single or precise data (atomic value) may be accessed logically from a relational database using
the combination of primary key value, table name, and column name.
This rule defines the systematic treatment of Null values in database records. The null value has
various meanings in the database, like missing the data, no value in a cell, inappropriate information,
unknown data and the primary key should not be null.
It represents the entire logical structure of the descriptive database that must be stored online and is
known as a database dictionary. It authorizes users to access the database and implement a similar
query language to access the database.
The relational database supports various languages, and if we want to access the database, the
language must be the explicit, linear or well-defined syntax, character strings and supports the
comprehensive: data definition, view definition, data manipulation, integrity constraints, and limit
transaction management operations. If the database allows access to the data without any language, it
is considered a violation of the database.
All views table can be theoretically updated and must be practically updated by the database systems.
Rule 7: Relational Level Operation (High-Level Insert, Update and delete) Rule
A database system should follow high-level relational operations such as insert, update, and delete in
each level or a single row. It also supports union, intersection and minus operation in the database
system.
All stored data in a database or an application must be physically independent to access the database.
Each data should not depend on other data or an application. If data is updated or the physical
structure of the database is changed, it will not show any effect on external applications that are
accessing the data from the database.
Rule 9: Logical Data Independence Rule
It is similar to physical data independence. It means, if any changes occurred to the logical level (table
structures), it should not affect the user's view (application). For example, suppose a table either split
into two tables, or two table joins to create a single table, these changes should not be impacted on the
user view application.
A database must maintain integrity independence when inserting data into table's cells using the SQL
query language. All entered values should not be changed or rely on any external factor or application
to maintain integrity. It is also helpful in making the database-independent for each front-end
application.
The distribution independence rule represents a database that must work properly, even if it is stored
in different locations and used by different end-users. Suppose a user accesses the database through an
application; in that case, they should not be aware that another user uses particular data, and the data
they always get is only located on one site. The end users can access the database, and these access
data should be independent for every user to perform the SQL queries.
The non-submersion rule defines RDBMS as a SQL language to store and manipulate the data in the
database. If a system has a low-level or separate language other than SQL to access the database
system, it should not subvert or bypass integrity to transform data.
KEY
A Key is an attribute or group of attributes, which is used to identify a row in a relation.
Key can be broadly classified into,
1. Super key
2. Candidate key, and
3. Primary key
4. Foreign Key
1. Super key
A superkey is a subset of attributes of an entity-set that uniquely identifies the entities.
Superkeys represent a constraint that prevents two entities from ever having the same
value for those attributes.
2. Candidate key
Candidate key is a minimal superkey.
A candidate key for a relation schema is a minimal set of attributes whose values
uniquely identify tuples in the corresponding relation.
3. Primary key
● The primary key is a designated candidate key. It is to be noted that the primary key
should not be [Link]
● Consider the employee relation, which is characterized by the attributes, employee
ID, employee name, employee age, employee experience, employee salary, etc. In this
employee relation:
● Superkeys can be employee ID, employee name, employee age, employee experience,
etc.
● Candidate keys can be employee ID, employee name, employee age. Primary key is
employee ID.
Note: If we declare a particular attribute as the primary key, then that attribute value cannot be
NULL. Also, it has to be distinct.
4. Foreign Key
Foreign key is a set of fields or attributes in one relation that is used to “refer” to a
tuple in another relation.
INTEGRITY
Data integrity constraints refer to the accuracy and correctness of data in the database.
Data integrity provides a mechanism to maintain data consistency for operations like
INSERT, UPDATE, and DELETE.
The different types of data integrity constraints are
1. Entity,
2. NULL,
3. Domain, and
4. Referential integrity
1. Entity Integrity
Entity integrity implies that a primary key cannot accept a null value.
The primary key of the relation uniquely identifies a row in a relation.
Entity integrity means that in order to represent an entity in the database it is necessary
to have a complete identification of the entity’s key attributes.
Consider the entity PLAYER; the attributes of the entity PLAYER are Name, Age, Nation, and
Rank. In this example, consider PLAYER’s name as the primary key even though two players can have the
same name. Cannot insert any data in the relation PLAYER without
entering the name of the player. This implies that primary key cannot be null.
2. NULL
Null implies that the data value is not known temporarily. Consider the relation
PERSON.
The attributes of the relation PERSON are name, age, and salary. The age of the
person cannot be NULL.
3. Domain
● Domains are used in the relational model to define the characteristics of the columns
of a table.
● Domain refers to the set of all possible values that an attribute can take.
● The domain specifies its own name, data type, and logical size.
● The logical size represents the size as perceived by the user, not how it is implemented
internally.
● For example, for an integer, the logical size represents the number of digits used to
display the integer, not the number of bytes used to store it.
● The domain integrity constraints are used to specify the valid values that a column
defined over the domain can take.
● Define the valid values by listing them as a set of values a range of values, or an
expression that accepts the valid values.
● Strictly speaking, only values from the same domain should ever be compared or be
integrated through a union operator.
● The domain integrity constraint specifies that each attribute must have values derived
from a valid range.
Example 1
The age of the person cannot have any letter from the alphabet. The age should be a
numerical value.
Example 2
Consider the relation APPLICANT. Here APPLICANT refers to the person who is applying
for job. The sex of the applicant should be either male (M) or female (F). Any entry other
than M or F violates the domain constraint.
4. Referential integrity
● In the relational data model, associations between tables are defined through the use
of foreign keys.
● The referential integrity rule states that a database must not contain any unmatched
foreign key values.
● It is to be noted that the referential integrity rule does not imply a foreign key cannot
be null.
● There can be situations where a relationship does not exist for a particular instance, in
which case the foreign key is null.
● A referential integrity is a rule that states that either each foreign key value must
match a primary key value in another relation or the foreign key value must be null.
RELATIONAL ALGEBRA
Relational Algebra is a procedural query language. Relational algebra mainly provides a theoretical
foundation for relational databases and SQL. The main purpose of using Relational Algebra is to
define operators that transform one or more input relations into an output relation. Given that these
operators accept relations as input and produce relations as output, they can be combined and used to
express potentially complex queries that transform potentially many input relations (whose data are
stored in the database) into a single output relation (the query results). As it is pure mathematics,
there is no use of English Keywords in Relational Algebra and operators are represented using
symbols.
FUNDAMENTAL OPERATORS
These are the basic/fundamental operators used in Relational Algebra.
1. Selection(σ)
2. Projection(π)
3. Union(U)
4. Set Difference(-)
5. Set Intersection(∩)
6. Rename(ρ)
7. Cartesian Product(X)
1 2 4
2 2 3
3 2 3
4 3 4
For the above relation, σ(c>3)R will select the tuples which have c more than 3.
A B C
1 2 4
4 3 4
Note: The selection operator only selects the required tuples but does not display them. For
display, the data projection operator is used.
2. Projection(π): It is used to project required column data from a relation. Example: Consider
Table 1. Suppose we want columns B and C from Relation R. π(B,C)R will show following
columns.
B C
2 4
2 3
3 4
3. Union(U): Union operation in relational algebra is the same as union operation in set theory.
Example:
FRENCH
Student_Name Roll_Number
Ram 01
Mohan 02
Vivek 13
Geeta 17
GERMAN
Student_Name Roll_Number
Vivek 13
Geeta 17
Shyam 21
Rohan 25
Consider the following table of Students having different optional subjects in their course.
π(Student_Name)FRENCH U π(Student_Name)GERMAN
Student_Name
Ram
Mohan
Student_Name
Vivek
Geeta
Shyam
Rohan
Note: The only constraint in the union of two relations is that both relations must have the same
set of Attributes.
4. Set Difference(-): Set Difference in relational algebra is the same set difference operation as
in set theory.
Example: From the above table of FRENCH and GERMAN, Set Difference is used as follows
π(Student_Name)FRENCH - π(Student_Name)GERMAN
Student_Name
Ram
Mohan
Note: The only constraint in the Set Difference between two relations is that both relations must have
the same set of Attributes.
5. Set Intersection(∩): Set Intersection in relational algebra is the same set intersection
operation in set theory.
Example: From the above table of FRENCH and GERMAN, the Set Intersection is used as
follows
π(Student_Name)FRENCH ∩ π(Student_Name)GERMAN
Student_Name
Vivek
Geeta
Note: The only constraint in the Set Difference between two relations is that both relations must have
the same set of Attributes.
7. Cross Product(X): Cross-product between two relations. Let’s say A and B, so the cross product
between A X B will result in all the attributes of A followed by each attribute of B. Each record of A
will pair with every record of B.
Example:
A ID Course B
Name Age Sex
1 DS
Ram 14 M
Sona 15 F 2 DBMS
Kim 20 M
AXB
Ram 14 M 1 DS
Ram 14 M 2 DBMS
Sona 15 F 1 DS
Sona 15 F 2 DBMS
Kim 20 M 1 DS
Kim 20 M 2 DBMS
Note: If A has ‘n’ tuples and B has ‘m’ tuples then A X B will have ‘ n*m ‘ tuples. Derived Operators
These are some of the derived operators, which are derived from the fundamental operators.
1. Natural Join(⋈)
2. Conditional Join
1. Natural Join(⋈): Natural join is a binary operator. Natural join between two or more relations
will result in a set of all combinations of tuples where they have an equal common attribute.
Example:
EMP
Name ID Dept_Name
A 120 IT
B 125 HR
C 110 Sales
Name ID Dept_Name
D 111 IT
DEPT
Dept_Name Manager
Sales Y
Production Z
IT A
A 120 IT A
C 110 Sales Y
D 111 IT A
2. Conditional Join: Conditional join works similarly to natural join. In natural join, by default
condition is equal between common attributes while in conditional join we can specify any
condition such as greater than, less than, or not equal.
Example:
R
ID Sex Marks
1 F 45
ID Sex Marks
2 F 55
3 F 60
S
ID Sex Marks
10 M 20
11 M 22
12 M 59
1 F 45 10 M 20
1 F 45 11 M 22
2 F 55 10 M 20
2 F 55 11 M 22
3 F 60 10 M 20
3 F 60 11 M 22
3 F 60 12 M 59
[Link] [Link] [Link] [Link] [Link] [Link]
ROLL_NO SPORTS
1 Badminton
2 Cricket
2 Badminton
4 Badminton
Table 2: EMPLOYEE
ADDRES
EMP_NO NAME PHONE AGE
S
Table 3: STUDENT
Selection operator (σ): Selection operator is used to selecting tuples from a relation based on some
condition. Syntax:
σ (Cond)(Relation Name)
Extract students whose age is greater than 18 from STUDENT relation given in Table 3
σ (AGE>18)(STUDENT)
[Note: SELECT operator does not show any result, the projection operator must be called before
the selection operator to generate or project the result. So, the correct syntax to generate the result
is: ∏(σ (AGE>18)(STUDENT))]
RESULT:
RESULT:
ROLL_NO NAME
1 RAM
2 RAMESH
3 SUJIT
4 SURESH
Note: If the resultant relation after projection has duplicate rows, it will be removed. For Example
∏(ADDRESS)(STUDENT) will remove one duplicate row with the value DELHI and return three
rows.
Cross Product(X): Cross product is used to join two relations. For every row of Relation1, each row
of Relation2 is concatenated. If Relation1 has m tuples and and Relation2 has n tuples, cross product
of Relation1 and Relation2 will have m X n tuples. Syntax:
Relation1 X Relation2
To apply Cross Product on STUDENT relation given in Table 1 and STUDENT_SPORTS relation
given in Table 2,
STUDENT X STUDENT_SPORTS
RESULT:
945512345
1 RAM DELHI 18 2 Cricket
1
945512345
1 RAM DELHI 18 2 Badminton
1
945512345
1 RAM DELHI 18 4 Badminton
1
RAMES GURGAO 965243154
2 18 1 Badminton
H N 3
915625313
3 SUJIT ROHTAK 20 1 Badminton
1
915625313
3 SUJIT ROHTAK 20 2 Cricket
1
915625313
3 SUJIT ROHTAK 20 2 Badminton
1
915625313
3 SUJIT ROHTAK 20 4 Badminton
1
SURES 915676897
4 DELHI 18 1 Badminton
H 1
SURES 915676897
4 DELHI 18 2 Cricket
H 1
SURES 915676897
4 DELHI 18 2 Badminton
H 1
SURES 915676897
4 DELHI 18 4 Badminton
H 1
Union (U): Union on two relations R1 and R2 can only be computed if R1 and R2 are union
compatible (These two relations should have the same number of attributes and corresponding
attributes in two relations have the same domain).
Union operator when applied on two relations R1 and R2 will give a relation with tuples that are
either in R1 or in R2. The tuples which are in both R1 and R2 will appear only once in the result
relation. Syntax:
Relation1 U Relation2
Find the person who is either student or employees, we can use Union operators like:
STUDENT U EMPLOYEE
RESULT:
Minus (-): Minus on two relations R1 and R2 can only be computed if R1 and R2 are union
compatible. Minus operator when applied on two relations as R1-R2 will give a relation with tuples
that are in R1 but not in R2. Syntax:
Relation1 - Relation2
Find the person who is a student but not an employee, we can use minus operator like:
STUDENT - EMPLOYEE
RESULT:
Ram 14 M
Sona 15 F
Kim 20 M
B
ID Course
1 DS
2 DBMS
AXB
Ram 14 M 1 DS
Ram 14 M 2 DBMS
Sona 15 F 1 DS
Sona 15 F 2 DBMS
Kim 20 M 1 DS
Kim 20 M 2 DBMS
Note: If A has ‘n’ tuples and B has ‘m’ tuples then A X B will have ‘ n*m ‘ tuples. Derived Operators
These are some of the derived operators, which are derived from the fundamental operators.
1. Natural Join(⋈)
2. Conditional Join
1. Natural Join(⋈): Natural join is a binary operator. Natural join between two or more relations
will result in a set of all combinations of tuples where they have an equal common attribute.
Example:
EMP
Name ID Dept_Name
A 120 IT
B 125 HR
C 110 Sales
D 111 IT
DEPT
Dept_Name Manager
Sales Y
Production Z
IT A
A 120 IT A
C 110 Sales Y
D 111 IT A
2. Conditional Join: Conditional join works similarly to natural join. In natural join, by default
condition is equal between common attributes while in conditional join we can specify any
condition such as greater than, less than, or not equal.
Example:
R
ID Sex Marks
1 F 45
2 F 55
3 F 60
S
ID Sex Marks
10 M 20
11 M 22
12 M 59
1 F 45 10 M 20
1 F 45 11 M 22
2 F 55 10 M 20
2 F 55 11 M 22
3 F 60 10 M 20
3 F 60 11 M 22
3 F 60 12 M 59
RELATIONAL CALCULUS
As Relational Algebra is a procedural query language, Relational Calculus is a non-procedural query
language. It basically deals with the end results. It always tells me what to do but never tells me how
to do it.
There are two types of Relational Calculus
1. Tuple Relational Calculus(TRC)
2. Domain Relational Calculus(DRC)
Notation:
For example:
Output: This query selects the tuples from the AUTHOR relation. It returns a tuple with 'name'
from Author who has written an article on 'database'.
TRC (tuple relation calculus) can be quantified. In TRC, we can use Existential (∃) and Universal
Quantifiers (∀).
For example:
Output: This query will yield the same result as the previous one.
Notation:
For example:
Output: This query will yield the article, page, and subject from the relational javatpoint, where
the subject is a database.
Query By Example
the database they should be correct and in a well-defined structure which means they should follow
a proper syntax if the syntax or query is wrong definitely we will get an error and due to that our
application or calculation definitely going to stop. So to overcome this problem QBE was introduced.
QBE stands for Query By Example and it was developed in 1970 by Moshe Zloof at IBM.
It is a graphical query language where we get a user interface and then we fill some required
fields to get our proper result.
In SQL we will get an error if the query is not correct but in the case of QBE if the query is
wrong either we get a wrong answer or the query will not be going to execute but we will never
get any error.
Example
Consider the example where a table ‘SAC’ is present in the database with Name,
Phone_Number, and Branch fields. And we want to get the name of the SAC-Representative
name who belongs to the MCA Branch. If we write this query in SQL we have to write it like
QUESTION BANK
MCQS
1. Who proposed the Relational Model?
A) Peter Chen
B) E.F. Codd
C) Alan Turing
D) Charles Babbage
Answer: B
2. A relation in DBMS is also called
A) Attribute
B) Table
C) Domain
D) Schema
Answer: B
3. A row in a relation is called
A) Attribute
B) Domain
C) Tuple
D) Degree
Answer: C
4. A column in a relation is called
A) Tuple
B) Attribute
C) Domain
D) Record
Answer: B
5. The number of attributes in a relation is called
A) Cardinality
B) Degree
C) Domain
D) Schema
Answer: B
6. The number of tuples in a relation is called
A) Cardinality
B) Degree
C) Domain
D) Attribute
Answer: A
7. A domain is
A) Set of valid values for an attribute
B) Set of tables
C) Set of tuples
D) Set of keys
Answer: A
8. Which key uniquely identifies a tuple?
A) Foreign Key
B) Alternate Key
C) Primary Key
D) Composite Key
Answer: C
9. A candidate key is
A) Non-unique key
B) Key that can uniquely identify tuples
C) Foreign key
D) Null key
Answer: B
10. Primary key cannot contain
A) Integers
B) Characters
C) NULL values
D) Numbers
Answer: C
11. A foreign key is used to
A) Delete records
B) Establish relationship between tables
C) Create database
D) Normalize table
Answer: B
12. Alternate key is
A) Candidate key not chosen as primary key
B) Foreign key
C) Composite key
D) Super key
Answer: A
13. A super key is
A) Minimal unique key
B) Set of attributes that uniquely identifies tuples
C) Foreign key
D) Composite key only
Answer: B
14. Composite key consists of
A) One attribute
B) Two or more attributes
C) No attribute
D) Foreign key only
Answer: B
15. Entity integrity rule states
A) Foreign key cannot be NULL
B) Primary key cannot be NULL
C) Attribute cannot be NULL
D) Relation cannot be empty
Answer: B
16. Referential integrity is maintained using
A) Primary Key
B) Foreign Key
C) Domain
D) Tuple
Answer: B
17. Selection operation is represented by
A) π
B) σ
C) ⋈
D) ∪
Answer: B
18. Projection operation is represented by
A) π
B) σ
C) ∪
D) ×
Answer: A
19. Selection operation retrieves
A) Columns
B) Rows
C) Tables
D) Keys
Answer: B
20. Projection operation retrieves
A) Rows
B) Tables
C) Columns
D) Domains
Answer: C
21. Union operation requires
A) Compatible relations
B) Same keys only
C) Same tuples only
D) Different schemas
Answer: A
22. Union operator symbol is
A) ∩
B) ∪
C) ×
D) ÷
Answer: B
23. Intersection operation returns
A) Common tuples
B) Different tuples
C) All tuples
D) Joined tuples
Answer: A
24. Difference operation returns
A) Common tuples
B) Tuples in first relation but not second
C) Joined tuples
D) All tuples
Answer: B
25. Cartesian Product operator is
A) σ
B) π
C) ×
D) ∪
Answer: C
26. Cartesian Product combines
A) Rows from same table
B) Every tuple of one relation with every tuple of another
C) Columns only
D) Keys only
Answer: B
27. Join operation is derived from
A) Selection and Cartesian Product
B) Projection and Union
C) Difference and Union
D) Projection and Difference
Answer: A
28. Natural Join eliminates
A) Duplicate rows
B) Duplicate columns
C) NULL values
D) Keys
Answer: B
29. Theta Join uses
A) Any comparison operator
B) Equality only
C) Union only
D) Difference only
Answer: A
30. Equi Join uses
A) < operator
B) > operator
C) = operator
D) != operator
Answer: C
31. Relational Algebra is
A) Procedural Query Language
B) Non-procedural Query Language
C) Programming Language
D) Operating System
Answer: A
32. Relational Calculus is
A) Procedural Language
B) Non-Procedural Language
C) Programming Language
D) DBMS Software
Answer: B
33. Tuple Relational Calculus works on
A) Attributes
B) Tuples
C) Tables
D) Domains
Answer: B
34. Domain Relational Calculus works on
A) Domains
B) Relations
C) Keys
D) Tuples
Answer: A
35. QBE stands for
A) Query By Example
B) Query Based Execution
C) Quick Basic Engine
D) Query Binary Example
Answer: A
36. QBE is an example of
A) Graphical Query Language
B) Programming Language
C) Operating System
D) DBMS Software
Answer: A
37. Which operation combines rows from two relations?
A) Projection
B) Join
C) Selection
D) Rename
Answer: B
38. Rename operation is represented by
A) σ
B) ρ
C) π
D) ∪
Answer: B
39. Which operation changes relation name?
A) Selection
B) Projection
C) Rename
D) Union
Answer: C
40. Relational Model is based on
A) Tables
B) Trees
C) Graphs
D) Networks
Answer: A
41. A relation should not contain
A) Duplicate tuples
B) Attributes
C) Keys
D) Domains
Answer: A
42. Which key can have NULL values?
A) Primary Key
B) Candidate Key
C) Foreign Key
D) Super Key
Answer: C
43. Degree of relation STUDENT(ID, NAME, MARK) is
A) 1
B) 2
C) 3
D) 4
Answer: C
44. A relation with 20 rows has cardinality
A) 10
B) 20
C) 3
D) 0
Answer: B
45. Which operation is unary?
A) Union
B) Join
C) Selection
D) Cartesian Product
Answer: C
46. Which operation is binary?
A) Projection
B) Selection
C) Join
D) Rename
Answer: C
47. The result of relational algebra operation is always
A) Domain
B) Relation
C) Key
D) Record
Answer: B
48. Which integrity rule ensures valid foreign key references?
A) Entity Integrity
B) Referential Integrity
C) Domain Integrity
D) Key Integrity
Answer: B
49. Relational Algebra forms the basis for
A) SQL Query Processing
B) Operating System
C) Compiler Design
D) Computer Network
Answer: A
50. Main advantage of Relational Model is
A) Simplicity and data independence
B) Complex structure
C) High redundancy
D) No security
Answer: A
5 Marks Questions
1. State Codd's Rules.
2. Define Relational Data Model.
3. What is a Key? Explain types of keys.
4. List the basic relational algebra operations.
5. Explain Selection operation.
6. Explain Projection operation.
7. What is Relational Calculus?
8. Define Domain Relational Calculus.
9. What is QBE?
10 Marks Questions
1. Explain integrity constraints in relational databases.
2. Explain relational algebra operations with examples.
3. Compare Relational Algebra and Relational Calculus.
4. Explain Tuple Relational Calculus and Domain Relational Calculus.
5. Explain advantages and limitations of Relational Model.