0% found this document useful (0 votes)
62 views83 pages

Module 3

This document discusses advanced SQL queries including more complex queries using comparisons involving NULL values, nested queries, and correlated subqueries. It also covers SQL functions like EXISTS that check if a subquery returns any results. Views, stored procedures, and accessing databases from applications are also mentioned as topics in the document.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
62 views83 pages

Module 3

This document discusses advanced SQL queries including more complex queries using comparisons involving NULL values, nested queries, and correlated subqueries. It also covers SQL functions like EXISTS that check if a subquery returns any results. Views, stored procedures, and accessing databases from applications are also mentioned as topics in the document.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 83

Database Management System [18CS53]

Module-3SQL : Advances Queries


SQL : Advances Queries:
More complex SQL retrieval queries, Specifying constraints as assertions and action triggers,
Views in SQL, Schema change statements in SQL.

Database Application Development:


Accessing databases from applications, An introduction to JDBC, JDBC classes and
interfaces, SQLJ, Stored procedures, Case study: The internet Bookshop.

Internet Applications:
The three-Tier application architecture, The presentation layer, The Middle Tier

Chapter 1: SQL- Advances Queries

1.1 More Complex SQL Retrieval Queries

Additional features allow users to specify more complex retrievals from database

1.1.1 Comparisons Involving NULL and Three-Valued Logic


SQL has various rules for dealing with NULL values. NULL is used to represent a
missing value, but that it usually has one of three different interpretations-value
Example

1. Unknown value. A person's date of birth is not known, so it is represented


by NULL in the database.
2. Unavailable or withheld value. A person has a home phone but does
not want it to be listed, so it is withheld and represented as NULL in the
database.

3. Not applicable attribute. An attribute CollegeDegree would be NULL for a person


who has no college degrees because it does not apply to that person.Each individual
NULL value is considered to be different from every other NULL value in the various
database records. When a NULL is involved in a comparison operation, the result
is considered to be UNKNOWN (it may be TRUE or it may be FALSE). Hence, SQL

Dept. of ISE, AJIET, MANGALORE Page 1


Database Management System [18CS53]

uses a three-valued logic with values TRUE, FALSE, and UNKNOWN instead of
the standard two-valued (Boolean) logic with values TRUE or FALSE. It is therefore
necessary to define the results (or truth values) of three- valued logical expressions
when the logical connectives AND, OR, and NOT are used

The rows and columns represent the values of the results of comparison conditions,

which would typically appear in the WHERE clause of an SQL query.

In select-project-join queries, the general rule is that only those combinations of tuples
that evaluate the logical expression in the WHERE clause of the query to TRUE are
selected. Tuple combinations that evaluate to FALSE or UNKNOWN are not
selected.

SQL allows queries that check whether an attribute value is NULL using the comparison
operators
IS or IS NOT.
Example: Retrieve the names of all employees who do not have supervisors.
SELECT Fname, Lname
FROM EMPLOYEE
WHERE Super_ssn IS NULL;

Dept. of ISE, AJIET, MANGALORE Page 2


Database Management System [18CS53]

1.1.2 Nested Queries, Tuples, and Set/Multiset Comparisons


Some queries require that existing values in the database be fetched and then used
in a comparison condition. Such queries can be conveniently formulated by using
nested queries, which are complete select-from-where blocks within the WHERE
clause of another query. That other query is called the outer query
Example1: List the project numbers of projects that have an employee with last
name 'Smith' as manager
SELECT DISTINCT Pnumber FROM PROJECT WHERE
Pnumber IN
(SELECT Pnumber FROM PROJECT, DEPARTMENT, EMPLOYEE
WHERE Dnum=Dnumber AND Mgr_ssn=Ssn AND Lname='smith');
Example2: List the project numbers of projects that have an employee with last
name 'Smith' as either manager or as worker.
SELECT DISTINCT Pnumber FROM PROJECT WHERE
Pnumber IN
(SELECT Pnumber FROM PROJECT, DEPARTMENT, EMPLOYEE
WHERE Dnum=Dnumber AND Mgr_ssn=Ssn AND Lname= 'smith')
OR Pnumber IN
(SELECT Pno FROM WORKS_ON, EMPLOYEE WHERE Essn=Ssn AND
Lname= 'smith');
We make use of comparison operator IN, which compares a value v with a set (or
multiset) of values V and evaluates to TRUE if v is one of the elements in V.

The first nested query selects the project numbers of projects that have an employee
with last name 'Smith' involved as manager. The second nested query selects the
project numbers of projects that have an employee with last name 'Smith' involved
as worker. In the outer query, we use the OR logical connective to retrieve a
PROJECT tuple if the PNUMBER value of that tuple is in the result of either nested
query.
SOL allows the use of tuples of values in comparisons by placing them within
parentheses. For example, the following query will select the Essns of all employees
who work the same (project, hours) combination on some project that employee
'John Smith' (whose Ssn = '123456789') works on

Dept. of ISE, AJIET, MANGALORE Page 3


Database Management System [18CS53]

In this example, the IN operator compares the subtuple of values in parentheses


(Pno,Hours) within each tuple in WORKS_ON with the set of type-compatible tuples
produced by the nested query.

Nested Queries::Comparison Operators


Other comparison operators can be used to compare a single value v to a set or multiset
V. The = ANY (or = SOME) operator returns TRUE if the value v is equal to some value
in the set V and is hence equivalent to IN. The two keywords ANY and SOME have the
same effect. The keyword ALL can also be combined with each of these operators.
For example, the comparison condition (v > ALL V) returns TRUE if the value v is
greater than a// the values in the set (or multiset) V. For example is the following query,
which returns the names of employees whose salary is greater than the salary of all
the employees in department 5:
SELECT Lname, Fname
FROM EMPLOYEE
WHERE Salary > ALL ( SELECT Salary
FROM EMPLOYEE
WHERE Dno=5 );
In general, we can have several levels of nested queries. We can once again be
faced with possible ambiguity among attribute names if attributes of the same name
exist-one in a relation in the FROM clause of the outer query, and another in a
relation in the FROM clause of the nested query. The rule is that a reference to an
unqualified attribute refers to the relation declared in the innermost nested query.
To avoid potential errors and ambiguities, create tuple variables (aliases) for all tables
referenced in SOL query

Dept. of ISE, AJIET, MANGALORE Page 4


Database Management System [18CS53]

Example: Retrieve the name of each employee who has a dependent with the
same first name and is the same sex as the employee
SELECT E.Fname, E.Lname
FROM EMPLOYEE AS E
WHERE E.Ssn IN ( SELECT Essn
FROM DEPENDENT AS D
WHERE E.Fname=D.Dependent_name
AND E.Sex=D.Sex );
In the above nested query, we must qualify E.Sex because it refers to the Sex
attribute of EMPLOYEE from the outer query, and DEPENDENT also has an
attribute called Sex.
1.1.3 Correlated Nested Queries
Whenever a condition in the WHERE clause of a nested query references
some attribute of a relation declared in the outer query, the two queries are said to
be correlated.
Example: SELECT E.Fname, E.Lname
FROM EMPLOYEE AS E
WHERE E.Ssn IN ( SELECT Essn
FROM DEPENDENT AS D
WHERE E.Fname=D.Dependent_name
AND E.Sex=D.Sex );
The nested query is evaluated once for each tuple (or combination of tuples) in the
outer query. we can think of query in above example as follows: For each
EMPLOYEE tuple, evaluate the nested query, which retrieves the Essn values for
all DEPENDENT tuples with the same sex and name as that EMPLOYEE tuple; if
the Ssn value of the EMPLOYEE tuple is in the result of the nested query, then select
that EMPLOYEE tuple.
1.1.4 The EXISTS and UNIQUE Functions in SQL
EXISTS Functions
The EXISTS function in SOL is used to check whether the result of a correlated nested
query is
empty (contains no tuples) or not. The result of EXISTS is a Boolean value
• TRUE if the nested query result contains at least one tuple, or

Dept. of ISE, AJIET, MANGALORE Page 5


Database Management System [18CS53]

• FALSE if the nested query result contains no tuples.


For example, the query to retrieve the name of each employee who has a dependent
with the same first name and is the same sex as the employee can be written using
EXISTS functions as follows:
SELECT E.Fname, E.Lname
FROM EMPLOYEE AS E
WHERE EXISTS (
SELECT * FROM
DEPENDENT AS D
WHERE E.Ssn=D.Essn AND E.Sex=D.Sex
AND E.Fname=D.Dependent_name);
Example: List the names of managers who have at least one dependent
SELECT Fname, Lname
FROM EMPLOYEE
WHERE EXISTS ( SELECT *
FROM DEPENDENT
WHERE Ssn=Essn )
AND
EXISTS ( SELECT *
FROM DEPARTMENT
WHERE Ssn=Mgr_ssn );

In general, EXISTS(Q) returns TRUE if there is at least one tuple in the result of the
nested query Q, and it returns FALSE otherwise.
NOT EXISTS Functions
NOT EXISTS(Q) returns TRUE if there are no tuples in the result of nested query Q, and
it returns
FALSE otherwise.
Example: Retrieve the names of employees who have no dependents.
SELECT Fname, Lname
FROM EMPLOYEE
WHERE NOT EXISTS ( SELECT *
FROM DEPENDENT
WHERE Ssn=Essn );
Dept. of ISE, AJIET, MANGALORE Page 6
Database Management System [18CS53]

For each EMPLOYEE tuple, the correlated nested query selects all DEPENDENT
tuples whose Essn value matches the EMPLOYEE Ssn; if the result is empty, no
dependents are related to the employee, so we select that EMPLOYEE tuple and
retrieve its Fname and Lname.
Example: Retrieve the name of each employee who works on all the
projects controlled by department number 5
SELECT Fname, Lname
FROM EMPLOYEE
WHERE NOT EXISTS ( ( SELECT Pnumber
FROM PROJECT
WHERE Dnum=5)
EXCEPT ( SELECT
Pno FROM
WORKS_ON
WHERE Ssn=Essn) );

UNIQUE Functions
UNIOUE(O) returns TRUE if there are no duplicate tuples in the result of query
O; otherwise, it returns FALSE. This can be used to test whether the result of a
nested query is a set or a multiset.

1.1.5 Explicit Sets and Renaming of Attributes in SQL


IN SOL it is possible to use an explicit set of values in the WHERE clause,
rather than a nested query. Such a set is enclosed in parentheses.
Example: Retrieve the Social Security numbers of all employees who work on
project numbers 1, 2, or 3.
SELECT DISTINCT
Essn FROM
WORKS_ON WHERE
Pno IN (1, 2, 3);
In SOL, it is possible to rename any attribute that appears in the result of a
query by adding the qualifier AS followed by the desired new name

Dept. of ISE, AJIET, MANGALORE Page 7


Database Management System [18CS53]

Example: Retrieve the last name of each employee and his or her supervisor
SELECT E.Lname AS
Employee_name, S.Lname AS
Supervisor_name
FROM EMPLOYEE
AS E, EMPLOYEE
AS S
WHERE E.Super_ssn=S.Ssn;

1.1.6 Joined Tables in SQL and Outer Joins


An SOL join clause combines records from two or more tables in a database. It
creates a set that can be saved as a table or used as is. A JOIN is a means for
combining fields from two tables by using values common to each. SOL specifies
four types of JOIN
1. INNER,
2. OUTER
3. EOUIJOIN and
4. NATURAL JOIN
INNER JOIN

An inner join is the most common join operation used in applications and can be
regarded as the default join-type. Inner join creates a new result table by combining
column values of two tables (A and B) based upon the join- predicate (the condition).
The result of the join can be defined as the outcome of first taking the Cartesian
product (or Cross join) of all records in the tables (combining every record in table
A with every record in table B)-then return all records which satisfy the join predicate
Example: SELECT* FROM employee

INNER JOIN department ON

employee.dno = department.dnumber;

EQUIJOIN and NATURAL JOIN


An EQUIJOIN is a specific type of comparator-based join that uses only equality
comparisons in the join-predicate. Using other comparison operators (such as <)
disqualifies a join as an equijoin.
Dept. of ISE, AJIET, MANGALORE Page 8
Database Management System [18CS53]

NATURAL JOIN is a type of EOUIJOIN where the join predicate arises implicitly by
comparing all columns in both tables that have the same column-names in the
joined tables. The resulting joined table contains only one column for each pair of
equally named columns.

