0% found this document useful (0 votes)
9 views20 pages

Module 2

Uploaded by

boobeshwaran80
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
9 views20 pages

Module 2

Uploaded by

boobeshwaran80
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 20

Module-2:

LECTURE-19

Relational Algebra:
Basic operations:
1. Selection (σ) Selects a subset of rows from relation.
2. Projection (π) Selects a subset of columns from relation.
3. Cross-product (×) Allows us to combine two relations.
4. Set-difference ( ) Tuples in relation. 1, but not in relationn. 2.
5. Union (U) Tuples in reln. 1 and in reln. 2.
6. Rename( ρ) Use new name for the Tables or fields.
Additional operations:
7. Intersection (∩), Join( ), Division(÷): Not essential, but (very!) useful.
Since each operation returns a relation, operations can be composed! (Algebra is
“closed”.)
Projection
 Deletes attributes that are not in projection list.
 Schema of result contains exactly the fields in the projection list, with the same names that
they had in the (only) input relation. ( Unary Operation)
 Projection operator has to eliminate duplicates! (as it returns a relation which is a set)
o Note: real systems typically don’t do duplicate elimination unless the user explicitly
asks for it. (Duplicate values may be representing different real world entity or
relationship).
Example: Consider the BOOK table:
Acc-No Title Author
100 “DBMS” “Silbershatz”
200 “DBMS” “Ramanuj”
300 “COMPILER” “Silbershatz”
400 “COMPILER” “Ullman”
500 “OS” “Sudarshan”
600 “DBMS” “Silbershatz”

Title

πTitle(BOOK) = “DBMS”
“COMPILER”
“OS”

Selection
 Selects rows that satisfy selection condition.
 No duplicates in result
 Schema of result identical to schema of (only) input relation.
 Result relation can be the input for another relational algebra operation! (Operator
composition.)
Example: For the example given above:
σAcc-no>300(BOOK) =
Acc- Title Author
No
400 “COMPILER “Ullman”

500 “OS” “Sudarshan”
600 “DBMS” “Silbershatz”
σTitle=”DBMS”(BOOK)=

Acc- Title Author


No
100 “DBMS” “Silbershatz”
200 “DBMS” “Ramanuj”
600 “DBMS” “Silbershatz”

πAcc-no (σTitle=”DBMS” (BOOK))= Acc-


No
100
200
600

Union, Intersection, Set-Difference

 All of these operations take two input relations, which must be union-compatible:
o Same number of fields.
o Corresponding’ fields have the same type.
 What is the schema of result?
Consider:
Borrower Depositor
Cust- Loan-no Cust-name Acc-no
name Suleman A-100
Ram L-13 Radheshyam A-300
Shyam L-30 Ram A-401
Suleman L-42

List of customers who are either borrower or depositor at bank= πCust-name (Borrower) U
πCust-name (Depositor)=

Cust-name
Ram
Shyam Customers who are both borrowers and depositors = πCust-name
Suleman (Borrower) ∩ πCust-name (Depositor)=
Radeshyam
Cust-
name
Ram
Suleman

Customers who are borrowers but not depositors = πCust-name (Borrower) πCust-name
(Depositor)=

Cust-name

