constraints. some basic ones some basic integrity constraints: primary keys, not null constraints,...

38
Constraints

Post on 19-Dec-2015

238 views

Category:

Documents


4 download

TRANSCRIPT

Page 1: Constraints. Some basic ones Some basic integrity constraints: primary keys, not null constraints, and unique constraints. Examples: CREATE TABLE Movies

Constraints

Page 2: Constraints. Some basic ones Some basic integrity constraints: primary keys, not null constraints, and unique constraints. Examples: CREATE TABLE Movies

Some basic ones• Some basic integrity constraints: primary keys, not null constraints, and unique constraints. Examples:

CREATE TABLE Movies (

title CHAR(40) PRIMARY KEY,

year INT,

length INT,

type CHAR(2)

);

CREATE TABLE Movies (

title CHAR(40),

year INT,

length INT,

type CHAR(2),

PRIMARY KEY (title, year)

);CREATE TABLE ABC ( A number NOT NULL, B number NULL, C number);

insert into ABC values ( 1, null, null);insert into ABC values ( 2, 3, 4);insert into ABC values (null, 5, 6);

The first two records can be inserted, the third cannot, throwing a ORA-01400: cannot insert NULL into ("userschema"."ABC"."A").

The not null/null constraint can be altered with: ALTER TABLE ABC MODIFY A null;After this modification, the column A can contain null values.

Page 3: Constraints. Some basic ones Some basic integrity constraints: primary keys, not null constraints, and unique constraints. Examples: CREATE TABLE Movies

• UNIQUE constraint doesn't allow duplicate values in a column. • If the unique constraint encompasses two or more columns, no two equal

combinations are allowed. • However, if a column is not explicitly defined as NOT NULL, nulls can be

inserted multiple times.

CREATE TABLE AB ( A NUMBER UNIQUE, B NUMBER);

insert into AB values (4, 5);insert into AB values (2, 1);insert into AB values (9, 8);insert into AB values (6, 9);insert into AB values (null,9);insert into AB values (null,9);insert into AB values (2,7);

• The last statement issues a ORA-00001: unique constraint (THOMO.SYS_C006985) violated

Page 4: Constraints. Some basic ones Some basic integrity constraints: primary keys, not null constraints, and unique constraints. Examples: CREATE TABLE Movies

Constraint names• Every constraint, has a name. In this case, the name is:

THOMO.SYS_C006985. • We can explicitly name the constraint for easier handling.

CREATE TABLE ABC (

A number,

B number,

C number,

CONSTRAINT my_unique_constraint2 UNIQUE (A,B)

);

• To find the constraint names and the tables on which the constraints are set do:

select CONSTRAINT_NAME, TABLE_NAME from user_constraints;

It can’t have the same name as another constraint even if it is in

another table.

Page 5: Constraints. Some basic ones Some basic integrity constraints: primary keys, not null constraints, and unique constraints. Examples: CREATE TABLE Movies

Dropping/AddingExamples

ALTER TABLE AB DROP CONSTRAINT SYS_C006985;

ALTER TABLE AB add

CONSTRAINT my_unique_constraint UNIQUE (A);

Page 6: Constraints. Some basic ones Some basic integrity constraints: primary keys, not null constraints, and unique constraints. Examples: CREATE TABLE Movies

Foreign key constraints• We specify a column or a list of columns as a foreign key of the

referencing table. • The referencing table is called the child table, and the referenced table is

called the parent table.

• Example: Each employee in the table EMP must work in a department that is contained in the table DEPT.

CREATE TABLE Emp (

empno INT CONSTRAINT pk_emp PRIMARY KEY,

... ,

deptno INT CONSTRAINT fk_deptno REFERENCES Dept(deptno)

);

Dept table has to exist first!

Page 7: Constraints. Some basic ones Some basic integrity constraints: primary keys, not null constraints, and unique constraints. Examples: CREATE TABLE Movies

Longer syntax for foreign keys

CREATE TABLE Movies (

title VARCHAR2(40),

year INT,

length INT,

type VARCHAR2(2),

PRIMARY KEY (title, year)

);

CREATE TABLE MovieStars(

name VARCHAR2(20) PRIMARY KEY,

address VARCHAR2(30),

gender VARCHAR2(1),

birthdate VARCHAR2(20)

);

CREATE TABLE StarsIn (

title VARCHAR2(40),

year INT,

starName VARCHAR2(20),

CONSTRAINT fk_movies FOREIGN KEY(title,year) REFERENCES Movies(title,year),

CONSTRAINT fk_moviestars FOREIGN KEY(starName) REFERENCES MovieStars(name)

);

