SlideShare ist ein Scribd-Unternehmen logo
1 von 87
Downloaden Sie, um offline zu lesen
RELATIONAL DATABASES & Relational Algebra
Dr.M.Pyingkodi
Dept of MCA
Kongu Engineering College
Erode,Tamilnadu,India
pyingkodikongu@gmail.com
Query Languages
• It is a language in which a user requests
information from the db.
2
Query 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
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
3
Basic operations
• Select
– It selects tuples that satisfy a given predicate, To
denote selection.
(Sigma) is used.
4
Syntax: condition (Table name)
Select
• E.g..
5
Sal>1000 (Employee)
Selects tuples whose emp sal is > 1000
Project Operation
• It selects attributes from the relation.
• – Symbol for project
6
∏
Syntax: ∏
<Attribute list>
(Table name)
E.g…
eid,sal
(employee)
∏
Project Operation
• Combining Select & Project Operation
E.G..
7
eid,sal
(employee)
∏ Sal>1000
( )
- Selects tuples where sal>1000 & from them only eid
and salary attributes are selected.
Mathematical Set Operations
• Union Operation:
– R1 U R2 - implies that tuples either from R1 or
tuples from R2 or both R1 & R2.
• E.G..
eid ename
1
2
3
4
A
B
C
D
8
eid dno dname
1
4
5
6
10
50
60
70
XXX
YYY
ZZZ
UUU
EMP1 EMP2
Union Operation:
∏
eid
1
2
3
4
5
6
9
eid
(EMP1)
U ∏ eid
(EMP2)
SET DIFFERENCE
• R1 – R2 implies tuples present in R1 but not in
R2.
10
eid
(EMP1)
∏
eid
(EMP2)
∏
-
E.G…
eid
2
3
CARTESIAN PRODUCT
• R1 R2 allows to combine tuples from any two
relations.
• E.G.. Emp1 Emp2
11
CARTESIAN PRODUCT
Emp1.ei
d
ename Emp2.eid dno dname
1
1
1
1
2
A
A
A
A
B
1
4
5
6
1
10
50
60
70
10
XXX
YYY
ZZZ
UUU
XXX 12
eid ename
1
2
3
4
A
B
C
D
eid dno dname
1
4
5
6
10
50
60
70
XXX
YYY
ZZZ
UUU
EMP1 EMP2
Emp1 Emp2
Rename Operation
• To rename the name of a relation or the name of an
attribute.
13
X
Relational Algebra Expression
rho
Returns the result of E under
the name x and with the
attributes renamed
A1,A2,……. An
(E)
A.INTERSECTION
eid
1
4
14
U
eid
(EMP1)
∏
eid
(EMP2)
∏
U
E.G…
2. ADDITIONAL OPERATIONS
(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:
15
(B) NATURAL JOIN OR EQUI JOIN
• Syntax:
• relation1 relation2 – Natural Join
• relation1 relation2 – EQUI Join
16
condition
(B) NATURAL JOIN OR EQUI JOIN
• E.g..
eid ename sal dno
1
2
3
4
X
Y
Z
A
10000
20000
30000
16000
10
20
10
40
Dno Dname mgr
10
20
30
XXX
YYY
ZZZ
ABC
PQR
MNO
17
EMPLOYEE DEPARTMENT
(B) NATURAL JOIN OR EQUI JOIN
• Employee
eid ename sal dno dname mgr
1
2
3
X
Y
Z
10000
20000
30000
10
20
10
XXX
YYY
xxx
ABC
PQR
ABC
18
Dept or
Employee
Employee.dno = department.dno
Dept
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
19
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…
20
LEFT OUTER JOIN-
21
eid ename sal dno
1
2
3
X
Y
Z
10000
20000
30000
10
20
40
EMPLOYEE
Dno Dname mgr
10
20
30
XXX
YYY
ZZZ
ABC
PQR
MNO
DEPARTMENT
EMPLOYEE DEPARTMENT
LEFT OUTER JOIN-
Eid Ename Sal Dno Dname mgr
1
2
3
X
Y
Z
10000
20000
30000
10
20
40
XXX
YYY
NULL
ABC
PQR
NULL
22
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.
23
RIGHT OUTER JOIN-
∏
eid sal dname
1
2
3
null
10000
20000
30000
null
XXX
ZZZ
ZZZ
MNO
24
Eid,dname,sal
(EMPLOYEE DEPARTMENT)
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.
25
FULL OUTER JOIN
26
∏
Eid,dname,sal
(EMPLOYEE DEPARTMENT)
eid sal dname
1
2
3
NULL
10000
20000
30000
NULL
XXX
YYY
null
AAA
Division Operation
• It is denoted by . It is suited to queries that
include the phrase ‘for all’.
• E.G.. Consider three relations
Acc-No. Branch-
name
Balance
B-101
B-102
B-103
B-104
B-105
P
Q
R
S
T
5000
4000
9000
7000
3500
27
Account
Division Operation
Branch-
Name
Branch-city assets
Q
P
R
T
S
Adayar
Adayar
Velachery
T-Nagar
Kodambakkam
10000
20000
9000
871110
600000
28
Customer-
Name
Acc-No.
Anu
Uma
Rekha
Ganesh
Rajesh
B-102
B-103
B-101
B-105
B-104
Depositor Branch:
Division Operation
• Suppose we want to find all customers who have an
account at all the branches located in Adayar.
• The query is,
•
29
∏ Customer-name,
branch-name
(depositor account)
∏branch-name Branch_city
= “Adayar” (branch)
( )
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
• R2=
30
∏branch-name Branch_city
( )
= “Adayar” (branch)
∏ Customer-name,
branch-name
(depositor account)
Division Operation
• Step3:
• We need to find customers who appear in r2
with every branch name in R1.
31
∏ Customer-name,
branch-name
(depositor account)
∏branch-name Branch_city
= “Adayar” (branch)
( )
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:
eid ename Salary
1
2
3
4
5
A
B
C
D
E
10000
20000
30000
4000
5000
32
O/p of
Sum (salary)
(r)
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’.
33
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.
34
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
35
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.
36
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.
37
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..
38
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.
39
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]);
40
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
41
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.
42
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;
43
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);
44
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;
45
alter table customer modify cust_id varchar2(6);
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.
46
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;
47
BASIC STRUCTURE
• II. Data Retrievalretrieves 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;
48
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;
49
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”.
50
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
number
51
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
52
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%’;
53
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%’;
54
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.
55
U
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);
56
Set Operations
• 3. Find all customers who have an account but no
loan,
• (select customer_name from depositor) except
(select customer_name from borrower);
57
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
58
AGGREGATE FUNCTIONS
• Find the average account balance at the
adayar branch.
• Select avg(balance) from account where
branch_name=“adayar”;
59
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.
60
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.
61
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.
62
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;
63
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’;
64
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.
65
Parts of SQL
• Data Control Language (DCL): gives and removes right to
oracle objects.
• E.g.. GRANT,REVOKE
66
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..
67
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.
68
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.
69
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;
…………..
}
70
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.
71
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.
72
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.
73
Constraints on a single relation
• In addition to Primary key constraint, the
allowed In.Consts are,
• Not null
• Unique
• Check(<predicate>)
74
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
75
Constraints on a single relation
• Unique:
• unique(A1,A2,….An)
• No 2 tuples in the relation can be equal to all
primary key attributes.
76
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))
77
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));
78
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.
79
E1 R E2
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));
80
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>
81
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.
82
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.
83
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.
84
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.
85
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.
86
SECURITY
5. Human Level:
Protection from stolen passwords etc…
Solution:
* Frequent change of passwords.
* Data audits
87