If the names of the join attributes are not the same in the base relations, it is possible
to rename the attributes so that they match, and then to apply NATURAL JOIN. In
this case, the AS construct can be used to rename a relation and all its attributes in
the FROM clause.

CROSS JOIN returns the Cartesian product of rows from tables in the join. In other
words, it will produce rows which combine each row from the first table with each
row from the second table.

OUTER JOIN
An outer join does not require each record in the two joined tables to have a
matching record. The joined table retains each record-even if no other matching
record exists. Outer joins subdivide further into
• Left outer joins
• Right outer joins
• Full outer joins
No implicit join-notation for outer joins exists in standard SQL.

► LEFT OUTER JOIN


Every tuple in left table must appear i n result
If no matching tuple
Padded with NULL values for attributes of right table

Dept. of ISE, AJIET, MANGALORE Page 9


Database Management System [18CS53]

Dept. of ISE, AJIET, MANGALORE Page 10


Database Management System [18CS53]

► RIGHT OUTER JOIN


► Every tuple in right table must appear in result
► If no matching tuple
• Padded wit h NULL val ues for the att ributes of left table
► FULL OUTER JOIN
► a full outer join combines the effect of applying both left and right outerjoins.
► Where records in the FULL OUTER JOINed tables do not match, the result set will
have NULL values for every column of the table that lacksa matching row.
► For those records that do match, a single row will be produced in theresult set
(containing fields populated from both tables).

► Not all SQL implementations have implemented the new syntax of joined
tables.
► In some systems, a different syntax was used to specify outer joins by using
the comparison operators += =+, and +=+ for left, right and full outer join
respectively
► For example, this syntax is available in Oracle. To specify theleft outer
join in Q8B using this syntax, we could write the query QSC as follows:

QSC: SELEC T E.Lname, S.Lname EMPLOYEE E


FROM EMPLOYEES
WHERE E.Super_ssn +== S.Ssn;

Dept. of ISE, AJIET, MANGALORE Page 11


Database Management System [18CS53]

MULTIWAY JOIN
It is also possible to nest join specifications; that is, one of the tables in a join may
itself be a joined table. This allows the specification of the join of three or more tables
as a single joined table, which is called a multiway join.
Example: For every project located in 'Stafford', list the project number, the
controlling department number, and the department manager's last name,address,
and birth date.
SELECT Pnumber, Dnum, Lname, Address, Bdate
FROM ((PROJECT JOIN DEPARTMENT ON Dnum=Dnumber)
JOIN EMPLOYEE ON Mgr_ssn=Ssn)
WHERE Plocation='Stafford';

1.1.7 Aggregate Functions in SQL


Aggregate functions are used to summarize information from multiple tuples into
a single-tuple summary. A number of built-in aggregate functions exist: COUNT,
SUM, MAX, MIN, and AVG. The COUNT function returns the number of tuples or
values as specified in a query. The functions SUM, MAX, MIN, and AVG can be
applied to a set or multiset of numeric values and return, respectively, the sum,
maximum value, minimum value, and average (mean) of those values. These
functions can be used in the SELECTclause or in a HAVING clause (which we
introduce later). The functions MAX and MIN can also be used with attributes that
have nonnumeric domains if the domain values have a total ordering among one
another.

Examples
1. Find the sum of the salaries of all employees, the maximum salary, the minimum
salary, and the average salary.
SELECT SUM (Salary), MAX (Salary), MIN (Salary), AVG (Salary)
FROM EMPLOYEE;

2. Find the sum of the salaries of all employees of the 'Research ' department, as well
as the maximum salary, the minimum salary, and the average salary in this
department.
Dept. of ISE, AJIET, MANGALORE Page 12
Database Management System [18CS53]

SELECT SUM (Salary), MAX (Salary), MIN (Salary), AVG (Salary)


FROM (EMPLOYEE JOIN DEPARTMENT ON Dno=Dnumber)
WHERE Dname='Research' ;

3. Count the number of distinct salary values in the database.


SELECT COUNT (DISTINCT Salary)
FROM EMPLOYEE;

4. To retrieve the names of all employees who have two or more dependents
SELECT Lname, Fname
FROM EMPLOYEE
WHERE ( SELECT COUNT (*)
FROM DEPENDENT
WHERE Ssn=Essn ) >= 2;

1.1.8 Grouping: The GROUP BY and HAVING Clauses


Grouping is used to create subgroups of tuples before summarization. For
example, we may want to find the average salary of employees in each department
or the number of employees who work on each project. In these cases we need to
partition the relation into non overlapping subsets (or groups) of tuples. Each
group (partition) will consist of the tuples that have the same value of some
attribute(s), called the grouping attribute(s).
SOL has a GROUP BY clause for this purpose. The GROUP BY clause specifies
the grouping attributes, which should also appear in the SELECT clause, so that the
value resulting from applying each aggregate function to a group of tuples appears
along with the value of the grouping attribute(s).
Example: For each department, retrieve the department number, the number of
employees in the department, and their average salary.
SELECT Ono, COUNT(*), AVG (Salary)
FROM EMPLOYEE
GROUP BY Ono;

Dept. of ISE, AJIET, MANGALORE Page 13


Database Management System [18CS53]

roupr g EMPLOYEE pies by he value o Ono


If NULLs exist in the grouping attribute, then a separate group is created for all
tuples with a NULL value in the grouping attribute. For example, if the EMPLOYEE
table had some tuples that had NULL for the grouping attribute Ono, there would
be a separate group for those tuples in the result of query

Example: For each project, retrieve the project number, the project name, and
the number of employees who work on that project.
SELECT Pnumber, Pname, COUNT (*)
FROM PROJECT , WORKS_ON
WHERE Pnumber=Pno
GROUP BY Pnumber, Pname;
Above query shows how we can use a join condition in conjunction with GROUP
BY. In this case, the grouping and functions are applied after the joining of the
two relations.

HAVING provides a condition on the summary information regarding the group


of tuples associated with each value of the grouping attributes. Only the groups
that satisfy the condition are retrieved in the result of the query.

Dept. of ISE, AJIET, MANGALORE Page 14


Database Management System [18CS53]

Example: For each project on which more than two employees work, retrieve
the project number, the project name, and the number of employees who work
on the project.
SELECT Pnumber, Pname, COUNT (*)
FROM PROJECT , WORKS_ON
WHERE Pnumber=Pno
GROUP BY Pnumber,
Pname HAVING
COUNT(*)> 2;

Dept. of ISE, AJIET, MANGALORE Page 15


Database Management System [18CS53]

Example: For each project, retrieve the project number, the project name, and
the number of employees from department 5 who work on the project.
SELECT Pnumber, Pname, COUNT (*)
FROM PROJECT , WORKS_ON, EMPLOYEE
WHERE Pnumber=Pno AND Ssn=Essn AND Dno=5
GROUP BY Pnumber, Pname;

Example: For each department that has more than five employees, retrieve the
department number and the number of its employees who are making more than
$40,000.
SELECT Dnumber, COUNT (*)
FROM DEPARTMENT , EMPLOYEE
WHERE Dnumber=Dno AND Salary>40000 AND
( SELECT Dno
FROM
EMPLOYEE
GROUP BY
Dno
HAVING COUNT(*)> 5);

Dept. of ISE, AJIET, MANGALORE Page 16


Database Management System [18CS53]

1.1.9 Discussion and Summary of SQL Queries


A retrieval query in SQL can consist of up to six clauses , but only the first two-
SELECT and FROM-are mandator.yThe query can span several lines, and is
ended by a semicolon. Query terms are separated by spaces, and parentheses
can be used to group relevant parts of a query in the standard way.The clauses
are specified in the following order, with the clauses between square brackets [
... ] being optional:

The SELECT clause lists the attributes or functions to be retrieved. The FROM
clause specifies all relations (tables) needed in the query, including joined relations,
but not those in nested queries. The WHERE clause specifies the conditions for
selecting the tuples from these relations, including join conditions if needed.
GROUP BY specifies grouping attributes, whereas HAVING specifies a condition
on the groups being selected rather than on the individual tuples. Finally, ORDER
BY specifies an order for displaying the result of a query.

A query is evaluated conceptually by first applying the FROM clause to identify all
tables involved in the query or to materialize any joined tables followed by the
WHERE clause to select and join tuples, and then by GROUP BY and HAVING.
ORDER BY is applied at the end to sort the query result Each DBMS has special
query optimization routines to decide on an execution plan that is efficient to execute

In general, there are numerous ways to specify the same query in SOL.This
flexibility in specifying queries has advantages and disadvantages.
■ The main advantage is that users can choose the technique with which they
are most comfortable when specifying a query. For example, many queries
may be specified with join conditions in the WHERE clause, or by using joined

Dept. of ISE, AJIET, MANGALORE Page 17


Database Management System [18CS53]

relations in the FROM clause, or with some form of nested queries and the
IN comparison. From the programmer's and the system's point of view
regarding query optimization, it is generally preferable to write a query with as
little nesting and implied ordering as possible.
■ The disadvantage of having numerous ways of specifying the same query is
that this may confuse the user, who may not know which technique to use to
specify particular types of queries. Another problem is that it may be more
efficient to execute a query specified in one way than the same query
specified in an alternative way

1.2 Specifying Constraints as Assertions and Actions as Triggers

1.2.1 Specifying General Constraints as Assertions in SQL


Assertions are used to specify additional types of constraints outside scope of built-
in relational model constraints. In SOL, users can specify general constraints via
declarative assertions, using the CREATE ASSERTION statement of the DDL.Each
assertion is given a constraint name and is specified via a condition similar to the
WHERE clause of an SOL query.

General form :
CREATE ASSERTION <Name_of_assertion> CHECK (<cond>)
For the assertion to be satisfied, the condition specified after CHECK clause must return
true.

For example, to specify the constraint that the salary of an employee must not be
greater than the salary of the manager of the department that the employee works
for in SOL, we can write the following assertion:
CREATE ASSERTION SALARY_CONSTRAINT
CHECK ( NOT EXISTS ( SELECT * FROM EMPLOYEE E,
EMPLOYEE M, DEPARTMENT D WHERE E.Salary>M.Salary
AND
E.Dno=D.Dnumber AND D.Mgr_ssn=M.Ssn ) );

The constraint name SALARY_CONSTRAINT is followed by the keyword CHECK,


which is followed by a condition in parentheses that must hold true on every
database state for the assertion to be satisfied. The constraint name can be used
Dept. of ISE, AJIET, MANGALORE Page 18
Database Management System [18CS53]

later to refer to the constraint or to modify or drop it. Any WHERE clause condition
can be used, but many constraints can be specified using the EXISTS and NOT
EXISTS style of SOL conditions.
By including this query inside a NOT EXISTS clause, the assertion will specify that
the result of this query must be empty so that the condition will always be TRUE. Thus,
the assertion is violated if the result of the query is not empty
Example: consider the bank database with the following tables

1. Write an assertion to specify the constraint that the Sum of loans taken by a
customer does not exceed 100,000
CREATE ASSERTION sumofloans
CHECK (100000> = ALL
SELECT customer_name,sum(amount)
FROM borrower b, loan I
WHERE b.loan_number=l.loan_number
GROUP BY customer_name );
2. Write an assertion to specify the constraint that the Number of accounts for
each customer in a given branch is at most two

CREATE ASSERTION NumAccounts


CHECK ( 2 >= ALL
SELECT customer_name,branch_name, count(*)
FROM account A , depositor D
WHERE A.account_number = D.account_number
GROUP BY customer_name, branch_name );

Dept. of ISE, AJIET, MANGALORE Page 19


Database Management System [18CS53]

1.2.2 Introduction to Triggers in SQL


A trigger is a procedure that runs automatically when a certain event occurs in the
DBMS. In many cases it is convenient to specify the type of action to be taken when
certain events occur and when certain conditions are satisfied. The CREATE
TRIGGER statement is used to implement such actions in SQL.
General form:
CREATE TRIGGER <name>
BEFORE I AFTER I <events>
FOR EACH ROW IFOR EACH STATEMENT
WHEN (<condition>)
<action>
A trigger has three components
1. Event: When this event happens, the trigger is activated
• Three event types : Insert, Update, Delete
• Two triggering times: Before the event
After the event

2. Condition (optional): If the condition is true, the trigger executes, otherwise


