SQLIII
SQLIII
R & G - Chapter 5
Buffer Management
Aggregate operations:
returns SUM
SELECT SUM(assets) --------
FROM branch2 11.1M
NULL is ignored
Same for AVG, MIN, MAX
• INNER is default
• Returns all sailors & bid for boat in any of their reservations
– Note: no match for s.sid? r.sid IS NULL!
SELECT s.sid, s.sname, r.bid
FROM Sailors s LEFT OUTER JOIN Reserves r
ON s.sid = r.sid;
NULL
Right Outer Join
• Returns all matched rows, plus all unmatched rows from
the table on the right of the join clause
– (use nulls in fields of non-matching tuples)
Redcount
Therefore: changes in branch are seen in the view version of branch2 (2)
but not for the (1) case.
Subqueries in FROM
• Like a “view create on the fly”
Example:
Then, the system will insert a new tuple ( “Perry”, L-308, NULL) into loan
SQL: Modification Commands
What about...
• Options:
– Make the query language “Turing complete”
• Avoids the “impedance mismatch”
• makes “simple” relational language complex
– Allow SQL to be embedded in regular programming
languages.
Cursors
• Can declare a cursor on a relation or query
• Can open a cursor
• Can repeatedly fetch a tuple (moving the cursor)
• Special return value when all tuples have been retrieved.
• ORDER BY allows control over the order tuples are
returned.
– Fields in ORDER BY clause must also appear in SELECT
clause.
• LIMIT controls the number of rows returned (good fit
w/ORDER BY)
• Can also modify/delete tuple pointed to by a cursor
– A “non-relational” way to get a handle to a particular tuple
Database APIs
• A library with database calls (API)
– special objects/methods
– passes SQL strings from language, presents result sets in a
language-friendly way
– ODBC a C/C++ standard started on Windows
– JDBC a Java equivalent
– Most scripting languages have similar things
– E.g. in Python there’s the “psycopg2” driver
• ODBC/JDBC try to be DBMS-neutral
– at least try to hide distinctions across different DBMSs
Summary
• Relational model has well-defined query semantics
• Three parts:
– Event (activates the trigger)
– Condition (tests whether the triggers should run) [Optional]
– Action (what happens if the trigger runs)
• Semantics:
– When event occurs, and condition is satisfied, the action is
performed.
Triggers – Event,Condition,Action
• Events could be :
BEGIN
END;
Example Trigger
CREATE TRIGGER minSalary BEFORE INSERT ON Professor
BEGIN
END;
Example Trigger
CREATE TRIGGER minSalary BEFORE INSERT ON Professor
BEGIN
END;
Example trigger
CREATE TRIGGER minSalary BEFORE INSERT ON Professor
FOR EACH ROW
BEGIN
IF (:new.salary < 60000)
THEN RAISE_APPLICATION_ERROR (-20004,
‘Violation of Minimum Professor Salary’);
END IF;
END;
.
run;
Details of Trigger Example
• BEFORE INSERT ON Professor
– This trigger is checked before the tuple is inserted
• FOR EACH ROW
– specifies that trigger is performed for each row inserted
• :new
– refers to the new tuple inserted
• If (:new.salary < 60000)
– then an application error is raised and hence the row is
not inserted; otherwise the row is inserted.
• Use error code: -20004;
– this is in the valid range
Example Trigger Using Condition
CREATE TRIGGER minSalary BEFORE INSERT ON Professor
FOR EACH ROW
WHEN (new.salary < 60000)
BEGIN
RAISE_APPLICATION_ERROR (-20004, ‘Violation of
Minimum Professor Salary’);
END;
.
run;
BEGIN
RAISE_APPLICATION_ERROR (-20004,
‘Violation of Minimum Professor Salary’);
END;
.
run;
Example Trigger
CREATE TRIGGER minSalary
BEFORE UPDATE ON Professor
REFERENCING OLD AS oldTuple NEW as newTuple
FOR EACH ROW
WHEN (newTuple.salary < oldTuple.salary)
BEGIN
RAISE_APPLICATION_ERROR (-20004, ‘Salary
Decreasing !!’);
END;
.
run;
• Three parts:
– Event (activates the trigger)
– Condition (tests whether the triggers should run) [Optional]
– Action (what happens if the trigger runs)
• Semantics:
– When event occurs, and condition is satisfied, the action is
performed.
Another Trigger Example (SQL:99)
CREATE TRIGGER youngSailorUpdate
AFTER INSERT ON SAILORS
REFERENCING NEW TABLE AS NewSailors
FOR EACH STATEMENT
INSERT
INTO YoungSailors(sid, name, age, rating)
SELECT sid, name, age, rating
FROM NewSailors N
WHERE N.age <= 18
Row vs Statement Level Trigger
• Row level: activated once per modified tuple
• Statement level: activate once per SQL statement