ENCh 09
ENCh 09
ENCh 09
Chapter Outline
9.1 General Constraints as Assertions
9.2 Views in SQL
9.3 Database Programming
9.4 Embedded SQL
9.5 Functions Calls, SQL/CLI
9.6 Stored Procedures, SQL/PSM
9.7 Summary
Elmasri/Navathe, Fundamentals of Database Systems, Fourth Edition
Copyright 2004 Ramez Elmasri and Shamkant Navathe
Chapter 9-3
Chapter Objectives
Specification of more general
constraints via assertions
SQL facilities for defining views (virtual
tables)
Various techniques for accessing and
manipulating a database via programs in
general-purpose languages (e.g., Java)
Chapter 9-4
Constraints as Assertions
General constraints: constraints that do
not fit in the basic SQL categories
(presented in chapter 8)
Mechanism: CREAT ASSERTION
components include: a constraint name,
followed by CHECK, followed by a condition
Chapter 9-5
Assertions: An Example
The salary of an employee must not be
greater than the salary of the manager of
the department that the employee works
for
CREAT ASSERTION SALARY_CONSTRAINT
CHECK (NOT EXISTS (SELECT *
FROM EMPLOYEE E, EMPLOYEE M, DEPARTMENT D
WHERE E.SALARY > M.SALARY AND
E.DNO=D.NUMBER AND D.MGRSSN=M.SSN))
Elmasri/Navathe, Fundamentals of Database Systems, Fourth Edition
Copyright 2004 Ramez Elmasri and Shamkant Navathe
Chapter 9-6
Chapter 9-7
SQL Triggers
Objective: to monitor a database and
take action when a condition occurs
Triggers are expressed in a syntax
similar to assertions and include the
following:
event (e.g., an update operation)
condition
action (to be taken when the condition is
satisfied)
Elmasri/Navathe, Fundamentals of Database Systems, Fourth Edition
Copyright 2004 Ramez Elmasri and Shamkant Navathe
Chapter 9-8
Chapter 9-9
Views in SQL
A view is a virtual table that is derived
from other tables
Allows for limited update operations
(since the table may not physically be
stored)
Allows full query operations
A convenience for expressing certain
operations
Chapter 9-10
Specification of Views
SQL command: CREATE VIEW
a table (view) name
a possible list of attribute names (for
example, when arithmetic operations are
specified or when we want the names to be
different from the attributes in the base
relations)
a query to specify the table contents
Chapter 9-11
Chapter 9-12
Chapter 9-13
Chapter 9-14
View Update
Update on a single view without
aggregate operations: update may map
to an update on the underlying base
table
Views involving joins: an update may
map to an update on the underlying base
relations
not always possible
Chapter 9-16
Un-updatable Views
Views defined using groups and
aggregate functions are not updateable
Views defined on multiple tables using
joins are generally not updateable
WITH CHECK OPTION: must be added to
the definition of a view if the view is to be
updated
to allow check for updatability and to plan
for an execution strategy
Chapter 9-17
Database Programming
Objective: to access a database from an
application program (as opposed to
interactive interfaces)
Why? An interactive interface is
convenient but not sufficient; a majority
of database operations are made thru
application programs (nowadays thru
web applications)
Chapter 9-18
Database Programming
Approaches
Embedded commands: database
commands are embedded in a generalpurpose programming language
Library of database functions: available
to the host language for database calls;
known as an API
A brand new, full-fledged language
(minimizes impedance mismatch)
Chapter 9-19
Impedance Mismatch
Incompatibilities between a host
programming language and the
database model, e.g.,
type mismatch and incompatibilities;
requires a new binding for each language
set vs. record-at-a-time processing
need special iterators to loop over query results
and manipulate individual values
Chapter 9-20
Embedded SQL
Most SQL statements can be embedded
in a general-purpose host programming
language such as COBOL, C, Java
An embedded SQL statement is
distinguished from the host language
statements by EXEC SQL and a
matching END-EXEC (or semicolon)
shared variables (used in both languages)
usually prefixed with a colon (:) in SQL
Chapter 9-22
Disconnection
DISCONNECT connection-name;
Chapter 9-24
Embedded SQL in C
Programming Examples
loop = 1;
while (loop) {
prompt (Enter SSN: , ssn);
EXEC SQL
select FNAME, LNAME, ADDRESS, SALARY
into :fname, :lname, :address, :salary
from EMPLOYEE where SSN == :ssn;
if (SQLCODE == 0) printf(fname, );
else printf(SSN does not exist: , ssn);
prompt(More SSN? (1=yes, 0=no): , loop);
END-EXEC
}
Chapter 9-25
Embedded SQL in C
Programming Examples
A cursor (iterator) is needed to process
multiple tuples
FETCH commands move the cursor to
the next tuple
CLOSE CURSOR indicates that the
processing of query results has been
completed
Chapter 9-26
Dynamic SQL
Objective: executing new (not previously
compiled) SQL statements at run-time
a program accepts SQL statements from the
keyboard at run-time
a point-and-click operation translates to certain SQL
query
Chapter 9-27
Chapter 9-28
Chapter 9-31
Chapter 9-32
Chapter 9-33
Chapter 9-34
Components of SQL/CLI
Environment record: keeps track of
database connections
Connection record: keep tracks of info
needed for a particular connection
Statement record: keeps track of info
needed for one SQL statement
Description record: keeps track of tuples
Chapter 9-37
Chapter 9-38
Chapter 9-40
A stored function
CREATE FUNCTION fun-name (params) RETRUNS return-type
local-declarations
function-body;
Chapter 9-41
Chapter 9-42
SQL/PSM: An Example
CREATE FUNCTION DEPT_SIZE (IN deptno INTEGER)
RETURNS VARCHAR[7]
DECLARE TOT_EMPS INTEGER;
SELECT COUNT (*) INTO TOT_EMPS
FROM SELECT EMPLOYEE WHERE DNO = deptno;
IF TOT_EMPS > 100 THEN RETURN HUGE
ELSEIF TOT_EMPS > 50 THEN RETURN LARGE
ELSEIF TOT_EMPS > 30 THEN RETURN MEDIUM
ELSE RETURN SMALL
ENDIF;
Chapter 9-43
Summary
Assertions provide a means to specify
additional constraints
Triggers are a special kind of
assertions; they define actions to be
taken when certain conditions occur
Views are a convenient means for
creating temporary (virtual) tables
Chapter 9-44
Summary (continued)
A database may be accessed via an
interactive database
Most often, however, data in a database is
manipulate via application programs
Several methods of database programming:
embedded SQL
dynamic SQL
stored procedure and function
Chapter 9-45