UNIT 2 Relational Model
What is Relational Model?
Relational Model (RM) represents the database as a collection of relations. A relation is
nothing but a table of values. Every row in the table represents a collection of related data
values. These rows in the table denote a real-world entity or relationship.
The table name and column names are helpful to interpret the meaning of values in each row.
The data are represented as a set of relations. In the relational model, data are stored as tables.
However, the physical storage of the data is independent of the way the data are logically
organized.
Some popular Relational Database management systems are:
DB2 and Informix Dynamic Server – IBM
Oracle and RDB – Oracle
SQL Server and Access – Microsoft
Relational Model Concepts in DBMS
1. Attribute: Each column in a Table. Attributes are the properties which define a
relation. e.g., Student_Rollno, NAME,etc.
2. Tables – In the Relational model the, relations are saved in the table format. It is
stored along with its entities. A table has two properties rows and columns. Rows
represent records and columns represent attributes.
3. Tuple – It is nothing but a single row of a table, which contains a single record.
4. Relation Schema: A relation schema represents the name of the relation with its
attributes.
5. Degree: The total number of attributes which in the relation is called the degree of the
relation.
6. Cardinality: Total number of rows present in the Table.
7. Column: The column represents the set of values for a specific attribute.
8. Relation instance – Relation instance is a finite set of tuples in the RDBMS system.
Relation instances never have duplicate tuples.
9. Relation key – Every row has one, two or multiple attributes, which is called relation
key.
10. Attribute domain – Every attribute has some pre-defined value and scope which is
known as attribute domain
11.
Relational Constraints in DBMS
Relational constraints are the restrictions imposed on the database contents and operations.
They ensure the correctness of data in the database.
Types of Constraints in DBMS-
In DBMS, there are following 5 different types of relational constraints-
1. Domain constraint
2. Tuple Uniqueness constraint
3. Key constraint
4. Entity Integrity constraint
5. Referential Integrity constraint
1. Domain Constraint-
Domain constraint defines the domain or set of values for an attribute.
It specifies that the value taken by the attribute must be the atomic value from its domain.
Example-
Consider the following Student table-
STU_ID Name Age
S001 Akshay 20
S002 Abhishek 21
S003 Shashank 20
S004 Rahul A
Here, value ‘A’ is not allowed since only integer values can be taken by the age attribute.
2. Tuple Uniqueness Constraint-
Tuple Uniqueness constraint specifies that all the tuples must be necessarily unique in any
relation.
Example-01:
Consider the following Student table-
STU_ID Name Age
S001 Akshay 20
S002 Abhishek 21
S003 Shashank 20
S004 Rahul 20
This relation satisfies the tuple uniqueness constraint since here all the tuples are unique.
Example-02:
Consider the following Student table-
STU_ID Name Age
S001 Akshay 20
S001 Akshay 20
S003 Shashank 20
S004 Rahul 20
This relation does not satisfy the tuple uniqueness constraint since here all the tuples are not
unique.
3. Key Constraint-
Key constraint specifies that in any relation-
All the values of primary key must be unique.
The value of primary key must not be null.
Example-
Consider the following Student table-
STU_ID Name Age
S001 Akshay 20
S001 Abhishek 21
S003 Shashank 20
S004 Rahul 20
This relation does not satisfy the key constraint as here all the values of primary key are not
unique.
4. Entity Integrity Constraint-
Entity integrity constraint specifies that no attribute of primary key must contain a null
value in any relation.
This is because the presence of null value in the primary key violates the uniqueness
property.
Example-
Consider the following Student table-
STU_ID Name Age
S001 Akshay 20
S002 Abhishek 21
S003 Shashank 20
Rahul 20
This relation does not satisfy the entity integrity constraint as here the primary key contains a
NULL value.
5. Referential Integrity Constraint-
This constraint is enforced when a foreign key references the primary key of a relation.
It specifies that all the values taken by the foreign key must either be available in the
relation of the primary key or be null.
Read more- Foreign Key in DBMS
Important Results-
The following two important results emerges out due to referential integrity constraint-
We can not insert a record into a referencing relation if the corresponding record does not
exist in the referenced relation.
We can not delete or update a record of the referenced relation if the corresponding record
exists in the referencing relation.
Example-
Consider the following two relations- ‘Student’ and ‘Department’.
Here, relation ‘Student’ references the relation ‘Department’.
Student
STU_ID Name Dept_no
S001 Akshay D10
S002 Abhishek D10
S003 Shashank D11
S004 Rahul D14
Department
Dept_no Dept_name
D10 ASET
D11 ALS
D12 ASFL
D13 ASHS
Here,
The relation ‘Student’ does not satisfy the referential integrity constraint.
This is because in relation ‘Department’, no value of primary key specifies
department no. 14.
Thus, referential integrity constraint is violated.
RELATIONAL LANGUAGE
Relational language is a type of programming language in which the programming logic is
composed of relations and the output is computed based on the query applied. Relational
language works on relations among data and entities to compute a result.
Relational database systems are expected to be equipped with a query language that can assist
its users to query the database instances. There are two kinds of query languages − relational
algebra and relational calculus.
Relational Algebra
Relational algebra is a procedural query language, which takes instances of relations as input
and yields instances of relations as output. It uses operators to perform queries. An operator
can be either unary or binary. They accept relations as their input and yield relations as
their output. Relational algebra is performed recursively on a relation and intermediate
results are also considered relations.
The fundamental operations of relational algebra are as follows −
Select
Project
Union
Set different
Cartesian product
Rename
We will discuss all these operations in the following sections.
Select Operation (σ)
It selects tuples that satisfy the given predicate from a relation.
Notation − σp(r)
Where σ stands for selection predicate and r stands for relation. p is prepositional logic
formula which may use connectors like and, or, and not. These terms may use relational
operators like − =, ≠, ≥, < , >, ≤.
For example −
σsubject = "database"(Books)
Output − Selects tuples from books where subject is 'database'.
σsubject = "database" and price = "450"(Books)
Output − Selects tuples from books where subject is 'database' and 'price' is 450.
σsubject = "database" and price = "450" or year > "2010"(Books)
Output − Selects tuples from books where subject is 'database' and 'price' is 450 or those
books published after 2010.
Project Operation (∏)
It projects column(s) that satisfy a given predicate.
Notation − ∏A1, A2, An (r)
Where A1, A2 , An are attribute names of relation r.
Duplicate rows are automatically eliminated, as relation is a set.
For example −
∏subject, author (Books)
Selects and projects columns named as subject and author from the relation Books.
Union Operation (∪)
It performs binary union between two given relations and is defined as −
r ∪ s = { t | t ∈ r or t ∈ s}
Notation − r U s
Where r and s are either database relations or relation result set (temporary relation).
For a union operation to be valid, the following conditions must hold −
r, and s must have the same number of attributes.
Attribute domains must be compatible.
Duplicate tuples are automatically eliminated.
∏ author (Books) ∪ ∏ author (Articles)
Output − Projects the names of the authors who have either written a book or an article or
both.
Set Difference (−)
The result of set difference operation is tuples, which are present in one relation but are not
in the second relation.
Notation − r − s
Finds all the tuples that are present in rbut not in s.
∏ author (Books) − ∏ author (Articles)
Output − Provides the name of authors who have written books but not articles.
Cartesian Product (Χ)
Combines information of two different relations into one.
Notation − r Χ s
Where r and s are relations and their output will be defined as −
r Χ s = { q t | q ∈ r and t ∈ s}
σauthor = 'tutorialspoint'(Books Χ Articles)
Output − Yields a relation, which shows all the books and articles written by tutorialspoint.
Rename Operation (ρ)
The results of relational algebra are also relations but without any name. The rename
operation allows us to rename the output relation. 'rename' operation is denoted with small
Greek letter rho ρ.
Notation − ρ x (E)
Where the result of expression E is saved with name of x.
Additional operations are −
Set intersection
Assignment
Natural join
Relational Calculus
In contrast to Relational Algebra, Relational Calculus is a non-procedural query language,
that is, it tells what to do but never explains how to do it.
Relational calculus exists in two forms −
Tuple Relational Calculus (TRC)
Filtering variable ranges over tuples
Notation − {T | Condition}
Returns all tuples T that satisfies a condition.
For example −
{ T.name | Author(T) AND T.article = 'database' }
Output − Returns tuples with 'name' from Author who has written article on 'database'.
TRC can be quantified. We can use Existential (∃) and Universal Quantifiers (∀).
For example −
{ R| ∃T ∈ Authors(T.article='database' AND R.name=T.name)}
Output − The above query will yield the same result as the previous one.
Domain Relational Calculus (DRC)
In DRC, the filtering variable uses the domain of attributes instead of entire tuple values (as
done in TRC, mentioned above).
Notation −
{ a1, a2, a3, ..., an | P (a1, a2, a3, ... ,an)}
Where a1, a2 are attributes and P stands for formulae built by inner attributes.
For example −
{< article, page, subject > | ∈ TutorialsPoint ∧ subject = 'database'}
Output − Yields Article, Page, and Subject from the relation TutorialsPoint, where subject
is database.
Just like TRC, DRC can also be written using existential and universal quantifiers. DRC also
involves relational operators.
The expression power of Tuple Relation Calculus and Domain Relation Calculus is
equivalent to Relational Algebra
What is the Basic Structure of SQL Queries?
The fundamental structure of SQL queries includes three clauses that are select, from, and
where clause. What we want in the final result relation is specified in the select clause. Which
relations we need to access to get the result is specified in from clause. How the relation must
be operated to get the result is specified in the whereclause.
select A1, A2, . . . , An
from r1, r2, . . . , rm
where P;
In the select clause, you have to specify the attributes that you want to see in the result
relation
In the from clause, you have to specify the list of relations that has to be accessed for
evaluating the query.
In the where clause involves a predicate that includes attributes of the relations that
we have listed in the from clause.
Though the SQL query has a sequence select, from, and where. To understand how the query
will operate? You must consider the query in the order, from, where and then focus on select.
So with the help of these three clauses, we can retrieve the information we want out of the
huge set of data. Let us begin with the structure of queries imposed on the single relation.
Queries on Single Relation
Consider that we have a relation ‘instructor’ with the attributes instr_id, name, dept_name,
and salary. Now we want the names of all the instructors along with their corresponding
department names.
The SQL query we would structure to get a result relation with instructor’s names along with
their department name.
select name,
from instructor;
Observe that here we have not included where clause in the query above as we want the
name of all the instructors with their department name. So there is no need of imposing any
condition.
Now, if we have asked only for the dept_name in the above query by default it would have
listed names of all the departments retaining the duplicates. To eliminate the duplicates you
can make use of the distinctkeyword.
select distinct dept_name
from instructor;
In where clause we can make use of logical connectives and, or & not. Along with these, we
can include expressions where we can use comparison operators to compare operands in the
expression. This was a query imposed on a single relation now let’s move ahead and discuss
queries requested on multiple relations.
Queries on Multiple Relation
The SQL queries often need to access multiple relations from the data set in order to get the
required result. Let us take an example we have two relations instructor and department.
Now, if you want to retrieve the names of all the instructors along with their department
names and the corresponding department building. We will get the instructor’s name and
department name in the instructor relation but building name in the department relation. So
the query would be:
select name, instructor.dept_name, building
from instructor, department
where instructor.dept_name= department.dept_name;
Here department name of each tuple of instructor relation will be matched with the
department name of each tuple of department relation. Observe that we have used relation
name as a prefix to the attribute name in where clause, as both the attributes to be compared
in where clause has the same name. The result of the query above is:
The from the clause in the queries with multiple relations act as a Cartesian product of the
relations present the from clause.
Consider the relation instructor and teaches and the Cartesian product between the two can be
expressed as:
(instructor.ID, instructor.name, instructor.dept_name, instructor.salary, teaches.ID,
teaches.course id, teaches.sec id, teaches.semester, teaches.year)
The prefix should not be added to the attributes that are only in one of the relations. So this
would be like:
(instructor.ID, name, dept_name, salary
teaches.ID, course id, sec id, semester, year)
The Cartesian product combines each tuple of instructor relation to every tuple of teaches
relation. This Cartesian product results in extremely large relations which are hardly of any
use. So, it is the where clause that restricts the unnecessary combinations, and let’s retain the
meaningful combination that will help in retrieving the desired result.
SQL Set Operation
The SQL Set operation is used to combine the two or more SQL SELECT statements.
Types of Set Operation
Union
UnionAll
Intersect
Minus
1. Union
The SQL Union operation is used to combine the result of two or more SQL SELECT
queries.
In the union operation, all the number of datatype and columns must be same in both
the tables on which UNION operation is being applied.
The union operation eliminates the duplicate rows from its resultset.
Syntax
SELECT column_name FROM table1
UNION
SELECT column_name FROM table2;
Example:
The First table
ID NAME
1 Jack
2 Harry
3 Jackson
The Second table
ID NAME
3 Jackson
4 Stephan
5 David
Union SQL query will be:
SELECT * FROM First
UNION
SELECT * FROM Second;
The resultset table will look like:
ID NAME
1 Jack
2 Harry
3 Jackson
4 Stephan
5 David
David
2. Union All
Union All operation is equal to the Union operation. It returns the set without removing
duplication and sorting the data.
Syntax:
SELECT column_name FROM table1
UNION ALL
SELECT column_name FROM table2;
Example: Using the above First and Second table.
Union All query will be like:
SELECT * FROM First
UNION ALL
SELECT * FROM Second;
The resultset table will look like:
ID NAME
1 Jack
2 Harry
3 Jackson
3 Jackson
4 Stephan
5 David
3. Intersect
It is used to combine two SELECT statements. The Intersect operation returns the
common rows from both the SELECT statements.
In the Intersect operation, the number of datatype and columns must be the same.
It has no duplicates and it arranges the data in ascending order by default.
Syntax
SELECT column_name FROM table1
INTERSECT
SELECT column_name FROM table2;
Example:
Using the above First and Second table.
Intersect query will be:
SELECT * FROM First
INTERSECT
SELECT * FROM Second;
The resultset table will look like:
ID NAME
3 Jackson
4. Minus
It combines the result of two SELECT statements. Minus operator is used to display
the rows which are present in the first query but absent in the second query.
It has no duplicates and data arranged in ascending order by default.
Syntax:
SELECT column_name FROM table1
MINUS
SELECT column_name FROM table2;
Example
Using the above First and Second table.
Minus query will be:
SELECT * FROM First
MINUS
SELECT * FROM Second;
The resultset table will look like:
ID NAME
1 Jack
2 Harry