skipped
3. Action: The actions performed by the trigger
When the Event occurs and Condition is true, execute the Action

Create Trigger ABC Create Trigger XYZ


After Update On Students
Students

This trigger is activated when an insert This trigger is activated when an


statement update statement is issued and after
is issued, but before the new record is the update is executed
inserted

Dept. of ISE, AJIET, MANGALORE Page 20


Database Management System [18CS53]

Does the trigger execute for each updated or deleted record, or once for the entire
statement?. We define such granularity as follows:

Create Trigger <name> This is the event


Before! After Insert! Update! Delete

For Each Row I For Each Statement

This is the granularity

Create Trigger XYZ

After Update ON <tablename>

This trigger is activated once (per UPDATE This trigger is activated before deleting
statement) after all records are each record
updated

Dept. of ISE, AJIET, MANGALORE Page 21


Database Management System [18CS53]

In the action, you may want to reference:


• The new values of inserted or updated records (:new)
• The old values of deleted or updated records (:old)

Create Trigger EmpSal


After Insert or Update On Employee Inside "When",the
"new" and "old" should
For Each Row not have ":"
---------------------------------------------------
-
Trigger When (new.salary >150,000)
body Begin
if (:new.salary < 100,000) ...
End; Inside the trigger body,
they should have ":"

Dept. of ISE, AJIET, MANGALORE Page 22


Database Management System [18CS53]

Examples:

1) If the employee salary increased by more than 10%, then increment the rank field by 1.

In the case of Update event only, we can specify which


columns

Create Trigger EmpS


Before Update Of sa - n Employee
For Each
Row Begin
IF (:new.salary > (:old.salary * 1.1)) Then
:new.rank := :old.rank +1;
End IF;
End;
I

We changed the new value of rank


field
The assignment operator has ":"

2) Keep the bonus attribute in Employee table always 3% of the salary attribute

3.Suppose we want to check whenever an employee's salary is greater than the


salary of his or her direct supervisor in the COMPANY database
■ Several events can trigger this rule:
• inserting a new employee record
• changing an employee's salary or
• changing an employee's supervisor

Dept. of ISE, AJIET, MANGALORE Page 23


Database Management System [18CS53]

■ Suppose that the action to take would be to call an external stored


procedure SALARY_VIOLATION which will notify the supervisor

CREATE TRIGGER SALARY_VIOLATION


BEFORE INSERT OR UPDATE OF SALARY, SUPERVISOR_SSN
ON EMPLOYEE
FOR EACH ROW
WHEN ( NEW.SALARY > ( SELECT SALARY FROM EMPLOYEE
WHERE SSN = NEW.SUPERVISOR_SSN ) )
INFORM_SUPERVISOR(NEW .Supervisor_ssn,N EW.Ssn );

■ The trigger is given the name SALARY_VIOLATION, which can be


used to remove or deactivate the trigger later
■ In this example the events are: inserting a new employee record, changing
an employee's salary, or changing an employee's supervisor
■ The action is to execute the stored procedure INFORM_SUPERVISOR

Triggers can be used in various applications, such as maintaining database


consistency, monitoring database updates.

Assertions vs. Triggers


■ Assertions do not modify the data, they only check certain conditions.
Triggers are more powerful because the can check conditions and also
modify the data
■ Assertions are not linked to specific tables in the database and not linked to
specific events. Triggers are linked to specific tables and specific events
■ All assertions can be implemented as triggers (one or more). Not all triggers
can be implemented as assertions

Dept. of ISE, AJIET, MANGALORE Page 24


Database Management System [18CS53]

Example: Trigger vs. Assertion

We need triggers, assertions cannot be used


ert

Create Trigger Opening Bal


Before Insert On Customer
For Each Row
Begin
IF (:new.balance is null or :new.balance < 100) Then
RAISE_APPLICATION_ERROR(-20004, 'Balance should be>= $100');
End IF;
End;

1.3 Views (Virtual Tables) in SQL


1.3.1 Concept of a View in SQL

A view in SOL terminology is a single table that is derived from other tables. other
tables can be base tables or previously defined views. A view does not necessarily
exist in physical form; it is considered to be a virtual table, in contrast to base tables,
whose tuples are always physically stored in the database. This limits the possible
update operations that can be applied to views, but it does not provide any limitations
on querying a view. We can think of a view as a way of specifying a table that we
need to reference frequently, even though it may not exist physically.

For example, referring to the COMPANY database, we may frequently issue queries
that retrieve the employee name and the project names that the employee works
on. Rather than having to specify the join of the three tables
EMPLOYEE,WORKS_ON, and PROJECT every time we issue this query, we can
define a view that is specified as the result of these joins. Then we can issue queries
on the view, which are specified as single table retrievals rather than as retrievals
Dept. of ISE, AJIET, MANGALORE Page 25
Database Management System [18CS53]

involving two joins on three tables. We call the EMPLOYEE,WORKS_ON, and


PROJECT tables the defining tables of the view.

1.3.2 Specification of Views in SQL

In SOL, the command to specify a view is CREATE VIEW. The view is given a
(virtual) table name (or view name), a list of attribute names, and a query to specify
the contents of the view. If none of the view attributes results from applying functions
or arithmetic operations, we do not have to specify new attribute names for the view,
since they would be the same as the names of the attributes of the defining tables
in the default case.

Example 1:

CREATE VIEW WORKS_ON1


AS SELECT Fname, Lname, Pname, Hours
FROM EMPLOYEE, PROJECT, WORKS_ON
WHERE Ssn=Essn AND Pno=Pnumber;
Example 2:
CREATE VIEW DEPT_INFO(Dept_name, No_of_emps, Total_sal)
AS SELECT Dname, COUNT (*), SUM (Salary)
FROM DEPARTMENT, EMPLOYEE
WHERE Dnumber=Dno
GROUP BY Dname;

In example 1, we did not specify any new attribute names for the view
WORKS_ON1. In this case,WORKS_ON1inherits the names of the view attributes
from the defining tables EMPLOYEE, PROJECT, and WORKS_ON.
Example 2 explicitly specifies new attribute names for the view DEPT_INFO, using
a one-to-one correspondence between the attributes specified in the CREATE
VIEW clause and those specified in the SELECT clause of the query that defines
the view.

Dept. of ISE, AJIET, MANGALORE Page 26


Database Management System [18CS53]

We can now specify SOL queries on a view-or virtual table-in the same way we
specify queries involving base tables.
For example, to retrieve the last name and first name of all employees who work on
the 'ProductX' project, we can utilize the WORKS_ON1 view and specify the query
as :

SELECT Fname, Lname


FROM WORKS_ON1
WHERE Pname='ProductX';

The same query would require the specification of two joins if specified on the base
relations directly. one of the main advantages of a view is to simplify the
specification of certain queries. Views are also used as a security and authorization
mechanism.

A view is supposed to be always up-to-date; if we modify the tuples in the base


tables on which the view is defined, the view must automatically reflect these
changes. Hence, the view is not realized or materialized at the time of view definition
but rather at the time when we specify a query on the view. It is the responsibility of
the DBMS and not the user to make sure that the view is kept up-to- date.
If we do not need a view any more, we can use the DROP VIEW command to
dispose of it. For example : DROP VIEW WORKS_ON1;

1.3.3 View Implementation, View Update and lnline Views


The problem of efficiently implementing a view for querying is complex. Two main
approaches have been suggested.
• One strategy, called query modification, involves modifying or transforming the
view query (submitted by the user) into a query on the underlying base tables.

Dept. of ISE, AJIET, MANGALORE Page 27


Database Management System [18CS53]

For example, the query


SELECT Fname, Lname
FROM WORKS_ON1
WHERE Pname='ProductX';

would be automatically modified to the following query by the DBMS:


SELECT Fname, Lname
FROM EMPLOYEE, PROJECT, WORKS_ON
WHERE Ssn=Essn AND Pno=Pnumber
AND Pname='ProductX;'

The disadvantage of this approach is that it is inefficient for views defined via
complex queries that are time-consuming to execute, especially if multiple queries
are going to be applied to the same view within a short period of time.
• The second strategy, called view materialization, involves physically creating a
temporary view table when the view is first queried and keeping that table on the
assumption that other queries on the view will follow. In this case, an efficient
strategy for automatically updating the view table when the base tables are
updated must be developed in order to keep the view up-to-date.

Techniques using the concept of incremental update have been developed for
this purpose, where the DBMS can determine what new tuples must be inserted,
deleted, or modified in a materialized view table when a database update is
applied to one of the defining base tables.
The view is generally kept as a materialized (physically stored) table as long as it is
being queried. If the view is not queried for a certain period of time, the system may
then automatically remove the physical table and recompute it from scratch when
future queries reference the view.
Updating of views is complicated and can be ambiguous. In general, an update on
a view defined on a single table without any aggregate functions can be mapped to
an update on the underlying base table under certain conditions. For a view involving
joins, an update operation may be mapped to update operations on the underlying
base relations in multiple ways. Hence, it is often not possible for the DBMS to
determine which of the updates is intended.
Dept. of ISE, AJIET, MANGALORE Page 28
Database Management System [18CS53]

To illustrate potential problems with updating a view defined on multiple tables,


consider the WORKS_ON1 view, and suppose that we issue the command to
update the PNAME attribute of 'John Smith' from 'ProductX' to 'ProductY'. This view
update is shown in UV1:
UV1: UPDATE WORKS_ON1
SET Pname = 'ProductY'
WHERE Lname='Smith' AND Fname='John'
AND Pname='ProductX';
This query can be mapped into several updates on the base relations to give the
desired update effect on the view. In addition, some of these updates will create
additional side effects that affect the result of other queries.
For example, here are two possible updates, (a) and (b), on the base relations
corresponding to the view update operation in UV1:
(a) : UPDATE WORKS_ON
SET Pno= (SELECT Pnumber
FROM PROJECT
WHERE
Pname='ProductY' )
WHERE Essn IN (
SELECT Ssn FROM
EMPLOYEE
WHERE Lname='Smith' AND Fname='John' )
AND
Pno= (SELECT Pnumber
FROM PROJECT
WHERE Pname='ProductX' );

Dept. of ISE, AJIET, MANGALORE Page 29


Database Management System [18CS53]

(b) : UPDATEPROJECT SET Pname = 'ProductY'


WHERE Pname = 'ProductX';

Update (a) relates 'John Smith' to the 'ProductY' PROJECT tuple instead of the
'ProductX' PROJECT tuple and is the most likely desired update. However, (b)
would also give the desired update effect on the view, but it accomplishes this by
changing the name of the 'ProductX' tuple in the PROJECT relation to 'ProductY'.

It is quite unlikely that the user who specified the view update UV1 wants the update
to be interpreted as in (b), since it also has the side effect of changing all the view
tuples with Pname = 'ProductX'.

Some view updates may not make much sense; for example, modifying the Total_sal
attribute of the DEPT_INFO view does not make sense because Total_sal is defined to
be the sum of the individual employee salaries. This request is shown as UV2:
UV2: UPDATE DEPT_INFO
SET Total_sal=100000
WHERE Dname='Research' ;
A large number of updates on the underlying base relations can satisfy this view update.

Generally, a view update is feasible when only one possible update on the base
relations can accomplish the desired update effect on the view. Whenever an update
on the view can be mapped to more than one update on the underlying base
relations, we must have a certain procedure for choosing one of the possible
updates as the most likely one.

In summary, we can make the following observations:


■ A view with a single defining table is updatable if the view attributes contain the
primary key of the base relation, as well as all attributes with the NOT NULL
constraint that do not have default values specified.
■ Views defined on multiple tables using joins are generally not updatable.
■ Views defined using grouping and aggregate functions are not updatable.

Dept. of ISE, AJIET, MANGALORE Page 30


Database Management System [18CS53]

In SQL, the clause WITH CHECK OPTION must be added at the end of the view
definition if a view is to be updated. This allows the system to check for view
updatability and to plan an execution strategy for view updates. It is also possible
to define a view table in the FROM clause of an SQL query. This is known as an in-
line view. In this case, the view is defined within the query itself.

1.4 Schema Change Statements in SQL


Schema evolution commands available in SOL can be used to alter a schema by
adding or dropping tables, attributes, constraints, and other schema elements. This
can be done while the database is operational and does not require recompilation
of the database schema.

1.4.1 The DROP Command


