DBMS Unit 2 Notes Full
DBMS Unit 2 Notes Full
DBMS Unit 2 Notes Full
The key fields are underlined, and the domain of each field is listed
after the field name. Thus, sid is the key for Sailors, bid is the key for
Boats, and all three fields together form the key for Reserves.
Q) Database systems use some variant of relational algebra to represent query
evaluation plans. Explain why algebra is suitable for this purpose.
Notation − σp(r)
Where σ stands for selection predicate and r stands for relation. P is
prepositional logic formula which may use connectors like and,
or, and not. These terms may use relational operators like − =, ≠, ≥, < ,
>, ≤.
The expression
Q) Describe Projection operator ?
For example, we can find out all sailor names and ratings by using .
The expression
For example, we can compute the names and ratings of highly rated
sailors by combining two of the preceding queries.
Q) Describe the set operations of relational algebra?
Set Operations
The following standard operations on sets are also available in
relational algebra:
union (U),
intersection (n),
set-difference (-),
cross-product (x).
Union Operation (∪)
It performs binary union between two given relations and is
defined as
Notion − r U s
The results of relational algebra are also relations but without any
name. The rename operation allows us to rename the output relation.
'rename' operation is denoted with small Greek letterrho ρ.
Notation − ρ x (E)
Q) Define all the variants of the join operation?
Ans :
– Condition joins
– Equijoin
– Natural join
Condition joins
The most general version of the join operation accepts a join condition
c and a pair of relation instances as arguments and returns a relation
instance.
Natural Join
Ans : Division
(Q1) Find the names of sailors who have reserved boat 103
Q) RELATIONAL CALCULUS
Examples :
Domain Relational Calculus
A DRC query has the form {(XI,X2, ... ,Xn ) I P((XI,X2, ... ,Xn ))}, where
each Xi is either a domain variable or a constant and p( (Xl, X2, ... ,xn ))
denotes a DRC formula
Example :
q) expalin expressive power of algebra and calculus ?
Ans )
Q) What are the parts of basic SQL query ? Are the input and
result tables of an SQL querry sets or multisets ?
How can obtain a set of tuples as the result of a query ?
• The from-list in the FROM clause is a list of table names. A table name
can be followed by a range variable; a range variable is particularly
useful when the same table name appears more than once in the
from-list.
(Q3) Find the names of sailors Who have reserved boat number 103.
SELECT S.sname
FROM Sailors S, Reserves R
WHERE S.sid = R.sid AND R.bid=103
(Q4) Find the sids of sailors who have reserved a Red boat.
SELECT R.sid
FROM Boats B ,Reserves R
WHERE B.bid = R.bid AND B.color = ‘red’
(Q5) Find the colors of boats reserved by Lubber.
SELECT B.color
FROM Sailors S, Reserves R, Boats B
WHERE S.sid = R.sid AND R.bid = B.bid AND S.sname = 'Lubber'
(Q6) Find the names of sailors who have Reserved at least one boat.
SELECT S.sname
FROM Sailors S, Reserves R
WHERE S.sid = R.sid
Q) What operation does provide over (multi)set of tuples , and how
would you use these in writing queries ?
Ans :
(Q1) Find the names of sailors who have reserved a red a green boat.
SELECT S.sname
FROM Sailors S, Reserves R, Boats B
WHERE S.sid = R.sid AND R.bid = B.bid AND (B.color = 'red' OR
B.color = 'green')
(Q2) Find the names of sailors who have reserved a red a green boat.
SELECT S.sname
FROM Sailors S, Reserves R, Boats B
WHERE S.sid = R.sid AND R.bid = B.bid AND (B.color = 'red' AND
B.color = 'green')
(Q3) Find the names of sailors who have reserved a red 01' a green
boat.
SELECT S.sname
FROM Sailors S, Reserves R, Boats B
WHERE S.sid = R.sid AND R.bid = B.bid AND B.color = 'red'
EXCEPT
SELECT S.sname
FROM Sailors S, Reserves R, Boats B
WHERE S.sid = R.sid AND R.bid = B.bid AND B.color = 'green'
(Q4) Find all sids of sailors who have a rating of 10 or reserved boat
104.
SELECT S.sid
FROM Sailors S
WHERE S.rating = 10
UNION
SELECT R.sid
FROM Reserves R
WHERE R.bid = 104
Q) What are Nested Queries ? What is correlation in Nested Queries ?
How would you use the operator IN,EXISTS,UNIQUE,ANY,AND ALL in
writing nested queries ? why are they useful ? Illustrate your answer
by showing how to write the division operator in SQL?
Ans :
NESTED QUERIES
A nested query is a query that has another query embedded within it;
the embedded query is called a subquery.
(Ql) Find the names of sailors who have reserved boat 103.
SELECT S.sname
FROM Sailors S
WHERE S.sid IN ( SELECT R.sid
FROM Reserves R
WHERE R.bid = 103 )
(Q2) Find the names of sailors 'who have reserved a red boat.
SELECT S.sname
FROM Sailors S
WHERE S.sid IN ( SELECT R.sid
FROM Reserves R
WHERE R.bid IN (IN (SELECT B.bid
FROM Boats B
WHERE B.color = 'red' )
(Q3) Find the names of sailors who have not reserved a red boat.
SELECT S.sname
FROM Sailors S
WHERE S.sid NOT IN ( SELECT R.sid
FROM Reserves R
WHERE R.bid IN (SELECT B.bid
FROM Boats B
WHERE B.color = 'red' )
Correlated Nested Queries
(Q1) Find the names of sailors who have reserved boat number 103.
SELECT S.sname
FROM Sailors S
WHERE EXISTS ( SELECT *
FROM Reserves R
WHERE R.bid = 103 AND R.sid = S.sid )
Set-Comparison Operators
op ANY, op ALL (to compare a value with the elements in a given set,
using comparison operator op),
SQL also supports op ANY and op ALL, where op is one of the arithmetic
comparison operators {<, <=, =, <>, >=, >}.
(Q1) Find sailors whose rating is better than some sailor called Horatio.
SELECT S.sid
FROM Sailors S
WHERE S.rating > ANY ( SELECT S2.rating
FROM Sailors S2
WHERE S2.sname = 'Horatio' )
If there are several sailors called Horatio, this query finds all sailors
whose rating is better than that of some sailor called Horatio.
(Q24J Find the sailor's with the highest rating.
SELECT S.sid
FROM Sailors S
WHERE S.rating >= ALL ( SELECT S2.rating FROM Sailors S2 )
AGGREGATE OPERATORS
(Q5) Find the names of sailors who are older than the oldest sailor with
a rating of 10.
SELECT S.sname
FROM Sailors S
WHERE S.age > ( SELECT MAX ( S2.age )
FROM Sailors S2
WHERE S2.rating = 10 )
SQL provides a special column value called null , We use null when the
column value is either Unknown or inapplicable.
We must define the logical operators AND, OR, and NOT using in logic
in which expressions evaluate to true, false, or unknown.
Outer Joins
Some interesting variants of the join operation that rely on null values,
called outer joins, are supported in SQL.
Consider the join of two tables, say Sailors Reserves.
Tuples of Sailors that do not match some row in Reserves according to
the join condition c do not appear in the result.
In an outer join, on the other hanel, Sailor rows without a matching
Reserves row appear exactly once in the result, with the result columns
inherited from Reserves assigned null values.
We can disallow null values by specifying NOT NULL as part of the field
definition;
for example, sname CHAR(20) NOT NULL. In addition, the fields in a
primary key are not allowed to take on null values. Thus, there is an
implicit NOT NULL constraint for every field listed in a PRIMARY KEY
constraint.
Q) What type of SQL constraints can be specified using the query
language?
Ans :
A user can define a new domain using the CREATE DOMAIN statement,
which uses CHECK constraints.
INTEGER is the underlying, or source, type for the domain ratingval, and
every ratingval value must be of this type.
Values in ratingval are further restricted by using a CHECK constraint; in
defining this constraint, we use the keyword VALUE to refer to a value
in the domain. By using this facility, we can constrain the values that
belong to a domain using the full power of SQL queries. Once a domain
is defined, the name of the domain can be used to restrict column
values in a table; we can use the following line in a schema declaration,
for example:
Rating ratingval
Or
Q) What are the trigger .What are its three parts ? What are the
differences between row-level and statement –level trigger ?
Ans :
A trigger is a procedure that is automatically invoked by the DBMS in
response to specified changes to the database, and is typically specified
by the DBA.
A database that has a set of associated triggers is called an active
database.
A trigger description contains three parts:
A trigger action can examine the answers to the query in the condition
part of the trigger, refer to old and new values of tuples modified by
the statement activating the trigger, execute Hew queries, and make
changes to the database.
Example :
If the action depends on individual changed records, for example, we
have to examine the age field of the inserted Students record to decide
whether to increment the count, the triggering event should be defined
to occur for each modified record; the FOR EACH ROW clause is used to
do this. Such a trigger is called a row-level trigger.
Suppose that we have a table called Orders with fields itemid, quantity,
customerid, and unitprice.
When a customer places an order, the first three field values are filled
in by the user (in this example, a sales clerk).
The fourth field's value can be obtained from a table called Items, but it
is important to include it in the Orders table to have a complete record
of the order, in case the price of the item is subsequently changed.
We can define a trigger to look up this value and include it in the fourth
field of a newly inserted record. In addition to reducing the number of
fields that the clerk has to type in, this trigger eliminates the possibility
of an entry error leading to an inconsistent price in the Orders table
Uses Of Triggers
Triggers can alert users to unusual events (& reflected in updates to the
database).
For example, we may want to check whether a customer placing an
order has made enough purchases in the past month to qualify for an
additional discount; if so, the sales clerk must be informed so that he
(or she) can tell the customer
Triggers can generate a log of events to support auditing and security
checks.
For example, each time a customer places an order, we can create a
record with the customer's ID and current credit limit and insert this
record in a customer history table. Subsequent analysis of this table
might suggest candidates for an increased credit limit (e.g., customers
who have never failed to pay a bill on time and who have come within
10% of their credit limit at least three times in the last month)..