Constraints

This section describes the DDL commands pertaining to constraints.

Create abstract constraint

Define a new abstract constraint.

  1. [ with [ module-alias := ] module module-name ]
  2. create abstract constraint name [ ( [argspec] [, ...] ) ]
  3. [ on ( subject-expr ) ]
  4. [ extending base [, ...] ]
  5. "{" subcommand; [...] "}" ;
  6. where argspec is:
  7. [ argname: ] argtype
  8. where subcommand is one of
  9. using constr-expression
  10. set errmessage := error-message
  11. create annotation annotation-name := value

Description

The command 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.

Parameters

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.

Example

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

  1. create abstract constraint uppercase {
  2. create annotation title := "Upper case constraint";
  3. using (str_upper(__subject__) = __subject__);
  4. set errmessage := "{__subject__} is not in upper case";
  5. };

Alter abstract constraint

Alter the definition of an abstract constraint.

  1. [ with [ module-alias := ] module module-name ]
  2. alter abstract constraint name
  3. "{" subcommand; [...] "}" ;
  4. where subcommand is one of
  5. rename to newname
  6. using constr-expression
  7. set errmessage := error-message
  8. reset errmessage
  9. create annotation annotation-name := value
  10. alter annotation annotation-name := value
  11. drop annotation annotation-name

Description

The command 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.

Parameters

[ 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.

Example

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

  1. alter abstract constraint uppercase rename to upper_case;

Drop abstract constraint

Remove an abstract constraint from the schema.

  1. [ with [ module-alias := ] module module-name ]
  2. drop abstract constraint name ;

Description

The command 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.

Parameters

[ 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.

Example

Drop abstract constraint upper_case:

  1. drop abstract constraint upper_case;

Create constraint

Define a concrete constraint on the specified schema item.

  1. [ with [ module-alias := ] module module-name ]
  2. create [ delegated ] constraint name
  3. [ ( [argspec] [, ...] ) ]
  4. [ on ( subject-expr ) ]
  5. [ except ( except-expr ) ]
  6. "{" subcommand; [...] "}" ;
  7. where argspec is:
  8. [ argname: ] argvalue
  9. where subcommand is one of
  10. set errmessage := error-message
  11. create annotation annotation-name := value

Description

The command 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.

Parameters

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.

Example

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

  1. alter type User create property score -> int64 {
  2. create constraint min_value(0)
  3. };

Create a Vector with a maximum magnitude:

  1. create type Vector {
  2. create required property x -> float64;
  3. create required property y -> float64;
  4. create constraint expression ON (
  5. __subject__.x^2 + __subject__.y^2 < 25
  6. );
  7. }

Alter constraint

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

  1. [ with [ module-alias := ] module module-name [, ...] ]
  2. alter constraint name
  3. [ ( [argspec] [, ...] ) ]
  4. [ on ( subject-expr ) ]
  5. [ except ( except-expr ) ]
  6. "{" subcommand; [ ... ] "}" ;
  7. -- or --
  8. [ with [ module-alias := ] module module-name [, ...] ]
  9. alter constraint name
  10. [ ( [argspec] [, ...] ) ]
  11. [ on ( subject-expr ) ]
  12. subcommand ;
  13. where subcommand is one of:
  14. set delegated
  15. set not delegated
  16. set errmessage := error-message
  17. reset errmessage
  18. create annotation annotation-name := value
  19. alter annotation annotation-name
  20. drop annotation annotation-name

Description

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

Parameters

[ 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.

Example

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

  1. alter type User alter property score
  2. alter constraint min_value(0)
  3. set errmessage := 'Score cannot be negative';

Drop constraint

Remove a concrete constraint from the specified schema item.

  1. [ with [ module-alias := ] module module-name [, ...] ]
  2. drop constraint name
  3. [ ( [argspec] [, ...] ) ]
  4. [ on ( subject-expr ) ]
  5. [ except ( except-expr ) ] ;

Description

The command drop constraint removes the specified constraint from its containing schema item.

Parameters

[ 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.

Example

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

  1. alter type User alter property score
  2. drop constraint min_value(0);

See also

Schema > Constraints

SDL > Constraints

Introspection > Constraints

Standard Library > Constraints