The DROP command can be used to drop named schema elements, such as tables,
domains, or constraints. One can also drop a schema. For example, if a whole
schema is no longer needed, the DROP SCHEMA command can be used.
There are two drop behavior options: CASCADE and RESTRICT. For example, to
remove the COMPANY database schema and all its tables, domains, and other
elements, the CASCADE option is used as follows:
DROP SCHEMA COMPANY CASCADE;
If the RESTRICT option is chosen in place of CASCADE, the schema is dropped
only if it has no elements in it; otherwise, the DROP command will not be executed.
To use the RESTRICT option, the user must first individually drop each element in
the schema, then drop the schema itself.

If a base relation within a schema is no longer needed, the relation and its definition
can be deleted by using the DROP TABLE command. For example, if we no longer
wish to keep track of dependents of employees in the COMPANY database, , we
can get rid of the DEPENDENT relation by issuing the following command:
DROP TABLE DEPENDENT CASCADE;
If the RESTRICT option is chosen instead of CASCADE, a table is dropped only if
it is not referenced in any constraints (for example, by foreign key definitions in
another relation) or views or by any other elements. With the CASCADE option, all
Dept. of ISE, AJIET, MANGALORE Page 31
Database Management System [18CS53]

such constraints, views, and other elements that reference the table being dropped
are also dropped automatically from the schema, along with the table itself.
The DROP TABLE command not only deletes all the records in the table if
successfu,l but also removes the table definition from the catalog. If it is desired to
delete only the records but to leave the table definition for future use, then the
DELETE command should be used instead of DROP TABLE.
The DROP command can also be used to drop other types of named schema
elements, such as constraints or domains.

1.4.2 The ALTER Command

The definition of a base table or of other named schema elements can be changed
by using the ALTER command. For base tables, the possible alter table actions
include adding or dropping a column (attribute), changing a column definition, and
adding or dropping table constraints.

For example, to add an attribute for keeping track of jobs of employees to the
EMPLOYEE base relation in the COMPANY schema, we can use the command:
ALTER TABLE COMPANY.EMPLOYEE ADD COLUMN Job VARCHAR(12);

We must still enter a value for the new attribute Job for each individual EMPLOYEE
tuple. This can be done either by specifying a default clause or by using the
UPDATE command individually on each tuple. If no default clause is specified, the
new attribute will have NULLs in all the tuples of the relation immediately after the
command is executed; hence, the NOT NULL constraint is not allowed in this case.
To drop a column, we must choose either CASCADE or RESTRICT for drop behavior.
If CASCADE
is chosen, all constraints and views that reference the column are dropped
automatically from the schema, along with the column. If RESTRICT is chosen, the
command is successful only if no views or constraints (or other schema elements)
reference the column.
For example, the following command removes the attribute Address from the
EMPLOYEE base table:
ALTER TABLE COMPANY.EMPLOYEEDROP COLUMN Address
CASCADE;

Dept. of ISE, AJIET, MANGALORE Page 32


Database Management System [18CS53]

It is also possible to alter a column definition by dropping an existing default


clause or by defining a new default clause. The following examples illustrate this
clause:
ALTER TABLE COMPANY.DEPARTMENT ALTER COLUMN Mgr_ssn DROP
DEFAULT;

ALTER TABLE COMPANY.DEPARTMENT ALTER COLUMN Mgr_ssn SET


DEFAULT

'333445555' ;

Alter Table - Alter/Modify Column


To change the data type of a column in a table, use the following syntax:
ALTER TABLE table_name
MODIFY column_name datatype;

For example we can change the data type of the column named "DateOfBirth"
from date to year in the "Persons" table using the following SQL statement:
ALTER TABLE Persons
ALTER COLUMN DateOfBirth year;

Dept. of ISE, AJIET, MANGALORE Page 33


Database Management System [18CS53]

Notice that the "DateOfBirth" column is now of type year and is going to hold a year in
a two- or four-digit format.

Dept. of ISE, AJIET, MANGALORE Page 34


Database Management System [18CS53]

Chapter 2: Database Application Development

2.1 Introduction
We often encounter a situation in which we need the greater flexibility of a general-
purpose programming language in addition to the data manipulation facilities
provided by SOL.For example, we may want to integrate a database applications
with GUI or we may want to integrate with other existing applications.

2.2 Accessing Databases from applications


SQL commands can be executed from within a program in a host language such as
C or Java. A language to which SQL queries are embedded are called Host
language.

2.2.1 Embedded SQL

The use of SQL commands within a host language is called Embedded SQL.
Conceptually, embedding SQL commands in a host language program is straight
forward. SQL statements can be used wherever a statement in the host language is
allowed. SQL statements must be clearly marked so that a preprocessor can deal
with them before invoking the compiler for the host language. Any host language
variable used to pass arguments into an SQL command must be declared in SQL.
There are two complications:
1. Data types recognized by SQL may not be recognized by the host language and vice
versa
- This mismatch is addressed by casting data values appropriately before
passing them to or from SQL commands.
2. SQL is set-oriented
- Addressed using cursors

Declaring Variables and Exceptions

SQL statements can refer to variables defined in the host program. Such host
language variables must be prefixed by a colon(:) in SQL statements and be

Dept. of ISE, AJIET, MANGALORE Page 35


Database Management System [18CS53]

declared between the commands

EXEC SQL BEGIN DECLARE SECTION and EXEC SQL END DECLARE SECTION

The declarations are similar to C, are separated by semicolons. For example, we


can declare variables c_sname, c_sid, c_rating, and c_age (with the initial c used
as a naming convention to emphasize that these are host language variables) as
follows:
EXEC SQL BEGIN DECLARE SECTION
char _sname[20;]
long c_sid;
short c_rating;
float c_age;
EXEC SQL END DECLARE SECTION

The first question that arises is which SQL types correspond to the various C types,
since we have just declared a collection of C variables whose values are intended
to be read (and possibly set) in an SQL run-time environment when an SQL statement
that refers to them is executed. The SQL-92 standard defines such a correspondence
between the host language types and SQL types for a number of host languages. In
our example, c_sname has the type CHARACTER(20) when referred to in an SQL
statement, c_sid has the type INTEGER, crating has the type SMALLINT, and
c_age has the type REAL.
We also need some way for SQL to report what went wrong if an error condition
arises when executing an SQL statement. The SQL-92 standard recognizes two
special variables for reporting errors, SQLCODE and SQLSTATE.
■ SQLCODE is the older of the two and is defined to return some negative
value when an error condition arises, without specifying further just what
error a particular negative integer denotes.
■ SQLSTATE, introduced in the SQL-92 standard for the first time, associates
predefined
values with several common error conditions, thereby introducing some
uniformity to how errors are reported.
One of these two variables must be declared. The appropriate C type for SQLCODE is
long and the appropriate C type for SQLSTATE is char [6] , that is, a character string
five characters long.
Dept. of ISE, AJIET, MANGALORE Page 36
Database Management System [18CS53]

Embedding SQL statements


All SQL statements embedded within a host program must be clearly marked with
the details dependent on the host language. In C, SQL statements must be prefixed
by EXEC SQL. An SQL statement can essentially appear in any place in the host
language program where a host language statement can appear.
Example: The following embedded SQL statement inserts a row, whose column
values are based on the values of the host language variables contained in it, into
the sailors relation
EXEC SQL INSERT INTO sailors VALUES (:c_sname, :c_sid,
:c_rating,:c_age);

The SQLSTATE variable should be checked for errors and exceptions after each
Embedded SOL statement.SOL provides the WHENEVER command to simplify
this task:
EXEC SQL WHENEVER [SQLERROR I NOT FOUND ] [CONTINUEIGOTO stmt]
If SQLERROR is specified and the value of SOLSTATE indicates an exception,
control is transferred to stmt, which is presumably responsible for error and
exception handling. Control is also transferred to stmt if NOT FOUND is specified
and the value of SOLSTATE is 02000, which denotes NO DATA.

2.2.2 Cursors
A major problem in embedding SOL statements in a host language like C is that an
impedance mismatch occurs because SOL operates on sets of records, whereas
languages like C do not cleanly support a set-of-records abstraction. The solution is
to essentially provide a mechanism that allows us to retrieve rows one at a time from
a relation- this mechanism is called a cursor
We can declare a cursor on any relation or on any SOL query. Once a cursor is declared, we
can
■ open it (positions the cursor just before the first row)
■ Fetch the next row
■ Move the cursor (to the next row,to the row after the next n, to the first row
or previous row etc by specifying additional parameters for the fetch
command)
■ Close the cursor
Dept. of ISE, AJIET, MANGALORE Page 37
Database Management System [18CS53]

Cursor allows us to retrieve the rows in a table by positioning the cursor at a


particular row and reading its contents.
Basic Cursor Definition and Usage
Cursors enable us to examine, in the host language program, a collection of rows
computed by an Embedded SOL statement:
■ We usually need to open a cursor if the embedded statement is a
SELECT. we can avoid opening a cursor if the answer contains a single row
■ INSERT, DELETE and UPDATE statements require no cursor. some variants of
DELETE
and UPDATE use a cursor.
Examples:
i) Find the name and age of a sailor, specified by assigning a value to the host
variable c_sid, declared earlier
EXEC SQL SELECT s.sname,s.age
INTO :c_sname, :c_age
FROM Sailaor s
WHERE s.sid=:c.sid;

The INTO clause allows us assign the columns of the single answer row to the host
variable c_sname and c_age. Therefore, we do not need a cursor to embed this
query in a host language program.

ii) Compute the name and ages of all sailors with a rating greater than the current
value of the host variable c_minrating
SELECT s.sname,s.age
FROM sailors s WHERE s.rating>:c_minrating;
The query returns a collection of rows. The INTO clause is inadequate. The solution
is to use a cursor:
DECLARE sinfo CURSOR FOR
SELECT s.sname,s.age
FROM sailors s
WHERE s.rating>:c_minrating;
This code can be included in a C program and once it is executed, the cursor
sinfo is defined. We can open the cursor by using the syntax:
OPEN sinfo;
Dept. of ISE, AJIET, MANGALORE Page 38
Database Management System [18CS53]

A cursor can be thought of as 'pointing' to a row in the collection of answers to the


query associated with it.When the cursor is opened, it is positioned just before the
first row.
We can use the FETCH command to read the first row of cursor sinfo into host language
variables:
FETCH sinfo INTO :c_sname, :c_age;
When the FETCH statement is executed, the cursor is positioned to point at the
next row and the column values in the row are copied into the corresponding host
variables. By repeatedly executing this FETCH statement, we can read all the rows
computed by the query, one row at time.
When we are done with a cursor, we can close it:
CLOSE sinfo;
iii) To retrieve the name, address and salary of an employee specified by the variable ssn

//Program Segment El:


0) loop = 1 ;
l) while (loop) {
2) prompt( "Enter a Social Security Number: ", ssn)
3) EXEC SQL
4) SELECT Fname, Minit, Lname, Address, Salary
S) INTO :fname, :minit, :lname, :address, :salary
6) FROM EMPLOYEE WHERE Ssn = : ssn ;
7) if (SQLCODE = = 0) printf(fname, minit, lname, address, salary)
8) else printf("Social Security Number does not exist:", ssn);
9) prompt("More Social Security Numbers (enter 1 for Yes, 0 for
No): " loop)
10) }

Properties of Cursors
The general form of a cursor declaration is:
DECLARE cursomame [INSENSITIVE] [SCROLL]
CURSOR [WITH HOLD]
FOR some query
[ORDER BY order-item-list]
[FOR READ ONLY I FOR UPDATE]
A cursor can be declared to be a read-only cursor (FOR READ ONLY) or updatable
cursor (FOR UPDATE).lf it is updatable, simple variants of the UPDATE and
DELETE commands allow us to update or delete the row on which the cursor is

Dept. of ISE, AJIET, MANGALORE Page 39


Database Management System [18CS53]

positioned. For example, if sinfo is an updatable cursor and open, we can execute
the following statement:
UPDATE Sailors S
SET S.rating = S.rating -1
WHERE CURRENT of sinfo;
A cursor is updatable by default unless it is a scrollable or insensitive cursor in which
case it is read- only by default.

If the keyword SCROLL is specified, the cursor is scrollable, which means that
variants of the FETCH command can be used to position the cursor in very flexible
ways; otherwise, only the basic FETCH command, which retrieves the next row, is
allowed

If the keyword INSENSITIVE is specified, the cursor behaves as if it is ranging over


