M358 - Constraints
fellstrider.com - the logo!
Home| OU Study Rooms | M358 Index | Block 1 - Information Systems | Block 2 - Relational Theory
 
Constraints

A constraint declaration constrains the operations that may be performed on relations:

Key declarations significant:

Primary keys

Declaration of a primary key of a relation is one form of constraint definition for candidate keys. Specifically a relation cannot have more than one tuple where the values of the primary keys are the same; i.e. They are always unique - "There can be only one". The primary key must always have a value. This is dictated by the entity integrity rule:

No attribute that forms part of the primary of a relation is allowed to be null.

Entity integrity must hold for all relations in relational theory.

Alternate keys

An alternate key imposes less restrictive constraints. Specifically any relation cannot have two tuples containing the same value at the alternate key. The entity integrity rule does not apply. This is guaranteed by the primary key. An alternate key can have a value of null dependent on semantics.

Foreign keys and referential integrity

A foreign key is an attribute (or combination of attributes) in one relation R2 whose values match those of the primary key of some relation R1 (where R1 and R2 are not necessarily distinct). Specifically a foreign key value must always be the same as some existing value of the primary key that it references otherwise the relation is invalid. The identifiers can be different names but their values are the same.

The referential integrity rule

If a relation R2 has a foreign key F that references the primary key P in another relation R1 then every R2/F entry must either be a value equal to an R1/P primary key value or be null.

This rule has two effects:

Deletions from a referenced relation

Deleting a tuple is straight forward if:

There are four strategies that can be used to maintain referential integrity:

Attribute constraints

Allowing null

Null indicates the absence of a value drawn from the domain on which the attribute is drawn. Not all attributes may be allowed to be null.

Attribute conditions

Some attributes may have constraint definitions on what their values are allowed to be - Registration may be linked to birth year in ascertaining an age of 18 for example.

General constraints

Constraint definitions in relational algebra can be used to represent the participation condition on the 1: side of a 1:n relationship. Constraint definitions using relational algebra can also be used for the inclusivity/exclusivity conditions for a relationship.

Participation conditions

A relation on the 1: side is a referenced relation, whereas the relation on :n side is the referencing relation. If the relation on the 1: side is mandatory, each tuple in the referenced relation has the constraint that the referencing tuple has a foreign key value that is the same as the primary key value of the referenced tuple. In other words, the following expression should produce an empty relation (one which contains no tuples), if the above conditions were applied:

(project Staff over StaffNo) difference (project Student over CounsellorNo)

In a nutshell, if this expression evaluated to an empty relation, each member of staff would be a counsellor for at least one student. This is an example of a mandatory participation condition.

The following constraint definition produces the above constraint. Note the use of is empty. This syntax sets the rule that tuples cannot break this constraint.

relation Staff
StaffNo: StaffNumbers
Name: PersonNames
Region: Regions
primary key StaffNo
constraint (project Staff over StaffNo) difference (project Student over CounsellorNo) is empty

For an optional participation condition the following expression (the same as above) can evaluate to either an empty relation or a tuple(s) containing StaffNo values for those who do not counsel. There is no constraint of is empty, and no constraint on the database state. Although we cannot explicitly represent such an optional condition, it is implicitly represented by the fact that we have not declared a mandatory participation condition.

(project Staff over StaffNo) difference (project Student over CounsellorNo)

Inclusivity/exclusivity conditions

There are four conditions that have to be catered for:

Attribute constraints handle the :n entity types for both inclusive and exclusive.

:n and exclusive

:n and exclusive participation condition

A car cannot be owned by both the hospital and a member of staff. Therefore, the participation conditions of StaffOwns and HospitalOwns is optional.

Staff (StaffNo, Name)
Hospital (HospitalCode, HospitalName)
Car (RegistrationNumber, TaxationClass, StaffNoOfOwner, HospitalCodeOfOwner)

Where Car.StaffNoOfOwner and Car.HospitalCodeOfOwner are foreign keys of Staff and Hospital respectively.

Car ownership is exclusive to either a member of staff or a hospital. The exclusivity of StaffOwns and HospitalOwns is represented by the fact that at least one of the possible owners must be null. The following constraint definition allows for this.

constraint (StaffNoOfOwner is null or HospitalCodeOfOwner is null)

In the above expression it is allowable for both to be null.

:n and inclusive

If the relationship HospitalOwns was changed to HospitalRegisters, the relationships StaffOwns and HospitalRegisters would be inclusive but optional for Car. If both are mandatory this would force inclusivity. Inclusivity cannot be achieved if one is mandatory and the other optional.

constraint (StaffNoOfOwner is null and HospitalPermitNo is null) or
(StaffNoOfOwner is not null and HospitalPermitNo is not null)

StaffNoOfOwner and HopitalPermitNo must either both be null or both be not null.

1: and inclusive

1: and inclusive participation condition
Entrepreneur(EntNo, Name)
Property (PropertyCode, Value, EntNo)
Shares (ShareCode, Value, EntNo)

Where Property.EntNo and Shares.EntNo are foreign keys referencing Entrepreneur.

Inclusivity means that any Property.EntNo value must also appear as a Shares.EntNo value. If property is owned then shares must also be owned.

Gives entrepreneurs that own property but not shares:

(project Property over EntNo) difference (project Shares over EntNo)

Gives entrepreneurs that own shares but not property:

(project Shares over EntNo) difference (project Property over EntNo)

These could be presented as constraint definitions using the syntax is empty forming part of the declaration of either Shares or Property (not both).

1: and exclusive

If the owning of OwnsShares was exclusive of OwnsProperty then any Property.EntNo value must not appear as a Shares.EntNo value.

constraint (project Property over EntNo) intersection (project Shares over EntNo) is empty
Home| OU Study Rooms | M358 Index | Block 1 - Information Systems | Block 2 - Relational Theory
Move on to Block 3 - SQL.

Valid CSS! Valid XHTML 1.0!

Comments, suggestions, ideas to
Stuart Banner