Weitere ähnliche Inhalte

Ähnlich wie Relational Database and Relational Algebra

E212d9a797dbms chapter3 b.sc2
E212d9a797dbms chapter3 b.sc2E212d9a797dbms chapter3 b.sc2
E212d9a797dbms chapter3 b.sc2
Mukund Trivedi
 
E212d9a797dbms chapter3 b.sc2 (2)
E212d9a797dbms chapter3 b.sc2 (2)E212d9a797dbms chapter3 b.sc2 (2)
E212d9a797dbms chapter3 b.sc2 (2)
Mukund Trivedi
 
E212d9a797dbms chapter3 b.sc2 (1)
E212d9a797dbms chapter3 b.sc2 (1)E212d9a797dbms chapter3 b.sc2 (1)
E212d9a797dbms chapter3 b.sc2 (1)
Mukund Trivedi
 
Lecture 06 relational algebra and calculus
Lecture 06 relational algebra and calculusLecture 06 relational algebra and calculus
Lecture 06 relational algebra and calculus
emailharmeet
 

Ähnlich wie Relational Database and Relational Algebra (20)

Relational Algebra
Relational AlgebraRelational Algebra
Relational Algebra
 
354 ch6
354 ch6354 ch6
354 ch6
 
Relational Algebra1.ppt
Relational Algebra1.pptRelational Algebra1.ppt
Relational Algebra1.ppt
 
