Академический Документы
Профессиональный Документы
Культура Документы
Outline:
• Insert, delete, and Update statements in SQL
• Specifying constraints as Assertion and Trigger
• Views(virtual table ) in SQL
• Additional features of SQL
• Database programming issues and techniques
• Embedded SQL
• Dynamic SQL
• Database stored procedures
The Insert Command
Method 1
Slide 7- 5
Comparisons Involving NULL
and Three-Valued Logic (cont’d.)
Slide 7- 6
Comparisons Involving NULL
and Three-Valued Logic (cont’d.)
• SQL allows queries that check whether an
attribute value is NULL
– IS or IS NOT NULL
Slide 7- 7
Use of WITH
• The WITH clause allows a user to define a
table that will only be used in a particular
query (not available in all SQL
implementations)
• Used for convenience to create a temporary
“View” and use that immediately in a query
• Allows a more straightforward way of looking
a step-by-step query
Slide 7- 12
Example of WITH
• See an alternate approach to doing Q28:
• Q28’: WITH BIGDEPTS (Dno) AS
( SELECT Dno
FROM EMPLOYEE
GROUP BY Dno
HAVING COUNT (*) > 5)
SELECT Dno, COUNT (*)
FROM EMPLOYEE
WHERE Salary>40000 AND Dno IN BIGDEPTS
GROUP BY Dno;
Slide 7- 13
Use of CASE
• SQL also has a CASE construct
• Used when a value can be different based on
certain conditions.
• Can be used in any part of an SQL query
where a value is expected
• Applicable when querying, inserting or
updating tuples
Slide 7- 14
EXAMPLE of use of CASE
• The following example shows that employees are
receiving different raises in different departments
(A variation of the update U6)
Slide 7- 15
Constraints as Assertion
• General constraints: constraints that do not
fit in the basic SQL categories
Slide 9- 17
Using general Assertion
• Specify a query that violates the condition.
UPDATE customers
SET salary = salary + 500
WHERE id = 2;
WORKS_ON1
Fname Lname Pname Hours
View example
DEPT_INFO
Dept_name No_Of_emp Total_sal
Query using View
Q. Retrieve first name and last name of
employee who work for productX project
WORKS_ON1
Fname Lname Pname Hours
Dropping a View
A view should be always up to date
WORKS_ON1
Slide 7- 40
Database Programming
Objective:
• To access a database from an application
program (as opposed to interactive interfaces)
• A majority of database operations are made
through application programs (increasingly
through web application)
Database Programming Approaches
• 1. Embedded SQL: database commands are embedded in a general-
purpose programming language
exec sql, end exec
• Precompiler scans the source program and identify database
statements and extracts
For e.g.
CONNECT TO Raj.gat.com AS sysdba AUTHORIZATION
raj/pwd;
Embedded SQL
• SQL is not enough! Needs to be embedded in a general
purpose language to get
– GUI
– Flow of control
– Generate SQL dynamically based on user input
• SQL commands can be called from within a host language
(e.g., C/C++, Basic, .NET or Java) program or scripting
language (e.g., PHP, Ruby).
• A query answer is a bag of records - with arbitrarily many
rows! No such data structure in most languages. Called
impedance mismatch.
• The SQL standard supports a cursor to handle this
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 or EXEC SQL BEGIN and a matching END EXEC
or EXEC SQL END (or semicolon)
int loop;
EXEC SQL BEGIN DECLARE SECTION;
varchar dname[16], fname[16], …;
char ssn[10], bdate[11], …;
int dno, dnumber;
int SQLCODE;
char SQLSTATE[6];
EXEC SQL END DECLARE SECTION;
SQL code and state
SQLCODE and SQLSTATE are communication
variables
Used by DBMS to communicate exceptions or error
conditions to the program
SQLCODE: integer variable
After each DBMS command is executed, DBMS
returns a value in SQLCODE
SQLCODE > 0; no more records are available
= 0; statement executed successfully
< 0; some error has occurred
Cont..
SQLSTATE=02000 ; no data
Program segment using embedded
SQL
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,minit,lname);
else printf(“SSN does not exist: “, ssn);
prompt(“More SSN? (1=yes, 0=no): “, loop)
END-EXEC
}
Program segment using embedded
SQL
EXEC SQL select S.sname, S.age
into :c_sname, :c_age
from Sailors S
where S.sid=:c_sid;
END EXEC
EXEC SQL select S.sname, S.age
from Sailors S
where S.rating>:c_minrating;
END EXEC
Embedded SQL in C-Retrieving multiple Tuples
example
• A cursor (iterator) is needed to process multiple
tuples.
– Points to a single tuple at a time from result of query
• FETCH commands move the cursor to the next
tuple
• OPEN CURSOR starts the sql execution
– It fetches query result and set cursor to a position before first row
in result .
– It becomes current row for cursor.
• CLOSE CURSOR indicates that the processing of
query results has been completed
General Form of a Cursor
Open sinfo;
Fetch sinfo INTO :c_name, :c_age;
CLOSE sinfo;
UPDATE Sailor S
SET S.rating=S.rating-1
WHERE CURRENT of sinfo;
Advantages:
i) If the procedure is needed by many applications, it
can be invoked by any of them (thus reduce
duplications).
ii) Execution by the server reduces communication
costs
iii) Enhance the modeling power of views
Database stored procedure
Disadvantages:
Every DBMS has its own syntax and this can make the
system less portable
stored procedure construct
A stored procedure
CREATE PROCEDURE <procedure-name >(parameters)
<local-declarations>
<procedure-body>;
A stored function
CREATE FUNCTION <fun-name>(parameters)
RETRUNS return-type
local-declarations
function-body;
Calling a procedure or function
CALL procedure-name/fun-name (arguments);
stored procedure construct
A stored procedure written in general purpose
language
EXEC getemployee;
CREATE OR REPLACE PROCEDURE
p_update_salary(p_dept_id IN number,
p_sal_incr IN number)
IS
BEGIN
update employee
set salary=salary+(salary*p_sal_incr)
where dept_id=p_dept_id;
DBMS_output.putline(‘salary updated for
p_dept_id’)
END;
EXEC p_update_salary(10,0.10);
CREATE OR REPLACE PROCEDURE p_update_salary
IS
BEGIN
update employee
Set salary=salary+(salary*0.10)
Where dept_id=10;
DBMS_output.putline(‘salary updated’)
END;
EXEC p_update_salary;
CREATE OR REPLACE PROCEDURE p_update_salary (p_dept_id IN
number, p_sal_incr IN number, p_first_name OUT varchar)
IS
BEGIN
select fname into p_first_name
from employee
where dept_id=p_dept_id;
Update employee
set salary=salary+(salary*p_sal_incr)
where dept_id=p_dept_id;
DBMS_output.putline(‘salary updated’)
END;
EXEC p_update_salary;