a private copy of the collection of answer rows. Otherwise, and by default, other
actions of some transaction could modify these rows, creating unpredictable
behavior.
A holdable cursor is specified using the WITH HOLD clause, and is not closed when
the transaction is committed.
Optional ORDER BY clause can be used to specify a sort order. The order-item-list
is a list of order- items. An order-item is a column name, optionally followed by one of
the keywords ASC or DESC Every column mentioned in the ORDER BY clause
must also appear in the select-list of the query associated with the cursor; otherwise
it is not clear what columns we should sort on
ORDER BY minage ASC , rating DESC

The answer is sorted first in ascending order by minage, and if several rows have
the same minage value, these rows are sorted further in descending order by
Rating minage
rating
8 25.5

3 25.5

7 35.0

Dept. of ISE, AJIET, MANGALORE Page 40


Database Management System [18CS53]

Dynamic SQL
Dynamic SOL Allow construction of SOL statements on-the-fly. Consider an
application such as a spreadsheet or a graphical front-end that needs to access
data from a DBMS. Such an application must accept commands from a user and,
based on what the user needs, generate appropriate SOL
statements to retrieve the necessary data. In such situations, we may not be able
to predict in advance just what SOL statements need to be executed. SOL provides
some facilities to deal with such situations; these are referred to as Dynamic SQL.
Example:

char c_sqlstring[] = {"DELETE FROM Sailors WHERE rating>5"};


EXEC SQL PREPARE readytogo FROM :csqlstring;
EXEC SQL EXECUTE readytogo;
■ The first statement declares the C variable c_sqlstring and initializes its
value to the string representation of an SOL command
■ The second statement results in this string being parsed and compiled as an SOL
command,
with the resulting executable bound to the SOL variable readytogo
■ The third statement executes the command

2.3 An Introduction to JDBC


Embedded SOL enables the integration of SOL with a general-purpose
programming language. A DBMS-specific preprocessor transforms the Embedded
SOL statements into function calls in the host language. The details of this
translation vary across DBMSs, and therefore even though the source code can be
compiled to work with different DBMSs, the final executable works only with one specific
DBMS.
ODBC and JDBC, short for Open DataBase Connectivity and Java DataBase
Connectivity, also enable the integration of SOL with a general-purpose
programming language .

■ In contrast to Embedded SOL, ODBC and JDBC allow a single


executable to access different DBMSs Without recompilation.

Dept. of ISE, AJIET, MANGALORE Page 41


Database Management System [18CS53]

■ While Embedded SOL is DBMS-independent only at the source code level,


applications using ODBC or JDBC are DBMS-independent at the source code
level and at the level of the executable
■ In addition, using ODBC or JDBC, an application can access not just one DBMS
but several different ones simultaneously
■ ODBC and JDBC achieve portability at the level of the executable by introducing
an extra
level of indirection
■ All direct interaction with a specific DBMS happens through a DBMS-specific driver.

A driver is a software program that translates the ODBC or JDBC calls into DBMS-
specific calls. Drivers are loaded dynamically on demand since the DBMSs the
application is going to access are known only at run-time. Available drivers are
registered with a driver manager a driver does not necessarily need to interact
with a DBMS that understands SOL. It is sufficient that the driver translates the
SOL commands from the application into equivalent commands that the DBMS
understands.
An application that interacts with a data source through ODBC or JDBC selects
a data source, dynamically loads the corresponding driver, and establishes a
connection with the data source. There is no limit on the number of open
connections. An application can have several open connections to different data
sources. Each connection has transaction semantics; that is, changes from one
connection are visible to other connections only after the connection has
committed its changes. While a connection is open, transactions are executed
by submitting SOL statements, retrieving results, processing errors, and finally
committing or rolling back. The application disconnects from the data source to
terminate the interaction.

2.3.1 Architecture
The architecture of JDBC has four main components:

■ Application
■ Driver manager
■ Drivers
Dept. of ISE, AJIET, MANGALORE Page 42
Database Management System [18CS53]

■ Data sources

Application

■ initiates and terminates the connection with a data source


■ sets transaction boundaries, submits SOL statements and retrieves the results

Driver manager

■ Load JDBC drivers and pass JDBC function calls from the application to the correct
driver
■ Handles JDBC initialization and information calls from the applications
and can log all function calls
■ Performs some rudimentary error checking

Drivers

■ Establishes the connection with the data source


■ Submits requests and returns request results
■ Translates data, error formats, and error codes from a form that is specific to
the data source into the JDBC standard

Data sources

■ Processes commands from the driver and returns the results

Drivers in JDBC are classified into four types depending on the architectural
relationship between the application and the data source:

Type I Bridges:
■ This type of driver translates JDBC function calls into function calls of another
API that is not native to the DBMS.
■ An example is a JDBC-ODBC bridge; an application can use JDBC calls
to access an ODBC compliant data source. The application loads only one
driver, the bridge.
■ Advantage:
• it is easy to piggyback the application onto an existing installation,
and no new drivers have to be installed.
■ Drawbacks:
Dept. of ISE, AJIET, MANGALORE Page 43
Database Management System [18CS53]

• The increased number of layers between data source and


application affects performance
• the user is limited to the functionality that the ODBC driver supports.

Type II Direct Translation to the Native API via Non-Java Driver:


• This type of driver translates JDBC function calls directly into method
invocations of the API of one specific data source.
• The driver is usually ,written using a combination of C++ and Java; it
is dynamically linked and specific to the data source.
■ Advantage
• This architecture performs significantly better than a JDBC-ODBC bridge.
■ Disadvantage
• The database driver that implements the API needs to be
installed on each computer that runs the application.
Type lll~~Network Bridges:
■ The driver talks over a network to a middleware server that translates the
JDBC requests into DBMS-specific method invocations.
■ In this case, the driver on the client site is not DBMS-specific.
■ The JDBC driver loaded by the application can be quite small, as the only
functionality it needs to implement is sending of SOL statements to the
middleware server.
■ The middleware server can then use a Type II JDBC driver to connect to the data
source.
Type IV-Direct Translation to the Native API via Java Driver:
■ Instead of calling the DBMS API directly, the driver communicates with
the DBMS through Java sockets
■ In this case, the driver on the client side is written in Java, but it is
DBMS-specific. It translates JDBC calls into the native API of the
database system.
■ This solution does not require an intermediate layer, and since the
implementation is all Java, its performance is usually quite good.

Dept. of ISE, AJIET, MANGALORE Page 44


Database Management System [18CS53]

2.4 JDBC CLASSES AND INTERFACES


JDBC is a collection of Java classes and interfaces that enables database access
from programs written in the Java language. It contains methods for connecting to
a remote data source, executing SOL statements, examining sets of results from
SOL statements, transaction management, and exception handling.
The classes and interfaces are part of the java.sql package. JDBC 2.0 also includes
the javax.sql package, the JDBC Optional Package. The package javax.sql adds,
among other things, the capability of connection pooling and the Row-Set interface.

2.4.1 JDBC Driver Management


In JDBC, data source drivers are managed by the Drivermanager class, which
maintains a list of all currently loaded drivers. The Drivermanager class has methods
registerDriver, deregisterDrive,rand getDrivers to enable dynamic addition and
deletion of drivers.
The first step in connecting to a data source is to load the corresponding JDBC driver.
The following Java example code explicitly loads a JDBC driver:

Class.forName("oracle/jdbc.driver.OracleDriver");

There are two other ways ofregistering a driver. We can include the driver with -
Djdbc. drivers=oracle/jdbc. driver at the command line when we start the Java
application. Alternatively, we can explicitly instantiate a driver, but this method is
used only rarely, as the name of the driver has to be specified in the application code,
and thus the application becomes sensitive to changes at the driver level.
After registering the driver, we connect to the data source.

2.4.2 Connections
2.4.3
A session with a data source is started through creation of a Connection object;
Connections are specified through a JDBC URL, a URL that uses the jdbc protocol.
Such a URL has the form
jdbc:<subprotocol>:<otherParameters>

Dept. of ISE, AJIET, MANGALORE Page 45


Database Management System [18CS53]

String uri = ..
jdbc:oracle:www.bookstore.com:3083..
Connection connection;
try
{
connection = DriverManager.getConnection(url,userld,password);
}
catch(SQLException excpt)
{
System.out.println(excpt.getMessageO);
return;
}

Program code: Establishing a Connection with JDBC


In JDBC, connections can have different properties. For example, a connection can
specify the granularity of transactions. If autocommit is set for a connection, then
each SOL statement is

considered to be its own transaction. If autocommit is off, then a series of


statements that compose a transaction can be committed using the commit()
method of the Connection class, or aborted using the rollback() method. The
Connection class has methods to set the autocommit mode (Connection .
setAutoCommit) and to retrieve the current autocommit mode (getAutoCommit) .
The following methods are part of the Connection interface and permit setting and
getting other properties:
■ public int getTransactionlsolation() throws
SQLException and public void
setTransactionlsolation(int 1) throws SQLException.
- These two functions get and set the current level of isolation for
transactions handled in the current connection. All five SQL levels of
isolation are possible, and argument I can be set as follows:
- TRANSACTION_NONE
-TRANSACTION_READ_UNCOMMITTED
-TRANSACTION_READ_COMMITTED
Dept. of ISE, AJIET, MANGALORE Page 46
Database Management System [18CS53]

- TRANSACTION_REPEATABLE_READ
- TRANSACTION_SERIALIZABLE
■ public boolean getReadOnlyO throws SQLException and
public void setReadOnly(boolean readOnly) throws SQLException.
- These two functions allow the user to specify whether the transactions
executed through this connection are read only.
■ public boolean isClosed() throws SQLException.
- Checks whether the current connection has already been closed.
■ setAutoCommit and get AutoCommit.
In case an application establishes many different connections from different parties
(such as a Web server), connections are often pooled to avoid this overhead. A
connection pool is a set of established connections to a data source. Whenever a
new connection is needed, one of the connections from the pool is used, instead of
creating a new connection to the data source.

2.4.4 Executing SQL Statements


JDBC supports three different ways of executing statements:
• Statement
• PreparedStatemen,t and
• CallableStatement.
The Statement class is the base class for the other two statement classes. It allows us
to query the data source with any static or dynamically generated SQL query.

The PreparedStatement class dynamically generates precompiled SOL


statements that can be used several times; these SOL statements can have
parameters, but their structure is fixed when the PreparedStatement object is
created.
I I initial quantity is always zero
String sql = "INSERT INTO Books VALUES('?, 7, '?, ?, 0, 7)";
PreparedStatement pstmt = con.prepareStatement(sq;l)
I I now instantiate the parameters with values
I I a,ssume that isbn, title, etc. are Java
variables that I I contain the values to be
inserted pstmt.clearParameters() ;

Dept. of ISE, AJIET, MANGALORE Page 47


Database Management System [18CS53]

pstmt.setString(I, isbn);
pstmt.setString(2, title);
pstmt.setString(3, author);
pstmt.setFloat(5, price);
pstmt.setlnt(6, year);
int numRows = pstmt.executeUpdate();

program code: SOL Update Using a PreparedStatement Object

The SOL query specifies the query string, but uses "?' for the values of the
parameters, which are set later using methods setString, setFloat,and setlnt. The
"?" placeholders can be used anywhere in SOL statements where they can be
replaced with a value. Examples of places where they can appear include the
WHERE clause (e.g., 'WHERE author=?'), or in SOL UPDATE and INSERT
statements. The method setString is one way to set a parameter value; analogous
methods are available for int, float, and date. It is good style to always use
clearParameters() before setting parameter values in order to remove any old data.

There are different ways of submitting the query string to the data source. In the
example, we used the executeUpdate command, which is used if we know that the
SOL statement does not return any records (SOL UPDATE, INSERT.ALTER, and
DELETE statements). The executeUpdate method returns
- an integer indicating the number of rows the SOL statement modified;
- 0 for successful execution without modifying any rows.
The executeOuery method is used if the SOL statement returns data, such as in a
regular SELECT query. JDBC has its own cursor mechanism in the form of a
ResultSet object.

Dept. of ISE, AJIET, MANGALORE Page 48


Database Management System [18CS53]

