0% found this document useful (0 votes)
11 views14 pages

4.UNIT 4

The document covers key concepts of the Relational Model and Relational Algebra, including integrity constraints, querying relational data, and logical database design. It explains the structure of relations, types of integrity constraints, and methods for enforcing these constraints. Additionally, it discusses querying techniques, views, and operations in relational algebra, such as selection, projection, and various types of joins.

Uploaded by

Sami Ahmed
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views14 pages

4.UNIT 4

The document covers key concepts of the Relational Model and Relational Algebra, including integrity constraints, querying relational data, and logical database design. It explains the structure of relations, types of integrity constraints, and methods for enforcing these constraints. Additionally, it discusses querying techniques, views, and operations in relational algebra, such as selection, projection, and various types of joins.

Uploaded by

Sami Ahmed
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 14

Lecture Notes for DBMS

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.

INTRODUCTION TO THE RELATIONAL MODEL

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.

A simple relation is represented as:

Concepts of Relational Model:


1
Lecture Notes for DBMS

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.

INTEGRITY CONSTRAINT OVER RELATIONS: Integrity constraints are a set of rules


used to maintain the quality of information. Integrity constraints ensure that the data insertion, updating, and
accessing have to be performed in such a way that data integrity is not affected and data is not lost.

Types of Integrity Constraint:


1. Domain Constraints
2. Entity Integrity Constraints
3. Key Constraints
4. Referential Integrity Constraint

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

ENFORCING INTEGRITY CONSTRAINTS: Constraints on a Single Relation are:


1. Not Null
2. Unique
3. Primary Key
4. Check (P ), where P is a predicate
5. Default
6. Referential Integrity

1. Not Null Constraint:


Ex: Declare name and budget to be not null
Create table customer (id number (5), name varchar (20) not null, budget number (12, 2) not null);

2. Unique Constraint: unique ( A1, A2, …, Am)


The unique specification states that the attributes A1, A2, … Am form a unique/candidate key. Note:
Unique keys are permitted to be null (in contrast to primary keys).

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)

QUERYING RELATIONAL DATA: Relational data is queried using Relational Query


Languages. Relational query languages are used to break the user requests and instruct the DBMS to execute
the requests. It is the language by which user communicates with the database. Types of relational query
languages include:

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 /ALTERING TABLES AND VIEWS:

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

Syntax to Delete a View: DROP View View_name;


Ex: To delete the View Course_View,
DROP View Course_View;

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);

2) Syntax to Alter a Table to ALTER/MODIFY a Column/Attribute:


ALTER TABLE table_name MODIFY column_name datatype;
Ex: ALTER TABLE Student MODIFY SName Varchar(45);

3) Syntax to Alter a Table to DROP a Column/Attribute:


ALTER TABLE table_name DROP COLUMN column_name;
Ex: ALTER TABLE Student DROP COLUMN Age;

Altering Views:
Syntax to Alter a View: ALTER VIEW view_name AS
SELECT column1, column2.....
FROM table_name
[WHERE condition];

Ex: ALTER VIEW StudentDetailsView AS SELECT Name, Address FROM Student;


RELATIONAL ALGEBRA: Relational algebra is a procedural query language. It provides detailed process
to obtain the result of the query. It uses arithmetic, relational and conditional operators to perform queries. It has
many operations to store, access and manipulate the data in the relations.

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

Syntax: σ predicate (relation)


Ex: σsubject = "database" (Books)
Output: The above query Selects records/tuples from books where subject is 'database'.

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

Ex: ∏ author (Books) U ∏ author (Articles)


Output: Projects the names of the authors who have either written a book or an article or both.

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

Ex: ∏ author (Books) ∩ ∏ author (Articles)


Output: Projects the names of the authors who have written both books and articles.

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

Different types of join operation include:


1. Natural join
2. Outer join − It is further classified into: Left outer join.
• Right outer join.
• Full outer join.

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

Right 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 Priya
5 NULL NULL Ramu

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 ⟗.

Example: Consider Table R


RegNo Branch Section
1 CSE A
2 ECE B
3 CIVIL A
5 IT A

Table S
Name RegNo
Raju 3
Priya 4
Ramu 5

Full Outer Join of Rand S is: R ⟗ S

RegNo Branch Section Name


11
Lecture Notes for DBMS

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.

RELATIONAL CALCULUS: Relational calculus is a non-procedural query language. In the


nonprocedural query language, the user is concerned with the details of how to obtain the end results. The
relational calculus tells what to do but never explains how to do. Most commercial relational languages are
based on aspects of relational calculus including SQL-QBE.

✓ It is based on Predicate calculus, where a predicate is a condition (truth-valued function with


arguments).

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.

Types of Relational calculus:


1. Tuple Relational Calculus
2. Domain Relational Calculus
TUPLE RELATIONAL CALCULUS (TRC): The tuple relational calculus is specified to select the tuples
in a relation. It is a non-procedural query language which is based on finding a number of tuple variables
(range variable) for which predicate holds true. In TRC, filtering variable uses the tuples of a relation. The
result of the relation can have one or more tuples.

A Query in the tuple relational calculus is expressed as:


Syntax: {T | P (T)} or {T | Condition (T)}
Where
T is the resulting tuples
P (T) is the condition used to fetch T.

Ex: {T.name | Author (T) AND T.article = 'database' }


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'.

✓ In TRC, we can use Existential (∃) and Universal Quantifiers (∀).

Ex: { R| ∃T ∈ Authors(T.article='database' AND R.name=T.name)} Output:


This query will yield the same result as the previous one.

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

QBE or Query by example is a query language related to domain relational calculus.


Syntax: {a1, a2, a3, ..., an | P (a1, a2, a3, ... ,an)}

13
Lecture Notes for DBMS

Where a1, a2 are attributes and P stands for formula built by inner attributes

Ex: {< article, page, subject > | ∈ Notes ∧ subject = 'database'}


Output: This query will yield the article, page, and subject from the relation Notes, where the subject is a
database.

EXPRESSIVE POWER OF ALGEBRA AND CALCULUS:


✓ Algebra and calculus have same expressive power, leading to the notion of relational completeness.
✓ Every query that can be expressed in relational algebra can be expressed as a safe query in DRC /
TRC; the converse is also true.
✓ Usually, the expressiveness of any given language is judged using relational algebra operations as a
standard.

14

You might also like