Remark. If you don’t specify primary keys or unique constraints in the parent tables, you can’t specify foreign keys in the child tables.

Page 8: Constraints. Some basic ones Some basic integrity constraints: primary keys, not null constraints, and unique constraints. Examples: CREATE TABLE Movies

Foreign key constraints (cont.)• In order to satisfy a foreign key constraint, each row in the child table has to

satisfy one of the following two conditions:

– the attribute value (list of attribute values) of the foreign key must appear as a primary key value in the parent table, or

– the attribute value of the foreign key is null • in case of a composite foreign key, at least one attribute value of the foreign key is

null

• According to the above definition,

for table EMP, an employee must not necessarily work in a department,

i.e., for the attribute DEPTNO, the value null is admissible.

• If we want to not allow NULL’s in a foreign key we must say so.

• Example: There should always be a project manager, who must be an employee.

CREATE TABLE PROJECT (

PNO number(3) CONSTRAINT prj_pk PRIMARY KEY,

PMGR number(4) NOT NULL CONSTRAINT fk_pmgr REFERENCES EMP,

. . .

); When only the name of the parent table is given, the primary key of that table is assumed.

Page 9: Constraints. Some basic ones Some basic integrity constraints: primary keys, not null constraints, and unique constraints. Examples: CREATE TABLE Movies

Foreign key constraints (cont.)• A foreign key constraint may also refer to the same table, i.e., parent

table and child table are identical. • Example: Every employee must have a manager who must be an

employee.

CREATE TABLE EMP (

empno INT CONSTRAINT emp_pk PRIMARY KEY,

. . .

mgr INT NOT NULL CONSTRAINT fk_mgr REFERENCES EMP,

. . .

);

Page 10: Constraints. Some basic ones Some basic integrity constraints: primary keys, not null constraints, and unique constraints. Examples: CREATE TABLE Movies

Enforcing Foreign-Key Constraints• If there is a foreign-key constraint from table R to 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.”

Example.

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)

);

Relation S

Relation R

Page 11: Constraints. Some basic ones Some basic integrity constraints: primary keys, not null constraints, and unique constraints. Examples: CREATE TABLE Movies

Action taken• 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.

1. Default : Reject the modification.

2. Cascade : Make the same changes in Sells.– Deleted beer: delete Sells tuples referencing that beer.

– Updated beer: change values in Sells.

3. Set NULL : Change the beer to NULL.

Page 12: Constraints. Some basic ones Some basic integrity constraints: primary keys, not null constraints, and unique constraints. Examples: CREATE TABLE Movies

ExampleCascade• 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'.

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':– Same change.

Page 13: Constraints. Some basic ones Some basic integrity constraints: primary keys, not null constraints, and unique constraints. Examples: CREATE TABLE Movies

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.

CREATE TABLE Sells (bar CHAR(20),beer CHAR(20),price REAL,FOREIGN KEY(beer)

REFERENCES Beers(name)ON DELETE SET NULLON UPDATE CASCADE

);

Oracle does not allow ON UPDATE options

Page 14: Constraints. Some basic ones Some basic integrity constraints: primary keys, not null constraints, and unique constraints. Examples: CREATE TABLE Movies

Chicken and egg• Suppose we want to say:

CREATE TABLE chicken (

cID INT PRIMARY KEY,

eID INT REFERENCES egg(eID)

);

CREATE TABLE egg(

eID INT PRIMARY KEY,

cID INT REFERENCES chicken(cID)

);

• But, if we simply type the above statements, we'll get an error. – The reason is that the CREATE TABLE statement for chicken refers to table

egg, which hasn't been created yet!

– Creating egg won't help either, because egg refers to chicken.

Page 15: Constraints. Some basic ones Some basic integrity constraints: primary keys, not null constraints, and unique constraints. Examples: CREATE TABLE Movies

Some first attempt• To work around this problem, first, create chicken and egg without foreign key

declarations.

CREATE TABLE chicken(cID INT PRIMARY KEY,eID INT

);CREATE TABLE egg(

eID INT PRIMARY KEY,cID INT

);• Then, we could add foreign key constraints.

ALTER TABLE chicken ADD CONSTRAINT chickenREFegg FOREIGN KEY (eID) REFERENCES egg(eID);

ALTER TABLE egg ADD CONSTRAINT eggREFchicken FOREIGN KEY (cID) REFERENCES chicken(cID);

Page 16: Constraints. Some basic ones Some basic integrity constraints: primary keys, not null constraints, and unique constraints. Examples: CREATE TABLE Movies

Inserting…INSERT INTO chicken VALUES(1, 2);

*

ERROR at line 1:

ORA-02291: integrity constraint (THOMO.CHICKENREFEGG) violated - parent key not found

INSERT INTO egg VALUES(2, 1);

*

ERROR at line 1:

ORA-02291: integrity constraint (THOMO.EGGREFCHICKEN) violated - parent key not found

Page 17: Constraints. Some basic ones Some basic integrity constraints: primary keys, not null constraints, and unique constraints. Examples: CREATE TABLE Movies

Deferrable Constraints• Solving the problem:

1. We need the ability to group several SQL statements into one unit called transaction.

2. Then, we need a way to tell the SQL system not to check the constraints until the transaction is committed.

• Any constraint may be declared “DEFERRABLE” or “NOT DEFERRABLE.” – NOT DEFERRABLE is the default, and means

that every time a database modification occurs, the constraint is immediately checked.

– DEFERRABLE means that we have the option of telling the system to wait until a transaction is complete before checking the constraint.

In ORACLE SQLPlus, when we login, a transaction is automatically created. We can (positively) finish this transaction by calling “commit” and a new transaction is created.

Page 18: Constraints. Some basic ones Some basic integrity constraints: primary keys, not null constraints, and unique constraints. Examples: CREATE TABLE Movies

Initially Deferred / Initially Immediate • If a constraint is deferrable, then we may also declare it

– INITIALLY DEFERRED, and the check will be deferred to the end of the current transaction.

– INITIALLY IMMEDIATE, (default) and the check will be made before any modification.

• But, because the constraint is deferrable, we have the option of later deciding to defer checking (SET CONSTRAINT MyConstraint DEFERRED).

Example Here we declare the constraints DEFERRABLE and INITIALLY DEFERRED.

First drop the current constraints and then do:

ALTER TABLE chicken ADD CONSTRAINT chickenREFegg FOREIGN KEY (eID) REFERENCES egg(eID) DEFERRABLE INITIALLY DEFERRED;

ALTER TABLE egg ADD CONSTRAINT eggREFchicken FOREIGN KEY (cID) REFERENCES chicken(cID)DEFERRABLE INITIALLY DEFERRED;

Page 19: Constraints. Some basic ones Some basic integrity constraints: primary keys, not null constraints, and unique constraints. Examples: CREATE TABLE Movies

Successful InsertionsNow we can finally insert:

INSERT INTO chicken VALUES(1, 2);

INSERT INTO egg VALUES(2, 1);

COMMIT;

Page 20: Constraints. Some basic ones Some basic integrity constraints: primary keys, not null constraints, and unique constraints. Examples: CREATE TABLE Movies

Dropping• Finally, to get rid of the tables, we have to drop the

constraints first, because we can’t to drop a table that's referenced by another table.

ALTER TABLE egg DROP CONSTRAINT eggREFchicken;

ALTER TABLE chicken DROP CONSTRAINT chickenREFegg;

DROP TABLE egg;

DROP TABLE chicken;

Page 21: Constraints. Some basic ones Some basic integrity constraints: primary keys, not null constraints, and unique constraints. Examples: CREATE TABLE Movies

Another Example of Deferring• Studio and President

Page 22: Constraints. Some basic ones Some basic integrity constraints: primary keys, not null constraints, and unique constraints. Examples: CREATE TABLE Movies

Check Constraints• Check constraints allow users to restrict possible attribute

values for columns to admissible ones. Syntax is:

[CONSTRAINT <name>] CHECK(<condition>)

Example: – The name of an employee must consist of upper case letters only; – The minimum salary of an employee is 500; – Department numbers must range between 10 and 100:

CREATE TABLE Emp (

empno NUMBER,

ename VARCHAR2(30) CONSTRAINT check_name

CHECK( ename = UPPER(ename) ),

sal NUMBER CONSTRAINT check_sal CHECK( sal >= 500 ),

deptno NUMBER CONSTRAINT check_deptno

CHECK(deptno BETWEEN 10 AND 100)

);

These three are column constraints, and can only refer the corresponding column.

Page 23: Constraints. Some basic ones Some basic integrity constraints: primary keys, not null constraints, and unique constraints. Examples: CREATE TABLE Movies

Checking• DBMS automatically checks the specified conditions each

time a database modification is performed on this relation. – For example, the insertion

INSERT INTO emp VALUES(7999,'SCOTT',450,10);

causes a constraint violation

ORA 02290: check constraint (SAL_CHECK) violated

and the insertion is rejected.

Page 24: Constraints. Some basic ones Some basic integrity constraints: primary keys, not null constraints, and unique constraints. Examples: CREATE TABLE Movies

Check Constraints (cont’d)• A check constraint can be specified as a table constraint, and the