Relational model
Relational modelRelational model
Relational model
 
Relational algebra in dbms
Relational algebra in dbmsRelational algebra in dbms
Relational algebra in dbms
 
Query Decomposition and data localization
Query Decomposition and data localization Query Decomposition and data localization
Query Decomposition and data localization
 
DDL,DML,1stNF
DDL,DML,1stNFDDL,DML,1stNF
DDL,DML,1stNF
 
sfdfds
sfdfdssfdfds
sfdfds
 
Database Management System-session 3-4-5
Database Management System-session 3-4-5Database Management System-session 3-4-5
Database Management System-session 3-4-5
 
316_16SCCCS4_2020052505222431.pptdatabasex
316_16SCCCS4_2020052505222431.pptdatabasex316_16SCCCS4_2020052505222431.pptdatabasex
316_16SCCCS4_2020052505222431.pptdatabasex
 
E212d9a797dbms chapter3 b.sc2
E212d9a797dbms chapter3 b.sc2E212d9a797dbms chapter3 b.sc2
E212d9a797dbms chapter3 b.sc2
 
E212d9a797dbms chapter3 b.sc2 (2)
E212d9a797dbms chapter3 b.sc2 (2)E212d9a797dbms chapter3 b.sc2 (2)
E212d9a797dbms chapter3 b.sc2 (2)
 
E212d9a797dbms chapter3 b.sc2 (1)
E212d9a797dbms chapter3 b.sc2 (1)E212d9a797dbms chapter3 b.sc2 (1)
E212d9a797dbms chapter3 b.sc2 (1)
 
Unit 04 dbms
Unit 04 dbmsUnit 04 dbms
Unit 04 dbms
 
Relational Algebra.ppt
Relational Algebra.pptRelational Algebra.ppt
Relational Algebra.ppt
 
Unit04 dbms
Unit04 dbmsUnit04 dbms
Unit04 dbms
 
Relational algebra calculus
Relational algebra  calculusRelational algebra  calculus
Relational algebra calculus
 
Relational algebra
Relational algebraRelational algebra
Relational algebra
 
DDL and DML statements.pptx
DDL and DML statements.pptxDDL and DML statements.pptx
DDL and DML statements.pptx
 
Lecture 06 relational algebra and calculus
Lecture 06 relational algebra and calculusLecture 06 relational algebra and calculus
Lecture 06 relational algebra and calculus
 

Mehr von Pyingkodi Maran

Mehr von Pyingkodi Maran (20)

Health Monitoring System using IoT.doc
Health Monitoring System using IoT.docHealth Monitoring System using IoT.doc
Health Monitoring System using IoT.doc
 
IoT Industry Adaptation of AI.ppt
IoT Industry Adaptation of AI.pptIoT Industry Adaptation of AI.ppt
IoT Industry Adaptation of AI.ppt
 
