cons - Constraints Foreign Keys Local and Global...

Info icon This preview shows pages 1–12. Sign up to view the full content.

View Full Document Right Arrow Icon
1 Constraints Foreign Keys Local and Global Constraints Triggers
Image of page 1

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
2 Constraints and Triggers A constraint is a relationship among data elements that the DBMS is required to enforce. Example : key constraints. Triggers are only executed when a specified condition occurs, e.g., insertion of a tuple. Easier to implement than complex constraints.
Image of page 2
3 Kinds of Constraints Keys . Foreign-key , or referential-integrity. Value-based constraints. Constrain values of a particular attribute. Tuple-based constraints. Relationship among components. Assertions : any SQL boolean expression.
Image of page 3

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
4 Review : Single-Attribute Keys Place PRIMARY KEY or UNIQUE after the type in the declaration of the attribute. Example : CREATE TABLE Beers ( name CHAR(20) UNIQUE, manf CHAR(20) );
Image of page 4
5 Review : Multiattribute Key The bar and beer together are the key for Sells: CREATE TABLE Sells ( bar CHAR(20), beer VARCHAR(20), price REAL, PRIMARY KEY (bar, beer) );
Image of page 5

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
6 Foreign Keys Values appearing in attributes of one relation must appear together in certain attributes of another relation. Example : in Sells(bar, beer, price) , we might expect that a beer value also appears in Beers.name .
Image of page 6
7 Expressing Foreign Keys Use keyword REFERENCES, either: 1. After an attribute (for one-attribute keys). 2. As an element of the schema: FOREIGN KEY (<list of attributes>) REFERENCES <relation> (<attributes>) Referenced attributes must be declared PRIMARY KEY or UNIQUE.
Image of page 7

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
8 Example : With Attribute CREATE TABLE Beers ( name CHAR(20) PRIMARY KEY, manf CHAR(20) ); CREATE TABLE Sells ( bar CHAR(20), beer CHAR(20) REFERENCES Beers(name), price REAL );
Image of page 8
9 Example : As Schema Element CREATE TABLE Beers ( name CHAR(20) PRIMARY KEY, manf CHAR(20) ); CREATE TABLE Sells ( bar CHAR(20), beer CHAR(20), price REAL, FOREIGN KEY(beer) REFERENCES Beers(name));
Image of page 9

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
10 Enforcing Foreign-Key Constraints If there is a foreign-key constraint from relation R to relation S , two violations are possible: 1. An insert or update to R introduces values not found in S . 2. A deletion or update to S causes some tuples of R to “dangle.”
Image of page 10
11 Actions Taken --- (1) Example : suppose R = Sells, S = Beers.
Image of page 11

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Image of page 12
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern