Constraints

This section describes the DDL commands pertaining to constraints.

Define a new abstract constraint.

[ WITH [ module-alias := ] MODULE module-name ]
CREATE ABSTRACT CONSTRAINT name [ ( [argspec] [, ...] ) ]
  [ ON ( subject-expr ) ]
  [ EXTENDING base [, ...] ]
"{" subcommand; [...] "}" ;

where argspec is:

  [ argname: ] argtype

where subcommand is one of

  USING constr-expression
  SET errmessage := error-message
  CREATE ANNOTATION annotation-name := value

CREATE ABSTRACT CONSTRAINT defines a new abstract constraint.

If name is qualified with a module name, then the constraint is created in that module, otherwise it is created in the current module. The constraint name must be distinct from that of any existing schema item in the module.

Most sub-commands and options of this command are identical to the SDL constraint declaration, with some additional features listed below:

[ module-alias := ] MODULE module-name

An optional list of module alias declarations to be used in the migration definition. When module-alias is not specified, module-name becomes the effective current module and is used to resolve all unqualified names.

SET errmessage := error_message

An optional string literal defining the error message template that is raised when the constraint is violated. Other than a slight syntactical difference this is the same as the corresponding SDL declaration.

CREATE ANNOTATION annotation-name := value;

Set constraint annotation-name to value.

See CREATE ANNOTATION for details.

Create an abstract constraint “uppercase” which checks if the subject is a string in upper case.

Copy
CREATE ABSTRACT CONSTRAINT uppercase {
    CREATE ANNOTATION title := "Upper case constraint";
    USING (str_upper(__subject__) = __subject__);
    SET errmessage := "{__subject__} is not in upper case";
};

Alter the definition of an abstract constraint.

[ WITH [ module-alias := ] MODULE module-name ]
ALTER ABSTRACT CONSTRAINT name
"{" subcommand; [...] "}" ;

where subcommand is one of

  RENAME TO newname
  USING constr-expression
  SET errmessage := error-message
  RESET errmessage
  CREATE ANNOTATION annotation-name := value
  ALTER ANNOTATION annotation-name := value
  DROP ANNOTATION annotation-name

ALTER ABSTRACT CONSTRAINT changes the definition of an abstract constraint item. name must be a name of an existing abstract constraint, optionally qualified with a module name.

[ module-alias := ] MODULE module-name

An optional list of module alias declarations to be used in the migration definition. When module-alias is not specified, module-name becomes the effective current module and is used to resolve all unqualified names.

name

The name (optionally module-qualified) of the constraint to alter.

The following subcommands are allowed in the ALTER ABSTRACT CONSTRAINT block:

RENAME TO newname

Change the name of the constraint to newname. All concrete constraints inheriting from this constraint are also renamed.

ALTER ANNOTATION annotation-name;

Alter constraint annotation-name. See ALTER ANNOTATION for details.

DROP ANNOTATION annotation-name;

Remove constraint annotation-name. See DROP ANNOTATION for details.

RESET errmessage;

Remove the error message from this abstract constraint. The error message specified in the base abstract constraint will be used instead.

All the subcommands allowed in a CREATE ABSTRACT CONSTRAINT block are also valid subcommands for an ALTER ABSTRACT CONSTRAINT block.

Rename the abstract constraint “uppercase” to “upper_case”:

Copy
ALTER ABSTRACT CONSTRAINT uppercase RENAME TO upper_case;

Remove an abstract constraint from the schema.

[ WITH [ module-alias := ] MODULE module-name ]
DROP ABSTRACT CONSTRAINT name ;

DROP ABSTRACT CONSTRAINT removes an existing abstract constraint item from the database schema. If any schema items depending on this constraint exist, the operation is refused.

[ module-alias := ] MODULE module-name

An optional list of module alias declarations to be used in the migration definition. When module-alias is not specified, module-name becomes the effective current module and is used to resolve all unqualified names.

name

The name (optionally module-qualified) of the constraint to remove.

Drop abstract constraint upper_case:

Copy
DROP ABSTRACT CONSTRAINT upper_case;

Define a concrete constraint on the specified schema item.

[ WITH [ module-alias := ] MODULE module-name ]
CREATE [ DELEGATED ] CONSTRAINT name
  [ ( [argspec] [, ...] ) ]
  [ ON ( subject-expr ) ]
"{" subcommand; [...] "}" ;

where argspec is:

  [ argname: ] argvalue

where subcommand is one of

  SET errmessage := error-message
  CREATE ANNOTATION annotation-name := value

