Presentation is loading. Please wait.

Presentation is loading. Please wait.

+ Assignment 5 Q’s CSCI 2141 W2013. + Foreign Key issue CONSTRAINT fk_model FOREIGN KEY (model) REFERENCES Desktop(Model) Problem – can’t have it reference.

Similar presentations


Presentation on theme: "+ Assignment 5 Q’s CSCI 2141 W2013. + Foreign Key issue CONSTRAINT fk_model FOREIGN KEY (model) REFERENCES Desktop(Model) Problem – can’t have it reference."— Presentation transcript:

1 + Assignment 5 Q’s CSCI 2141 W2013

2 + Foreign Key issue CONSTRAINT fk_model FOREIGN KEY (model) REFERENCES Desktop(Model) Problem – can’t have it reference the model in two different tables My solution avoids that by collapsing 2 tables into devices Problem: how to enforce constraint of a null value for screen if not a laptop? Could do it semantically (screen is last attribute, trust those inserting data to not provide data if it is not a laptop) Or need some kind of a constraint

3 + Solution from Aaron Hallink: http://www.w3schools.com/sql/sql_check.asp I found out it actually hasn't been implemented in MYsql. http://stackoverflow.com/questions/14247655/mysql-check- constraint I had to create a trigger instead, to set the screen to null when the device is a desktop. DELIMITER $$ CREATE TRIGGER device_screen BEFORE INSERT ON Device FOR EACH ROW BEGIN IF (new.type ='DESKTOP') THEN SET new.screen = NULL; END IF; END$$ DELIMITER ;

4 + Tuple based constraints Part 3 asks you to write constraints as tuple based constraints Tuple based constraints are required instead of attribute-based constraints when you need to check more than 1 attribute in a tuple – they must be defined after the attributes are defined CHECK (year > 1909 AND length <480) Both attribute-based and tuple-based can refer to other tables through subqueries So you can do a subquery to see if a studio name is in at least one Movies tuple So that section actually doesn’t necessarily need tuple based constraints, just more complex constraints

5 + Attribute-Based Checks Constraints on the value of a particular attribute. Add CHECK( ) 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. 5

6 + Example: Attribute-Based Check CREATE TABLE Sells ( canteenCHAR(20), productCHAR(20)CHECK ( product IN (SELECT name FROM products)), priceREAL CHECK ( price <= 5.00 ) ); 6

7 + Timing of Checks Attribute-based checks are performed only when a value for that attribute is inserted or updated. Example: CHECK (price <= 5.00) checks every new price and rejects the modification (for that tuple) if the price is more than $5. Example: CHECK (product IN (SELECT name FROM products)) not checked if a product is deleted from products (unlike foreign-keys). 7

8 + Tuple-Based Checks CHECK ( ) may be added as a relation-schema element. The condition may refer to any attribute of the relation. But other attributes or relations require a subquery. Checked on insert or update only. 8

9 + Example: Tuple-Based Check Only Joe ’ s canteen can sell product for more than $5: CREATE TABLE Sells ( canteenCHAR(20), productCHAR(20), priceREAL, CHECK (canteen = ‘ Joes canteen ’ OR price <= 5.00) ); 9


Download ppt "+ Assignment 5 Q’s CSCI 2141 W2013. + Foreign Key issue CONSTRAINT fk_model FOREIGN KEY (model) REFERENCES Desktop(Model) Problem – can’t have it reference."

Similar presentations


Ads by Google