2.4.5 ResultSets
ResultSet cursors in JDBC 2.0 are very powerful; they allow forward and reverse
scrolling and in- place editing and insertions. In its most basic form, the ResultSet
object allows us to read one row of the output of the query at a time. Initially, the
ResultSet is positioned before the first row, and we have to retrieve the first row
with an explicit call to the next() method. The next method returns false if there are
no more rows in the query answer, and true other\vise. The code fragment shown
below illustrates the basic usage of a ResultSet object:
ResultSet rs=stmt.executeQuerry(sqlQuery);
I I rs is now a cursor
I I first call to rs.nextO moves to the first record
I I rs.nextO moves to the next row String sqlQuery;
ResultSet rs = stmt.executeQuery(sqlQuery)
while (rs.next())
{
I I process the data
}
While next () allows us to retrieve the logically next row in the query answer, we can
move about in the query answer in other ways too:
■ previous() moves back one row.
■ absolute(int num) moves to the row with the specified number.
■ relative(int num) moves forward or backward (if num is negative) relative
to the current position. relative (-1) has the same effect as previous.
■ first() moves to the first row, and last() moves to the last row.

Matching Java and SQL Data Types


In considering the interaction of an application with a data source, the issues we
encountered in the context of Embedded SQL (e.g., passing information between the
application and the data source through shared variables) arise again. To deal with
such issues, JDBC provides special data types and specifies their relationship to
corresponding SQL data types. Table 2.4.4 shows the accessor methods in a ResultSet
object for the most common SQL datatypes.

Dept. of ISE, AJIET, MANGALORE Page 49


Database Management System [18CS53]

With these accessor methods, we can retrieve values from the current row of the
query result referenced by the ResultSet object. There are two forms for each
accessor method. One method retrieves values by column index, starting at one,
and the other retrieves values by column name.

The following example shows how to access fields of the current ResultSet row
using accesssor methods.
ResultSet rs=stmt.executeOuery(sqIOuery);

String sqlOuerYi
ResultSet rs = stmt.executeOuery(sqlOuery)

while (rs.nextO)
{
isbn = rs.getString(I);
title = rs.getString("TITLE");
I I process isbn and title
}

SQL Type Java class ResultSet get


method
BIT Boolean getBoolean()
CHAR String getString()
VARCHAR String getString()
DOUBLE Double getDouble()
FLOAT Double getDouble()
INTEGER Integer getlnt()
REAL Double getFloat()
DATE java.sql.Date getDate()
TIME java.sql.Time getTime()
TIMESTAMP java.sql.TimeStam getTimestamp()
p

Table 2.4.4 : Reading SOL Datatypes from a ResultSet Object

Dept. of ISE, AJIET, MANGALORE Page 50


Database Management System [18CS53]

2.4.6 Exceptions and Warnings


Similar to the SOLSTATE variable, most of the methods in java. sql can throw an
exception of the type SOLException if an error occurs. The information includes
SOLState, a string that describes the error (e.g., whether the statement contained
an SOL syntax error). In addition to the standard getMessage() method inherited
from Throwable, SOLException has two additional methods that provide further
information, and a method to get (or chain) additional exceptions:
■ public String getSOLState() returns an SOLState identifier based on the
SOL:1999 specification
■ public int getErrorCode () retrieves a vendor-specific error code.

■ public SQLException getNextExceptionO gets the next exception in a chain


of exceptions associated with the current SQLException object.

An SQLWarning is a subclass of SQLException. Warnings are not as severe as


errors and the program can usually proceed without special handling of warnings.
Warnings are not thrown like other exceptions, and they are not caught as part of the
try-catch block around a java.sql statement. We need to specifically test whether
warnings exist. Connection, Statement, and ResultSet objects all have a
getWarnings() method with which we can retrieve SOL warnings if they exist.
Duplicate retrieval of warnings can be avoided through clearWarnings().
Statement objects clear warnings automatically on execution of the next statement;
ResultSet objects clear warnings every time a new tuple is accessed.
Typical code for obtaining SQLWarnings looks similar to the code
shown below: try
{
stmt = con.createStatemen(t);

warning = con.getWarnings(); while( warning != null)


{
I I handleSQLWarnings //code to process warning

warning = warning.getNextWarningO;

Dept. of ISE, AJIET, MANGALORE Page 51


Database Management System [18CS53]

!get next warning


}
Con.clear\Varnings() ;
stmt.executeUpdate( queryString);
warning = stmt.getWarnings();

while( warning != null)


{
II handleSQLWarnings //code to process
warning warning = warning.getNextWarningO; I
} !get next warning
} //end
try

catch ( SQLException SQLe)


{
I I code to handle exception
} / / end catch

Dept. of ISE, AJIET, MANGALORE Page 52


Database Management System [18CS53]

2.4.7 Examining Database Metadata


We can use the DatabaseMetaData object to obtain information about the database
system itself, as well as information from the database catalog. For example, the
following code fragment shows how to obtain the name and driver version of the
JDBC driver:
Databa..seMetaData md =
con.getMetaD<Lta():
System.out.println("DriverInformation:");
System.out.println("Name:" +
md.getDriverNameO
+ "; version:" + mcl.getDriverVersion());
The DatabaseMetaData object has many more methods (in JDBC 2.0, exactly
134). Some of the methods are:
• public ResultSet getCatalogs() throws SqLException. This function
returns a ResultSet that can be used to iterate over all public int
getMaxConnections() throws SqLException the catalog relations.This
function returns the maximum number of connections possible.
Example: code fragment that examines all database
metadata DatabaseMetaData dmd =
con.getMetaDataO; ResultSet tablesRS =
dmd.getTables(nul,lnull,null,null); string
tableName;
while(tablesRS .next())
{
tableNarne = tablesRS .getString("TABLE_NAME";)

I I print out the attributes of this table


System.out.println("Theattributes of table"+ tableName + "
are:");

ResultSet columnsRS = dmd.getColums(null,null,tableNamen, ull);

while (columnsRS.next())

Dept. of ISE, AJIET, MANGALORE Page 53


Database Management System [18CS53]

{
System.out.print(colummsRS.getString("COLUMN_NAME")
+" ");
}
I I print out the primary keys of this table

System.out.println("Thekeys of table"+ tableName + " are:");

ResultSet keysRS = dmd.getPrimaryKeys(null,null,tableName);

while (keysRS. next())


{
System.out.print(keysRS.getStringC'COLUMN_NAME"+
) " ");
}

}
7 steps for jdbc :
1. Import the package
-- import java.sql.*;
2. Load and register the driver
--class.forname();
3. Establish the connection
-- Connection con;
4. Create a Statement object
-- Statement st;
5. Execute a query
-- st.execute();
6. Process the result
7. Close the connection

Step1:import the package


Step 2: load the corresponding JDBC driver
Class.forName("oracle/jdbc.driver.OracleDriver");
Step 3: create a session with data source through creation of Connection object.
Connection connection= DriverManager.getConnection(database_url,
userld, password);

Dept. of ISE, AJIET, MANGALORE Page 54


Database Management System [18CS53]

EX: Connection con= DriverManager.getConnection


("jdbc:orale:thin:@localhost:1521:xesid","system","ambika");
Step 4:create a statement object
• JDBC supports three different ways of executing statements:
- Statement
- PreparedStatement and

- CallableStatement.
• The Statement class is the base class for the other two statement classes. It
allows us to query the data source with any static or dynamically
generated SOL query.
• The PreparedStatement class dynamically generates precompiled SOL
statements that can be used several times
• CallableStatement are used to call stored procedures from JDBC.
CallableStatement is a subclass of PreparedStatement and provides the
same functionality.
• Example:
Statement st=con.createStatement();
Step 5: executing a query

String query="select * from students where

usn='4W15CS001"';

ResultSet rs=st.executeQuery(query);

Step 6: process the result

String sname=rs.getString(2);

System.out.println(sname);

Step 7: close the

connection

con.close();

Dept. of ISE, AJIET, MANGALORE Page 55


Database Management System [18CS53]

Dept. of ISE, AJIET, MANGALORE Page 56


Database Management System [18CS53]

2.5 SQLJ: SQL-JAVA

SQLJ enables applications programmers to embed SQL statements in Java code in a


way that is compatible with the Java design philosophy
Example: SQLJ code fragment that selects records from the Books table that match a
given author.

String title;
Float price;
String author;

#sql iterator Books (String title, Float price);

Books books;

#sql books= {

SELECT title, price INTO :title, :price

FROM Books WHERE author= :author

};

while (books.next()) {

System.out.println(books.title() + ", "+ books.price());

books.close() ;

All SQLJ statements have the special prefix #sql. In SQLJ, we retrieve the results
of SQL queries with iterator objects, which are basically cursors. An iterator is an
instance of an iterator class.
Usage of an iterator in SQLJ goes through five steps:
1. Declare the Iterator Class: In the preceding code, this happened through
the statement #sql iterator Books (String title, Float price);
This statement creates a new Java class that we can use to instantiate objects.
2. Instantiate an Iterator Object from the New

Iterator Class: We instantiated our iterator in the

DEPT. OF ISE, AJIET, MANGALORE 57


Database Management System [18CS53]

statement Books books;.

3. Initialize the Iterator Using a SQL Statement:


In our example, this happens through the statement #sql books =....
4. Iteratively, Read the Rows From the Iterator Object:
This step is very similar to reading rows through a ResultSet
object in JDBC.
5. Close the Iterator Object.

There are two types of iterator classes:


• named iterators
• positional iterators
For named iterators, we specify both the variable type and the name of each column
of the iterator. This allows us to retrieve individual columns by name. This method is
used in our example.
For positional iterators, we need to specify only the variable type for each column
of the iterator. To access the individual columns of the iterator, we use a FETCH ...
INTO construct, similar to Embedded SOL
We can make the iterator a positional iterator through the following

statement: #sql iterator Books (String, Float);

We then retrieve the individual rows from the iterator as follows:

while (true)

#sql { FETCH :books INTO :title, :price, };

if (books.endFetch())

{ break: }

I I process the book

2.6 STORED PROCEDURES

Stored procedure is a set of logical group of SOL statements which are

DEPT. OF ISE, AJIET, MANGALORE 58


Database Management System [18CS53]

grouped to perform a specific task.

Benefits:

• reduces the amount of information transfer between client and database server

• Compilation step is required only once when the stored procedure is created.
Then after it does not require recompilation before executing unless it is
modified and reutilizes the same execution plan whereas the SOL statements
need to be compiled every time whenever it is sent for execution even if we
send the same SOL statement every time

• It helps in re usability of the SOL code because it can be used by multiple


users and by multiple clients since we need to just call the stored procedure
instead of writing the same SOL statement every time. It helps in reducing
the development time

Syntax:

Create or replace procedure <procedure Name> [(arg1 datatype, arg2


datatype)]

ls/As

<declaration>

Begin

<SQL Statement>

Exception

End procedurename;

2.6.1 Creating a Simple Stored Procedure

Consider the following schema:

Student(usn:string,sname:string)

DEPT. OF ISE, AJIET, MANGALORE 59


Database Management System [18CS53]

Let us now write a stored procedure to retrieve the count of students with

sname 'Akshay' create or replace procedure ss

is stu_cnt int;

begin

select count(*) into stu_cnt from students where sname='AKSHA Y';

dbms_output.put_line('the count of student is :' II stu_cnt);


end ss;

Stored procedures can also have parameters. These parameters have to be valid
SQL types, and have one of three different modes: IN, OUT, or INOUT.

■ IN parameters are arguments to the stored procedure

■ OUT parameters are returned from the stored procedure; it assigns


values to all OUT parameters that the user can process

■ INOUT parameters combine the properties of IN and OUT parameters: They


contain values to be passed to the stored procedures, and the stored
procedure can set their values as return values

Example:

CREATE PROCEDURE Addlnventory

( IN book_isbn CHAR(IO),

IN addedOty INTEGER)

UPDATE Books SET qty_in_stock = qtyjn_stock + addedOty

WHERE bookjsbn = isbn

In Embedded SOL, the arguments to a stored procedure are usually variables in


the host language. For example, the stored procedure AddInventory would be
called as follows:
EXEC SOL BEGIN DECLARE SECTION
char isbn[/O];
long qty;
EXEC SOL END DECLARE SECTION

DEPT. OF ISE, AJIET, MANGALORE 60


Database Management System [18CS53]

I I set isbn and qty to some values


EXEC SOL CALL Addlnventory( :isbn,:qty);

Stored procedures enforce strict type conformance: If a parameter is of type


INTEGER, it cannot be called with an argument of type VARCHAR.

Procedures without parameters are called static procedures and with parameters are
called
dynamic procedures.

Example: stored procedure with parameter

create or replace procedure

emp(Essn int) as

eName varchar(20);

begin

select fname into eName from employee where ssn=Essn and dno=5;

dbms_output.put_line(' the employee name is :'IIEssn lleName);

end emp;

2.6.2Calling Stored Procedures

Stored procedures can be called in interactive SOL with the CALL

statement:

CALL storedProcedureName(arg, larg2, .. ,argN);

Calling Stored Procedures from JDBC


We can call stored procedures from JDBC using the CallableStatment class.A
stored procedure could contain multiple SOL statements or a series of SOL
statements-thus, the result could be many different ResultSet objects .We illustrate the
case when the stored procedure result is a single ResultSet.
CallableStatement cstmt= con. prepareCall(" {call
ShowNumberOfOrders}"); ResultSet rs = cstmt.executeQuery();
while (rs.next())

DEPT. OF ISE, AJIET, MANGALORE 61


Database Management System [18CS53]

Calling Stored Procedures from SQLJ


The stored procedure 'ShowNumberOfOrders' is called as follows using SQLJ:

I I create the cursor class

#sql Iterator Customerlnfo(int cid, String cname, int count);

I I create the cursor

Customerlnfo customerinfo;

I I call the stored procedure

#sql customerinfo = {CALL

ShowNumberOfOrders};

