Integrity Constraints

Let’s overview integrity constraints.

Integrity constraints (also referred to as just constraints) are logical conditions on the data of an application. They may take many different forms.

Property constraints

The most important type of constraint, property constraints, define conditions on the admissible property values of an object. They are defined for an object type or class, so they apply to all objects of that type. We’ll concentrate on the most important cases of property constraints:

  • String length constraints: These require that the length of a string value for an attribute is less than a certain maximum number or greater than a minimum number.
  • Mandatory value constraints: These require that a property must have a value. For instance, a person must have a name, so the name attribute must not be empty.
  • Range constraints: These require that an attribute must have a value from the value space of the type that has been defined as its range. For instance, an integer attribute must not have the value “aaa”.
  • Interval constraints: These require that the value of a numeric attribute must be in a specific interval.
  • Pattern constraints: These require that a string attribute’s value must match a certain pattern defined by a regular expression.
  • Cardinality constraints: These apply to multi-valued properties only and require that the cardinality of a multi-valued property’s value set not be less than a given minimum cardinality, or not greater than a given maximum cardinality.
  • Uniqueness constraints: Also called key constraints, these require that a property’s value is unique among all instances of the given object type.
  • Referential integrity constraints: These require that the values of a reference property refer to an existing object in the range of the reference property.
  • Frozen value constraints: These require that the value of a property must not be changed after it has been assigned initially.

Constraints in UML

The visual language of UML class diagrams supports defining integrity constraints in two ways. They can be defined in a special way for special cases—like with predefined keywords—or in the general case, they can be defined with the help of invariants. Invariants are conditions expressed either in plain English or in the Object Constraint Language (OCL) and are shown in a special type of rectangle attached to the model element concerned. We use UML class diagrams for modeling constraints in design models that are independent of a specific programming language or technology platform.

UML class diagrams provide special support for expressing multiplicity constraints, also called cardinality constraints. This type of constraint allows specifying a lower multiplicity (minimum cardinality), an upper multiplicity (maximum cardinality), or both for a property or association end.

In UML, this takes the form of a multiplicity expression l…u, where the lower multiplicity l is a non-negative integer and the upper multiplicity u is either a positive integer the same or larger than l or the special value ∗ standing for unbounded.

To show property multiplicity constraints in a class diagram, multiplicity expressions are enclosed in brackets and appended to the property name.

Any systematic approach to constraint validation also requires defining a set of error—or “exception”—classes, including one for each of the standard property constraints listed above.

Get hands-on with 1200+ tech skills courses.