CREATE CONSTRAINT defines a new concrete constraint. It can only be used in the context of CREATE SCALAR TYPE, ALTER SCALAR TYPE, CREATE PROPERTY, ALTER PROPERTY, CREATE LINK, or ALTER LINK.

name must be a name (optionally module-qualified) of previously defined abstract constraint.

Most sub-commands and options of this command are identical to the SDL constraint declaration, with some additional features listed below:

[ module-alias := ] MODULE module-name

An optional list of module alias declarations to be used in the migration definition. When module-alias is not specified, module-name becomes the effective current module and is used to resolve all unqualified names.

SET errmessage := error_message

An optional string literal defining the error message template that is raised when the constraint is violated. Other than a slight syntactical difference this is the same as the corresponding SDL declaration.

CREATE ANNOTATION annotation-name := value;

An optional list of annotations for the constraint. See CREATE ANNOTATION for details.

Create a “score” property on the “User” type with a minimum value constraint:

Copy
ALTER TYPE User CREATE PROPERTY score -> int64 {
    CREATE CONSTRAINT min_value(0)
};

Create a Vector with a maximum magnitude:

Copy
CREATE TYPE Vector {
    CREATE REQUIRED PROPERTY x -> float64;
    CREATE REQUIRED PROPERTY y -> float64;
    CREATE CONSTRAINT expression ON (
        __subject__.x^2 + __subject__.y^2 < 25
    );
}

Alter the definition of a concrete constraint on the specified schema item.

[ WITH [ module-alias := ] MODULE module-name [, ...] ]
ALTER CONSTRAINT name
  [ ( [argspec] [, ...] ) ]
  [ ON ( subject-expr ) ]
"{" subcommand; [ ... ] "}" ;

-- or --

[ WITH [ module-alias := ] MODULE module-name [, ...] ]
ALTER CONSTRAINT name
  [ ( [argspec] [, ...] ) ]
  [ ON ( subject-expr ) ]
  subcommand ;

where subcommand is one of:

  SET DELEGATED
  SET NOT DELEGATED
  SET errmessage := error-message
  RESET errmessage
  CREATE ANNOTATION annotation-name := value
  ALTER ANNOTATION annotation-name
  DROP ANNOTATION annotation-name

ALTER CONSTRAINT changes the definition of a concrete constraint. As for most ALTER commands, both single- and multi-command forms are supported.

[ module-alias := ] MODULE module-name

An optional list of module alias declarations to be used in the migration definition. When module-alias is not specified, module-name becomes the effective current module and is used to resolve all unqualified names.

name

The name (optionally module-qualified) of the concrete constraint that is being altered.

argspec

A list of constraint arguments as specified at the time of CREATE CONSTRAINT.

ON ( subject-expr )

A expression defining the subject of the constraint as specified at the time of CREATE CONSTRAINT.

The following subcommands are allowed in the ALTER CONSTRAINT block:

SET DELEGATED

Makes the constraint delegated.

SET NOT DELEGATED

Makes the constraint non-delegated.

RENAME TO newname

Change the name of the constraint to newname.

ALTER ANNOTATION annotation-name;

Alter constraint annotation-name. See ALTER ANNOTATION for details.

DROP ANNOTATION annotation-name;

Remove an annotation. See DROP ANNOTATION for details.

RESET errmessage;

Remove the error message from this constraint. The error message specified in the abstract constraint will be used instead.

All the subcommands allowed in the CREATE CONSTRAINT block are also valid subcommands for ALTER CONSTRAINT block.

Change the error message on the minimum value constraint on the property “score” of the “User” type:

Copy
ALTER TYPE User ALTER PROPERTY score
ALTER CONSTRAINT min_value(0)
SET errmessage := 'Score cannot be negative';

Remove a concrete constraint from the specified schema item.

[ WITH [ module-alias := ] MODULE module-name [, ...] ]
DROP CONSTRAINT name
  [ ( [argspec] [, ...] ) ]
  [ ON ( subject-expr ) ] ;

DROP CONSTRAINT removes the specified constraint from its containing schema item.

[ module-alias := ] MODULE module-name

An optional list of module alias declarations to be used in the migration definition. When module-alias is not specified, module-name becomes the effective current module and is used to resolve all unqualified names.

name

The name (optionally module-qualified) of the concrete constraint to remove.

argspec

A list of constraint arguments as specified at the time of CREATE CONSTRAINT.

ON ( subject-expr )

A expression defining the subject of the constraint as specified at the time of CREATE CONSTRAINT.

Remove constraint “min_value” from the property “score” of the “User” type:

Copy
ALTER TYPE User ALTER PROPERTY score
DROP CONSTRAINT min_value(0);
Light
Dark
System