90 likes | 193 Views
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 the model in two different tables My solution avoids that by collapsing 2 tables into devices
E N D
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 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
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 ;
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
Attribute-Based Checks • Constraints on the value of a particular 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.
Example: Attribute-Based Check CREATE TABLE Sells ( canteen CHAR(20), product CHAR(20) CHECK ( product IN (SELECT name FROM products)), price REAL CHECK ( price <= 5.00 ) );
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).
Tuple-Based Checks • CHECK (<condition>) 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.
Example: Tuple-Based Check • Only Joe’s canteen can sell product for more than $5: CREATE TABLE Sells ( canteen CHAR(20), product CHAR(20), price REAL, CHECK (canteen = ‘Joes canteen’OR price <= 5.00) );