SQL Constraints and Triggers Overview

csce 608 database systems n.w
1 / 67
Embed
Share

Explore the fundamentals of SQL constraints and triggers in database systems, including their importance, types, and applications. Get insights into database modifications, insertion, deletion, and semantic considerations. Enhance your understanding of SQL with practical examples and images.

  • SQL
  • Constraints
  • Triggers
  • Database Systems

Uploaded on | 0 Views


Download Presentation

Please find below an Image/Link to download the presentation.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author. If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.

You are allowed to download the files provided on this website for personal or commercial use, subject to the condition that they are used lawfully. All files are the property of their respective owners.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author.

E N D

Presentation Transcript


  1. CSCE-608 Database Systems Spring 2025 Instructor: Jianer Chen Office: PETR 428 Phone: 845-4259 Email: chen@cse.tamu.edu Notes 13: SQL Constraints and triggers

  2. SQL: Structured Query language a very-high-level language. * say what to do rather than how to do it. * avoid a lot of data-manipulation details needed in procedural languages like C++ or Java. Database management system figures out the best way to execute queries * called query optimization For both data definition and data manipulation.

  3. Database Modifications A modification command does not return a result (as a query does), but changes the database in some way. Three kinds of modifications: 1. Insert a tuple or tuples. 2. Delete a tuple or tuples. 3. Update the value(s) of an existing tuple or tuples.

  4. Likes(drinker, beer) Insertion To insert a single tuple: INSERT INTO <relation> VALUES (<list of values>); Example: INSERT INTO Likes VALUES( Sally , Bud ); May add a list of attributes to <relation>, reasons: * Forget the order of attributes for the relation. * Don t have values for all attributes, and want the system to fill in missing ones with default values. INSERT INTO Likes(beer, drinker) VALUES( Bud , Sally );

  5. Inserting Many Tuples We may insert the entire result of a query into a relation, using the form: INSERT INTO <relation> (<subquery>); INSERT INTO PotBuddies (SELECT d2.drinker FROM Frequents d1, Frequents d2 WHERE d1.drinker = Sally AND d2.drinker <> Sally AND d1.bar = d2.bar);

  6. Likes(drinker, beer) Deletion To delete tuples satisfying a condition from some relation: DELETE FROM <relation> WHERE <condition>; Example 1. DELETE FROM Likes WHERE drinker = Sally AND beer = Bud ; Example 2. To delete all tuples in a relation: DELETE FROM Likes (no WHERE clause is needed)

  7. name manf Semantics of Deletion Bud Anheuser-Busch Bud Lite Anheuser-Busch Suppose Anheuser-Busch makes only Bud and Bud Lite. If we come to the tuple b for Bud first. The subquery is nonempty, because of the Bud Lite tuple, so we delete Bud. (?) Now, when b is the tuple for Bud Lite, do we delete that tuple too? Answer: we do delete Bud Lite as well. Reason: Deletion proceeds in two stages: -- Mark all tuples for which WHERE condition holds. -- Delete the marked tuples. Delete from Beers(name, manf) all beers for which there is another beer by the same manufacturer.

  8. Drinkers(name, addr, phone) Sells(bar, beer, price) Updates To change certain attributes in certain tuples of a relation: UPDATE <relation> SET <list of attribute assignments> WHERE <condition on tuples>; Change Fred s phone number to 555-1212: UPDATE Drinkers SET phone = 555-1212 WHERE name = Fred ; Make $4 the maximum price for beer (make updates for several tuples): UPDATE Sells SET price = 4.00 WHERE price > 4.00;

  9. Database is just another model of information processing Then why study DB? Much more data, regular data techs would be very inefficient: How should data be stored? Operations are simpler & more specific: How do we take advantage of it? New programming languages for the above. (ACID) Reliability, security, consistency, currency , , , , , \, , , C Data (in disks) SQL

  10. Constraints and Triggers A constraint is a relationship among data elements that the DBMS is required to enforce. -- e.g., key constraints. A trigger is an action only executed when a specified condition occurs, e.g., insertion of a tuple. -- easier to implement than complex constraints.

  11. Constraints and Triggers A constraint is a relationship among data elements that the DBMS is required to enforce. -- e.g., key constraints. A trigger is an action only executed when a specified condition occurs, e.g., insertion of a tuple. -- easier to implement than complex constraints.

  12. Kinds of Constraints Keys (unique, cannot be NULL). Foreign-key -- referential-integrity. Value-based constraints. -- constrain values of a particular attribute. Tuple-based constraints. -- relationship among components. Assertions. -- any SQL boolean expression.

  13. Kinds of Constraints Keys (unique, cannot be NULL). Foreign-key -- referential-integrity. Value-based constraints. -- constrain values of a particular attribute. Tuple-based constraints. -- relationship among components. Assertions. -- any SQL boolean expression.

  14. Primary keys: unique, not NULL CREATE TABLE Drinkers ( name CHAR(30) PRIMARY KEY, addr CHAR(50) DEFAULT 123 Sesame St. , phone CHAR(16) NOT NULL ); CREATE TABLE Likes ( drinker CHAR(30), beer CHAR(30), PRIMARY KEY (drinker, beer) );

  15. Kinds of Constraints Keys (unique, cannot be NULL). Foreign-key -- referential-integrity. Value-based constraints. -- constrain values of a particular attribute. Tuple-based constraints. -- relationship among components. Assertions. -- any SQL boolean expression.

  16. Sells(bar, beer, price) Beers(name, manf) Foreign Keys Consider the relation Sells(bar, beer, price). We might expect that a beer value in Sells is a real beer --- something appearing in Beers.name. 16

  17. Sells(bar, beer, price) Beers(name, manf) Foreign Keys Consider the relation Sells(bar, beer, price). We might expect that a beer value in Sells is a real beer --- something appearing in Beers.name. A constraint that requires a beer in Sells to be a beer in Beers is called a foreign-key constraint. 17

  18. Expressing Foreign Keys Use the keyword REFERENCES, either: -- Within the declaration of an attribute (only for one-attribute keys). -- As an element of the schema: FOREIGN KEY (<attributes>) REFERENCES <relation> (<attributes>) 18

  19. Expressing Foreign Keys Use the keyword REFERENCES, either: -- Within the declaration of an attribute (only for one-attribute keys). -- As an element of the schema: FOREIGN KEY (<attributes>) REFERENCES <relation> (<attributes>) Referenced attributes must be declared PRIMARY KEY or UNIQUE in <relation>. 19

  20. Sells(bar, beer, price) Beers(name, manf) Example: With Attribute CREATE TABLE Beers ( name manf CHAR(20) PRIMARY KEY, CHAR(20) ); CREATE TABLE Sells ( bar beer price CHAR(20), CHAR(20) REFERENCES Beers(name), REAL ); 20

  21. Sells(bar, beer, price) Beers(name, manf) Example: With Attribute CREATE TABLE Beers ( name manf CHAR(20) PRIMARY KEY, CHAR(20) ); CREATE TABLE Sells ( bar beer price CHAR(20), CHAR(20) REFERENCES Beers(name), REAL ); 21

  22. Sells(bar, beer, price) Beers(name, manf) Example: As Element CREATE TABLE Beers ( name manf CHAR(20) PRIMARY KEY, CHAR(20) ); CREATE TABLE Sells ( bar price FOREIGN KEY(beer) REFERENCES Beers(name)); CHAR(20), REAL, 22

  23. Sells(bar, beer, price) Beers(name, manf) Example: As Element CREATE TABLE Beers ( name manf CHAR(20) PRIMARY KEY, CHAR(20) ); CREATE TABLE Sells ( bar price FOREIGN KEY(beer) REFERENCES Beers(name)); CHAR(20), REAL, can be a list of more than one attributes 23

  24. Sells(bar, beer, price) Beers(name, manf) Example: As Element CREATE TABLE Beers ( name manf CHAR(20) PRIMARY KEY, CHAR(20) ); Remark. Attributes in a foreign key MAY have value NULL CREATE TABLE Sells ( bar price FOREIGN KEY(beer) REFERENCES Beers(name)); CHAR(20), REAL, can be a list of more than one attributes 24

  25. Enforcing Foreign-Key Constraints If there is a foreign-key constraint from attributes of relation R to a key of relation S, two violations are possible: 25

  26. Enforcing Foreign-Key Constraints If there is a foreign-key constraint from attributes of relation R to a key of relation S, two violations are possible: -- An insert or update to R introduces values not found in S. 26

  27. Sells(bar, beer, price) Beers(name, manf) Enforcing Foreign-Key Constraints If there is a foreign-key constraint from attributes of relation R to a key of relation S, two violations are possible: -- An insert or update to R introduces values not found in S. Sells (R) Beers (S) bar beer price name manf Joe s Bud 3.00 Bud A.B. Sue s Miller 3.50 Miller MBC 27

  28. Sells(bar, beer, price) Beers(name, manf) Enforcing Foreign-Key Constraints If there is a foreign-key constraint from attributes of relation R to a key of relation S, two violations are possible: -- An insert or update to R introduces values not found in S. Sells (R) Beers (S) bar beer price name manf Joe s Bud 3.00 Bud A.B. Sue s Miller 3.50 Miller MBC Sue s Bud Lite 3.20 28 No Bud Lite

  29. Sells(bar, beer, price) Beers(name, manf) Enforcing Foreign-Key Constraints If there is a foreign-key constraint from attributes of relation R to a key of relation S, two violations are possible: -- An insert or update to R introduces values not found in S. -- A deletion or update to S causes some tuples of R to dangle. Sells (R) Beers (S) Sells (R) Beers (S) bar beer price name manf bar beer price name manf Joe s Bud 3.00 Bud A.B. Joe s Bud 3.00 Bud A.B. Sue s Miller 3.50 Miller MBC Sue s Miller 3.50 Miller MBC Sue s Bud Lite 3.20 29 No Bud Lite

  30. Sells(bar, beer, price) Beers(name, manf) Enforcing Foreign-Key Constraints If there is a foreign-key constraint from attributes of relation R to a key of relation S, two violations are possible: -- An insert or update to R introduces values not found in S. -- A deletion or update to S causes some tuples of R to dangle. Sells (R) Beers (S) Sells (R) Beers (S) bar beer price name manf bar beer price name manf ? Joe s Bud 3.00 Bud A.B. Joe s Bud 3.00 Bud A.B. Sue s Miller 3.50 Miller MBC Sue s Miller 3.50 Miller MBC Sue s Bud Lite 3.20 30 No Bud Lite

  31. Sells(bar, beer, price) Beers(name, manf) Actions Taken Suppose R = Sells, S = Beers. 31

  32. Sells(bar, beer, price) Beers(name, manf) Actions Taken Suppose R = Sells, S = Beers. An insert or update to Sells that introduces a nonexistent beer must be rejected. 32

  33. Sells(bar, beer, price) Beers(name, manf) Actions Taken Suppose R = Sells, S = Beers. An insert or update to Sells that introduces a nonexistent beer must be rejected. A deletion or update to Beers that removes a beer value found in some tuples of Sells can be handled in three ways: 33

  34. Sells(bar, beer, price) Beers(name, manf) Actions Taken Suppose R = Sells, S = Beers. An insert or update to Sells that introduces a nonexistent beer must be rejected. A deletion or update to Beers that removes a beer value found in some tuples of Sells can be handled in three ways: -- Default: Reject the modification. 34

  35. Sells(bar, beer, price) Beers(name, manf) Actions Taken Suppose R = Sells, S = Beers. An insert or update to Sells that introduces a nonexistent beer must be rejected. A deletion or update to Beers that removes a beer value found in some tuples of Sells can be handled in three ways: -- Default: Reject the modification. -- Cascade: Make the same changes in Sells. 35

  36. Sells(bar, beer, price) Beers(name, manf) Actions Taken Suppose R = Sells, S = Beers. An insert or update to Sells that introduces a nonexistent beer must be rejected. A deletion or update to Beers that removes a beer value found in some tuples of Sells can be handled in three ways: -- Default: Reject the modification. -- Cascade: Make the same changes in Sells. * Deleted beer: delete Sells tuple. 36

  37. Sells(bar, beer, price) Beers(name, manf) Actions Taken Suppose R = Sells, S = Beers. An insert or update to Sells that introduces a nonexistent beer must be rejected. A deletion or update to Beers that removes a beer value found in some tuples of Sells can be handled in three ways: -- Default: Reject the modification. -- Cascade: Make the same changes in Sells. * Deleted beer: delete Sells tuple. * Updated beer: change value in Sells. 37

  38. Sells(bar, beer, price) Beers(name, manf) Actions Taken Suppose R = Sells, S = Beers. An insert or update to Sells that introduces a nonexistent beer must be rejected. A deletion or update to Beers that removes a beer value found in some tuples of Sells can be handled in three ways: -- Default: Reject the modification. -- Cascade: Make the same changes in Sells. * Deleted beer: delete Sells tuple. * Updated beer: change value in Sells. -- Set NULL: Change the beer (in Sells) to NULL. 38

  39. Sells(bar, beer, price) Beers(name, manf) Example: Cascade Delete the Bud tuple from Beers: Sells (R) Beers (S) bar beer price name manf Joe s Bud 3.00 Bud A.B. Sue s Miller 3.50 Miller MBC Sue s Bud 3.20 39

  40. Sells(bar, beer, price) Beers(name, manf) Example: Cascade Delete the Bud tuple from Beers: -- Then delete all tuples from Sells that have beer = Bud . Sells (R) Beers (S) bar beer price name manf Joe s Bud 3.00 Bud A.B. Sue s Miller 3.50 Miller MBC Sue s Bud 3.20 40

  41. Sells(bar, beer, price) Beers(name, manf) Example: Cascade Delete the Bud tuple from Beers: -- Then delete all tuples from Sells that have beer = Bud . Update the Bud tuple by changing Bud to Budweiser : Sells (R) Beers (S) name Budweiser manf bar beer price Bud A.B. Joe s Bud 3.00 Miller MBC Sue s Miller 3.50 Sue s Bud 3.20 41

  42. Sells(bar, beer, price) Beers(name, manf) Example: Cascade Delete the Bud tuple from Beers: -- Then delete all tuples from Sells that have beer = Bud . Update the Bud tuple by changing Bud to Budweiser : -- Then change all Sells tuples with beer = Bud so that beer = Budweiser . Sells (R) Beers (S) name Budweiser manf bar beer Budweiser price Bud A.B. Joe s Bud 3.00 Miller MBC Sue s Miller Budweiser 3.50 Sue s Bud 3.20 42

  43. Sells(bar, beer, price) Beers(name, manf) Example: Set NULL Delete the Bud tuple from Beers: Sells (R) Beers (S) bar beer price name manf Joe s Bud 3.00 Bud A.B. Sue s Miller 3.50 Miller MBC Sue s Bud 3.20 43

  44. Sells(bar, beer, price) Beers(name, manf) Example: Set NULL Delete the Bud tuple from Beers: -- Change all tuples of Sells that have beer = Bud to have beer = NULL. Sells (R) Beers (S) bar beer price name manf Bud NULL Joe s 3.00 Bud A.B. Sue s Miller NULL 3.50 Miller MBC Sue s Bud 3.20 44

  45. Sells(bar, beer, price) Beers(name, manf) Example: Set NULL Delete the Bud tuple from Beers: -- Change all tuples of Sells that have beer = Bud to have beer = NULL. Update the Bud tuple by changing Bud to Budweiser : Sells (R) Beers (S) name Budweiser manf bar beer price Bud A.B. Joe s Bud 3.00 Miller MBC Sue s Miller 3.50 Sue s Bud 3.20 45

  46. Sells(bar, beer, price) Beers(name, manf) Example: Set NULL Delete the Bud tuple from Beers: -- Change all tuples of Sells that have beer = Bud to have beer = NULL. Update the Bud tuple by changing Bud to Budweiser : -- Change all tuples of Sells that have beer = Bud to have beer = NULL. Sells (R) Beers (S) name Budweiser manf bar beer NULL price Bud A.B. Joe s Bud 3.00 Miller MBC Sue s Miller NULL 3.50 Sue s Bud 3.20 46

  47. Choosing a Policy When we declare a foreign key, we may choose policies SET NULL or CASCADE independently for deletions and updates. 47

  48. Choosing a Policy When we declare a foreign key, we may choose policies SET NULL or CASCADE independently for deletions and updates. Follow the foreign-key declaration by: ON [UPDATE, DELETE][SET NULL, CASCADE] Two such clauses may be used. Otherwise, the default (reject) is used. 48

  49. Sells(bar, beer, price) Beers(name, manf) Example CREATE TABLE Sells ( bar CHAR(20), beer CHAR(20), price REAL, FOREIGN KEY(beer) REFERENCES Beers(name) ON DELETE SET NULL ON UPDATE CASCADE ); 49

  50. Attribute-Based Checks Constraints on the value of an attribute. Add: CHECK (<condition>) to the declaration for the attribute. The condition may use the name of the attribute, but any other relation or attribute name must be in a subquery.

Related


More Related Content