SQL - Part 2 Flashcards
What are the pros/cons of assertions, attribute/tuple checks, and triggers?
Assertions Pro: Powerful; Con: Don’t know when they need to be checked
Attribute/tuple Checks: Pro-checked at known times Con-not powerful
Triggers- Pro let use decide when to check, powerful; Con-complex!
What are the 3 criteria for triggers?
ECA:
Event-type of data base modification (insert on sells)
Condition: SQL boolean valued expression
Action- ANY SQL Statement
What are the 6 parts of trigger Syntax?
1 CREATE TRIGGER BeerTrig
2 AFTER INSERT ON SELL (event)
3 REFERENCING NEW ROW AS NewTuple
4 FOR EACH ROW
5 WHEN (NewTuple.beer NOT IN (SELECT name FROM Beers)) (condition)
6 INSERT INTO Beers(Name) VALUES NEwTuple.ber (action)
What is another option instead of CREATE TRIGGER?
REPLACE TRIGGER
What are the EVENT options?
AFTER/BEFORE/INSTEAD OF
INSERT/DELETE/UPDATE
(update may be on a particular attribute)
What are the REFERENCING options and when is it used?
REFERENCING [NEW OLD][ROW TABLE] AS
- when inserting (use new)
- when deleting (use old)
- when updating (use both)
When is FOR EACH ROW used and what is an alternative?
- indicates row-level; executed one for each modified tuple
- absence indicated statement-level; executed once for an SQL statement regardless of how many tuples are modified
What is the condition? How do I access the different tuples? When is it evaluated?
Any boolean-value condition (When tuple NOT IN (Select statement))
- access the new/old tuples using the names in REFERENCING clause
- evaluated before or after depending on event
What is the action? What if I have multiple?
SQL Statements that aren’t query; limited to modifications (insert, delete, update);
surround multiple by BEGIN..END
What to “modify” views?
Use INSTEAD OF Triggers
- turn insertion of a view into actual insertions
What are 3 differences between SQL triggers and Oracle Triggers?
- Action is a PL/SQL statement
- New/old tuples referenced automatically
- Stronger restrictions on trigger actions to avoid infinite sequence of triggers
What are 3 kind of database modification?
- Insert tuples 2. Delete tuples 3. Update value of tuple(s)
What is the insertion syntax?
INSERT INTO Likes VALUES ('Sally', 'Bud') OR (specify attributes) INSERT INTO Likes(beer, drinker) VALUES ('Bud, 'Sally');
How do you insert using a subquery?
INSERT INTO PotBddfiers
(SELECT d2. drinker ..);
What is the deletion syntax? How to delete all tupels?
DELETE FROM WHERE ;
DELETE FROM ;