IoT_Testing.ppt
IoT_Testing.pptIoT_Testing.ppt
IoT_Testing.ppt
 
Azure Devops
Azure DevopsAzure Devops
Azure Devops
 
Creation of Web Portal using DURPAL
Creation of Web Portal using DURPALCreation of Web Portal using DURPAL
Creation of Web Portal using DURPAL
 
AWS Relational Database Instance
AWS Relational Database InstanceAWS Relational Database Instance
AWS Relational Database Instance
 
AWS S3 Buckets
AWS S3  BucketsAWS S3  Buckets
AWS S3 Buckets
 
Creation of AWS Instance in Cloud Platform
Creation of AWS Instance in Cloud PlatformCreation of AWS Instance in Cloud Platform
Creation of AWS Instance in Cloud Platform
 
Amazon Web Service.pdf
Amazon Web Service.pdfAmazon Web Service.pdf
Amazon Web Service.pdf
 
Cloud Security
Cloud SecurityCloud Security
Cloud Security
 
Cloud Computing Introduction
Cloud Computing IntroductionCloud Computing Introduction
Cloud Computing Introduction
 
Supervised Machine Learning Algorithm
Supervised Machine Learning AlgorithmSupervised Machine Learning Algorithm
Supervised Machine Learning Algorithm
 
Unsupervised Learning in Machine Learning
Unsupervised Learning in Machine LearningUnsupervised Learning in Machine Learning
Unsupervised Learning in Machine Learning
 
Feature Engineering in Machine Learning
Feature Engineering in Machine LearningFeature Engineering in Machine Learning
Feature Engineering in Machine Learning
 
Normalization in DBMS
Normalization in DBMSNormalization in DBMS
Normalization in DBMS
 
Transaction in DBMS
Transaction in DBMSTransaction in DBMS
Transaction in DBMS
 
IoT_Frameworks_.pdf
IoT_Frameworks_.pdfIoT_Frameworks_.pdf
IoT_Frameworks_.pdf
 
IoT Real world Applications.pdf
IoT Real world Applications.pdfIoT Real world Applications.pdf
IoT Real world Applications.pdf
 
IoT_Introduction.pdf
IoT_Introduction.pdfIoT_Introduction.pdf
IoT_Introduction.pdf
 
Keys in DBMS
Keys in DBMSKeys in DBMS
Keys in DBMS
 

Kürzlich hochgeladen

VIP Call Girls Palanpur 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Palanpur 7001035870 Whatsapp Number, 24/07 BookingVIP Call Girls Palanpur 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Palanpur 7001035870 Whatsapp Number, 24/07 Booking
dharasingh5698
 
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
ssuser89054b
 
Call Girls In Bangalore ☎ 7737669865 🥵 Book Your One night Stand
Call Girls In Bangalore ☎ 7737669865 🥵 Book Your One night StandCall Girls In Bangalore ☎ 7737669865 🥵 Book Your One night Stand
Call Girls In Bangalore ☎ 7737669865 🥵 Book Your One night Stand
amitlee9823
 
Call Girls in Ramesh Nagar Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
Call Girls in Ramesh Nagar Delhi 💯 Call Us 🔝9953056974 🔝 Escort ServiceCall Girls in Ramesh Nagar Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
Call Girls in Ramesh Nagar Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
9953056974 Low Rate Call Girls In Saket, Delhi NCR
 
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
dollysharma2066
 
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 BookingVIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
dharasingh5698
 
AKTU Computer Networks notes --- Unit 3.pdf
AKTU Computer Networks notes ---  Unit 3.pdfAKTU Computer Networks notes ---  Unit 3.pdf
AKTU Computer Networks notes --- Unit 3.pdf
ankushspencer015
 
Call Girls in Netaji Nagar, Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
Call Girls in Netaji Nagar, Delhi 💯 Call Us 🔝9953056974 🔝 Escort ServiceCall Girls in Netaji Nagar, Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
Call Girls in Netaji Nagar, Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
9953056974 Low Rate Call Girls In Saket, Delhi NCR
 