while (customerinfo.next()

System.out.println(customerinf.ocid() + "," +

customerinfo.count()) ;

2.6.3 SQUPSM

SOL/Persistent Stored Modules is an ISO standard mainly defining an extension


of SOL with procedural language for use in stored procedures.

In SQL/PSM, we declare a stored procedure as follows:

CREATE PROCEDURE name (parameter1,... , parameterN)

local variable declarations

procedure code;

• Each parameter is a triple consisting of the mode (IN, OUT, or INOUT ), the parameter name,
and the SQL datatype of the parameter.
• The following SQL/PSM function returns the rating of the customer, which is defined as follows:
Customers who have bought more than ten books during the year are rated 'two'; customer who
have purchased between 5 and 10 books are rated 'one', otherwise the customer is rated 'zero'.
The following SQL/PSM code computes the rating for a given customer and year.
CREATE PROCEDURE RateCustomer (IN custId INTEGER, IN year INTEGER)

DEPT. OF ISE, AJIET, MANGALORE 62


Database Management System [18CS53]

RETURNS INTEGER
DECLARE rating INTEGER;
DECLARE numOrders INTEGER;
SET numOrders = (SELECT COUNT(*) FROM Orders 0 WHERE O.tid = custId);
IF (numOrders> 10) THEN rating=2;
ELSEIF (numOrders>5) THEN rating=1;
ELSE rating=O;
END IF;
RETURN rating;

Short overview of some SQL/PSM constructs:


1. Declare local variables using the DECLARE statement.
DECLARE rating INTEGER;
DECLARE numOrders INTEGER;

2. PSM/SQL functions return values via the RETURN statement.


RETURN rating;

3. Assign values to variables with the SET statement


In our example, we assigned the return value of a query to the variable 'numOrders'.
4. SQL/PSM has branches and loops. Branches have the following form:
IF (condition) THEN statements;
ELSEIF statements;

ELSEIF statements;
ELSE statements; END IF
Loops are of the form
LOOP
statements:
END LOOP
5. Queries can be used as part of expressions in branches, queries that return a single value can
be assigned to variables.
6. We can use the same cursor statements as in Embedded SQL (OPEN, FETCH, CLOSE), but we
do not need the EXEC SQL constructs, and variables do not have to be prefixed by a colon ':'

DEPT. OF ISE, AJIET, MANGALORE 63


Database Management System [18CS53]

We can declare a function similarly as follows:

CREATE FUNCTION name (parameter,! ... , parameterN)

RETURNS

sqlDataType local

variable declarations

function code;

Example:

CREATE FUNCTION RateCustomer (IN custld INTEGER, IN year

INTEGER) RETURNS INTEGER

DECLARE rating INTEGER;

DECLARE numOrders INTEGER;

SET numOrders = (SELECT COUNT(*) FROM Orders O WHERE

O.tid = custld); IF (numOrders> 10) THEN rating=2;

ELSEIF (numOrders>5) THEN

rating=1; ELSE rating=O;

END IF;

RETURN rating;

• We can declare local variables using the DECLARE statement. In our example,
we declare two local variables: 'rating', and 'numOrders'.

• PSM/SQL functions return values via the RETURN statement. In our example,
we return the value of the local variable 'rating'.

• We can assign values to variables with the SET statement. In our example,
we assigned the return value of a query to the variable 'numOrders'.

• SQL/PSM has branches and loops. Branches have the

following form: IF (condition) THEN statements;

Dept. of ISE, AJIET Page 64


Database Management System [18CS53]

ELSEIF
statements;

ELSEIF
statements;

ELSE
statements;

END IF

• Loops are of the form

LOOP

statements:

END LOOP

Queries can be used as part of expressions in branches; queries that return a single
value can be assigned to variables.We can use the same cursor statements as in
Embedded SQL (OPEN, FETCH, CLOSE), but we do not need the EXEC SQL
constructs, and variables do not have to be prefixed by a colon ':'.

Dept. of ISE, AJIET Page 65


Database Management System [18CS53]

Chapter 3: Internet Applications

3.1 Introduction

Data-intensive is used to describe applications with a need to process large volumes


of data. The volume of data that is processed can be in the size of terabytes and
petabytes and this type of data is also referred as big data. Data-intensive computing
is used in many applications ranging from social networking to computational science
where a large amount of data needs to be accessed, stored, indexed and analyzed.
It is more challenging as the amount of data keeps on accumulating over time and the
rate at which the data is generating also increases

3.2 THE THREE-TIER APPLICATION ARCHITECTURE


Data-intensive Internet applications can be understood in terms of three different
functional components:
1. Data management
2. Application logic
3. Presentation
The component that handles data management usually utilizes a DBMS for data storage,
but application logic and presentation involve much more than just the DBMS itself.

3.2.1 Single-Tier

Initially, data-intensive applications were combined into a single tier, including the
DBMS, application logic, and user interface. The application typically ran on a
mainframe, and users accessed it through dumb terminals that could perform only data
input and display.

Dept. of ISE, AJIET Page 66


Database Management System [18CS53]

Figure 3.2.1 : A Single-Tier Architecture

■ Benefit
• easily maintained by a central administrator
■ Drawback:
• Users expect graphical interfaces that require much more
computational power than simple dumb terminals.
• Do not scale to thousands of users

3.2.2 Two-tier architectures


Two-tier architectures, often also referred to as client-server architectures, consist
of a client computer and a server computer, which interact through a well-defined
protocol. What part of the functionality the client implements, and what part is left to
the server, can vary.
In the traditional client server architecture, the client implements just the graphical
user interface- such clients are often called thin clients the server implements both
the business logic and the data management.
Other divisions are possible, such as more powerful clients that implement both

Dept. of ISE, AJIET Page 67


Database Management System [18CS53]

user interface and business logic, or clients that implement user interface and part
of the business logic, with the remaining part being implemented at the server level;
such clients are often called thick clients.

Figur e3.2.2(a) : A Two-Server Archit ecture : thin clientFigure3.2.2(a) : A Two-Server


Architecture : thick client
The thick-client model has several disadvantages when compared to the thin client model
1. There is no central place to update and maintain the business logic, since the
application code runs at many client sites.
2. A large amount of trust is required between the server and the clients. As an
example, the DBMS of a bank has to trust the application executing at an ATM
machine to leave the database in a consistent state.
3. Thick-client architecture does not scale with the number of clients; it typically cannot
handle more than a few hundred clients. The application logic at the client issues
SOL queries to the server and the server returns the query result to the client,
where further processing takes place. Large query results might be transferred
between client and server.

Dept. of ISE, AJIET Page 68


Database Management System [18CS53]

Single-tier architecture vis Two-tier architectures


• Compared to the single-tier architecture, two-tier architectures physically
separate the user interface from the data management layer
• To implement two tier architectures, we can no longer have dumb terminals
on the client side, we require computers that run sophisticated presentation
code and possibly, application logic

3.2.3 Three-Tier Architectures


The thin-client two-tier architecture essentially separates presentation issues from
the rest of the application. The three-tier architecture goes one step further, and
also separates application logic from data management:
• Presentation Tier
• Middle Tier
• Data Management Tier
Different technologies have been developed to enable distribution of the three tiers
of an application across multiple hardware platforms and different physical sites

Figure 3.2.3: Technologies for the Three Tiers

Dept. of ISE, AJIET Page 69


Database Management System [18CS53]

3.2.3.1 Overview of the Presentation Tier

At the presentation layer, we need to provide forms through which the user can issue
requests, and display responses that the middle tier generates. It is important that
this layer of code be easy to adapt to different display devices and formats; for
example, regular desktops versus handheld devices versus cell phones. This
adaptivity can be achieved either at the middle tier through generation of different
pages for different types of client, or directly at the client through style sheets that
specify how the data should be presented. The hypertext markup language (HTML)
is the basic data presentation language.

Technologies for the client side of the three-tier architecture

HTML Forms
HTML forms are a common way of communicating data from the client tier to
the middle tier . The general format of a form :
<FORM ACTION="page.jsp" METHOD="GET" NAME="LoginForm">

</FORM>
• ACTION: Specifies the URI of the page to which the form contents are
submitted. If the ACTION attribute is absent, then the URI of the
current page is used
• METHOD: The HTTP/1.0 method used to submit the user input from the
filled-out form to the webserver. There are two choices: GET
and POST
• NAME: This attribute gives the form a name

A single HTML document can contain more than one form. Inside an HTML form,
we can have any HTML tags except another FORM element

Passing Arguments to Server-Side Scripts

There are two different ways to submit HTML Form data to the webserver. If the
method GET is used, then the contents of the form are assembled into a query URI

Dept. of ISE, AJIET Page 70


Database Management System [18CS53]

(as discussed next) and sent to the server. If the method POST is used, then the
contents of the form are encoded as in the GET method, but the contents are sent
in a separate data block instead of appending them directly to the URI. Thus, in the
GET method the form contents are directly visible to the user as the constructed
URI, whereas in the POST method, the form contents are sent inside the HTTP
request message body and are not visible to the user.

JavaScript

JavaScript is a scripting language at the client tier with which we can add programs
to webpages that run directly at the client. JavaScript is often used for the following
types of computation at the client:
■ Browser Detection: JavaScript can be used to detect the browser type and
load a
browser-specific page.
■ Form Validation: JavaScript is used to perform simple consistency checks on form
fields
■ Browser Control: This includes opening pages in customized windows;
examples include the annoying pop-up advertisements that you see at many
websites, which are programmed using JavaScript.
JavaScript is usually embedded into an HTML document with a special tag, the SCRIPT tag

<SCRIPT LANGUAGE=" JavaScript" SRC="validateForm.js"> </SCRIPT>

The SCRIPT tag has the attribute LANGUAGE, which indicates the language in
which the script is written. For JavaScript, we set the language attribute to
JavaScript. Another attribute of the SCRIPT tag is the SRC attribute, which specifies
an external file with JavaScript code that is automatically embedded into the HTML
document. Usually JavaScript source code files use a '.js' extension.

Style Sheets

A style sheet is a method to adapt the same document contents to different


presentation formats. A style sheet contains instructions that tell a web how to
translate the data of a document into a presentation that is suitable for the client's
display. The use of style sheets has many advantages:

Dept. of ISE, AJIET Page 71


Database Management System [18CS53]

• we can reuse the same document many times and display it differently
depending on the context

• we can tailor the display to the reader's preference such as font size, color
style, and even level of detail.

• we can deal with different output formats, such as different output devices
(laptops versus cell phones), different display sizes (letter versus legal
paper), and different display media (paper versus digital display)

• we can standardize the display format within a corporation and thus apply
style sheet conventions to documents at any time.

• changes and improvements to these display conventions can be managed at


a central place.

There are two style sheet


languages:

■ XSL
■ css

Cascading Style Sheets (CSS)

• CSS was created for HTML with the goal of separating the display
characteristics of different formatting tags from the tags themselves
• CSS defines how to display HTML elements.
• Styles are normally stored in style sheets, which are files that contain style
definitions.
• Many different HTML documents, such as all documents in a website, can
refer to the same CSS.
• Thus, we can change the format of a website by changing a single file.
• Each line in a CSS sheet consists of three parts; a selector, a property, and a
value.They are syntactically arranged in the following way:
selector {property: value}
• The selector is the element or tag whose format we are defining.
• The property indicates the tag's attribute whose value we want to set in the style
sheet
• Example: BODY {BACKGROUND-COLOR:
yellow} P {MARGIN-LEFT: 50px;
COLOR: red}

Dept. of ISE, AJIET Page 72


Database Management System [18CS53]

XSL

• XSL is a language for expressing style sheets


• An XSL style sheet is, like CSS,a file that describes how to display an XML
document of a given type.
• XSL contains the XSL Transformation language, or XSLT, a language that
allows us to transform the input XML document into a XML document with
another structure
• For example, with XSLT we can change the order of elements that we are
displaying (e.g.; by sorting them), process elements more than once,
suppress elements in one place and present them in another, and add
generated text to the presentation

3.2.3.2 Overview of the Middle Tier

The middle layer runs code that implements the business logic of the application.
The middle tier code is responsible for supporting all the different roles involved in
the application. For example, in an Internet shopping site implementation, we would
like
• customers to be able to browse the catalog and make purchases
• administrators to be able to inspect current inventory, and
• data analysts to ask summary queries about purchase histories
• Each of these roles can require support for several complex actions
The first generation of middle-tier applications was stand-alone programs written in
a general- purpose programming language such as C, C++, and Perl.
Programmers quickly realized that interaction with a stand-alone application was
quite costly. The overheads include starting the application every time it is invoked
and switching processes between the webserver and the application. Therefore,
such interactions do not scale to large numbers of concurrent users. Most of today's
large-scale websites use an application server to run application code at the middle tier
. Application server provides the run-time for several technologies that can be used
to program middle-tier application components.

CGI: The Common Gateway Interface

Dept. of ISE, AJIET Page 73


Database Management System [18CS53]

The Common Gateway Interface connects HTML forms with application programs.
■ It is a protocol that defines how arguments from forms are passed to programs
at the server side
■ CGI is the part of the Web server that can communicate with other programs
running on the server
■ With CGI, the Web server can call up a program, while passing user-specific
data to the program (such as what host the user is connecting from, or input
the user has supplied using HTML form syntax)
■ The program then processes that data and the server passes the program's
response back to the Web browser.

Figure: Simple diagram of CGI

<HTML><HEAD><TITLE>The Database Bookstore</TITLE></HEAD>


<BODY>
<FORM ACTION="find_books.cgi II
METHOD=POST> Type an author name:
<INPUT TYPE="text II
NAME=lauthorName" SIZE=30
MAXLENGTH=50>
<INPUT TYPE="submitil value="Send it">
<INPUT TYPE=lreset" VALUE="Clearform II>
</FORM>
</BODY></HTML>

Program fragment: A Sample 'web Page Where Form Input Is Sent to a CGI
Script

Dept. of ISE, AJIET Page 74


Database Management System [18CS53]

Application Servers
Application logic can be enforced through server-side programs that are invoked
using the CGI protocol. However, since each page request results in the creation of
a new process, this solution does not scale well to a large number of simultaneous
requests. An application server maintains a pool of threads or processes and uses
these to execute requests. Thus, it avoids the startup cost of creating a new process
for each request. They facilitate concurrent access to several heterogeneous data
sources (e.g., by providing JDBC drivers), and provide session management
services.

Fig: Process Structure with CGI Scripts

Fig: Process Structure in the Application Server Architecture

Servlets
Java servlets are pieces of Java code that run on the middle tier, in either webservers
Dept. of ISE, AJIET Page 75
Database Management System [18CS53]

or application servers. Servlets can build webpages, access databases, and


maintain state.Servlets usually handle requests from HTML forms and maintain
state between the client and the server.
Servlets are compiled Java classes executed and maintained by a servlet container.
The servlet container manages the lifespan of individual servlets by creating and
destroying them. Although servlets can respond to any type of request, they are
commonly used to extend the applications hosted by webservers.

JavaServer Pages
Java Server Pages (JSP) is a server-side programming technology that enables the
creation of dynamic, platform-independent method for building Web-based
applications. JSP have access to the entire family of Java APls, including the JDBC
API to access enterprise databases
JavaServer pages (.JSPs) interchange the roles of output aml application logic.
JavaServer pages are written in HTML with servlet-like code embedded in special
HT1VIL tags. Thus, in comparison to servlets, JavaServer pages are better suited to
quickly building interfaces that have some logic inside, whereas servlets are better
suited for complex application logic.

Maintaining State
There is a need to maintain a user's state across different pages. As an example,
consider a user who wants to make a purchase at the Barnes and Nobble website.
The user must first add items into her shopping basket, which persists while she
navigates through the site Thus, we use the notion of state mainly to remember
information as the user navigates through the site.
The HTTP protocol is stateless. We call an interaction with a webserver stateless if
no information is retained from one request to the next request. We call an interaction
with a webserver stateful, or we say that state is maintained, if some memory is
stored between requests to the server, and different actions are taken depending
on the contents stored.

Since we cannot maintain state in the HTTP protocol, where should we maintain
state? There are basically two choices:
Dept. of ISE, AJIET Page 76
Database Management System [18CS53]

■ We can maintain state in the middle tier, by storing information in the local main
memory of
the application logic, or even in a database system
■ Alternatively , we can maintain state on the client side by storing data in the form of a
cookie.

Maintaining State at the Middle Tier


At the middle tier, we have several choices as to where we maintain state.

■ First, we could store the state at the bottom tier, in the database server. The state
survives crashes of the system, but a database access is required to query or
update the state, a potential performance bottleneck
■ An alternative is to store state in main memory at the middle tier. The drawbacks
are that this information is volatile and that it might take up a lot of main memory
■ We can also store state in local files at the middle tier, as a compromise between
the first two approaches.

Maintaining State at the Presentation Tier: Cookies


A cookie is a collection of (name, value)pairs that can be manipulated at the
presentation and middle tiers. Cookies are easy to use in Java servlets and Java server
Pages. They survive several client sessions because they persist in the browser cache
even after the browser is closed. One disadvantage of cookies is that they are often
perceived as as being invasive, and many users disable cookies in their Web browser;
browsers allow users to prevent cookies from being saved on their machines. Another
disadvantage is that the data in a cookie is currently limited to 4KB, but for most
applications this is not a bad limit.

Advantages of the Three-Tier Architecture


The three-tier architecture has the following advantages:
• Heterogeneous Systems: Applications can utilize the strengths of different
platforms and different software components at the different tiers. It is easy to modify
or replace the code at any tier without affecting the other tiers.
• Thin Clients: Clients only need enough computation power for the presentation
layer. Typically, clients are Web browsers.
• Integrated Data Access: In many applications, the data must be accessed from
several sources. This can be handled transparently at the middle tier, where we
Dept. of ISE, AJIET Page 77
Database Management System [18CS53]

can centrally manage connections to all database systems involved.


• Scalability to Many Clients: Each client is lightweight and all access to the
system is through the middle tier. The middle tier can share database connections
across clients, and if the middle tier becomes the bottle-neck, we can deploy
several servers executing the middle tier code; clients can connect to anyone of
these servers, if the logic is designed appropriately.

• Software Development Benefits: By dividing the application cleanly into parts


that address presentation, data access, and business logic, we gain many
advantages. The business logic is centralized, and is therefore easy to maintain,
debug, and change. Interaction between tiers occurs through well-defined,
standardized APls. Therefore, each application tier can be built out of reusable
components that can be individually developed, debugged, and tested.

Dept. of ISE, AJIET Page 78


Database Management System [18CS53]

Question Bank

1. Discuss how NULLs are treated in comparison operators in SOL. How are
NULLs treated when aggregate functions are applied in an SOL query? How
are NULLs treated if they exist in grouping attributes?
2. Describe the six clauses in the syntax of an SOL retrieval query. Show what
type of constructs can be specified in each of the six clauses . Which of the six
clauses are required and which are optional?
3. Describe conceptually how an SOL retrieval query will be executed by specifying
the conceptual order of executing each of the six clauses.
4. Explain how the GROUP BY clause works. What is the difference between the
WHERE and HAVING clause?
5. Explain insert, delete and update statements in SOL and give example for each.
6. Write a note on:
i) Views in SOL
ii) Aggregate functions in SOL
7. Explain DROP command with an example.
8. How is view created and dropped? What problems are associated with updating views?
9. How are triggers and assertions defined in SOL? Explain.
10. Consider the following schema for a COMPANY database:
EMPLOYEE (Fname, Lname, Ssn, Address, Super-ssn,
Salary, Ono) DEPARTMENT (Dname, Dnumber, Mgr-ssn,
Mgr-start-date)
DEPT-LOCATIONS (Dnumber,
Dlocation) PROJECT (Pname,
Pnumber, Placation, Dnum) WORKS-ON
(Ess!!, Pno, Hours)
DEPENDENT (Essn, Dependent-name, Sex, Bdate,
Relationship) write the SOL query for the following:
i) List the names of managers who have at least one dependent.
ii) Retrieve the list of employees and the projects they are working on, ordered by
department and, within each department, ordered alphabetically by last name,

