9
+ Assignment 5 Q’s CSCI 2141 W2013

Assignment 5 Q’s

Embed Size (px)

DESCRIPTION

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 - PowerPoint PPT Presentation

Citation preview

Page 1: Assignment 5 Q’s

+

Assignment 5 Q’s

CSCI 2141W2013

Page 2: Assignment 5 Q’s

+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

Page 3: Assignment 5 Q’s

+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 ;

Page 4: Assignment 5 Q’s

+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

Page 5: Assignment 5 Q’s

+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.

5

Page 6: Assignment 5 Q’s

+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 )

);

6

Page 7: Assignment 5 Q’s

+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

Page 8: Assignment 5 Q’s

+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.

8

Page 9: Assignment 5 Q’s

+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)

);

9