Kürzlich hochgeladen (20)

Top Rated Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
Top Rated  Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...Top Rated  Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
Top Rated Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
 
Call Girls Walvekar Nagar Call Me 7737669865 Budget Friendly No Advance Booking
Call Girls Walvekar Nagar Call Me 7737669865 Budget Friendly No Advance BookingCall Girls Walvekar Nagar Call Me 7737669865 Budget Friendly No Advance Booking
Call Girls Walvekar Nagar Call Me 7737669865 Budget Friendly No Advance Booking
 
Thermal Engineering -unit - III & IV.ppt
Thermal Engineering -unit - III & IV.pptThermal Engineering -unit - III & IV.ppt
Thermal Engineering -unit - III & IV.ppt
 
VIP Call Girls Palanpur 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Palanpur 7001035870 Whatsapp Number, 24/07 BookingVIP Call Girls Palanpur 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Palanpur 7001035870 Whatsapp Number, 24/07 Booking
 
Double rodded leveling 1 pdf activity 01
Double rodded leveling 1 pdf activity 01Double rodded leveling 1 pdf activity 01
Double rodded leveling 1 pdf activity 01
 
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
 
Call Girls In Bangalore ☎ 7737669865 🥵 Book Your One night Stand
Call Girls In Bangalore ☎ 7737669865 🥵 Book Your One night StandCall Girls In Bangalore ☎ 7737669865 🥵 Book Your One night Stand
Call Girls In Bangalore ☎ 7737669865 🥵 Book Your One night Stand
 
Thermal Engineering Unit - I & II . ppt
Thermal Engineering  Unit - I & II . pptThermal Engineering  Unit - I & II . ppt
Thermal Engineering Unit - I & II . ppt
 
Work-Permit-Receiver-in-Saudi-Aramco.pptx
Work-Permit-Receiver-in-Saudi-Aramco.pptxWork-Permit-Receiver-in-Saudi-Aramco.pptx
Work-Permit-Receiver-in-Saudi-Aramco.pptx
 
University management System project report..pdf
University management System project report..pdfUniversity management System project report..pdf
University management System project report..pdf
 
Water Industry Process Automation & Control Monthly - April 2024
Water Industry Process Automation & Control Monthly - April 2024Water Industry Process Automation & Control Monthly - April 2024
Water Industry Process Automation & Control Monthly - April 2024
 
Call Girls in Ramesh Nagar Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
Call Girls in Ramesh Nagar Delhi 💯 Call Us 🔝9953056974 🔝 Escort ServiceCall Girls in Ramesh Nagar Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
Call Girls in Ramesh Nagar Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
 
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
 
Double Revolving field theory-how the rotor develops torque
Double Revolving field theory-how the rotor develops torqueDouble Revolving field theory-how the rotor develops torque
Double Revolving field theory-how the rotor develops torque
 
(INDIRA) Call Girl Bhosari Call Now 8617697112 Bhosari Escorts 24x7
(INDIRA) Call Girl Bhosari Call Now 8617697112 Bhosari Escorts 24x7(INDIRA) Call Girl Bhosari Call Now 8617697112 Bhosari Escorts 24x7
(INDIRA) Call Girl Bhosari Call Now 8617697112 Bhosari Escorts 24x7
 
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
 
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 BookingVIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
 
AKTU Computer Networks notes --- Unit 3.pdf
AKTU Computer Networks notes ---  Unit 3.pdfAKTU Computer Networks notes ---  Unit 3.pdf
AKTU Computer Networks notes --- Unit 3.pdf
 
Call Girls in Netaji Nagar, Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
Call Girls in Netaji Nagar, Delhi 💯 Call Us 🔝9953056974 🔝 Escort ServiceCall Girls in Netaji Nagar, Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
Call Girls in Netaji Nagar, Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
 