Shyam
Cartesian-Product or Cross-Product (S1 × R1)
 Each row of S1 is paired with each row of R1.
 Result schema has one field per field of S1 and R1, with field names `inherited’ if possible.
 Consider the borrower and loan tables as follows:

Borrower: Loan:
Cust-name Loan-no Loan-no Amount
Ram L-13 L-13 1000
Shyam L-30 L-30 20000
Suleman L-42 L-42 40000

Cross product of Borrower and Loan, Borrower × Loan =

Borrower.Cust- Borrower.Loan- Loan.Loan- Loan.Amount


name no no
Ram L-13 L-13 1000
Ram L-13 L-30 20000
Ram L-13 L-42 40000
Shyam L-30 L-13 1000
Shyam L-30 L-30 20000
Shyam L-30 L-42 40000
Suleman L-42 L-13 1000
Suleman L-42 L-30 20000
Suleman L-42 L-42 40000

The rename operation can be used to rename the fields to avoid confusion when two field names are
same in two participating tables:

For example the statement, ρLoan-borrower(Cust-name,Loan-No-1, Loan-No-2,Amount)( Borrower × Loan) results


into- A new Table named Loan-borrower is created where it has four fields which are renamed as
Cust-name, Loan-No-1, Loan-No-2 and Amount and the rows contains the same data as the cross
product of Borrower and Loan.

Loan-borrower:
Cust- Loan-No-1 Loan- Amount
name No-2
Ram L-13 L-13 1000
Ram L-13 L-30 20000
Ram L-13 L-42 40000
Shyam L-30 L-13 1000
Shyam L-30 L-30 20000
Shyam L-30 L-42 40000
Suleman L-42 L-13 1000
Suleman L-42 L-30 20000
Suleman L-42 L-42 40000
Rename Operation:
It can be used in two ways :
 return the result of expression E in the table named x.
 return the result of expression E in the table named x with the attributes
renamed to A1, A2,…, An.
 It’s benefit can be understood by the solution of the query “ Find the largest account balance
in the bank”
It can be solved by following steps:
 Find out the relation of those balances which are not largest.
 Consider Cartesion product of Account with itself i.e. Account × Account
 Compare the balances of first Account table with balances of second Account table in the
product.
 For that we should rename one of the account table by some other name to avoid the
confusion
It can be done by following operation
ΠAccount.balance (σAccount.balance < d.balance(Account× ρd(Account))
 So the above relation contains the balances which are not largest.
 Subtract this relation from the relation containing all the balances i.e . Πbalance (Account).
So the final statement for solving above query is
Πbalance (Account)- ΠAccount.balance (σAccount.balance < d.balance(Account× ρd(Account))
LECTURE-20

Additional Operations
Natural Join ( )
 Forms Cartesian product of its two arguments, performs selection forcing equality on
those attributes that appear in both relations
 For example consider Borrower and Loan relations, the natural join between them
will automatically perform the selection on the table returned by
Borrower × Loan which force equality on the attribute that appear in both Borrower
and Loan i.e. Loan-no and also will have only one of the column named Loan-No.
 That means = σBorrower.Loan-no = Loan.Loan-no (Borrower × Loan).
 The table returned from this will be as follows:

Eliminate rows that does not satisfy the selection criteria “σBorrower.Loan-no = Loan.Loan-no” from Borrower
× Loan =
Borrower.Cust- Borrower.Loan- Loan.Loan- Loan.Amount
name no no
Ram L-13 L-13 1000
Ram L-13 L-30 20000
Ram L-13 L-42 40000
Shyam L-30 L-13 1000
Shyam L-30 L-30 20000
Shyam L-30 L-42 40000
Suleman L-42 L-13 1000
Suleman L-42 L-30 20000
Suleman L-42 L-42 40000

And will remove one of the column named Loan-no.

 i.e. =
Cust-name Loan-no Amount
Ram L-13 1000
Shyam L-30 20000
Suleman L-42 40000

Division Operation:
 denoted by ÷ is used for queries that include the phrase “for all”.
 For example “Find customers who has an account in all branches in branch city
Agra”. This query can be solved by following statement.
ΠCustomer-name. branch-name ( ) ÷ Πbranch-name (σBranch-city=”Agra”(Branch)
 The division operations can be specified by using only basic operations as follows:
Let r(R) and s(S) be given relations for schema R and S with
r ÷ s = ΠR-S(r) - ΠR-S ((ΠR-S (r) × s) - ΠR-S,S (r))
LECTURE-21

Tuple Relational Calculus

Relational algebra is an example of procedural language while tuple relational calculus is a


nonprocedural query language.
A query is specified as:
{t | P(t)}, i.e it is the set of all tuples t such that predicate P is true for t.

The formula P(t) is formed using atoms which uses the relations, tuples of relations and fields of
tuples and following symbols

These atoms can then be used to form formulas with following symbols

For example : here are some queries and a way to express them using tuple calculus:
o Find the branch-name, loan-number and amount for loans over Rs 1200.
.

o Find the loan number for each loan of an amount greater that Rs1200.

o Find the names of all the customers who have a loan from the Sadar branch.

o Find all customers who have a loan , an account, or both at the bank

o Find only those customers who have both an account and a loan.

o Find all customers who have an account but do not have loan.

o Find all customers who have an account at all branches located in Agra
Domain Relational Calculus
1. Domain relational calculus is another non procedural language for expressing database
queries.
2. A query is specified as:
{<x1,x2,…,xn> | P(x1,x2,…,xn)} where x1,x2,…,xn represents domain variables. P represent a
predicate formula as in tuple calculus
 Since the domain variables are referred in place of tuples the formula doesn’t refer the fields
of tuples rather they refer the domain variables.
 For example the queries in domain calculus are mentioned as follows:
o Find the branch-name, loan-number and amount for loans over Rs 1200.
.
o Find the loan number for each loan of an amount greater that Rs1200.

o Find the names of all the customers who have a loan from the Sadar branch and find
the loan amount

o Find names of all customers who have a loan , an account, or both at the Sadar
Branch

o Find only those customers who have both an account and a loan.

o Find all customers who have an account but do not have loan.

o Find all customers who have an account at all branches located in Agra

Outer Join.
Outer join operation is an extension of join operation to deal with missing information
 Suppose that we have following relational schemas:
Employee( employee-name, street, city)
Fulltime-works(employee-name, branch-name, salary)
A snapshot of these relations is as follows:
Employee: employee- street city
name
Ram M G Road Agra
Shyam New Mandi Mathura
Road
Suleman Bhagat Singh Aligarh
Road
Fulltime-works

employee- branch- salary


name name
Ram Sadar 30000
Shyam Sanjay Place 20000
Rehman Dayalbagh 40000
Suppose we want complete information of the full time employees.
 The natural join ( )will result into the loss of information for
Suleman and Rehman because they don’t have record in both the tables ( left and right
relation). The outer join will solve the problem.
 Three forms of outer join:
o Left outer join( :the tuples which doesn’t match while doing natural join from
left relation are also added in the result putting null values in missing field of right
relation.
o Right outer join( :the tuples which doesn’t match while natural join from right
relation are also added in the result putting null values in missing field of left
relation.
o Full outer join( ): include both of the left and right outer joins i.e. adds the
tuples which did not match either in left relation or right relation and put null in
place of missing values.
 The result for three forms of outer join are as follows:
Left join: =
employee- street City branch- salary
name name
Ram M G Road Agra Sadar 30000
Shyam New Mandi Mathura Sanjay 20000
Road Place
Suleman Bhagat Singh Aligarh Null Null
Road

Right join: =

employee- street city branch- salary


name name
Ram M G Road Agra Sadar 30000
Shyam New Mandi Mathura Sanjay 20000
Road Place
Rehman null null Dayalbagh 40000

Full join: =

employee- street city branch- salary


name name
Ram M G Road Agra Sadar 30000
Shyam New Mandi Mathura Sanjay 20000
Road Place
Suleman Bhagat Singh Aligarh null null
Road
Rehman null null Dayalbagh 40000
LECTURE-22

Structured Query Language (SQL)


Introduction
Commercial database systems use more user friendly language to specify the queries.
SQL is the most influential commercially marketed product language.
Other commercially used languages are QBE, Quel, and Datalog.
Basic Structure
 The basic structure of an SQL consists of three clauses: select, from and where.
 select: it corresponds to the projection operation of relational algebra. Used to list the
attributes desired in the result.
 from: corresponds to the Cartesian product operation of relational algebra. Used to list the
relations to be scanned in the evaluation of the expression
 where: corresponds to the selection predicate of the relational algebra. It consists of a
predicate involving attributes of the relations that appear in the from clause.
 A typical SQL query has the form:
select A1, A2,…, An
from r1, r2,…, rm
where P
o Ai represents an attribute
o rj represents a relation
o P is a predicate
o It is equivalent to following relational algebra expression:
o
[Note: The words marked in dark in this text work as keywords in SQL language. For example
“select”, “from” and “where” in the above paragraph are shown in bold font to indicate that they
are keywords]
Select Clause
Let us see some simple queries and use of select clause to express them in SQL.
 Find the names of all branches in the Loan relation
select branch-name
from Loan
 By default the select clause includes duplicate values. If we want to force the elimination of
duplicates the distinct keyword is used as follows:
select distinct branch-name
from Loan
 The all key word can be used to specify explicitly that duplicates are not removed. Even if
we not use all it means the same so we don’t require all to use in select clause.
select all branch-name
from Loan
 The asterisk “*” can be used to denote “all attributes”. The following SQL statement will
select and all the attributes of Loan.
select *
from Loan
 The arithmetic expressions involving operators, +, -, *, and / are also allowed in select
clause. The following statement will return the amount multiplied by 100 for the rows in
Loan table.
select branch-name, loan-number, amount * 10 from Loan.
Where Clause
 Find all loan numbers for loans made at “Sadar” branch with loan amounts greater than Rs
1200.
select loan-number
from Loan
where branch-name= “Sadar” and amount > 1200
 where clause uses uses logival connectives and, or, and not
 operands of the logical connectives can be expressions involving the comparison operators
<, <=, >, >=, =, and < >.
 between can be used to simplify the comparisons
select loan-number
from Loan
where amount between 90000 and 100000
From Clause
 The from clause by itself defines a Cartesian product of the relations in the clause.
 When an attribute is present in more than one relation they can be referred as relation-
name.attribute-name to avoid the ambiguity.
 For all customers who have loan from the bank, find their names and loan numbers
select distinct customer-name, Borrower.loan-number
from Borrower, Loan
where Borrower.loan-number = Loan.loan-number
The Rename Operation
 Used for renaming both relations both relations and attributes in SQL
 Use as clause: old-name as new-name
 Find the names and loan numbers of the customers who have a loan at the “Sadar” branch.
select distinct customer-name, borrower.loan-number as loan-id
from Borrower, Loan
where Borrower.loan-number = Loan.loan-number and
branch-name = “Sadar”
we can now refer the loan-number instead by the name loan-id.
 For all customers who have a loan from the bank, find their names and loan-numbers.
select distinct customer-name, T.loan-number
from Borrower as T, Loan as S
where T.loan-number = S.loan-number
 Find the names of all branches that have assets greater than at least one branch located in
“Mathura”.
select distinct T.branch-name
from branch as T, branch as S
where T.assets > S.assets and S.branch-city = “Mathura”
String Operation
 Two special characters are used for pattern matching in strings:
o Percent ( % ) : The % character matches any substring
o Underscore( _ ): The _ character matches any character
 “%Mandi”: will match with the strings ending with “Mandi” viz. “Raja Ki mandi”, “Peepal
Mandi”
 “_ _ _” matches any string of three characters.
 Find the names of all customers whose street address includes the substring “Main”
select customer-name
from Customer
where customer-street like “%Main%”
Set Operations
 union, intersect and except operations are set operations available in SQL.
 Relations participating in any of the set operation must be compatible; i.e. they must have
the same set of attributes.
 Union Operation:
o Find all customers having a loan, an account, or both at the bank
(select customer-name from Depositor )
union
(select customer-name from Borrower )
It will automatically eliminate duplicates.
o If we want to retain duplicates union all can be used
(select customer-name from Depositor )
union all
(select customer-name from Borrower )
 Intersect Operation
o Find all customers who have both an account and a loan at the bank
(select customer-name from Depositor )
intersect
(select customer-name from Borrower )
o If we want to retail all the duplicates
(select customer-name from Depositor )
intersect all
(select customer-name from Borrower )
 Except Opeartion
o Find all customers who have an account but no loan at the bank
(select customer-name from Depositor )
except
(select customer-name from Borrower )
o If we want to retain the duplicates:
(select customer-name from Depositor )
except all
(select customer-name from Borrower )
Aggregate Functions
 Aggregate functions are those functions which take a collection of values as input and return
a single value.
 SQL offers 5 built in aggregate functions-
o Average: avg
o Minimum:min
o Maximum:max
o Total: sum
o Count:count
 The input to sum and avg must be a collection of numbers but others may have collections
of non-numeric data types as input as well
 Find the average account balance at the Sadar branch
select avg(balance)
from Account
where branch-name= “Sadar”
The result will be a table which contains single cell (one row and one column) having
numerical value corresponding to average balance of all account at sadar branch.
 group by clause is used to form groups, tuples with the same value on all attributes in the
group by clause are placed in one group.
 Find the average account balance at each branch
select branch-name, avg(balance)
from Account
group by branch-name
 By default the aggregate functions include the duplicates.
 distinct keyword is used to eliminate duplicates in an aggregate functions:
 Find the number of depositors for each branch
select branch-name, count(distinct customer-name)
from Depositor, Account
where Depositor.account-number = Account.account-number
group by branch-name
 having clause is used to state condition that applies to groups rather than tuples.
 Find the average account balance at each branch where average account balance is more
than Rs. 1200
select branch-name, avg(balance)
from Account
group by branch-name
having avg(balance) > 1200
 Count the number of tuples in Customer table
select count(*)
from Customer
 SQL doesn’t allow distinct with count(*)
 When where and having are both present in a statement where is applied before having.
LECTURE-23

Nested Sub queries


A subquery is a select-from-where expression that is nested within another query.
Set Membership
The in and not in connectives are used for this type of subquery.
“Find all customers who have both a loan and an account at the bank”, this query can be written
using nested subquery form as follows
select distinct customer-name
from Borrower
where customer-name in(select customer-name
from Depositor )
 Select the names of customers who have a loan at the bank, and whose names are neither
“Smith” nor “Jones”
select distinct customer-name
from Borrower
where customer-name not in(“Smith”, “Jones”)
Set Comparison
Find the names of all branches that have assets greater than those of at least one branch located in
Mathura
select branch-name
from Branch
where asstets > some (select assets
from Branch
where branch-city = “Mathura” )
1. Apart from > some others comparison could be < some , <= some , >= some , = some , <
> some.
2. Find the names of all branches that have assets greater than that of each branch located in
Mathura
select branch-name
from Branch
where asstets > all (select assets
from Branch
where branch-city = “Mathura” )
 Apart from > all others comparison could be < all , <= all , >= all , = all , < >all.

Views
In SQL create view command is used to define a view as follows:
create view v as <query expression>
where <query expression> is any legal query expression and v is the view name.
 The view consisting of branch names and the names of customers who have either an
account or a loan at the branch. This can be defined as follows:

create view All-customer as


(select branch-name, customer-name
from Depositor, Account
where Depositor.account-number=account.account-number)
union
(select branch-name, customer-name
from Borrower, Loan
where Borrower.loan-number = Loan.loan-number)

 The attributes names may be specified explicitly within a set of round bracket after the name
of view.
 The view names may be used as relations in subsequent queries. Using the view
Allcustomer
Find all customers of Sadar branch
select customer-name
from All-customer
where branch-name= “Sadar”
 A create-view clause creates a view definition in the database which stays until a command
- drop view view-name - is executed.
Modification of Database
Deletion
 In SQL we can delete only whole tuple and not the values on any particular
attributes. The command is as follows:

delete from r where P.


where P is a predicate and r is a relation.
 delete command operates on only one relation at a time. Examples are as follows:
 Delete all tuples from the Loan relation
delete from Loan
o Delete all of the Smith’s account record
delete from Depositor
where customer-name = “Smith”
o Delete all loans with loan amounts between Rs 1300 and Rs 1500.
delete from Loan
where amount between 1300 and 1500
o Delete the records of all accounts with balances below the average at the bank
delete from Account
where balance < ( select avg(balance)
from Account)

Insertion
In SQL we either specify a tuple to be inserted or write a query whose result is a
set of tuples to be inserted. Examples are as follows:
Insert an account of account number A-9732 at the Sadar branch having balance
of Rs 1200
insert into Account
values(“Sadar”, “A-9732”, 1200)
the values are specified in the order in which the corresponding attributes are
listed in the relation schema.
SQL allows the attributes to be specified as part of the insert statement
insert into Account(account-number, branch-name, balance)
values(“A-9732”, “Sadar”, 1200)
insert into Account(branch-name, account-number, balance)
values(“Sadar”, “A-9732”, 1200)

Provide for all loan customers of the Sadar branch a new Rs 200 saving account
for each loan account they have. Where loan-number serve as the account number
for these accounts.
insert into Account
select branch-name, loan-number, 200
from Loan
where branch-name = “Sadar”

Updates
Used to change a value in a tuple without changing all values in the tuple.
Suppose that annual interest payments are being made, and all balances are to be
increased by 5 percent.
update Account
set balance = balance * 1.05
Suppose that accounts with balances over Rs10000 receive 6 percent interest,
whereas all others receive 5 percent.
update Account
set balance = balance * 1.06
where balance > 10000
update Account
set balance = balance * 1.05
where balance <= 10000
Data Definition Language
Data Types in SQL
char(n): fixed length character string, length n.
varchar(n): variable length character string, maximum length n.
int: an integer.
smallint: a small integer.
numeric(p,d): fixed point number, p digits( plus a sign), and d of the p digits are
to right of the decimal point.
real, double precision: floating point and double precision numbers.
float(n): a floating point number, precision at least n digits.
date: calendar date; four digits for year, two for month and two for day of month.
time: time of day n hours minutes and seconds.
Domains can be defined as
create domain person-name char(20).
the domain name person-name can be used to define the type of an attribute just like
built-in domain.
Schema Definition in SQL
create table command is used to define relations.
create table r (A1D1, A2D2,… , AnDn,
<integrity constraint1>,
…,
<integrity constraintk>)

where r is relation name, each Ai is the name of attribute, Di is the domain type of
values of Ai. Several types of integrity constraints are available to define in SQL.

Integrity Constraints which are allowed in SQL are

primary key(Aj1, Aj2,… , Ajm)


and
check(P) where P is the predicate.

drop table command is used to remove relations from database.


alter table command is used to add attributes to an existing relation
alter table r add A D
it will add attribute A of domain type D in relation r.
alter table r drop A
it will remove the attribute A of relation r.
LECTURE-24

Integrity Constraints
 Integrity Constraints guard against accidental damage to the database.
 Integrity constraints are predicates pertaining to the database.
 Domain Constraints:
 Predicates defined on the domains are Domain constraints.
 Simplest Domain constraints are defined by defining standard data types of the attributes
like Integer, Double, Float, etc.
 We can define domains by create domain clause also we can define the constraints on such
domains as follows:
create domain hourly-wage numeric(5,2)
constraint wage-value-test check(value >= 4.00)
 So we can use hourly-wage as data type for any attribute where DBMS will automatically
allow only values greater than or equal to 4.00.
 Other examples for defining Domain constraints are as follows:
create domain account-number char(10)
constraint account-number-null-test check(value not null)
create domain account-type char(10)
constraint account-type-test
check (value in ( “Checking”, “Saving”))
By using the later domain of two above the DBMS will allow only values for any attribute having
type as account-type i.e. Checking and Saving.
 Referential Integrity:
 Foreign Key: If two table R and S are related to each other, K1 and K2 are primary keys of
the two relations also K1 is one of the attribute in S. Suppose we want that every row in S
must have a corresponding row in R, then we define the K1 in S as foreign key. Example in
our original database of library we had a table for relation BORROWEDBY, containing two
fields Card No. and Acc. No. . Every row of BORROWEDBY relation must have
corresponding row in USER Table having same Card No. and a row in BOOK table having
same Acc. No.. Then we will define the Card No. and Acc. No. in BORROWEDBY relation
as foreign keys.
 In other way we can say that every row of BORROWEDBY relation must refer to some row
in BOOK and also in USER tables.
 Such referential requirement in one table to another table is called Referential Integrity.
LECTURE-25

Query by Example (QBE)


Query by Example (QBE) is a method of query creation that allows the user to search for
documents based on an example in the form of a selected text string or in the form of a document
name or a list of documents. Because the QBE system formulates the actual query, QBE is easier to
learn than formal query languages, such as the standard Structured Query Language (SQL), while
still enabling powerful searches.

Selections in QBE
QBE uses skeleton tables to represent table name and fieldnames like:
Table name Field1 Field2 …..

For selection, P operator along with variable name/constant name is used to display one or more
fields.
Example 1:
Consider the relation: student (name, roll, marks)
The following query can be represented as:
SQL: select name from student where marks>50;
student name roll marks
P.X >50
Here X is a constant; alternatively we can use _X as a variable.
Example 2:
For the relation given above
The following query can be represented as:
SQL: select * from student where marks>50 and marks <80;
student name roll marks
P. _X

CONDITION

_X>50  _X<80

We can use condition box to represent complex conditions.


Example 3:
For the relation given above
The following query can be represented as:
SQL: select name, roll from student where marks<50 or marks >80;
student name roll marks
P.A P.B <50
P.A P.B >80
OR operation retrieves results in multiple rows.
Example 4:
(Joins in QBE)
Consider the following tables:
Student (name, roll, marks)
Grades (roll, grade)
The following query can be represented as:
SQL: select s.name, g.grade from Student s, Grades g where s.roll=g.roll and s.marks>50;
Uses two skeleton tables:
Student name roll marks
P.A _X >50
And
Grades roll grade
_X P.B

Insertions in QBE:
Uses operator I. on the table.
Example: Consider the following query on Student table
SQL: insert into student values (‘abc’,10,60);

Student name roll marks


I. abc 10 60

Multiple insertions can be represented by separate rows in skeleton table.

Deletions in QBE:
Uses operator D. on the table.
Example: Consider the following query on Student table
SQL: delete from student where marks=0;

Student name roll marks


D. 0

Multiple deletions can be represented by separate rows in skeleton table.


Deletions without any condition can truncate the entire table.

Updation in QBE:
Uses operator U. on the table.
Example: Consider the following query on Student table
SQL: update student set mark=50 where roll=40;

Student name roll marks


U. 50
40

You might also like