1. Subject Name Code Credit HoursSubject Name Code Credit Hours
Database System COMP 219 3Database System COMP 219 3
RELATIONAL DATABASES
11
UNIT-2
2. Subject Name Code Credit HoursSubject Name Code Credit Hours
Database System COMP 219 3Database System COMP 219 3
Query Languages
• It is a language in which a user requests
information from the db.
22
Query LanguagesQuery Languages
Procedural Language Non Procedural Language
Specifies what data are
required & specify how to get
those data
e.g Relational Algebra
Specifies what data are
required without specify how
to get those data
e.g. Relational calculus
3. Subject Name Code Credit HoursSubject Name Code Credit Hours
Database System COMP 219 3Database System COMP 219 3
Relational Algebra
• It consists of set of operations that take one
or more relations as input and produces new
relation as output.
• The operations can be divided into,
– Basic operations: Select, Project, Union, rename,
set difference and Cartesian product
– Additional operations: Set intersections, natural
join, division and assignment.
– Extended operations: Aggregate operations and
outer join
33
4. Subject Name Code Credit HoursSubject Name Code Credit Hours
Database System COMP 219 3Database System COMP 219 3
Basic operations
• Select
– It selects tuples that satisfy a given predicate, To
denote selection.
(Sigma) is used.
44
Syntax: condition (Table name)
5. Subject Name Code Credit HoursSubject Name Code Credit Hours
Database System COMP 219 3Database System COMP 219 3
Select
• E.g..
55
Sal>1000 (Employee)
Selects tuples whose emp sal is > 1000
6. Subject Name Code Credit HoursSubject Name Code Credit Hours
Database System COMP 219 3Database System COMP 219 3
Project Operation
• It selects attributes from the relation.
• – Symbol for project
66
∏
Syntax: ∏
<Attribute list>
(Table name)
E.g…
eid,sal
(employee)∏
7. Subject Name Code Credit HoursSubject Name Code Credit Hours
Database System COMP 219 3Database System COMP 219 3
Project Operation
• Combining Select & Project Operation
E.G..
77
eid,sal
(employee)
∏ Sal>1000( )
- Selects tuples where sal>1000 & from them only eid
and salary attributes are selected.
8. Subject Name Code Credit HoursSubject Name Code Credit Hours
Database System COMP 219 3Database System COMP 219 3
Mathematical Set Operations
• Union Operation:
– R1 U R2 - implies that tuples either from R1 or
tuples from R2 or both R1 & R2.
• E.G..
eideid enameename
11
22
33
44
AA
BB
CC
DD
88
eideid dnodno dnamedname
11
44
55
66
1010
5050
6060
7070
XXXXXX
YYYYYY
ZZZZZZ
UUUUUU
EMP1 EMP2
9. Subject Name Code Credit HoursSubject Name Code Credit Hours
Database System COMP 219 3Database System COMP 219 3
Union Operation:
∏
eideid
11
22
33
44
55
66
99
eid
(EMP1)
U ∏ eid
(EMP2)
10. Subject Name Code Credit HoursSubject Name Code Credit Hours
Database System COMP 219 3Database System COMP 219 3
SET DIFFERENCE
• R1 – R2 implies tuples present in R1 but not in
R2.
1010
eid
(EMP1)
∏
eid
(EMP2)∏
--E.G…E.G…
eideid
22
33
11. Subject Name Code Credit HoursSubject Name Code Credit Hours
Database System COMP 219 3Database System COMP 219 3
CARTESIAN PRODUCT
• R1 R2 allows to combine tuples from any
two relations.
• E.G.. Emp1 Emp2
1111
12. Subject Name Code Credit HoursSubject Name Code Credit Hours
Database System COMP 219 3Database System COMP 219 3
CARTESIAN PRODUCT
Emp1.eidEmp1.eid enameename Emp2.eidEmp2.eid dnodno dnamedname
11
11
11
11
22
AA
AA
AA
AA
BB
11
44
55
66
11
1010
5050
6060
7070
1010
XXXXXX
YYYYYY
ZZZZZZ
UUUUUU
XXXXXX
1212
eideid enameename
11
22
33
44
AA
BB
CC
DD
eideid dnodno dnamedname
11
44
55
66
1010
5050
6060
7070
XXXXXX
YYYYYY
ZZZZZZ
UUUUUU
EMP1 EMP2
Emp1Emp1 Emp2Emp2
13. Subject Name Code Credit HoursSubject Name Code Credit Hours
Database System COMP 219 3Database System COMP 219 3
Rename Operation
• To rename the name of a relation or the name of an
attribute.
1313
XX
Relational Algebra ExpressionRelational Algebra Expression
rhorho
Returns the result of E underReturns the result of E under
the name x and with thethe name x and with the
attributes renamed A1,A2,attributes renamed A1,A2,
……. An……. An
(E)(E)
14. Subject Name Code Credit HoursSubject Name Code Credit Hours
Database System COMP 219 3Database System COMP 219 3
A.INTERSECTION
• R1 R2 implies tuples present in both R1 &
R2
eideid
11
44
1414
UU
eid
(EMP1)
∏
eid
(EMP2)∏
UUE.G…E.G…
2. ADDITIONAL OPERATIONS2. ADDITIONAL OPERATIONS
15. Subject Name Code Credit HoursSubject Name Code Credit Hours
Database System COMP 219 3Database System COMP 219 3
(B) NATURAL JOIN OR EQUI JOIN
• Used to combine related tupules from two
relations.
• It requires that the two join attributes have
the same name, otherwise renaming
operation is applied first and then join
operation is applied.
• Symbol:
1515
16. Subject Name Code Credit HoursSubject Name Code Credit Hours
Database System COMP 219 3Database System COMP 219 3
(B) NATURAL JOIN OR EQUI JOIN
• Syntax:
• relation1 relation2 – Natural Join
• relation1 relation2 – EQUI Join
1616
conditioncondition
17. Subject Name Code Credit HoursSubject Name Code Credit Hours
Database System COMP 219 3Database System COMP 219 3
(B) NATURAL JOIN OR EQUI JOIN
• E.g..
eideid enameename salsal dnodno
11
22
33
44
XX
YY
ZZ
AA
1000010000
2000020000
3000030000
1600016000
1010
2020
1010
4040
DnoDno DnameDname mgrmgr
1010
2020
3030
XXXXXX
YYYYYY
ZZZZZZ
ABCABC
PQRPQR
MNOMNO
1717
EMPLOYEEEMPLOYEE DEPARTMENTDEPARTMENT
19. Subject Name Code Credit HoursSubject Name Code Credit Hours
Database System COMP 219 3Database System COMP 219 3
OUTER JOIN
• It is an extension of the join operation to deal
with missing information.
• In natural join, only the matching tuples
comes in the result and the unmatched tuples
are lost. To avoid this loss of information we
use outer join.
• There are 3 forms of outer join operation They
are Left outer join, Right outer join and full
outer join 1919
20. Subject Name Code Credit HoursSubject Name Code Credit Hours
Database System COMP 219 3Database System COMP 219 3
LEFT OUTER JOIN-
• It takes all tuples in the left relation that did
not match with any tuple in the right relation
and pads the tuples with null values for all
other attributes from the right relation and
adds them to the result of the natural join.
• E.g…
2020
21. Subject Name Code Credit HoursSubject Name Code Credit Hours
Database System COMP 219 3Database System COMP 219 3
LEFT OUTER JOIN-
2121
eideid enameename salsal dnodno
11
22
33
XX
YY
ZZ
1000010000
2000020000
3000030000
1010
2020
4040
EMPLOYEEEMPLOYEE
DnoDno DnameDname mgrmgr
1010
2020
3030
XXXXXX
YYYYYY
ZZZZZZ
ABCABC
PQRPQR
MNOMNO
DEPARTMENTDEPARTMENT
EMPLOYEEEMPLOYEE DEPARTMENTDEPARTMENT
22. Subject Name Code Credit HoursSubject Name Code Credit Hours
Database System COMP 219 3Database System COMP 219 3
LEFT OUTER JOIN-
EidEid EnameEname SalSal DnoDno DnameDname mgrmgr
11
22
33
XX
YY
ZZ
1000010000
2000020000
3000030000
1010
2020
4040
XXXXXX
YYYYYY
NULLNULL
ABCABC
PQRPQR
NULLNULL
2222
23. Subject Name Code Credit HoursSubject Name Code Credit Hours
Database System COMP 219 3Database System COMP 219 3
RIGHT OUTER JOIN-
• It takes all tuples in the right relation that did
not match with any tuple in the left relation
and pads the tuples with null values for all
other attributes and adds them to the result
of the natural join.
2323
24. Subject Name Code Credit HoursSubject Name Code Credit Hours
Database System COMP 219 3Database System COMP 219 3
RIGHT OUTER JOIN-
∏
eideid salsal dnamedname
11
22
33
nullnull
1000010000
2000020000
3000030000
nullnull
XXXXXX
ZZZZZZ
ZZZZZZ
MNOMNO
2424
Eid,dname,salEid,dname,sal
(EMPLOYEE(EMPLOYEE DEPARTMENT)DEPARTMENT)
25. Subject Name Code Credit HoursSubject Name Code Credit Hours
Database System COMP 219 3Database System COMP 219 3
FULL OUTER JOIN
• Padding tuples from the left relation that
didn’t match any from the right relation, as
well as tuples from the right relation that did
not match any from the left relation & adding
them to the result of the join.
2525
26. Subject Name Code Credit HoursSubject Name Code Credit Hours
Database System COMP 219 3Database System COMP 219 3
FULL OUTER JOIN
2626
∏
Eid,dname,salEid,dname,sal
(EMPLOYEE(EMPLOYEE DEPARTMENT)DEPARTMENT)
eideid salsal dnamedname
11
22
33
NULLNULL
1000010000
2000020000
3000030000
NULLNULL
XXXXXX
YYYYYY
nullnull
AAAAAA
27. Subject Name Code Credit HoursSubject Name Code Credit Hours
Database System COMP 219 3Database System COMP 219 3
Division Operation
• It is denoted by . It is suited to queries that
include the phrase ‘for all’.
• E.G.. Consider three relations
Acc-No.Acc-No. Branch-Branch-
namename
BalanceBalance
B-101B-101
B-102B-102
B-103B-103
B-104B-104
B-105B-105
PP
QQ
RR
SS
TT
50005000
40004000
90009000
70007000
35003500
2727
AccountAccount
29. Subject Name Code Credit HoursSubject Name Code Credit Hours
Database System COMP 219 3Database System COMP 219 3
Division Operation
• Suppose we want to find all customers who have an
account at all the branches located in Adayar.
• The query is,
•
2929
∏ Customer-name,Customer-name,
branch-namebranch-name
(depositor account)(depositor account)
∏branch-namebranch-name Branch_cityBranch_city
= “Adayar” (branch)= “Adayar” (branch)
(( ))
30. Subject Name Code Credit HoursSubject Name Code Credit Hours
Database System COMP 219 3Database System COMP 219 3
Division Operation
• Step 1:
• List out all the branches located in Adayar
• Step2: R1=
• Find all customer name, branch_name pairs
for which the customer has an account at a
branch 3030
∏branch-namebranch-name Branch_cityBranch_city(( ))= “Adayar” (branch)= “Adayar” (branch)
∏ Customer-name,Customer-name,
branch-namebranch-name
(depositor account)(depositor account)
31. Subject Name Code Credit HoursSubject Name Code Credit Hours
Database System COMP 219 3Database System COMP 219 3
Division Operation
• Step3:
• We need to find customers who appear in r2
with every branch name in R1.
3131
∏ Customer-name,Customer-name,
branch-namebranch-name
(depositor account)(depositor account)
∏branch-namebranch-name Branch_cityBranch_city
= “Adayar” (branch)= “Adayar” (branch)
(( ))
32. Subject Name Code Credit HoursSubject Name Code Credit Hours
Database System COMP 219 3Database System COMP 219 3
Aggregate Functions
• It takes a collection of values and return a single
value as a result.
• Avg., min., max., sum., count are few aggregate
functions.
• Ex:
eideid enameename SalarySalary
11
22
33
44
55
AA
BB
CC
DD
EE
1000010000
2000020000
3000030000
40004000
50005000
3232
O/p ofO/p of
Sum (salary)Sum (salary)
(r)(r)
33. Subject Name Code Credit HoursSubject Name Code Credit Hours
Database System COMP 219 3Database System COMP 219 3
Relational Calculus
• It can be divided as Tuple Relational calculus
and Domain Relational Calculus
• Tuple relational Calculus:
• It is a non procedural query language
• Specifies what data are required without
describing how to get those data
• Each Query is in the form of {t | P(t)}.
• It is the set of all tuples ‘t’ such that predicate
P is true for ‘t’.
3333
34. Subject Name Code Credit HoursSubject Name Code Credit Hours
Database System COMP 219 3Database System COMP 219 3
Relational Calculus
• Notations Used:
• t is a tuple variable, t[A] denotes the value of
tuple t on attribute A
• t r denotes that tuple ‘t’ is in relation ‘r’.
• P is the formula similar to that of the
predicate calculus.
3434
35. Subject Name Code Credit HoursSubject Name Code Credit Hours
Database System COMP 219 3Database System COMP 219 3
Predicate calculus formula
• Set of attributes and constants
• Set of comparison operators(e.g. <, >, <, >, =,
=).
• Set of connectives: and(^), or( ), not( )
• Implication( ) : X Y, if X is true, then Y is
True
3535
^^
36. Subject Name Code Credit HoursSubject Name Code Credit Hours
Database System COMP 219 3Database System COMP 219 3
Set of Quantifiers:
• - there exists
• Definition for there exists
• t r(Q(t)) = ‘there exists’ a tuple in relation
r such that predicate Q(t) is true.
V - For all
• Definition for ‘For all’
• V t r(Q(t))=Q(t) is true “ for all” tuples ‘t’ in
relation r. 3636
37. Subject Name Code Credit HoursSubject Name Code Credit Hours
Database System COMP 219 3Database System COMP 219 3
Domain Relational calculus
• The domain relational calculus uses domain variables
that take on values from an attribute domain rather
than values for entire tuple.
• Each Query is an expression of the form,
• {<x1,x2,…xn>/P(x1,x2,…xn)}
• Where x1,x2,…xn represent domain variables.
• P represents a formula similar to that of
predicate calculus. 3737
38. Subject Name Code Credit HoursSubject Name Code Credit Hours
Database System COMP 219 3Database System COMP 219 3
SQL• Structured Query Language (SQL) is the standard
command set used to communicate with the
relational database management systems (RDBMS).
• The DBMS processes the SQL request, retrieves the
requested data from the Database, and returns it.
• This process of requesting data from a database and
receiving back the results is called a db query and
hence the name Structured Query Language.
• It is a non-procedural language..
3838
39. Subject Name Code Credit HoursSubject Name Code Credit Hours
Database System COMP 219 3Database System COMP 219 3
Advantages of SQL
• It is a high level language that provides a greater degree of
abstraction than procedural language.
• Applications written in SQL can be easily ported across the
systems, so it is the common language for all relational
databases.
• The queries are written by using English like language, so easy
to understand.
3939
40. Subject Name Code Credit HoursSubject Name Code Credit Hours
Database System COMP 219 3Database System COMP 219 3
Parts of SQL
• The SQL language has several parts:
• I. Data Definition Language (DDL): creates, changes and removes a
table’s structure.
• E.G.. CREATE,ALTER,DROP,RENAME and TRUNCATE
• 1. Create:
It is used to create a new table in oracle.
• Syntax:
Create table table_name(colunmn_name1 data type1
[constraint], column_name2 data type2[constraint],…
column_ name n datatype n [constraint]);
4040
41. Subject Name Code Credit HoursSubject Name Code Credit Hours
Database System COMP 219 3Database System COMP 219 3
Data Types
• SQL supports following data types,
• 1. varchar2(size) – to store variable length character data.
• 2. char(size) – to store fixed length character data.
• 3. number(P) – to store integer values, P- represents
maximum
• length.
• 4. number(P,S) – to store fixed point decimal values, P is the
precision and S is the scale.
5. Date - to store date and time values.
Format is 14-MAR-08, 2:50:40 4141
42. Subject Name Code Credit HoursSubject Name Code Credit Hours
Database System COMP 219 3Database System COMP 219 3
Data Definition Language (DDL):
• 3 types of constraints are used,
• NOT NULL - Prevents null values entered into a
column.
• UNIQUE – Prevents duplicate values entered into a
column.
• PRIMARY – Requires all values entered into the
column be unique as well as not null.
4242
43. Subject Name Code Credit HoursSubject Name Code Credit Hours
Database System COMP 219 3Database System COMP 219 3
Data Definition Language (DDL):
• Example:
• Create table customer(customer_name varchar(10),
cust_id number(5) primary key, dob date not
null,customer_address varchar(20));
• To see the structure of a table,
• desc table name;
• E.G.. desc customer;
4343
44. Subject Name Code Credit HoursSubject Name Code Credit Hours
Database System COMP 219 3Database System COMP 219 3
Data Definition Language (DDL):
• 2. ALTER:
• It is used to add a new column to the table, modifying the
existing column, including or dropping an integrity constraints
(primary key, not null, etc..)
• A. Adding new column:
• Syntax:
• alter table tablename add column_name data_type;
• E.G. alter table customer add pno number(10); 4444
45. Subject Name Code Credit HoursSubject Name Code Credit Hours
Database System COMP 219 3Database System COMP 219 3
Data Definition Language (DDL):
• B. MODIFYING AN EXISTING COLUMN:
• Syntax:
• alter table tablename modify column_name newdata_type;
• E.G…
• B. DROPPING A COLUMN:
• It is used to delete a table.
• Syntax:
• alter table tablename drop column column_name;
• E.g.. alter table customer drop column cust_add; 4545
alter table customer modify cust_id varchar2(6);alter table customer modify cust_id varchar2(6);
46. Subject Name Code Credit HoursSubject Name Code Credit Hours
Database System COMP 219 3Database System COMP 219 3
Data Definition Language (DDL):
• 3.DROPPING A TABLE
• It is used to drop a table.
• Syntax: drop table table_name;
• E.G.. Drop table customer;
• All data and table structure of a customer table is
permanently removed.
4646
47. Subject Name Code Credit HoursSubject Name Code Credit Hours
Database System COMP 219 3Database System COMP 219 3
Data Definition Language (DDL):
• 3.RENAMING A TABLE
• SYNTAX: rename oldtablename to newtablename;
• E.G.. Rename customer to cust_det;
• 4.TRUNCATE A TABLE
• It removes all records or rows from the table.
• SYNTAX: truncate table tablename;
• E.G.. Truncate table cust_det; 4747
48. Subject Name Code Credit HoursSubject Name Code Credit Hours
Database System COMP 219 3Database System COMP 219 3
BASIC STRUCTURE
• II. Data Retrieval retrieves data from the database.
• E.g.. Select
• The basic structure of an SQL expression consists of 3 clauses,
• Select
• From
• Where
• SELECT: It is used to list the attributes desired in the result of a query.
Syntax: select A1,A2,…..An from R1,R2,…Rm where P
E.g.. Select cust_name from customer;
4848
49. Subject Name Code Credit HoursSubject Name Code Credit Hours
Database System COMP 219 3Database System COMP 219 3
BASIC STRUCTURE
• E.g.2 Select distinct cust_name from customer;
• if we want duplicates removed.
• E.g.3 Select all cust_name from customer;
• To specify explicitly that duplicates are not allowed
• From Clause: It is used to list the relations involved in the query.
• E.G.. Select * from customer;
4949
50. Subject Name Code Credit HoursSubject Name Code Credit Hours
Database System COMP 219 3Database System COMP 219 3
BASIC STRUCTURE
• Where clause: It is used for specifying the
condition.
• E.g.. Find the names of all customer whose city is
“chennai”.
• select customer_name from customer where city=“chennai”.
5050
51. Subject Name Code Credit HoursSubject Name Code Credit Hours
Database System COMP 219 3Database System COMP 219 3
RENAME OPERATION
• Syntax:
• oldname as newname
• E.g..
• Select customer-name,borrower.loan_number from
borrower,loan where borrower.loan-number= loan.loan
number
• If we want the attribute name loan-number to be replaced
with the name loan-id, we can rewrite the preceding query
as,
• Select customer-name,borrower.loan_number as loan-id
from borrower,loan where borrower.loan-number= loan.loan
5151
52. Subject Name Code Credit HoursSubject Name Code Credit Hours
Database System COMP 219 3Database System COMP 219 3
Tuple variables
• Tuple variables are defined in the from clause via the use of
the as clause.
• E.g .. For all customers who have a loan from a bank, find
their names and loan numbers as,
• Select customer-name,T.loan-number from
borrower as T, loan as S where T.loan-
number=S.loan-number
5252
53. Subject Name Code Credit HoursSubject Name Code Credit Hours
Database System COMP 219 3Database System COMP 219 3
STRING OPERATIONS
• SQL includes a string matching operator for comparisons on
character strings. Patterns are described using 2 special
characters,
• Percent(%) : The % character, matches any substring.
• Underscore( _ ) : The _ character matches any character.
• E.G.. Find the names of customers where the first
and second characters are ‘Ba’.
• Select customer_name from customer where
customer_name like ‘Ba%’; 5353
54. Subject Name Code Credit HoursSubject Name Code Credit Hours
Database System COMP 219 3Database System COMP 219 3
STRING OPERATIONS
• Eg..
• Find the names of customer where the second
character is ‘n’ or ‘a’.
• select customer_name from customer where
customer-name like ’_n%’ or ‘_a%’;
5454
55. Subject Name Code Credit HoursSubject Name Code Credit Hours
Database System COMP 219 3Database System COMP 219 3
Set Operations
• The set operations union, intersect and except
operate on relations and correspond to the relational
algebra operations U, , and -.
• Each above operations automatically eliminates the
duplicates and to retain all duplicates use union all,
intersect all, except all.
5555
UU
56. Subject Name Code Credit HoursSubject Name Code Credit Hours
Database System COMP 219 3Database System COMP 219 3
Set Operations
• 1. Find all customers who have a loan or an
account,or both
• (select customer_name from depositor) union
(select customer_name from borrower);
• 2. Find all customers who have both loan and an
account,
• select customer_name from depositor) intersect
(select customer_name from borrower); 5656
57. Subject Name Code Credit HoursSubject Name Code Credit Hours
Database System COMP 219 3Database System COMP 219 3
Set Operations
• 3. Find all customers who have an account but no
loan,
• (select customer_name from depositor) except
(select customer_name from borrower);
5757
58. Subject Name Code Credit HoursSubject Name Code Credit Hours
Database System COMP 219 3Database System COMP 219 3
AGGREGATE FUNCTIONS
• It takes collection of values as input and
return a single value as output.
• SQL has 5 built in agg.fns,
• avg – Find average value.
• min – Find minimum value.
• max – Find Maximum value.
• Sum – Find sum of values.
• Count – Counts number of values
5858
59. Subject Name Code Credit HoursSubject Name Code Credit Hours
Database System COMP 219 3Database System COMP 219 3
AGGREGATE FUNCTIONS
• Find the average account balance at the
adayar branch.
• Select avg(balance) from account where
branch_name=“adayar”;
5959
60. Subject Name Code Credit HoursSubject Name Code Credit Hours
Database System COMP 219 3Database System COMP 219 3
Complex Queries
• Complex queries are often hard or impossible to
write a single SQL block.
• There are 2 ways for composing multiple SQL blocks
to express a complex query.
• 1.Derived relations 2. With clause.
• Derived Relations:
• SQL allows a subquery expression to used in
the from clause. If we use such an expression, then
we must give the result relation a name, and we can
rename the attributes.
• For renaming as clause is used. 6060
61. Subject Name Code Credit HoursSubject Name Code Credit Hours
Database System COMP 219 3Database System COMP 219 3
Complex Queries
• For e.g.. Find the average account balance of those
branches where the average account balance is
greater than 10,000
• select branch_name, avg_balance from(select branch_name,
avg(balance) from account group by branch_name) as
branch_avg(branch_name,avg_balance) where avg_balance >
10000;
• Here subquery result is named branch_avg with the attributes
branch_name and avg_balance. 6161
62. Subject Name Code Credit HoursSubject Name Code Credit Hours
Database System COMP 219 3Database System COMP 219 3
Complex Queries
• With Clause:
• It provides a way of defining a temporary view,
whose definition available only to the query in which
the with clause occurs.
• Consider the following query, which selects accounts
with the maximum balance. If there are many
accounts with the same maximum balance, all of
them are selected.
6262
63. Subject Name Code Credit HoursSubject Name Code Credit Hours
Database System COMP 219 3Database System COMP 219 3
Complex Queries
• with max_balance(value) as
select max(balance)
from account
select account_no from account, max_balance
where account.balance=max_balance.value;
6363
64. Subject Name Code Credit HoursSubject Name Code Credit Hours
Database System COMP 219 3Database System COMP 219 3
Parts of SQL
• III.Data Manipulation Language (DML):
• insert new rows, changes existing rows and
removes unwanted rows.
• E.G.. INSERT,UPDATE and DELETE
• Insert into account values(101,adayar,1000);
• Update account set balance=balance*100;
• Delete from account where
branch_name=‘adayar’; 6464
65. Subject Name Code Credit HoursSubject Name Code Credit Hours
Database System COMP 219 3Database System COMP 219 3
Parts of SQL
• IV.Transaction control Language (TCL):
• Manages and changes the logical transactions.
Transactions are changes made to the data by
DML statements group together.
• E.G.. COMMIT,ROLLBACK and SAVEPOINT.
6565
66. Subject Name Code Credit HoursSubject Name Code Credit Hours
Database System COMP 219 3Database System COMP 219 3
Parts of SQL
• Data Control Language (DCL): gives and removes right
to oracle objects.
• E.g.. GRANT,REVOKE
6666
67. Subject Name Code Credit HoursSubject Name Code Credit Hours
Database System COMP 219 3Database System COMP 219 3
Parts of SQL
• Embedded SQL and Dynamic SQL:
• defines how SQL statements can be
embedded within general purpose
programming languages such as C, C++, VB,
Java etc..
6767
68. Subject Name Code Credit HoursSubject Name Code Credit Hours
Database System COMP 219 3Database System COMP 219 3
Embedded SQL• They are SQL statements included in the programming
language. The Programming language in which
the SQL statements are included is called the
host language.
• host language – C, COBOL, PASCAL etc…
• The Program is written in the host language and
whenever data access or data manipulation is need
the SQL statements are embedded. This embedded
source code is submitted to an SQL precompiler, which
process the SQL statements.
6868
69. Subject Name Code Credit HoursSubject Name Code Credit Hours
Database System COMP 219 3Database System COMP 219 3
Embedded SQL
• Variables of the host language can be referenced in
the embedded SQL statements thus allowing the
values calculated by the program to be used by the
SQL statements.
• The host variables are used by the embedded SQL
statements to receive the results of the SQL queries .
• Special program variables ( null indicators) are used
to assign and retrieve the NULL values to and from
the database.
6969
70. Subject Name Code Credit HoursSubject Name Code Credit Hours
Database System COMP 219 3Database System COMP 219 3
Embedded SQL
• E.g.. The following code segment shows how an SQL
statement is included in ‘C’ Program,
• #include<stdio.h>
Void main()
{
char name1[20];
…………
EXEC SQL
Select name into: name1 from employee where empno=1001;
…………..
} 7070
71. Subject Name Code Credit HoursSubject Name Code Credit Hours
Database System COMP 219 3Database System COMP 219 3
Dynamic SQL
• The dynamic SQL component of SQL allows programs
to construct and submit SQL queries at run time.
• This statements must be completely present at
compile time, they are compiled by the embedded
SQL preprocessor.
7171
72. Subject Name Code Credit HoursSubject Name Code Credit Hours
Database System COMP 219 3Database System COMP 219 3
Dynamic SQL
• E.g…
• #include<stdio.h>
void main( )
{
char *sqlprog=“update account set balance=balance * 1.10 where
acno=?”
EXEC SQL prepare dynprog from: sqlprog;
char account[10]=“A101”;
EXEC SQL execute dynprog using account;
}
The dynamic SQL program contains a ?, which is a place holder
for a value that is provided when the SQL program is
executed.
7272
73. Subject Name Code Credit HoursSubject Name Code Credit Hours
Database System COMP 219 3Database System COMP 219 3
Integrity Constraints• They ensure that changes made to the db by
authorized users so not result in a lost of data
consistency.
• The integrity constraints guard against accidental
damage to the db.
• E.G…
• An a/c balance cannot be null.
• No 2 accounts can have the same acc_no.
• Every acc-No in the depositor relation must have a matching
acc-no in the A/c relation.
7373
74. Subject Name Code Credit HoursSubject Name Code Credit Hours
Database System COMP 219 3Database System COMP 219 3
Constraints on a single relation
• In addition to Primary key constraint, the
allowed In.Consts are,
• Not null
• Unique
• Check(<predicate>)
7474
75. Subject Name Code Credit HoursSubject Name Code Credit Hours
Database System COMP 219 3Database System COMP 219 3
Constraints on a single relation
• Not null:
– The result as a legal value for every attribute.
• E.g… By restricting the domain of
attributes acc_no and balance to exclude null
values by declaring them as,
acc_no char(20) not null,
balance number(10,2) not null
7575
76. Subject Name Code Credit HoursSubject Name Code Credit Hours
Database System COMP 219 3Database System COMP 219 3
Constraints on a single relation
• Unique:
• unique(A1,A2,….An)
• No 2 tuples in the relation can be equal to all
primary key attributes.
7676
77. Subject Name Code Credit HoursSubject Name Code Credit Hours
Database System COMP 219 3Database System COMP 219 3
Constraints on a single relation
• Check:
• A common use of check constraint is to
ensure that attribute values satisfy specified
conditions.
• E.g…
• A clause check (assets>=0) in the create table command for
relation branch would ensure that the value of assets is non-
negative,
• Create table branch(branchname varchar(10), branchcity
varchar(10),assets number(15,2), primary key(branch_name)
check(assets>=0))
7777
78. Subject Name Code Credit HoursSubject Name Code Credit Hours
Database System COMP 219 3Database System COMP 219 3
Referential Integrity
• Ensures that a value appears in one relation
for a given set of attributes also appears for a
certain set of attributes in another relation.
• For E.g… in the banking database,
• create table account(acc_no char(10), branch_name
char(15), balance number(10,2), primary
key(acc_no), foreign key(branch_name) references
branch, check(balance>=0));
7878
79. Subject Name Code Credit HoursSubject Name Code Credit Hours
Database System COMP 219 3Database System COMP 219 3
Referential Integrity
• Referential Integrity in E-R Model:
• Consider the relationship set R between entity sets E1 and
E2. The relational schema for R includes the primary keys K1
of E1 and K2 of E2.
• Then K1 and K2 form foreign keys on the relational
schemas for E1 and E2 respectively.
7979
E1E1 RR E2E2
80. Subject Name Code Credit HoursSubject Name Code Credit Hours
Database System COMP 219 3Database System COMP 219 3
Referential Integrity
• Referential Integrity in SQL:
• Primary key, candidate and foreign keys can be
specified as part of the SQL create table statement.
• E.g…
• Create table customer(cust_name
char(10),cust_street char(20),cus_city
char(30),primary key(cust_name));
8080
81. Subject Name Code Credit HoursSubject Name Code Credit Hours
Database System COMP 219 3Database System COMP 219 3
ASSERTIONS
• It IS a predicate expressing a condition that
we wish the database always to satisfy.
• An assertion in SQL takes the form,
• Create assertion <assertion_name> check
<predicate>
8181
82. Subject Name Code Credit HoursSubject Name Code Credit Hours
Database System COMP 219 3Database System COMP 219 3
TRIGGERS
• A Trigger is a statement that is executed
automatically by the system as a side effect of
modification to the db.
• To design a trigger mechanism, we must
* Specify the conditions under which the trigger is to be
executed.
* Specify the actions to be taken when the triggers
executed. 8282
83. Subject Name Code Credit HoursSubject Name Code Credit Hours
Database System COMP 219 3Database System COMP 219 3
TRIGGERS
• Suppose that instead of allowing negative account
balances, the bank deals with overdrafts by,
* Setting the account balance to zero.
* Creating the loan in the amount of overdraft.
* Giving this loan a loan no identical to the
account number to the overdrawn amount.
• The condition for executing the trigger is an update
to the account relation that results in a negative
balance value.
8383
84. Subject Name Code Credit HoursSubject Name Code Credit Hours
Database System COMP 219 3Database System COMP 219 3
TRIGGERS• E.G…
create trigger overdraft_trigger after update on account
referencing new row as nrow
for each row when nrow.balance<0
begin atomic
insert into borrower(select customer_name,acc_no
from depositor where nrow.acc_no=depositor.acc_no);
insert into loan values
(nrow.acc_no,nrow.branch_name,nrow.balance);
update account set balance = 0 where
account.acc_no=nrow.acc_no
end.
8484
85. Subject Name Code Credit HoursSubject Name Code Credit Hours
Database System COMP 219 3Database System COMP 219 3
SECURITY
• Security of data is an important concept in DBMS because it is
essential to safeguard the data against any unwanted users.
• There are 5 different levels of security,
• 1. DB system level:
• Authentication (verification) and authorization mechanism to allow
specific users access only to required data.
• 2. Operating System Level:
• * Protection from invalid logins
• * File-level access protection
• * Protection from improper use of “superuser” authority,
• * Protection from imprope use of privileged machine instructions.
8585
86. Subject Name Code Credit HoursSubject Name Code Credit Hours
Database System COMP 219 3Database System COMP 219 3
SECURITY
3. Network level:
* Each site must ensure that it communicates with treated sites.
* Links must be protected from theft or modification of messages.
Mechanisms Used:
* Identification protocol (password based).
* Cryptography.
4. Physical Level:
* Protection of equipment from floods, power failure etc..
* Protection of disks from theft etc…
* Protection of network and terminal cables from wire tapes etc…
Solution:
* Physical security by locks etc…
* Software techniques to detect physical security breaches.
8686
87. Subject Name Code Credit HoursSubject Name Code Credit Hours
Database System COMP 219 3Database System COMP 219 3
SECURITY
5. Human Level:
Protection from stolen passwords etc…
Solution:
* Frequent change of passwords.
* Data audits
8787
88. Subject Name Code Credit HoursSubject Name Code Credit Hours
Database System COMP 219 3Database System COMP 219 3
Relational Database Design
• It requires that we find a “good” collection of
relation schemas.
• Pit-falls in Relational Database design:
• A bad design may lead to
• a. Repetition of information – that leads to
• insertion, deletion, updation problems.
• b. Inability to represent certain Information.
8888
89. Subject Name Code Credit HoursSubject Name Code Credit Hours
Database System COMP 219 3Database System COMP 219 3
Relational Database Design
• Design Goals:
• a. Avoid redundant data.
• b. Ensure that relationships among attributes are
represented.
• c. Facilitate the checking of updates for violation of
db integrity constraints.
8989
90. Subject Name Code Credit HoursSubject Name Code Credit Hours
Database System COMP 219 3Database System COMP 219 3
Relational Database Design
• Example: Consider the relation schema:
• Lending-schema=(branch_name,branch_city,assets,customer_name,
loan_no,amount)
Here branch Adayar details are represented 2 times . This leads to a redudancy
problem.
Branch_Branch_
NameName
BranchBranch
_city_city
assetsassets CustomerCustomer
_name_name
Loan_noLoan_no amountamount
AdayarAdayar
BbbbBbbb
CcccCccc
AdayarAdayar
ChennaiChennai
XXXXXXXX
YYYYYYYY
ChennaiChennai
90,00,00090,00,000
20,00,00020,00,000
30,00,00030,00,000
90,00,00090,00,000
AnuAnu
aaaaaa
bbbbbb
BarathiBarathi
L-01L-01
L-02L-02
L-03L-03
L-04L-04
10001000
20002000
30003000
35003500
9090
91. Subject Name Code Credit HoursSubject Name Code Credit Hours
Database System COMP 219 3Database System COMP 219 3
Relational Database Design
• Redundancy:
• Data for branch_name,branch_city, assets are represented
for each loan that a branch makes
a. wastage space
b. Complicates updating,introducing inconsistency of
assets value.
Decomposition:
* Decompose the relation-schema, lending schema into,
9191
92. Subject Name Code Credit HoursSubject Name Code Credit Hours
Database System COMP 219 3Database System COMP 219 3
Relational Database Design
• Branch-schema=(branch_name,branch_city,assets)
• Loan-schema =
(customer_name,loan_no,branch_name,amount)
• All attributes of original schema R must appear in
decomposition(R1,R2)
• R= R1 U R2
• Lossless join decomposition.
• All possible relations r on schema R.
• r = (r) (r)
• R1 R2 9292
∏ ∏
93. Subject Name Code Credit HoursSubject Name Code Credit Hours
Database System COMP 219 3Database System COMP 219 3
Functional Dependencies
• It requires that the value for a certain set of attributes
determines uniquely the value for another set of attributes.
• In a given relation R, X and Y are attributes. Attributes Y is
functionally dependent on attribute X if each value of X
determines exactly one value of Y, which is represented as
X Y
i.e… “ X determines Y” or “Y is functionally dependent on X”
E.G…
Marks Grade 9393
94. Subject Name Code Credit HoursSubject Name Code Credit Hours
Database System COMP 219 3Database System COMP 219 3
Functional Dependencies• Types:
• A. Full Dependencies:
• In relation R, X and Y are attributes. X is functionally determines Y.
Subset of X should not be functionally determine Y.
• In the above eg. Marks is fully functionally dependent on student_no and
course_no together and not on subset of {student_no,course_no}
9494
MarksMarks
Student_NoStudent_No
Course_noCourse_no
95. Subject Name Code Credit HoursSubject Name Code Credit Hours
Database System COMP 219 3Database System COMP 219 3
Functional Dependencies
• B. Partial Dependencies:
• Attribute Y is partial dependent on the
attribute X only if it is dependent on a subset
of attribute X.
• For eg.. Course_name, Instructor_name
are partially dependent on composite
attributes { student no, course_no} because
course_no alone defines course_name, 9595
96. Subject Name Code Credit HoursSubject Name Code Credit Hours
Database System COMP 219 3Database System COMP 219 3
Functional Dependencies
• C. Transitive Dependencies:
X,Y and Z are 3 attributes in the relation R
X Y
Y Z
X Z
For e.g.. Grade depends on marks and in turn make depends on
{student_no course_no}, hence Grade depends fully
transitively on {student_no course_no} 9696
97. Subject Name Code Credit HoursSubject Name Code Credit Hours
Database System COMP 219 3Database System COMP 219 3
NORMAL FORMS
• Normalisation:
Db designed based on E-R model may have some
amount of inconsistency (variation), uncertainty (in security)
and redundancy (duplication).
To eliminate these drawbacks some refinement has to
be done on the db.
Refinement process is called normalization.
Normalisation is defined as a step by step process of
decomposing a complex relation into a simple and stable
relations. 9797
98. Subject Name Code Credit HoursSubject Name Code Credit Hours
Database System COMP 219 3Database System COMP 219 3
Purpose of Normalisation
• Minimize redundancy in data
• Remove insert, delete and update anomaly
(irregularity) during db activities.
• Reduce the need to recognize the data when
it is modified or enhanced.
• Because of duplicate data elimination, we will
be able to reduce the overall size of the db.
9898
99. Subject Name Code Credit HoursSubject Name Code Credit Hours
Database System COMP 219 3Database System COMP 219 3
Normal Forms
• The different stages of normalization is called as normal
forms. They are,
• * 1NF
• * 2NF
• * 3NF
• * BCNF
9999
100. Subject Name Code Credit HoursSubject Name Code Credit Hours
Database System COMP 219 3Database System COMP 219 3
First Normal Form(1NF)
• A relation schema R is in 1NF if
* all the attributes of the relation R are atomic in nature.
E.G… DEPT
Suppose we extend it by including DLOCATIONS attribute as
shown above. We assume that each dept may have a no. of
Locations.
This is not 1NF bcoz DLOCATIONS is not an atomic attribute.
DNAMEDNAME DNODNO DHEADDHEAD DLOCATIONSDLOCATIONS
100100
101. Subject Name Code Credit HoursSubject Name Code Credit Hours
Database System COMP 219 3Database System COMP 219 3
First Normal Form(1NF)
DNAMEDNAME DNODNO DHEADDHEAD DLOCATIONSDLOCATIONS
ResearchResearch 33 JohnJohn (Mianus,Rye,Stratford)(Mianus,Rye,Stratford)
AdministratorAdministrator 22 princeprince MianusMianus
HeadquarterHeadquarter 11 PeterPeter RyeRye
101101
Dept:Dept:
102. Subject Name Code Credit HoursSubject Name Code Credit Hours
Database System COMP 219 3Database System COMP 219 3
First Normal Form(1NF)
• There are 2 main techniques to achieve 1NF,
1. Remove the attribute DLOCATIONS and place it in separate relation
DEPT_LOCATIONS along with a primary key DNO. The primary key of the
original DEPT is the combination {DNO,DLOCATIONS}
DEPT-LOCATIONS
DNODNO DLOCATIONSDLOCATIONS
11
22
33
33
33
RyeRye
MianusMianus
RyeRye
MianusMianus
stratfordstratford
102102
103. Subject Name Code Credit HoursSubject Name Code Credit Hours
Database System COMP 219 3Database System COMP 219 3
First Normal Form(1NF)
• 2. Expand the key so that there will be a separate tuple in the orginal DEPT
relation for each location of a DEPT as shown below,
• So the first technique is superior.
DNAMEDNAME DNODNO DHEADDHEAD DLOCATIONSDLOCATIONS
ResearchResearch
ResearchResearch
ResearchResearch
AdministrationAdministration
HQHQ
33
33
33
22
11
JohnJohn
JohnJohn
JohnJohn
PrincyPrincy
PeterPeter
MianusMianus
RyeRye
StatfordStatford
MianusMianus
RyeRye
103103
104. Subject Name Code Credit HoursSubject Name Code Credit Hours
Database System COMP 219 3Database System COMP 219 3
Second Normal Form(2NF)
• A relation R is in 2NF if and only if,
• It is in the 1NF and
• No partial dependency exists between non-key
attributes and key attributes.
• The test for 2NF involves testing for functional
dependencies whose left hand side attributes are
part of primary key. If the primary key contains a
single attribute, the test need not be applied at all.
• A relation schema R is in 2NF if every non-prime attribute
A in R is fully functionally dependent on the primary key of
R.
104104
105. Subject Name Code Credit HoursSubject Name Code Credit Hours
Database System COMP 219 3Database System COMP 219 3
Second Normal Form(2NF)
• E.G.. Consider the EMP_PROJ relation, it is in 1NF but not in 2NF,
The non-prime attribute ENAME violates 2NF because of FD2, as do the non-prime
attribute PNAME and PLOCATION because of FD2 and FD3 make ENAME, PNAME
and PLOCATION partially dependent on the primary key {SSN,PNO}, thus violating
2NF test.
105105
PNAMEPNAMESSNSSN PNOPNO ENAMEENAME PLOCATIONPLOCATIONHOURSHOURS
FD1FD1
FD2FD2
FD3FD3
EMP-PROJEMP-PROJ EMP-PROJ Relation in 1NFEMP-PROJ Relation in 1NF
106. Subject Name Code Credit HoursSubject Name Code Credit Hours
Database System COMP 219 3Database System COMP 219 3
Second Normal Form(2NF)
• The Functional dependencies FD1,FD2 and FD3 leads
to the decomposition of EMP_PROJ into the 3 relation
schemas EP1,EP2 and EP3, each of which is in 2NF.
SSNSSN PNOPNO HOURSHOURS SSNSSN ENAMEENAME
106106
EP2EP2EP1EP1
PNOPNO PNAMEPNAME PLOCATIONPLOCATION
EP3EP3
FD1FD1 FD2FD2 FD3FD3
107. Subject Name Code Credit HoursSubject Name Code Credit Hours
Database System COMP 219 3Database System COMP 219 3
Third Normal Form (3NF)
• A relation R is said to be in the 3NF if and only if
* It is in 2 NF and
* No transitive dependency exists between non-key attributes and key attributes.
E.G… Consider the relation schema EMP_DEPT
The dependency SSN DNGRSSN is transitive through DNO in EMP-DEPT,
because both the dependencies SSN DNO and DNO DNGRSSN hold
and DNO a key itself nor a subset of the key of EMP-DEPT is neither.
A relation schema R is n 3NF, if it satisfies 2NF and no non-prime attribute
of R is transitively dependent on the primary key.
ENAMEENAME SSNSSN BDATEBDATE ADDRESSADDRESS DNODNO DNAMEDNAME DNGRSSNDNGRSSN
107107
EMP_DEPT Relation Schema in 2NFEMP_DEPT Relation Schema in 2NF
108. Subject Name Code Credit HoursSubject Name Code Credit Hours
Database System COMP 219 3Database System COMP 219 3
Third Normal Form (3NF)
• We can normalize schemas ED1 and ED2,
• 3NF relation schemas ED1 and ED2
• Here ED1 and ED2 represet independent entity facts about employees and
departments.
ENAMEENAME SSNSSN BDATEBDATE ADDRESS DNOADDRESS DNO
DNODNO DNAMEDNAME DNGRSSNDNGRSSN
108108
ED1ED1
ED2ED2
109. Subject Name Code Credit HoursSubject Name Code Credit Hours
Database System COMP 219 3Database System COMP 219 3
Boyce-Codd Normal Form (BCNF)
• A relation R is said to be in BCNF, if and only if all the
determinant are candidate keys.
• BCNF relation is a strong 3NF, but not every 3NF relation is
BCNF.
• A relation schema R is in BCNF with respect to a set F of
functional dependencies if for all functional dependencies in
F of the form
• Where R and R, 109109
110. Subject Name Code Credit HoursSubject Name Code Credit Hours
Database System COMP 219 3Database System COMP 219 3
Boyce-Codd Normal Form (BCNF)
• at least one of the following holds
• 1. is trivial (i.e )
• 2. is a super key of R
110110
111. Subject Name Code Credit HoursSubject Name Code Credit Hours
Database System COMP 219 3Database System COMP 219 3
First Normal Form
• Domain is atomic if its elements are considered to be
indivisible units
– Examples of non-atomic domains:
• Set of names, composite attributes
• Identification numbers like CS101 that can be broken up into parts
• A relational schema R is in first normal form if the domains of
all attributes of R are atomic
• Non-atomic values complicate storage and encourage
redundant (repeated) storage of data
– Example: Set of accounts stored with each customer, and set of
owners stored with each account
– We assume all relations are in first normal form (and revisit this in
Chapter 9) 111111