Dept. of ISE, AJIET Page 79


Database Management System [18CS53]

first name.
iii) For each project, retrieve the project number, the project name, and
the number of employees who work on that project.
iv) For each project on which more than two employees work, retrieve
the project number, the project name, and the number of employees
who work on the project.
v) For each project, retrieve the project number, the project name, and
the number of employees from department 4 who work on the
project.

Dept. of ISE, AJIET Page 80


Database Management System [18CS53]

11. Consider the following tables:


Works(Pname,Cname,Salar
y) Lives(Pname,Street,City)
Located-in(Cname ,City)
Manager(Pname,mgrname)
write the SOL query for the following:
i) Find the names of all persons who live in the city 'Mumbai' ;
ii) Retrieve the names of all person of 'Infosys' ehose salary is between
Rs.30,000 and Rs.50,000.
iii) Find the names of all persons who live and work in the same city.
iv) List the names of the people who work for 'Wipro' along with the cities they live in.
v) Find the average salary of all 'lnfosyians'.
12. Consider the following
schema
Sailors(sid,sname,rating,
age)
Boats(bid,bname,color)
Reserves(sid ,bid,day)
write the SOL query for the following:
i) Retrieve the sailors name who have reserved red and green
boats. ii)Retrieve the sailors names with age over 20 years and
reserved black boat.
iii) Retrieve the number of boats which are not reserved.
iv) Retrieve the sailors names who have reserved green boat on Monday.
v) Retrieve the sailors names who is oldest sailor with rating 10.
13. Consider the following schema and write the
SOL queries: STUDENT-
ID,SNAME,MAJOR,GPA)
FACULTY(FACULTY_ID,FNAME,DEPT,DESIGNATION,SALARY)
COURSE(COURSE_ID,CNAME,FACULTY_ID)
ENROLL(COURSE_ID,STUDENT_ID.GRADE)
i) Retrieve the student name who is studying under faculties of "Mechanical dept".
Dept. of ISE, AJIET Page 81
Database Management System [18CS53]

ii) Retrieve the student name who have enrolled under any of the courses in
which 'kumar' has enrolled.
iii) Retrieve the faculty name who earn salary which is greater than the
average salary of all the faculties.
iv) Retrieve the sname who are not bee taught by faculty 'kumar'.
v) Retrieve the faculty names who are assistant professors of CSE dept.
14. How do we use SOL statements within a host langl.lage? How do we check for
errors in statement execution?

Dept. of ISE, AJIET Page 82


Database Management System [18CS53]

15. Define cursor. what properties can cursors have?


16. What is Dynamic SOL and how is it different from Embedded SOL?
17. What is JDBC and what are its advantages?
18. What are the components of the JDBC architecture? Describe four
different architectural alternatives for JDBC drivers.
19. With an example, explain SOLJ?
20. Illustrate with an example stored procedure. Mention its benefits.
21. What is a three-tier architecture? 'What advantages does it offer over single
tier and two-tier architectures? Give a short overview of the functionality at
each of the three tiers.

Dept. of ISE, AJIET Page 83

You might also like