1 / 20

Constraining Attribute Values

Constraining Attribute Values. Constrain invalid values NOT NULL gender CHAR(1) CHECK (gender IN (‘F’, ‘M’)) MovieName CHAR(30) CHECK (MovieName IN (SELECT MovieName FROM Movies)) The constraint is checked whenever the value of the attribute is changed or added.

hvassallo
Télécharger la présentation

Constraining Attribute Values

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Constraining Attribute Values • Constrain invalid values • NOT NULL • gender CHAR(1) CHECK (gender IN (‘F’, ‘M’)) • MovieName CHAR(30) CHECK (MovieName IN (SELECT MovieName FROM Movies)) • The constraint is checked whenever the value of the attribute is changed or added. • Isn’t this the same as REFERENCE?

  2. Constraining Values with User Defined ‘Types’ • Can define new domains to use as the attribute type... CREATE DOMAIN GenderDomain CHAR(1) CHECK (VALUE IN (‘F’, ‘M’)); • Then update our attribute definition... gender GenderDomain • Note use of VALUE to refer to the attribute value. • Deferred constraints: deal with circular referencial integrity constraints.

  3. More Complex Constraints... • …Among several attributes in one table • Specify at the end of CREATE TABLE CHECK (gender = ‘F’ OR name NOT LIKE ‘Ms.%’) • Checked whenever a tuple of the relation is added or updated. • Note, changes in other places in the database may cause the constraint to be violated. If this is important, use assertions.

  4. Declaring Assertions • CREATE ASSERTION <name> CHECK (<condition>) CREATE ASSERTION RichPres CHECK (NOT EXISTS (SELECT * FROM Studio, MovieExec WHERE presC# = cert# AND netWorth < 10000000)) Checked whenever any change occurs to the database.

  5. Another Example CREATE ASSERTION SumLength CHECK (10000 < ALL (SELECT SUM(length) FROM Movie GROUP BY studioName)). Can we also write this as a tuple constraint?

  6. Different Constraint Types Type Where Declared When activated Guaranteed to hold? Attribute with attribute on insertion not if CHECK or update subquery Tuple relation schema insertion or not if CHECK update to subquery relation Assertion database schema on change to Yes any relation mentioned

  7. Triggers • Enable the database programmer to specify: • when to check a constraint, • what exactly to do. • A trigger has 3 parts: • An event (e.g., update to an attribute) • A condition (e.g., a query to check) • An action (deletion, update, insertion) • When the event happens, the system will check the constraint, and • if satisfied, will perform the action. • NOTE: triggers may cause cascading effects. • Database vendors did not wait for standards with triggers!

  8. Elements of Triggers (in SQL3) • Timing of action execution: before, after or instead of triggering • event • The action can refer to both the old and new state of the database. • Update events may specify a particular column or set of columns. • A condition is specified with a WHEN clause. • The action can be performed either for • once for every tuple, or • once for all the tuples that are changed by the database operation.

  9. Example: Row Level Trigger CREATE TRIGGER NoLowerPrices AFTER UPDATE OF price ON Product REFERENCING OLD AS OldTuple NEW AS NewTuple WHEN (OldTuple.price > NewTuple.price) UPDATE Product SET price = OldTuple.price WHERE name = NewTuple.name FOR EACH ROW

  10. Statement Level Trigger CREATE TRIGGER average-price-preserve INSTEAD OF UPDATE OF price ON Product REFERENCING OLD_TABLE AS OldStuff NEW_TABLE AS NewStuff WHEN (1000 < (SELECT AVG (price) FROM ((Product EXCEPT OldStuff) UNION NewStuff)) DELETE FROM Product WHERE (name, price, company) IN OldStuff; INSERT INTO Product (SELECT * FROM NewStuff)

  11. Bad Things Can Happen CREATE TRIGGER Bad-trigger AFTER UPDATE OF price IN Product REFERENCING OLD AS OldTuple NEW AS NewTuple WHEN (NewTuple.price > 50) UPDATE Product SET price = NewTuple.price * 2 WHERE name = NewTuple.name FOR EACH ROW

  12. Embedded SQL • Direct SQL is rarely used: usually, SQL is embedded in some application code. • We need some method to reference SQL statements. • But: there is an impedance mismatch problem. • So: we use cursors. • Many things can be explained with the impedance mismatch.

  13. Programs with SQL Host language + Embedded SQL Preprocessor Preprocessor Host Language + function calls Host language compiler Host language compiler Host language program

  14. The Impedance Mismatch Problem • The host language manipulates variables, values, pointers • SQL manipulates relations. • There is no construct in the host language for • manipulating relations. • Why not use only one language? • Forgetting SQL: “we can quickly dispense with this idea” • [Ullman & Widom, pg. 363]. • SQL cannot do everything that the host language can do.

  15. Interface: SQL / Host Language Values get passed through shared variables. Colons precede shared variables when they occur within the SQL statements. EXEC SQL: precedes every SQL statement in the host language. The variable SQLSTATE provides error messages and status reports (e.g., 00000 says that the operation completed with no problem). EXEC SQLBEGIN DECLARE SECTION; char productName[30]; EXEC SQLEND DECLARE SECTION;

  16. Using Shared Variables Void simpleInsert() { EXEC SQL BEGIN DECLARE SECTION; char productName[20], company[30]; char SQLSTATE[6]; EXEC SQL END DECLARE SECTION; /* get values for productName and company somehow */ EXEC SQL INSERT INTO Product(name, company) VALUES (:productName, :company); }

  17. Single-Row Select Statements Void getPrice() { EXEC SQL BEGIN DECLARE SECTION; char productName[20], company[30]; integer price; char SQLSTATE[6]; EXEC SQL END DECLARE SECTION; /* read value of product name */ EXEC SQL SELECT price INTO :price FROM Product WHERE Product.name = :productName; /* print out value of price */ }

  18. Cursors EXEC SQL DECLARE cursorName CURSOR FOR SELECT …. FROM …. WHERE …. ; EXEC SQL OPEN cursorName; while (true) { EXEC SQL FETCH FROM cursorName INTO :variables; if (NO_MORE_TUPLES) break; /* do something with values */ } EXEC SQL CLOSE cursorName;

  19. More on Cursors • cursors can modify a relation as well as read it. • We can determine the order in which the cursor will get • tuples by the ORDER BY keyword in the SQL query. • Cursors can be protected against changes to the • underlying relations. • The cursor can be a scrolling one: can go forward, backward • +n, -n, Abs(n), Abs(-n).

  20. Recursion in SQL-3 Limited forms of recursion are considered important. Linear recursion: only 1 occurrence of a recursive predicate in the body Path( X, Y ) :- Edge( X, Y ) Path( X, Y ) :- Edge( X, Z ), Path( Z, Y ). WITH Pairs AS SELECT origin, dest FROM EDGE RECURSIVE Path(origin, dest) AS Pairs UNION (SELECT Pairs.origin, Path.to FROM Pairs, Path WHERE Pairs.to = Path.origin) SELECT * FROM Path;

More Related