<condition> can refer to any column of the table. • Example:

– At least two persons must participate in a project, and – project's start date must be before project's end date

CREATE TABLE Project (

... ,

pstart DATE,

pend DATE,

persons NUMBER CONSTRAINT check_pers CHECK (persons>=2),

... ,

CONSTRAINT dates_ok CHECK (pend > pstart)

);

Column constraint

Table constraint

Page 25: Constraints. Some basic ones Some basic integrity constraints: primary keys, not null constraints, and unique constraints. Examples: CREATE TABLE Movies

Checks with subqueriesExample

CREATE TABLE Sells (

bar CHAR(20),

beer CHAR(20) CONSTRAINT beer_check

CHECK ( beer IN (SELECT name FROM Beers)),

price REAL CHECK ( price <= 5.00 )

);

This is a column constraint. The condition may use the name of the attribute (beer), but any other relation or attribute name must be in a subquery.

Page 26: Constraints. Some basic ones Some basic integrity constraints: primary keys, not null constraints, and unique constraints. Examples: CREATE TABLE Movies

Timing of Checks• Column checks are performed only when a value for that attribute is

inserted or updated. • Table checks are performed only when values for the involved

attributes (not those in subqueries) are 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 (beer IN (SELECT name FROM Beers))

isn’t checked if a beer is deleted from Beers (unlike foreign-keys).

Page 27: Constraints. Some basic ones Some basic integrity constraints: primary keys, not null constraints, and unique constraints. Examples: CREATE TABLE Movies

Violating TuplesREM Adding a violating constraint

ALTER TABLE Emp DROP CONSTRAINT check_sal;

INSERT INTO Emp(empno, ename, sal, deptno)

VALUES(9, 'ALEX', 300, 20);

ALTER TABLE Emp ADD CONSTRAINT check_sal CHECK(sal >= 500) EXCEPTIONS INTO Exceptions;

REM The constraint cannot be created at all, because there is

REM a violating tuple.

• In order to identify those tuples that violate a constraint whose activation failed, one can use the clause EXCEPTIONS INTO Exceptions.

• Exceptions is a table that we create and stores information about the violating tuples.

Page 28: Constraints. Some basic ones Some basic integrity constraints: primary keys, not null constraints, and unique constraints. Examples: CREATE TABLE Movies

Violating Tuples (cont.)• First we have to create the Exceptions table:

CREATE TABLE Exceptions(row_id ROWID,

owner VARCHAR2(30),

table_name VARCHAR2(30),

constraint VARCHAR2(30)

);

• Then, we can query it:SELECT Emp.*, constraint

FROM Emp, Exceptions

WHERE Emp.rowid = Exceptions.row_id;

Every tuple has a (pseudo) column of type rowid that is used to identify tuples.

row_id here will reference to rowid in the Emp table.

Besides the row_id, the name of the table, the table owner, as well as the name of the violated constraint are stored.

Page 29: Constraints. Some basic ones Some basic integrity constraints: primary keys, not null constraints, and unique constraints. Examples: CREATE TABLE Movies

Writing Constraints Correctly• Create the table MovieStar. If the star gender is 'M', then

his name must not begin with 'Ms.'.

CREATE TABLE MovieStar (

name CHAR(20) PRIMARY KEY,

address VARCHAR(255),

gender CHAR(1),

CHECK (gender='F' OR name NOT LIKE 'Ms.')

);We can’t use an “implication.” We should formulate it in terms of OR. p->q is the same as (not p) OR q.

Page 30: Constraints. Some basic ones Some basic integrity constraints: primary keys, not null constraints, and unique constraints. Examples: CREATE TABLE Movies

Exercise – mutually exclusive subclassesCREATE TABLE Vehicles ( vin CHAR(17) PRIMARY KEY, vehicle_type CHAR(3) CHECK(vehicle_type IN ('SUV', 'ATV')), fuel_type CHAR(4), door_count INT CHECK(door_count >= 0), UNIQUE(vin, vehicle_type));CREATE TABLE SUVs ( vin CHAR(17) PRIMARY KEY, vehicle_type CHAR(3) CHECK(vehicle_type ='SUV'), FOREIGN KEY (vin, vehicle_type) REFERENCES Vehicles (vin, vehicle_type) ON DELETE CASCADE);CREATE TABLE ATVs ( vin CHAR(17) PRIMARY KEY, vehicle_type CHAR(3) CHECK(vehicle_type =‘ATV'), FOREIGN KEY (vin, vehicle_type) REFERENCES Vehicles (vin, vehicle_type) ON DELETE CASCADE);

Page 31: Constraints. Some basic ones Some basic integrity constraints: primary keys, not null constraints, and unique constraints. Examples: CREATE TABLE Movies

Exercise – PCs, Laptops, PrintersProduct(maker, model, type)

PC(model, speed, ram, hd, rd, price)

Laptop(model, speed, ram, hd, screen, price)

Printer(model, color, type, price)

First create keys and foreign key references.

Then create the following constraints.

a) The speed of a laptop must be at least 800.

b) The only types of printers are laser, ink-jet, and bubble.

