4.UNIT 4
4.UNIT 4
UNIT – IV
Introduction to the Relational Model – Integrity Constraint Over relations, Enforcing Integrity constraints,
Querying relational data, Logical data base Design, Introduction to views, Destroying /altering Tables and
Views.
Relational Algebra – Selection and projection set operations, Renaming, Joins, Division, Examples of
Algebra overviews, Relational calculus, Tuple relational Calculus, Domain relational calculus, Expressive
Power of Algebra and calculus
COURSE OBJECTIVES:
To know the concepts of Relational model and Relational Algebra.
COURSE OUTCOMES:
✓ Understand the concepts of Relational model and Relational Algebra.
Relational Model is the primary data model that is used widely around the world for data storage and
processing. Relational Model represents the database as a collection of relations. A relation is nothing but a
table of values. Each relation/table is a collection of rows (records/tuples) and columns (attributes/fields).
Every row in the table represents a collection of related data values.
1. Tables: In the Relational model the relations are saved in the table format. A table has two properties
rows and columns. Rows represent records and columns represent attributes.
2. Record/Tuple: Each row of a table is known as record/tuple. It contains data of the relation.
3. Attribute: Each column in a table is known as an attribute. Attributes are the properties which define
a relation. e.g., Student_Rollno, NAME,etc.
4. Domain Value: It contains a set of atomic values that an attribute can take.
5. Relation Schema: A relation schema represents the name of the relation with its attributes.
6. Degree: The total number of attributes which in the relation is called the degree of the relation.
7. Cardinality: Total number of rows present in the Table.
8. Relation key: Every row has one, two or multiple attributes, which are unique, are known as relation
key.
Properties of a Relation:
• Each relation has a unique name by which it is identified in the database.
• Each attribute contains a distinct name Relation does not contain duplicate tuples.
• The tuples of a relation have no specific order.
1. Domain constraints: Domain constraints can be defined as the definition of a valid set of values for an
attribute. The value of the attribute must be available in the corresponding domain.
Example:
SNo SName Age MobileNo
101 Ravi 20 9966012345
102 Rani 21 9848012345
103 Raju 19 9849012345
104 Ramu A 9963012345
105 Rajesh 19 9866012345
2
Lecture Notes for DBMS
Not Allowed, as Age is an integer attribute (For SNo 104, Age is not an integer).
2. Entity integrity constraints: The entity integrity constraint states that primary key value can't be null.
This is because the primary key value is used to identify individual rows in relation and if the primary
key has a null value, then we can't identify those rows. A table can contain a null value other than the
primary key field.
Example:
SNo SName Age MobileNo
101 Ravi 20 9966012345
102 Rani 21 9848012345
Raju 19 9849012345
104 Ramu A 9963012345
105 Rajesh 19 9866012345
Not Allowed, as Primary key (SNo) can not contain a NULL value.
3. Key constraints: Keys are used to uniquely identify records of a relation. A relation can have multiple
keys, but out of which one key will be the primary key.
Example:
SNo SName Age MobileNo
101 Ravi 20 9966012345
102 Rani 21 9848012345
103 Raju 19 9849012345
102 Ramu A 9963012345
105 Rajesh 19 9866012345
Not Allowed, as Primary key (SNo) does not contain UNIQUE values.
4. Referential Integrity Constraints: A referential integrity constraint is specified between two tables. In
the Referential integrity constraints, if a foreign key in Table 1 refers to the Primary Key of Table 2, then
every value of the Foreign Key in Table 1 must be null or be available in Table 2.
3
Lecture Notes for DBMS
3. Primary key: Both unique and not null Ex: dept_name vachar(20) primary key;
or
primary key (dept_name);
4
Lecture Notes for DBMS
4. Check clause: The check clause permits domain values to be restricted with user specific
constraints/conditions.
Syntax: check (P), where P is a predicate
Ex: ensure that semester is one of fall, winter, spring or summer:
create table section ( course_id varchar (8), sec_id varchar (8), semester varchar (6),
year number (4), primary key (course_id, sec_id, semester, year),
check (semester in (’Fall’, ’Winter’, ’Spring’, ’Summer’)));
5. Default: SQL allows a default value to be specified for an attribute as illustrated by the following create
table statement: create table student (ID number (5), name varchar (20) not null, deptname varchar (20),
totcred number (3) default 0, primary key (ID));
The default value of the totcred attribute is declared to be 0. As a result, when a tuple is inserted into the
student relation, if no value is provided for the totcred attribute, its value is set to 0. The following insert
statement illustrates how an insertion can omit the value for the tot cred attribute. insert into student(ID,
name, deptname) values (’12789’, ’Newman’, ’Comp. Sci.’);
6. Referential Integrity: Referential Integrity rule in DBMS is based on Primary and Foreign Key. The
Rule defines that a foreign key have a matching primary key. Reference from a table to another table
should be valid. For example, the dept_name in the Course table has a matching valid dept_name in the
Department table.
Ex: Create table Course (course_id varchar (8), course_name varchar (25), dept_name varchar (20),
credits number (3) check (credits > 0), primary key (course_id), foreign key (dept_name)
references department)
1. Procedural Query Language: A procedural query language will have set of queries instructing the
DBMS to perform various transactions in the sequence to meet the user request. Procedural query
language tells the database what is required from the database and how to get them from the database.
Relational algebra and SQL are generally used procedural query languages.
5
Lecture Notes for DBMS
2. Non-Procedural Query Language: Non-procedural query languages inform what to do with the tables,
but don’t inform how to accomplish this. These will have single query on one or more tables to get result
from the database. Relational Calculus is a non procedural language.
LOGICAL DATA BASE DESIGN: Logical database design is the process of deciding how to
arrange the attributes of the entities into the tables of a relational database. The goal of logical database
design is to create well structured tables that properly reflect the user’s requirements.
The tables will be able to store data in a non-redundant manner and foreign keys will be placed in the tables
so that all the relationships among the entities will be supported.
INTRODUCTION TO VIEWS: Views in SQL are considered as a virtual table. A view also contains
rows and columns. To create the view, we can select the fields from one or more tables present in the
database. A view can either have specific rows based on certain condition or all the rows of a table.
Creating View: A view can be created using the Create View statement. We can create a view from a single
table or multiple tables.
Syntax: CREATE VIEW view_name AS
SELECT column1, column2.....
FROM table_name
[WHERE condition];
Ex: In this example, we create a View named StudentDetailsView from the table Student.
CREATE VIEW StudentDetailsView AS
SELECT Name, Address FROM Student;
Query to see the student details in the above View is: SELECT * FROM StudentDetailsView;
DESTROYING TABLES AND VIEWS: A table/view can be destroyed/deleted using the Drop statement.
Syntax to Delete a Table: DROP Table Table_name;
Ex: To delete Course Table,
DROP Table Course;
6
Lecture Notes for DBMS
ALTERING TABLES AND VIEWS: The Alter command is used to alter the tables and views.
Altering Tables: The ALTER TABLE statement is used to add, delete, or modify columns in an existing
table.
1) Syntax to Alter a Table to ADD a Column/Attribute:
ALTER TABLE table_name ADD column_name datatype;
Ex: ALTER TABLE Student ADD Address Varchar(150);
Altering Views:
Syntax to Alter a View: ALTER VIEW view_name AS
SELECT column1, column2.....
FROM table_name
[WHERE condition];
Selection Operation (σ): The select operation selects records/tuples that satisfy a given predicate.
Predicate/condition that can use connectors like AND, OR and NOT and relational operators like =, ≠, ≥,
<, >, ≤.
7
Lecture Notes for DBMS
Projection Operation (∏): This operation obtains the list of attributes that user wish to view in the result.
Syntax: ∏ A1, A2, . . . , An (relation)
Where A1, A2, An are used as attribute names of relation.
Ex: ∏subject, author (Books)
Output: The above query Selects and projects columns named as subject and author from the relation Books.
Set Operations:
1. Union Operation (U): It performs binary union between two given relations Syntax: r
U s, where r and s are relations
2. Set Intersection (∩): The set intersection operation contains all records that are common in both the
relations.
Syntax: r ∩ s, where r and s are relations
3. Set Difference (−): The set difference operation generates records/tuples, which are present in one
relation but are not in the second relation.
Syntax: r - s, where r and s are relations
Ex: ∏ author (Books) − ∏ author (Articles)
Output: Projects the names of the authors who have written books but not written any articles.
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 ρ.
Syntax: ρ x (E), where the result of expression E is saved with name of x.
8
Lecture Notes for DBMS
Joins: A Join operation combines related records/tuples from different relations, if and only if a given join
condition is satisfied. It is denoted by .
Syntax: r s, where r and s are relations Ex:
EMPLOYEE SALARY
1. Natural Join: If we join relations R and S on equal condition then it is called natural join or equi join.
Generally, join is referred to as natural join. It is denoted by . Example: Consider Table R
RegNo Branch Section
1 CSE A
2 ECE B
3 CIVIL A
4 IT B
5 IT A
Table S
Name RegNo
Ravi 1
Bhanu 2
Raju 3
Priya 4
Ramu 5
Natural join of R and S is − {we select those tuples from cartesian product where R.regno=S.regno}
R S
RegNo Branch Section Name
1 CSE A Ravi
2 ECE B Bhanu
3 CIVIL A Raghu
4 IT B Priya
5 IT A Ramu
9
Lecture Notes for DBMS
Outer Join: It is an extension of natural join to deal with missing values of relation. Types are:
1. Left Outer Join: Left outer join is a join operation that occurs when the relation R on the left contains
more records than the relation S on the right. In the left outer join, tuples in R have no matching tuples
in S. It is denoted by ⟕.
Example: Consider Table R
RegNo Branch Section
1 CSE A
2 ECE B
3 CIVIL A
4 IT B
5 IT A
Table S
Name RegNo
Ravi 1
Bhanu 2
Raju 3
Left Outer Join of Rand S is: R ⟕ S
RegNo Branch Section Name
1 CSE A Ravi
2 ECE B Bhanu
3 CIVIL A Raghu
4 IT B NULL
5 IT A NULL
2. Right Outer Join: Right outer join is a join operation that occurs when the relation S on the right contains
more records than the relation R on the left. In right outer join, tuples in S have no matching tuples in R.
It is denoted by ⟖.
Example: Consider Table R
RegNo Branch Section
1 CSE A
2 ECE B
3 CIVIL A
4 IT B
Table S
10
Lecture Notes for DBMS
Name RegNo
Ravi 1
Bhanu 2
Raju 3
Priya 4
Ramu 5
3. Full Outer Join: Full outer join is like a left or right join except that it contains all rows from both tables.
In full outer join, tuples in R that have no matching tuples in S and tuples in S that have no matching
tuples in R in their common attribute name. It is denoted by ⟗.
Table S
Name RegNo
Raju 3
Priya 4
Ramu 5
1 CSE A NULL
2 ECE B NULL
3 CIVIL A Raghu
4 NULL NULL Priya
5 IT A Ramu
Division Operation (÷): The division operator is used for queries which involve the ‘all’. For relations R
and S R ÷ S = tuples of R associated with all tuples of S.
Ex: Retrieve the name of the subject that is taught in all courses.
Name Course
System B.E
Database M.E
Database B.E
Output:
Algebra B.E
Name
Database
Course
B.E
M.E
The resulting operation must have all combinations of tuples of relation S that are present in the first relation
or R.
12
Lecture Notes for DBMS
Many of the relational calculus expressions involves the use of Quantifiers. There are two types of
quantifiers:
1. Universal Quantifiers: The universal quantifier denoted by ∀ is read as for all which means that in
a given set of records exactly all records satisfy a given condition.
2. Existential Quantifiers: The existential quantifier denoted by ∃ is read as for all which means that
in a given set of records there is at least one occurrences whose value satisfy a given condition.
DOMAIN RELATIONAL CALCULUS: In domain relational calculus, filtering variable uses the domain
of attributes. Domain relational calculus uses the same operators as tuple calculus. It uses logical connectives
∧ (and), ∨ (or) and ┓ (not). It uses Existential (∃) and Universal Quantifiers (∀) to bind the variable. The
13
Lecture Notes for DBMS
Where a1, a2 are attributes and P stands for formula built by inner attributes
14