KubeKraft presentation @CloudNativeHooghly
KubeKraft presentation @CloudNativeHooghlyKubeKraft presentation @CloudNativeHooghly
KubeKraft presentation @CloudNativeHooghly
 

Relational Database and Relational Algebra

  • 1. RELATIONAL DATABASES & Relational Algebra Dr.M.Pyingkodi Dept of MCA Kongu Engineering College Erode,Tamilnadu,India pyingkodikongu@gmail.com
  • 2. Query Languages • It is a language in which a user requests information from the db. 2 Query 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. 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 3
  • 4. Basic operations • Select – It selects tuples that satisfy a given predicate, To denote selection. (Sigma) is used. 4 Syntax: condition (Table name)
  • 5. Select • E.g.. 5 Sal>1000 (Employee) Selects tuples whose emp sal is > 1000
  • 6. Project Operation • It selects attributes from the relation. • – Symbol for project 6 ∏ Syntax: ∏ <Attribute list> (Table name) E.g… eid,sal (employee) ∏
  • 7. Project Operation • Combining Select & Project Operation E.G.. 7 eid,sal (employee) ∏ Sal>1000 ( ) - Selects tuples where sal>1000 & from them only eid and salary attributes are selected.
  • 8. Mathematical Set Operations • Union Operation: – R1 U R2 - implies that tuples either from R1 or tuples from R2 or both R1 & R2. • E.G.. eid ename 1 2 3 4 A B C D 8 eid dno dname 1 4 5 6 10 50 60 70 XXX YYY ZZZ UUU EMP1 EMP2
  • 10. SET DIFFERENCE • R1 – R2 implies tuples present in R1 but not in R2. 10 eid (EMP1) ∏ eid (EMP2) ∏ - E.G… eid 2 3
  • 11. CARTESIAN PRODUCT • R1 R2 allows to combine tuples from any two relations. • E.G.. Emp1 Emp2 11
  • 12. CARTESIAN PRODUCT Emp1.ei d ename Emp2.eid dno dname 1 1 1 1 2 A A A A B 1 4 5 6 1 10 50 60 70 10 XXX YYY ZZZ UUU XXX 12 eid ename 1 2 3 4 A B C D eid dno dname 1 4 5 6 10 50 60 70 XXX YYY ZZZ UUU EMP1 EMP2 Emp1 Emp2
  • 13. Rename Operation • To rename the name of a relation or the name of an attribute. 13 X Relational Algebra Expression rho Returns the result of E under the name x and with the attributes renamed A1,A2,……. An (E)
  • 15. (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: 15
  • 16. (B) NATURAL JOIN OR EQUI JOIN • Syntax: • relation1 relation2 – Natural Join • relation1 relation2 – EQUI Join 16 condition
  • 17. (B) NATURAL JOIN OR EQUI JOIN • E.g.. eid ename sal dno 1 2 3 4 X Y Z A 10000 20000 30000 16000 10 20 10 40 Dno Dname mgr 10 20 30 XXX YYY ZZZ ABC PQR MNO 17 EMPLOYEE DEPARTMENT
  • 18. (B) NATURAL JOIN OR EQUI JOIN • Employee eid ename sal dno dname mgr 1 2 3 X Y Z 10000 20000 30000 10 20 10 XXX YYY xxx ABC PQR ABC 18 Dept or Employee Employee.dno = department.dno Dept
  • 19. 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 19
  • 20. 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… 20
  • 21. LEFT OUTER JOIN- 21 eid ename sal dno 1 2 3 X Y Z 10000 20000 30000 10 20 40 EMPLOYEE Dno Dname mgr 10 20 30 XXX YYY ZZZ ABC PQR MNO DEPARTMENT EMPLOYEE DEPARTMENT
  • 22. LEFT OUTER JOIN- Eid Ename Sal Dno Dname mgr 1 2 3 X Y Z 10000 20000 30000 10 20 40 XXX YYY NULL ABC PQR NULL 22
  • 23. 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. 23
  • 24. RIGHT OUTER JOIN- ∏ eid sal dname 1 2 3 null 10000 20000 30000 null XXX ZZZ ZZZ MNO 24 Eid,dname,sal (EMPLOYEE DEPARTMENT)
  • 25. 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. 25
  • 26. FULL OUTER JOIN 26 ∏ Eid,dname,sal (EMPLOYEE DEPARTMENT) eid sal dname 1 2 3 NULL 10000 20000 30000 NULL XXX YYY null AAA
  • 27. Division Operation • It is denoted by . It is suited to queries that include the phrase ‘for all’. • E.G.. Consider three relations Acc-No. Branch- name Balance B-101 B-102 B-103 B-104 B-105 P Q R S T 5000 4000 9000 7000 3500 27 Account
  • 29. Division Operation • Suppose we want to find all customers who have an account at all the branches located in Adayar. • The query is, • 29 ∏ Customer-name, branch-name (depositor account) ∏branch-name Branch_city = “Adayar” (branch) ( )
  • 30. 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 • R2= 30 ∏branch-name Branch_city ( ) = “Adayar” (branch) ∏ Customer-name, branch-name (depositor account)
  • 31. Division Operation • Step3: • We need to find customers who appear in r2 with every branch name in R1. 31 ∏ Customer-name, branch-name (depositor account) ∏branch-name Branch_city = “Adayar” (branch) ( )
  • 32. 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: eid ename Salary 1 2 3 4 5 A B C D E 10000 20000 30000 4000 5000 32 O/p of Sum (salary) (r)
  • 33. 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’. 33
  • 34. 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. 34
  • 35. 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 35
  • 36. 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. 36
  • 37. 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. 37
  • 38. 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.. 38
  • 39. 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. 39
  • 40. 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]); 40
  • 41. 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 41
  • 42. 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. 42
  • 43. 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; 43
  • 44. 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); 44
  • 45. 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; 45 alter table customer modify cust_id varchar2(6);
  • 46. 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. 46
  • 47. 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; 47
  • 48. BASIC STRUCTURE • II. Data Retrievalretrieves 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; 48
  • 49. 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; 49
  • 50. 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”. 50
  • 51. 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 number 51
  • 52. 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 52
  • 53. 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%’; 53
  • 54. 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%’; 54
  • 55. 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. 55 U
  • 56. 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); 56
  • 57. Set Operations • 3. Find all customers who have an account but no loan, • (select customer_name from depositor) except (select customer_name from borrower); 57
  • 58. 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 58
  • 59. AGGREGATE FUNCTIONS • Find the average account balance at the adayar branch. • Select avg(balance) from account where branch_name=“adayar”; 59
  • 60. 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. 60
  • 61. 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. 61
  • 62. 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. 62
  • 63. 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; 63
  • 64. 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’; 64
  • 65. 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. 65
  • 66. Parts of SQL • Data Control Language (DCL): gives and removes right to oracle objects. • E.g.. GRANT,REVOKE 66
  • 67. 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.. 67
  • 68. 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. 68
  • 69. 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. 69
  • 70. 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; ………….. } 70
  • 71. 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. 71
  • 72. 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. 72
  • 73. 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. 73
  • 74. Constraints on a single relation • In addition to Primary key constraint, the allowed In.Consts are, • Not null • Unique • Check(<predicate>) 74
  • 75. 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 75
  • 76. Constraints on a single relation • Unique: • unique(A1,A2,….An) • No 2 tuples in the relation can be equal to all primary key attributes. 76
  • 77. 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)) 77
  • 78. 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)); 78
  • 79. 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. 79 E1 R E2
  • 80. 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)); 80
  • 81. 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> 81
  • 82. 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. 82
  • 83. 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. 83
  • 84. 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. 84
  • 85. 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. 85
  • 86. 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. 86
  • 87. SECURITY 5. Human Level: Protection from stolen passwords etc… Solution: * Frequent change of passwords. * Data audits 87