Unit2 2021
Unit2 2021
All data are uniquely identified and accessible via this identity.
Rule 3 : Systematic treatment of null values.
Non-subversion rule
Example:
instructor = (ID, name, dept_name, salary)
Formally, given sets D1, D2, …. Dn a relation r is a subset of Di
Tuples/ Records
(or rows)
Attribute/ Field Types
The set of allowed values for each attribute is called the
domain of the attribute
(Ex. ID number is a set of valid 9 digit number is a domain)
Relational Calculus
Relational Algebra
Relational Algebra is a procedural query language. It consists of
set of operations that take one or two relations as input and
produce a new relation as their result.
Relational Algebra is of following eight types:
1. Select (σ )
2. Project (Π )
3. Product (X)
4. Unions (U)
5. Differences (- )
6. Intersection ( )
7. Join ( )
8. Divide/ Division ( / )
Selection Operation
The selection operation selected records that satisfy
a given condition. It is denoted by (σ ) sigma.
Relation r
σ
A=B and D > 5 (r)
Project Operation or
Selection of Columns (Attributes)
This operation shows the list of those attributes
that we wish to appear in the result. It is denoted
by (Π ) Pie.
Relation r:
Select A and C
Projection
Π A, C (r)
Joining Two Relations – Product Operation
This operation allows us to combine information
from two relations at r and s and relations product
denoted as r x s
Relations r, s:
r x s:
Union Operation
This operation allows us to combine information
from two relations at r and s relations and duplicates
records are eliminated
Relations r, s:
r s:
Difference Operation
This operation allows us to find the records that are in
one relation, but are not in another. Let the two relation
r and s and relations difference denoted by r - s
Relations r, s:
r – s:
Intersection Operation
This operation allows us to find the records that are in
common. Let the two relation r and s and relations
intersection denoted by r s
Relation r, s:
rs
Join Operation
The join operation denoted by , is used to combine related records from
two relations into single records. This operation is very important for any
relational database with more than a single relation.
The general form of join operation on two relation r (A1, A2, …, An ) and
s (B1, B2, …, Bm ). The result of the join is relation Q with (n + m) attributes
Q (A1, A2, …, An , B1, B2, …, Bm ) in that order. Q has one record for each
combination of records, one from r and one from s whenever the
combination satisfies the join condition.
Join Operation is four types:
Natural Join
Outer Join
Equal Join
Theta Join
Natural Join
This Natural Join is a binary operation that allows us to combine
certain selection and product into one operation
Employee Salary
E_Code E_Name E_Code Salary
E001 Hari E001 2000
E002 Om E002 5000
E003 Smith E003 7000
E004 Jay E004 1000
The result of (Employee Salary) is as follows:
E_Code E_Name Salary
E001 Hari 2000
E002 Om 5000
E003 Smith 7000
E004 Jay 1000
Outer Join
The Outer Join operation is a extension of the join operation to
deal with missing information. It is basically three types: left outer
join, Right outer join and full outer join Office
Employee E_Name Dept Salary
E_Name Street City
Ram EDP 2000
Ram Civil line Mumbai
Shyam MIS 5000
Shyam Park street Kolkata
Kuber Account 7000
Ravi MG Street Delhi
Rohit Admin 1000
Rohit Nehru Nagar Ghaziabad
The result of (Employee Salary) is as follows:
E_Name Street City Dept Salary
Ram Civil line Mumbai EDP 2000
Shyam Park street Kolkata MIS 5000
Rohit Nehru Nagar Ghaziabad Admin 1000
From the result, only common attributes are displayed. Hence the
avoid such loss of information we can use the concept of outer join
Left Outer Join
It is denoted by the symbol . All information from the left
relation is present in the result of the left outer join.
Note that the tuple (Ravi , MG Street, Delhi, Null, Null) is such a tuple,
which is added.
Right Outer Join
It is denoted by the symbol . All information from the right
relation is present in the result of the right outer join.
Note that the tuple (Kuber, Null, Null, Account, 7000) is such a tuple,
which is added.
Full Outer Join
It is denoted by the symbol . All information from the left and
right relations are present in the result.
Note that the tuple (Ravi , MG Street, Delhi, Null, Null) and
(Kuber, Null, Null, Account, 7000) are such a tuples, which are added.
Equality Join
The most common use of join involves join condition with equality
comparisons only. Such a join, where the only comparison operation
(‘=‘) is used is called as Equi Join
SQL-86,
SQL-89,
SQL-92
SQL:1999,
SQL:2003,
SQL:2008
The ANSI and ISO standard language for the definition and
manipulation of relational database.
47
SQL: DDL Commands
FoodCart
ALTER TABLE FoodCart ( date food qty rate
ADD rate float );
FoodCart
ALTER TABLE FoodCart(
DROP COLUMN profit ); date food sold
FoodCart
Example: UPDATE FoodCart SET rate = 349 WHERE date = ’02/25/08’ AND food = ‘pizza’;
FoodCart
date food rate date food rate
02/25/08 pizza 350 02/25/08 pizza 349
02/26/08 hotdog 500 02/26/08 hotdog 500
02/26/08 pizza 70 02/26/08 pizza 70
SQL: DELETE Statement
To delete rows from the table:
DELETE statement syntax:
DELETE FROM <table name> WHERE <condition>;
FoodCart
date food rate date food rate
02/25/08 pizza 349 02/25/08 pizza 349
02/26/08 hotdog 500 02/26/08 pizza 70
02/26/08 pizza 70
Note: If the WHERE clause is omitted all rows of data are deleted from the table.
SQL Statements, Operations, Clauses
SQL Statements:
Select
SQL Operations:
Join
Left Join
Right Join
Like
SQL Clauses:
Order By
Group By
Having
SQL: SELECT Statement
A basic SELECT statement includes 3 clauses
Weight Weight
80 80
54
54
SQL: Join operation
A join can be specified in the FROM clause which list the
two input relations and the WHERE clause which lists the
join condition.
Example:
Emp Dept
ID State ID Division
1000 CA 1001 IT
1001 MA 1002 Sales
1002 TN 1003 Biotech
SQL: Join operation (cont.)
inner join = join
% (arbitrary string)
SELECT * FROM emp WHERE ID like ‘%01’;
(Finds ID that ends with 01, e.g. 1001, 2001, etc )
_ (a single character)
SELECT * FROM emp WHERE ID like ‘_01_’;
(Finds ID that has the second and third character as 01, e.g. 1010,
1011, 1012, 1013, etc)
SQL: The ORDER BY Clause
Ordered result selection:
COUNT(attr)
SUM(attr)
MAX(attr)
MIN(attr)
AVG(attr)
SELECT itemSold
from DeptSales
Where deptName = (SELECT deptName from Emp where name=‘Anil’);
Indexes
• An index can be created in a table to find out more quickly
and efficiently.
• Indexing is a way to sort and search records in the table.
• Indexing are essential to improve the speed with which
record can be located and retrieved from a table.
• Indexes in database are similar to indexes in books.
Syntax :
DECLARE
declaration statements;
BEGIN
executable statements
EXCEPTIONS
exception handling statements
76
END;
77
Cursors
In PL/SQL, the context area is controlled by Cursor. A cursor contains
information on a select statement and the rows of data accessed by it.
A cursor is used to referred to a program to fetch and process the rows
returned by the SQL statement, one at a time.
78
Syntax:
DECLARE variables;
records;
create a cursor;
BEGIN OPEN cursor;
FETCH cursor;
process the records;
CLOSE cursor;
END;
79
Trigger
In a DBMS, a Trigger is a SQL procedure that initiates an action (i.e., fires an
action) when an event (INSERT, DELETE or UPDATE) occurs. Since triggers are
event-driven specialized procedures, they are stored in and managed by the
DBMS.
A trigger cannot be called or executed; the DBMS automatically fires the trigger
as a result of a data modification to the associated table. Triggers are used to
maintain the referential integrity of data by changing the data in a systematic
fashion. Each trigger is attached to a single, specified table in the database.
A trigger can also contain INSERT, UPDATE and DELETE logic within itself, so
when the trigger is fired because of data modification it can also cause another
data modification, thereby firing another trigger.
80
Create the Triggers
.
Step1: Create Database
81
Create the Triggers
.
82