c) A model of a product must also be the model of a PC, a laptop, or a printer.

Page 32: Constraints. Some basic ones Some basic integrity constraints: primary keys, not null constraints, and unique constraints. Examples: CREATE TABLE Movies

What’s [usually] allowed in check• Note that in most DBMS’es (including ORACLE) only simple conditions

are allowed. For example – It is not allowed to refer to columns of other tables– No queries as check conditions.

• Solution: Use views WITH CHECK OPTION

Example

CREATE TABLE Hotel (

room_nbr INT NOT NULL,

arrival_date DATE NOT NULL,

departure_date DATE NOT NULL,

guest_name CHAR(15) NOT NULL,

PRIMARY KEY (room_nbr, arrival_date),

CHECK (departure_date > arrival_date)

);

We want to add the constraint that reservations do not overlap.

Page 33: Constraints. Some basic ones Some basic integrity constraints: primary keys, not null constraints, and unique constraints. Examples: CREATE TABLE Movies

Exercise – Hotel StaysCREATE TABLE Hotel ( room_nbr INT NOT NULL, arrival_date DATE NOT NULL, departure_date DATE NOT NULL, guest_name CHAR(15) NOT NULL, PRIMARY KEY (room_nbr, arrival_date), CHECK (departure_date > arrival_date));

CREATE VIEW HotelStays ASSELECT room_nbr, arrival_date, departure_date, guest_nameFROM Hotel H1WHERE NOT EXISTS ( SELECT * FROM Hotel H2 WHERE H1.room_nbr = H2.room_nbr AND (H2.arrival_date < H1.arrival_date AND H1.arrival_date < H2.departure_date))WITH CHECK OPTION;

We want to add the constraint that reservations do not overlap.

Page 34: Constraints. Some basic ones Some basic integrity constraints: primary keys, not null constraints, and unique constraints. Examples: CREATE TABLE Movies

Exercise – Hotel Stays – Inserting

INSERT INTO HotelStays (room_nbr, arrival_date, departure_date, guest_name)

VALUES(1, '01-Jan-2008', '03-Jan-2008', 'Alex');

This goes Ok.

INSERT INTO HotelStays (room_nbr, arrival_date, departure_date, guest_name)

VALUES(1, '02-Jan-2008', '05-Jan-2008', 'Ben');

*

ERROR at line 1:

ORA-01402: view WITH CHECK OPTION where-clause violation

Page 35: Constraints. Some basic ones Some basic integrity constraints: primary keys, not null constraints, and unique constraints. Examples: CREATE TABLE Movies

Assertions• These are database-schema elements, like relations or

views.• Defined by:

CREATE ASSERTION <name>

CHECK (<condition>);• Condition may refer to any relation or attribute in the

database schema.

• Remark. NOT implemented in most DBMS’es!

Page 36: Constraints. Some basic ones Some basic integrity constraints: primary keys, not null constraints, and unique constraints. Examples: CREATE TABLE Movies

Example: Assertion

• In Sells(bar, beer, price), no bar may charge an average of more than $5.

CREATE ASSERTION NoRipoffBars CHECK (

NOT EXISTS (

SELECT bar FROM Sells

GROUP BY bar

HAVING 5.00 < AVG(price)

));

Bars with anaverage priceabove $5

Page 37: Constraints. Some basic ones Some basic integrity constraints: primary keys, not null constraints, and unique constraints. Examples: CREATE TABLE Movies

Example: Assertion• In Drinkers(name, addr, phone) and

Bars(name, addr, license), there cannot be more bars than drinkers.

CREATE ASSERTION FewBar CHECK (

(SELECT COUNT(*) FROM Bars) <=

(SELECT COUNT(*) FROM Drinkers)

);

Page 38: Constraints. Some basic ones Some basic integrity constraints: primary keys, not null constraints, and unique constraints. Examples: CREATE TABLE Movies

Timing of Assertion Checks• In principle, we must check every assertion after every

modification to any relation of the database.

• A clever system can observe that only certain changes could cause a given assertion to be violated.– Example:

• No change to Beers can affect FewBar. • Neither can an insertion to Drinkers.