general constraints CREATE ASSERTION name CHECK condition must always be true

advertisement
Why?
reduce redundancy
avoid update/delete/insert anomalies
A relation R is in BCNF whenever X −> A holds in R, and A is
not in X, then X is a candidate key for R
Lab 3: Boyce−Codd?
most relations in 3NF are also in BCNF
the candidate keys in R are composite keys
When can it happen that a 3NF is not in BCNF?
there is more than one candidate key in R
the keys are not disjoint (some attributes are common)
example: OH (Source: Wikipedia)
Glimpses from the past
primary key
key
unique
referential
Constraints this far
references
foreign key constructs
directly after attribute
attribute−based check
in schema when create table
after attributes
tuple−based check
in schema when create table
Remember to name your constraints!
more about tuple−based contraints
general constraints
CREATE ASSERTION name CHECK condition
different time when they are invoked
triggers
assertions
takes care of updates that concern several tables
different from tuple−based
assertions
Lab 4: Today’s lecture
must always be true
pl/sql (next)
note: cannot refer to components in a tuple without first
introducing the tuples with a SELECT
What is it?
example: slide 12
procedural language
extension to SQL
cf. ordinary programming language
checks and assertions offer only one option: abort
no impedance mismatch
greater flexibility: the user can decide what action to take
queries to db
different data types
insert
delete
associated w/certain events
Why?
Why?
data manipulation power of SQL
flow−of−control to process oracle data
update
schema−level triggers
once for each modified tuple
trap runtime errors
when to consider the trigger; insert, delete or update
after
logical unit
event
pack sql statements together
easier to manage
can be nested
procedures
instead of
when to execute the action
sql action
The block
trigger = event + condition + action
declare basic units
condition
functions
variables and constants
(also: anonymous blocks)
optional
pl/sql block or stored procedure
sql statements separated by semicolon
1 block = 1 problem/subproblem
triggers
action
examples: slides 4 and 5
CHAR
examples: slides 14 and 15
VARCHAR
before triggering event
after
define procedures and functions
only performed when the event occurs
once for all the tuples that are changed in one operation
before
declare constants and variables
data processing power of procedural languages
immediately (default)
instead of
wait until transaction has finished
more later ...
NUMBER
When is the condition evaluated?
TDDB48: PL/SQL
fixed
floating
BINARY_INTEGER
deferred
BOOLEAN
detached
data types and variables
immediately
dates
deferred
When is the action executed?
detached
true integer
no corresponding in db
TRUE, FALSE or NULL
DATE and date conversions
see lab compendium
data type according to db
constraint checking
notification
maintenance of derived attributes
gives same data type as in db
emp_no := 999
Main uses
assignment
SELECT attribute INTO variable FROM ... WHERE ...
note: must return ONE tuple and appropriate number of
variables
duplicated tables
how data is stored and indexed on disk
how primary memory is used in a db
attribute%TYPE
PL/SQL
indexing
INSERT INTO student(nr, name, e−mail)
More in lecture 6!
sql statements that are treated as one atomic unit
DML
cf. stored procedure!
DDL statement COMMIT
user disconnects
commit
transaction
DDL statement ROLLBACK
unhandled exception
WHERE nr IN (SELECT student FROM course);
DELETE FROM student
it begins w/first sql statement issued
and ends with ...
next lectures
ELSE type=adult;
rollback
commit and rollback
abnormal termination
make permanent the changes made by the transaction
undo the changes made by the transaction
undo up to previous commit or savepoint
intermediate marker
FOR i IN 1..500 LOOP ... END LOOP
conditions and iterators
iterators
commit
WHILE condition ... END LOOP
LOOP ... EXIT WHEN condition
GOTO
rollback
examples: slides 6 and 7
savepoint
to name a work area to access stored info
example: slide 17
used by pl/sql to execute sql and store processing info
More in lectures 7 and 8!
cursor
work area
implicit: declared by pl/sql for all sql data
explicit
for queries that return more than one row
when you want to process the row individually
examples: supplemental slides
sql standard: PSM
persistent stored module
programs stored and executed by dbms on the server
stored persistently, just as p. data
if db program needed by several apps
stored procedures and functions
Why?
reduced duplication
improved sw modularity
can reduce db transfer and comm cost
allows more complex types of derived data in views
can enhance modelling power by views
be used to check complex constraints beyond triggers and
assertions
procedure example: slide 8
function example: slide 9
exception no data found
how to debug
dbms_output.put_line
sho err
examples: slides 10−11
Download