Constraint

from Wikipedia, the free encyclopedia

With Constraints (German "restriction") are in various programming languages Conditions set forth a mandatory value of variables must be met so that the value can be accepted into the system. Constraints are widely used in database systems in order to restrict the range of values ( domain ) of a scalar and to check values ​​for their admissibility. In program analysis, constraints are used when analyzing functional programs.

Constraints in programming languages

Constraints help the compiler and the runtime environment , programming errors easier to spot. Let us assume a variable Anzahl_der_Personen_im_Aufzug, here it is clearly clear that this variable must not have a negative value and must not be above a maximum value.

In the Ada programming language it can look like this:

type Kapazitaet is range 0..13;
Anzahl_der_Personen_im_Aufzug : Kapazitaet := 15;

Here the compiler can immediately recognize that the value 15 is outside the previously specified condition range. If the variable were not restricted, the programmer would have to check this condition himself with every change.

Constraints in database systems

In principle , constraints work in the same way in database systems . Constraints define conditions that must be met when inserting, changing and deleting data records in the database.

In addition to constraints , many database systems also know transactions . The combination of these two concepts is very powerful, since all constraints are checked at the end of a transaction at the latest . If a constraint fails, any changes to the data made in that transaction are rolled back as if they had never been made.

Constraints are defined in database systems by integrity conditions.

List of possible constraints in database systems:

  • NOT NULL→ the scalar cannot NULLbe
  • PRIMARY KEY→ the scalar must be unique and cannot NULLbe
  • FOREIGN KEY→ the scalar must be checked for referential integrity
  • UNIQUE → the scalar must be unique within the attribute
  • CHECK()→ explicit verification instruction to the DBMS ; What needs to be checked for is defined as an option of this directive

There are also different types of constraints :

  • Attribute constraints refer to a single column
  • Relation constraints , refer to several attributes (columns)
  • Named constraints can be manipulated based on the name
  • Unnamed constraints are given a system-generated name

Example: Constraint attribute

In the example below, it is PRIMARY KEYan unnamed constraint and CONSTRAINT persons_fpa named constraint .

CREATE TABLE person(
    id          PRIMARY KEY,
    fingerprint BYTEA CONSTRAINT persons_fp UNIQUE
);

Example: Relations Constraint

In the example below, it is CONSTRAINT person_primea named constraint and UNIQUE()an unnamed constraint .

CREATE TABLE person(
    id      SERIAL,
    name    VARCHAR,
    dob     DATE,
    born_in VARCHAR,
    CONSTRAINT person_prime PRIMARY KEY(id),
    UNIQUE(name, dob, born_in)
);

Constraints in logistics

In this case too, a constraint is used as a restriction for an action taken . A distinction is made between requirements or requirements and constraints. For the supply of goods and services in logistics , the constraints z. For example, the restrictions that apply to payloads in terms of volume or weight for certain trucks .

Constraints in Evolution

In development, constraints restrict evolutionary paths, for example due to historical circumstances in the construction plan.

Constraints in theoretical computer science

In theoretical computer science and especially in AI research , relational statements that contain free variables are often referred to as constraints . Then a variable assignment or interpretation is sought that fits a given set of constraints , thus fulfilling them simultaneously.

An example: xis really above y, yis really above z, zis above x(this constraint set cannot be fulfilled).

An assignment that fulfills all constraints is often referred to as a " model ".

Constraints in and-or trees

In And-Or trees there are constraints at the And nodes, here several subgoals must be fulfilled conjunctively in order to fulfill the main goal.

See also

Web links

Individual evidence

  1. Chapter 5. Data Definition - 5.4. Constraints. (No longer available online.) In: PostgreSQL: The Official Handbook. PostgreSQL Global Development Group, archived from the original on October 27, 2011 ; accessed on November 3, 2011 (English: http://www.postgresql.org/docs/current/static/ddl-constraints.html ). Info: The archive link was inserted automatically and has not yet been checked. Please check the original and archive link according to the instructions and then remove this notice. @1@ 2Template: Webachiv / IABot / www.postgresql.org