0% found this document useful (0 votes)
25 views

dbms unit2

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)
25 views

dbms unit2

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/ 22

UNIT - II

2.1 Introduction to Relational Model:


The main construct for representing data in the relational model is a relation. A
relation consists of a relation schema and a relation instance. The relation instance is a
table, and the relation schema describes the column heads for the table. We first describe
the relation schema and then the relation instance. The schema species the relation's name,
the name of each field (or column, or attribute) and the domain of each field. A domain
is referred to in a relation schema by the domain name and has a set of associated
values.
We use the example of student information:
Students (sid: string, name: string, login: string, age: integer, gpa:real)
This says, for instance, that the field named sid has a domain named string. The set of
values associated with domain string is the set of all character strings.We now turn to the
instances of a relation. An instance of a relation is a set of tuples, also called records, in
which each tuple has the same number of fields as the relation schema. A relation instance
can be thought of as a table in which each tuple is a row, and all rows have the same
number of fields. (The term relation instance is often abbreviated to just relation, when
there is no confusion with other aspects of a relation such as its schema.)
Fields (Attributes, columns) Field names

sid name login age gpa


50000 Jones Jones@cs 19 1.8
53666 Smith Smith@cs 21 2.0
53668 Roy Roy@cs 18 3.2
54532 Mealey Mealey@cs 20 3.8
54566 Rony Rony@cs 14 3.4
53452 Guldu Guldu@cs 12 3.3
An instance of the Students relation Tuples (Records, rows)
Table 2.1
The instance S1 contains six tuples and has, as we expect from the schema, five fields. Note
that no two rows are identical. This is a requirement of the relational model each relation is
defined to be a set of unique tuples or rows.1. The order in which the rows are listed is not
important. Thus sid is field 1 of Students, login is field 3,and so on. If this convention is
used, the order of fields is significant.
Most database systems use a combination of these conventions. For example, in SQL the
named fields convention is used in statements that retrieve tuples, and the ordered fields
convention is commonly used when inserting tuples.
A relation schema specifies the domain of each field or column in the
relation instance.
These domain constraints in the schema specify an important condition that we want each
instance of the relation to satisfy: The values that appear in a column must be drawn from
the domain associated with that column. Thus, the domain of a field is essentially the type
of that field, in programming language terms, and restricts the values that can appear in
the field.
The degree, also called arity, of a relation is the number of fields. The cardinality
of a relation instance is the number of tuples in it. In Figure the degree of the relation (the
number of columns) is five, and the cardinality of this instance is six. A relational
database is a collection of relations with distinct relation names. The relational database
schema is the collection of schemas for the relations in the database.
Creating and Modifying relations using SQL:
The SQL that supports the creation, deletion, and modification of tables is called the
Data Definition Language (DDL). Now, we will just consider domains that are built-in
types, such as integer.
The CREATE TABLE statement is used to define a new table. To create the Students
relation, we can use the following statement:
CREATE TABLE Students (sid CHAR (20),
name CHAR (30),
login CHAR (20),
age INTEGER,
gpa REAL)
Tuples are inserted using the INSERT command. We can insert a single tuple into the
Students table as follows:
INSERT INTO Students (sid, name, login, age, gpa)
VALUES (53688, `Smith', `smith@ee', 18, 3.2)
We can optionally omit the list of column names in the INTO clause and list the values
in the appropriate order, but it is good style to be explicit about column names. We can
delete tuples using the DELETE command. We can delete all Students tuples with name
equal to Smith using the command:
DELETE FROM Students S WHERE S.name = `Smith'
We can modify the column values in an existing row using the UPDATE command. For
example, we can increment the age and decrement the gpa of the student with sid 53668:
UPDATE Students S
SET S.age = S.age + 1, S.gpa = S.gpa - 1
WHERE S.sid = 53688
These examples illustrate some important points. The WHERE clause is applied first and
determines which rows are to be modified. The SET clause then determines how these rows
are to be modified. If the column that is being modified is also used to determine the new
value, the value used in the expression on the right side of equals (=) is the old value, that
is, before the modification. To illustrate these points further,
consider the following variation of the previous query:
UPDATE Students S
SET S.gpa = S.gpa - 0.1
WHERE S.gpa >= 3.3
If this query is applied on the instance S1 of Students shown in Figure, we obtain the
instance shown in following figure

sno name login age gpa


50000 Jones Jones@cs 19 3.2
53666 Smith Smith@cs 21 3.3
53668 Roy Roy@cs 13 3.2
54532 Mealey Mealey@cs 15 3.7
54566 Rony Rony@cs 11 1.8
53452 Guldu Guldu@cs 12 2.0
Students Instance after update
Table 2.2

2.2 INTEGRITY CONSTRAINTS OVER RELATIONS:


A database is only as good as the information stored in it, and a DBMS must
therefore help prevent the entry of incorrect information. An integrity constraint (IC) is a
condition that is specified on a database schema, and restricts the data that can be stored
in an instance of the database. If a database instance satisfies all the integrity constraints
specified on the database schema, it is a legal instance. A DBMS enforces integrity
constraints, in that it permits only legal instances to be stored in the database.
Integrity constraints are specified and enforced at different times:
1. When the DBA or end user defines a database schema, he or she specifies the ICs that
must hold on any instance of this database.
2. When a database application is run, the DBMS checks for violations and disallows
changes to the data that violate the specified ICs. (In some situations, rather than disallow
the change, the DBMS might instead make some compensating changes to the data to
ensure that the database instance satisfies all ICs. In any case, changes to the database are
not allowed to create an instance that violates any IC.)
Many kinds of integrity constraints can be specified in the relational model.

TYPES OF INTEGRITY CONSTRAINTS


Various types of integrity constraints are-
1. Domain Integrity
2. Entity Integrity Constraint
3. Referential Integrity Constraint
4. Key Constraints
2.2.1. Domain Integrity- Domain integrity means the definition of a valid set of values for
an attribute. You define data type, length or size, is null value allowed, is the value unique
or not for an attribute, the default value, the range (values in between) and/or specific
values for the attribute.

2.2.2. Entity Integrity Constraint- This rule states that in any database relation value of
attribute of a primary key can't be null.

EXAMPLE- Consider a relation "STUDENT" Where "Stu_id" is a primary key and it must not
contain any null value whereas other attributes may contain null value e.g "Branch" in the
following relation contains one null value.
Stu_id Name Branch
113421 Ajay IT
113564 Sandy CSE
112453 Sujith
114532 Rose CIVIL
113422 Jack IT
Table 2.3
2.2.3. Referential Integrity Constraint-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 home
relation or the foreign key value must be null.
The rules are:
1. You can't delete a record from a primary table if matching records exist in a related table.
2. You can't change a primary key value in the primary table if that record has related
records.
3. You can't enter a value in the foreign key field of the related table that doesn't exist in
the primary key of the primary table.
4. However, you can enter a Null value in the foreign key, specifying that the records are
unrelated.

EXAMPLE-Consider 2 relations "stu" and "stu_1" Where "Stu_id " is the primary key in the
"stu" relation and foreign key in the "stu_1" relation.

Relation "stu”

Stu_id Name Branch


113421 Ajay IT
113564 Sandy CSE
112453 Sujith ECE
114532 Rose CIVIL
113422 Jack IT
Table 2.4

Relation "stu_1"
Stu_id Course Duration
113421 B.Tech 4 years
113564 B.Tech 4 years
112453 B.Tech 4 years
114532 B.Tech 4 years
113422 B.Tech 4 years
Table 2.5
Examples:

Rule 1. You can't delete any of the rows in the ”stu” relation that are visible since all the
”stu” are in use in the “stu_1” relation.
Rule 2. You can't change any of the ”Stu_id” in the “stu” relation since all the “Stu_id” are
in use in the ”stu_1” relation.
Rule 3. The values that you can enter in the” Stu_id” field in the “stu_1” relation must be in
the” Stu_id” field in the “stu” relation.
Rule 4 You can enter a null value in the "stu_1" relation if the records are unrelated.

2.2.4. Key Constraints- A Key Constraint is a statement that a certain minimal subset of
the fields of a relation is a unique identifier for a tuple.
SQL Constraints are rules used to limit the type of data that can go into a table, to maintain
the accuracy and integrity of the data inside table.
Constraints can be divided into following two types,
 Column level constraints: limits only column data. The style is called Inline
specification.
 Table level constraints: limits whole table data. The style is called out of line
specification
Constraints are used to make sure that the integrity of data is maintained in the database.
Following are the most used constraints.
 NOT NULL
 UNIQUE
 PRIMARY KEY
 FOREIGN KEY
 UNIQUE
2.2.4.1 NOT NULL Constraint:
NOT NULL constraint restricts a column from having a NULL value. Once NOT NULL
constraint is applied to a column, you cannot pass a null value to that column. It enforces a
column to contain a proper value. One important point to note about NOT NULL constraint is
that it cannot be defined at table level.
Example using NOT NULL constraint:
CREATE table Student (s_id int NOT NULL, Name varchar(60), Age int);
The above query will declare that the s_id field of Student table will not take NULL value.
2.2.4.2UNIQUE Constraint:
UNIQUE constraint ensures that a field or column will only have unique values. A UNIQUE
constraint field will not have duplicate data. UNIQUE constraint can be applied at column
level or table level.
Example using UNIQUE constraint when creating a Table (Table Level)
CREATE table Student(s_id int NOT NULL UNIQUE, Name varchar(60), Age int);
The above query will declare that the s_id field of Student table will only have unique
values and won’t take NULL value.
Example using UNIQUE constraint after Table is created (Column Level)
ALTER table Student add UNIQUE(s_id);
The above query specifies that s_id field of Student table will only have unique value.

2.2.4.3 Primary Key Constraint:


Primary key constraint uniquely identifies each record in a database. A Primary Key must
contain unique value and it must not contain null value. Usually Primary Key is used to
index the data inside the table.
Example using PRIMARY KEY constraint at Table Level:
CREATE table Student (s_id int PRIMARY KEY, Name varchar(60) NOT NULL, Age int);
The above command will creates a PRIMARY KEY on the s_id.
Example using PRIMARY KEY constraint at Column Level:
ALTER table Student add PRIMARY KEY (s_id);
The above command will creates a PRIMARY KEY on the s_id.

2.2.4.4 Foreign Key Constraint:


FOREIGN KEY is used to relate two tables. FOREIGN KEY constraint is also used to restrict
actions that would destroy links between tables. To understand FOREIGN KEY, let's see it
using two table.
Customer_detail table: Order_detail table:
C_id Customer_name Address O_id Order_name C_id
101 Ajay Delhi 10 Order1 101
102 Adam Kolkata 11 Order2 102
103 Anand Noidak 12 Order3 103
Table 2.6 Table 2.7
In Customer_Detail table, c_id is the primary key which is set as foreign key in
Order_Detail table. The value that is entered in c_id which is set as foreign key in
Order_Detail table must be present in Customer_Detail table where it is set as primary
key. This prevents invalid data to be inserted into c_id column of Order_Detail table.

Example using FOREIGN KEY constraint at Table Level:


CREATE table Order_Detail(order_id int PRIMARY KEY,order_name varchar(60) NOT NULL,
c_id int FOREIGN KEY REFERENCES Customer_Detail(c_id));
In this query, c_id in table Order_Detail is made as foriegn key, which is a reference of c_id
column of Customer_Detail.

Example using FOREIGN KEY constraint at Column Level:


ALTER table Order_Detail add FOREIGN KEY (c_id) REFERENCES Customer_Detail(c_id);

Behaviour of Foriegn Key Column on Delete:


There are two ways to maintin the integrity of data in Child table, when a particular record
is deleted in main table. When two tables are connected with Foriegn key, and certain data
in the main table is deleted, for which record exit in child table too, then we must have
some mechanism to save the integrity of data in child table.

 On Delete Cascade : This will remove the record from child table, if that value of
foriegn key is deleted from the main table.
 On Delete Null : This will set all the values in that record of child table as NULL, for
which the value of foriegn key is deleted from the main table.
 If we don't use any of the above, then we cannot delete data from the main table for
which data in child table exists. We will get an error if we try to do so.
ERROR : Record in child table exist
2.2.4.5 CHECK Constraint:
CHECK constraint is used to restrict the value of a column between a range. It performs
check on the values, before storing them into the database. Its like condition checking
before saving data into a column.
Example using CHECK constraint at Table Level:
create table Student(s_id int NOT NULL CHECK(s_id > 0),
Name varchar(60) NOT NULL, Age int);
The above query will restrict the s_id value to be greater than zero.
Example using CHECK constraint at Column Level:
ALTER table Student add CHECK(s_id > 0);

2.3 QUERYING RELATIONAL DATA


A relational database query is a question about the data, and the answer consists
of a new relation containing the result. For example, we might want to find all students
younger than 18 or all students enrolled in Reggae203. A query language is a specialized
language for writing queries.
SQL is the most popular commercial query language for a relational DBMS. We now present
some SQL examples that illustrate how easily relations can be queried. Consider the
instance of the Students relation shown figure . We can retrieve rows corresponding to
students who are younger than 18 with the following SQL query:
SELECT *FROM Students S
WHERE S.age < 18
The symbol * means that we retain all fields of selected tuples in the result. To understand
this query, think of S as a variable that takes on the value of each tuple in Students, one
tuple after the other. The condition S.age < 18 in the WHERE clause specifies that we want
to select only tuples in which the age field has a value less than 18. This query evaluates to
the relation shown in below figure.

sid name login age gpa


54566 Rony Rony@cs 14 3.4
53452 Guldu Guldu@cs 12 3.3
Table 2.8
Students with age < 18 on Instance S1
This example illustrates that the domain of a field restricts the operations that are permitted
on field values, in addition to restricting the values that can appear in the field. The
condition S.age < 18 involves an arithmetic comparison of an age value with an integer and
is permissible because the domain of age is the set of integers. In addition to selecting a
subset of tuples, a query can extract a subset of the fields of each selected tuple. We can
compute the names and logins of students who are younger than 18 with the following
query:
SELECT S.name, S.login FROM Students S WHERE S.age < 18

The solution to this query is the following figure:

name Login
Rony Rony@cs
Guldu Guldu@cs

2.4 LOGICAL DATABASE DESIGN: ER TO RELATIONAL

The ER model is convenient for representing an initial, high-level database design.


Given an ER diagram describing a database, there is a standard approach to generating a
relational database schema that closely approximates the ER design. It is discussed in 2nd
unit.
SQL Alias:
Alias is used to give an alias name to a table or a column. This is quite useful in case
of large or complex queries. Alias is mainly used for giving a short alias name for a column
or a table with complex names.

Syntax of Alias for table names,


SELECT column-name from table-nameas alias-name
Following is an Example using Alias,
SELECT * from Employee_detail as ed;
Alias syntax for columns will be like,
SELECT column-name as alias-name fromtable-name
Example using alias for columns,
SELECT customer_id as cid from Emp;
2.5 Introduction to views:
A view is nothing more than a SQL statement that is stored in the database
with a associated name. A view is actually a composition of a table in the form of a
predefined SQL query.
A view can contain all rows of a table or select rows from a table. A view can be created
from one or many tables which depends on the written SQL query to create a view.
Views, which are a type of virtual tables allow users to do the following −
 Structure data in a way that users or classes of users find natural or intuitive.
 Restrict access to the data in such a way that a user can see and (sometimes) modify
exactly what they need and no more.
 Summarize data from various tables which can be used to generate reports.
 View is a virtual table or logical table or it acts like a mirror image.
 View in practically contains no data by itself.
 The table upon which a view is based are called as base tables.
 Views can be created as object views or relational views.
 View is simply a select statement.
Creating Views:
Database views are created using the CREATE VIEW statement. Views can
be created from a single table, multiple tables or another view.
To create a view, a user must have the appropriate system privilege according to the specific
implementation.
The basic CREATE VIEW syntax is as follows −

CREATE VIEW view_name AS SELECT column1, column2.....


FROM table_name WHERE [condition];

You can include multiple tables in your SELECT statement in a similar way as you use them in
a normal SQL SELECT query.
Example: Consider the CUSTOMERS table having the following records −

+----+----------+-----+-----------+----------+
| ID | NAME | AGE | ADDRESS | SALARY |
+----+----------+-----+-----------+----------+
| 1 | Ramesh | 32 | Ahmedabad | 2000.00 |
| 2 | Khilan | 25 | Delhi | 1500.00 |
| 3 | kaushik | 23 | Kota | 2000.00 |
| 4 | Chaitali | 25 | Mumbai | 6500.00 |
| 5 | Hardik | 27 | Bhopal | 8500.00 |
| 6 | Komal | 22 | MP | 4500.00 |
| 7 | Muffy | 24 | Indore | 10000.00 |
+----+----------+-----+-----------+----------+

Following is an example to create a view from the CUSTOMERS table. This view would be used
to have customer name and age from the CUSTOMERS table.

SQL > CREATE VIEW CUSTOMERS_VIEW AS SELECT name, age FROM CUSTOMERS;

Now, you can query CUSTOMERS_VIEW in a similar way as you query an actual table.
Following is an example for the same.

SQL > SELECT * FROM CUSTOMERS_VIEW;

This would produce the following result.

+----------+-----+
| name | age |
+----------+-----+
| Ramesh | 32 |
| Khilan | 25 |
| kaushik | 23 |
| Chaitali | 25 |
| Hardik | 27 |
| Komal | 22 |
| Muffy | 24 |
+----------+-----+

WITH CHECK OPTION


The WITH CHECK OPTION is a CREATE VIEW statement option. The purpose of the WITH
CHECK OPTION is to ensure that all UPDATE and INSERTs satisfy the condition(s) in the view
definition.
If they do not satisfy the condition(s), the UPDATE or INSERT returns an error.
The following code block has an example of creating same view CUSTOMERS_VIEW with the
WITH CHECK OPTION.

CREATE VIEW CUSTOMERS_VIEW AS


SELECT name, age
FROM CUSTOMERS
WHERE age IS NOT NULL
WITH CHECK OPTION;
The WITH CHECK OPTION in this case should deny the entry of any NULL values in the view's
AGE column, because the view is defined by data that does not have a NULL value in the AGE
column.
Force View Creation
Force keyword is used while creating a view. This keyword force to create View even
if the table does not exist. After creating a force View if we create the base table and enter
values in it, the view will be automatically updated.
Syntax for forced View is,
CREATE or REPLACE force view view_name AS
SELECT column_name(s)
FROM table_name
WHERE condition
Read-Only View
We can create a view with read-only option to restrict access to the view.
Syntax to create a view with Read-Only Access
CREATE or REPLACE force view view_name AS
SELECT column_name(s)
FROM table_name
WHERE condition with read-only
The above syntax will create view for read-only purpose, we cannot Update or Insert data
into read-only view. It will throw an error.
Updating a View
A view can be updated under certain conditions which are given below −
 The SELECT clause may not contain the keyword DISTINCT.
 The SELECT clause may not contain summary functions.
 The SELECT clause may not contain set functions.
 The SELECT clause may not contain set operators.
 The SELECT clause may not contain an ORDER BY clause.
 The FROM clause may not contain multiple tables.
 The WHERE clause may not contain subqueries.
 The query may not contain GROUP BY or HAVING.
 Calculated columns may not be updated.
 All NOT NULL columns from the base table must be included in the view in order for the
INSERT query to function.
So, if a view satisfies all the above-mentioned rules then you can update that view. The
following code block has an example to update the age of Ramesh.

SQL > UPDATE CUSTOMERS_VIEW SET AGE = 35 WHERE name = 'Ramesh';

This would ultimately update the base table CUSTOMERS and the same would reflect in the
view itself. Now, try to query the base table and the SELECT statement would produce the
following result.

+----+----------+-----+-----------+----------+
| ID | NAME | AGE | ADDRESS | SALARY |
+----+----------+-----+-----------+----------+
| 1 | Ramesh | 35 | Ahmedabad | 2000.00 |
| 2 | Khilan | 25 | Delhi | 1500.00 |
| 3 | kaushik | 23 | Kota | 2000.00 |
| 4 | Chaitali | 25 | Mumbai | 6500.00 |
| 5 | Hardik | 27 | Bhopal | 8500.00 |
| 6 | Komal | 22 | MP | 4500.00 |
| 7 | Muffy | 24 | Indore | 10000.00 |
+----+----------+-----+-----------+----------+

Inserting Rows into a View


Rows of data can be inserted into a view. The same rules that apply to the UPDATE command
also apply to the INSERT command.
Here, we cannot insert rows in the CUSTOMERS_VIEW because we have not included all the
NOT NULL columns in this view, otherwise you can insert rows in a view in a similar way as
you insert them in a table.
Deleting Rows from a View
Rows of data can be deleted from a view. The same rules that apply to the UPDATE and
INSERT commands apply to the DELETE command.
Following is an example to delete a record having AGE = 22.

SQL > DELETE FROM CUSTOMERS_VIEW WHERE age = 22;

This would ultimately delete a row from the base table CUSTOMERS and the same would
reflect in the view itself. Now, try to query the base table and the SELECT statement would
produce the following result.

+----+----------+-----+-----------+----------+
| ID | NAME | AGE | ADDRESS | SALARY |
+----+-------------+------+------------------+--------------+
| 1 | Ramesh | 35 | Ahmedabad | 2000.00 |
| 2 | Khilan | 25 | Delhi | 1500.00 |
| 3 | kaushik | 23 | Kota | 2000.00 |
| 4 | Chaitali | 25 | Mumbai | 6500.00 |
| 5 | Hardik | 27 | Bhopal | 8500.00 |
| 7 | Muffy | 24 | Indore | 10000.00 |
+----+----------+-----+-----------+----------+

Dropping Views
Obviously, where you have a view, you need a way to drop the view if it is no longer needed.
The syntax is very simple and is given below −
DROP VIEW view_name;

Following is an example to drop the CUSTOMERS_VIEW from the CUSTOMERS table.

DROP VIEW CUSTOMERS_VIEW;

Types of View
There are two types of view,
 Simple View
 Complex View
Simple View Complex View
Created from one table Created from one or more table
Does not contain functions Contain functions
Does not contain groups of data Contains groups of data
2.6 DESTROYING/ALTERING TABLES AND VIEWS:
If we decide that we no longer need a base table and want to destroy it, we can use
the DROP TABLE command. For example, DROP TABLE Students RESTRICT destroys the
Students table unless some view or integrity constraint refers to Students; if so, the
command fails. If the keyword RESTRICT is replaced by CASCADE, Students is dropped and
any referencing views or integrity constraints are (recursively) dropped as well; one of
these two keywords must always be specified. A view can be dropped using the DROP VIEW
command, which is just like DROP TABLE.ALTER TABLE modifies the structure of an existing
table. To add a column called maiden-name to Students, for example, we would use the
following command:
ALTER TABLE Students ADD COLUMN maiden-name char(10)
The definition of Students is modified to add this column, and all existing rows are padded
with null values in this column. ALTER TABLE can also be used to delete columns and to add
or drop integrity constraints on a table; we will not discuss these aspects of the command
beyond remarking that dropping columns is treated very similarly to dropping tables or
views.
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 proposed by E. F. Codd in 1976.

2.7 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.
Terminology
 Relation – a set of tuples.
 Tuple – a collection of attributes which describe some real world entity.
 Attribute – a real world role played by a named domain.
 Domain – a set of atomic values.
 Set – a mathematical definition for a collection of objects which contains no duplicates.
Unary operations: Operates on one relation. These include:
 Selection
 Projection
 Renaming
Selection 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.
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.
Binary operations: Operates on pairs of relation. These include:
 Union
 Set Difference
 Cartesian product
 Division
 Joins
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.

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 r but 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 = 'tutorial'(Books Χ Articles)

Output − Yields a relation, which shows all the books and articles written by tutorial.
Division:
The division operator divides a dividend relation A of degree (means number of columns in a
relation) m + n by a divisor relation B of degree n, and produces a\ resultant relation of
degree m. It is denoted by ÷.

In this example dividend relation A has two attributes of Sno, Pno (of degree 2) and division
relation B has only one attribute Pno (of degree 1). Then, A divide by B gives a resultant
relation of degree 1. It means it has only one attribute of Sno.
A = SNO* SNO = SNO. B = PNO
The resultant relation has those tuples that are common values of those attributes, which
appears in the resultant attribute sno. For example, in CASE II,
P2 has Snos. -- Sl, S2, S3,S4 P4 has Snos. -- SI, S4
S1, S4 are the common supplier who supply both P2 and P4. So the resultant relation has
tuples S1 and S4.
In CASE III : There is only one supplier S1 who supply all the parts from PI to P6.
Joins:
We understand the benefits of taking a Cartesian product of two relations, which gives
us all the possible tuples that are paired together. But it might not be feasible for us in certain
cases to take a Cartesian product where we encounter huge relations with thousands of tuples
having a considerable large number of attributes.
Join is a combination of a Cartesian product followed by a selection process. A Join operation
pairs two tuples from different relations, if and only if a given join condition is satisfied.
We will briefly describe various join types in the following sections.
Theta (θ) Join:
Theta join combines tuples from different relations provided they satisfy the theta condition.
The join condition is denoted by the symbol θ.
Notation

R1 ⋈θ R2

R1 and R2 are relations having attributes (A1, A2, .., An) and (B1, B2,.. ,Bn) such that the
attributes don’t have anything in common, that is R1 ∩ R2 = Φ.
Theta join can use all kinds of comparison operators.
Student
SID Name Std
101 Alex 10
102 Maria 11
Subjects
Class Subject
10 Math
10 English
11 Music
11 Sports
Student_Detail −

STUDENT ⋈Student.Std = Subject.Class SUBJECT


Student_detail
SID Name Std Class Subject
101 Alex 10 10 Math
101 Alex 10 10 English
102 Maria 11 11 Music
102 Maria 11 11 Sports
Equijoin:
When Theta join uses only equality comparison operator, it is said to be equijoin. The
above example corresponds to equijoin.
Natural Join (⋈):
Natural join does not use any comparison operator. It does not concatenate the way
a Cartesian product does. We can perform a Natural Join only if there is at least one common
attribute that exists between two relations. In addition, the attributes must have the same
name and domain.
Natural join acts on those matching attributes where the values of attributes in both the
relations are same.
Courses
CID Course Dept
CS01 Database CS
ME01 Mechanics ME
EE01 Electronics EE

HoD
Dept Head
CS Alex
ME Maya
EE Mira

Courses ⋈ HoD
Dept CID Course Head
CS CS01 Database Alex
ME ME01 Mechanics Maya
EE EE01 Electronics Mira
Outer Joins: Theta Join, Equijoin, and Natural Join are called inner joins. An inner join
includes only those tuples with matching attributes and the rest are discarded in the resulting
relation. Therefore, we need to use outer joins to include all the tuples from the participating
relations in the resulting relation. There are three kinds of outer joins − left outer join, right
outer join, and full outer join.

Left Outer Join(R S):


All the tuples from the Left relation, R, are included in the resulting relation. If there are tuples
in R without any matching tuple in the Right relation S, then the S-attributes of the resulting
relation are made NULL.
Left
A B
100 Database
101 Mechanics
102 Electronics

Right
A B
100 Alex
102 Maya
104 Mira

Courses HoD
A B C D
100 Database 100 Alex
101 Mechanics --- ---
102 Electronics 102 Maya

Right Outer Join: ( R S ):


All the tuples from the Right relation, S, are included in the resulting relation. If there are
tuples in S without any matching tuple in R, then the R-attributes of resulting relation are
made NULL.
Courses HoD
A B C D
100 Database 100 Alex
102 Electronics 102 Maya
--- --- 104 Mira
Full Outer Join: ( R S):
All the tuples from both participating relations are included in the resulting relation. If there
are no matching tuples for both relations, their respective unmatched attributes are made
NULL.
Courses HoD
A B C D
100 Database 100 Alex
101 Mechanics --- ---
102 Electronics 102 Maya
--- --- 104 Mira

TEXT BOOK:
1. Raghurama Krishnan, Johannes Gehrke, Database Management Systems, 3rd Edition,
TATA McGraw hill.
2. Web pages

You might also like