UltipaDocs
Try Playground
  • Introduction
  • Terminologies
    • Graphset
    • Schema
    • Property
    • Constraints
    • Insert
    • Overwrite or Insert
    • Upsert
    • Update
    • Delete
    • Find Nodes
    • Find Edges
      • AB
      • Autonet
      • Spread
      • Path Template
      • K-Hop
      • K-Hop Template
    • GROUP BY
    • ORDER BY
    • SKIP
    • LIMIT
    • WHERE
    • RETURN
    • WITH
    • UNCOLLECT
    • UNION
    • UNION ALL
    • CALL
    • All Functions
    • Path Functions
    • Aggregate Functions
    • Mathematical Functions
    • Trigonometric Functions
    • String Functions
    • List Functions
    • Datetime Functions
    • Spatial Functions
    • Table Functions
    • Null Functions
    • Type Conversion Functions
  • Operators
  • Expressions
    • Index
    • Full-text Index
    • Vector Index
    • Cache
    • Overview
    • Managing HDC Graphs
    • HDC Graph Queries
    • Process
    • Job
    • Execution Plan
    • Alias
    • Filter
    • Values and Types
    • Data Flow in Queries
    • Comments
    • Reserved Words
  • Access Control
  1. Docs
  2. /
  3. UQL
  4. /
  5. Graph Management

Constraints

Overview

Constraints enforce rules on the property values of nodes and edges in the graph. Any attempt to insert or update data that violates these rules will result in an error.

The following constraints are supported:

  • NOT NULL
  • EDGE KEY

Showing Constraints

To retrieve information about constraints created in the current graph:

UQL
// Shows all constraints
show().constraint()

// Shows all constraints created on node properties
show().node_constraint()

// Shows all constraints created on edge properties
show().edge_constraint()

The information about constraints is organized into the _nodeConstraint or _edgeConstraint table. Each table includes fields that provide essential details about each constraint:

Field
Description
nameConstraint name.
typeConstraint type.
schemaThe node or edge schemas where the constraint applies.
propertiesThe node or edge properties where the constraint applies.
statusConstraint status, which can be DONE or CREATING.

Creating Constraints

A constraint is created with the CREATE CONSTRAINT statement. Constraint creation is executed as a job; you may then run show().job() to verify its success.

Note that creating a constraint in a large, active graph may take time, as the system must scan all existing data to ensure compliance. The creation will fail if any existing data violates the constraint. To maintain data consistency, all other data modification operations are temporarily suspended during the constraint creation process.

NOT NULL

The NOT NULL constraint enforces that a property cannot have null values, guaranteeing that a value is always provided.

Details

  • A NOT NULL constraint must be created for a single specified schema.
  • Only one property can be designated per NOT NULL constraint.

To create a NOT NULL constraint named nn_1 on the property name of the user nodes:

UQL
CREATE CONSTRAINT nn_1
FOR (u:user) REQUIRE u.name IS NOT NULL

To create a NOT NULL constraint named nn_2 on the property weight of the link edges:

UQL
CREATE CONSTRAINT nn_2
FOR ()-[e:link]-() REQUIRE e.weight IS NOT NULL

These constraints can only be successfully created when no null values exist in the specified property.

EDGE KEY

An EDGE KEY constraint designates one or multiple edge properties as the unique identifier for all edges in the graph, ensuring that these properties are both non-null and unique. When multiple properties are specified, it is also referred to as a composite EDGE KEY.

Details

  • Only one EDGE KEY can be defined per graph - either a single-property EDGE KEY or a composite EDGE KEY.
  • EDGE KEY doesn't apply to properties of the type list.
  • EDGE KEY properties are automatically cached to accelerate query performance.
  • When the EDGE KEY is created, uniqueness is enforced within each shard. Duplicates may exist across shards at creation time, but all subsequent data modifications must comply with global uniqueness.

Single-Property EDGE KEY

To create a single-property EDGE KEY constraint named eUID:

UQL
CREATE CONSTRAINT eUID
FOR ()-[e]-() REQUIRE e.createdOn IS EDGE KEY
OPTIONS {
  type: {createdOn: "datetime"}
}

To successfully create the EDGE KEY:

  • All edge schemas in the graph must possess an createdOn property of type datetime.
  • createdOn doesn't contain existing null or duplicated values.

When the property value type is not specified, it defaults to string:

UQL
CREATE CONSTRAINT eUID
FOR ()-[e]-() REQUIRE e.createdOn IS EDGE KEY

In this case, all edge schemas must have a createdOn property of type string.

Composite EDGE KEY

To create a composite EDGE KEY constraint named eUIDs:

UQL
CREATE CONSTRAINT eUIDs
FOR ()-[e]-() REQUIRE (e.createdOn, e.weight) IS EDGE KEY
OPTIONS {
  type: {createdOn: "datetime", weight: "float"}
}

To successfully create the EDGE KEY:

  • All edge schemas in the graph must possess an createdOn property of type datetime and a weight property of type float.
  • Neither createdOn nor weight may contain existing null values.
  • The combination of createdOn and weight must not contain any duplicated values.

Using IF NOT EXISTS

Constraint names in a graph must be unique. If you attempt to create a constraint with a name that already exists, the creation will fail, and an error message will indicate the duplication.

If the IF NOT EXISTS flag is used, the job completes with a FINISHED status when a duplicate name is detected. No error message will be returned, and no new constraint is created.

To create a NOT NULL constraint named nn_1. If the constraint name already exists, skip the creation without returning an error message:

UQL
CREATE CONSTRAINT nn_1
FOR (u:user) REQUIRE u.age IS NOT NULL

Naming Conventions

Constraint name must:

  • Contain 2 to 64 characters.
  • Begin with a letter.
  • Allowed characters: letters (A-Z, a-z), numbers (0-9) and underscores (_).

Constraint names must be unique.

Dropping Constraints

A constraint can be dropped with the DROP CONSTRAINT statement.

To drop a constraint named nn_1:

UQL
DROP CONSTRAINT nn_1

Using IF EXISTS

If the specified constraint name does not exist, deleting the constraint fails and returns an error message.

With the IF EXISTS flag, no error message will be returned when the specified constraint name is not found, and no constraint is deleted.

To drop a constraint named nn. If the constraint name does not exist, skip the deletion without returning an error message:

UQL
DROP CONSTRAINT nn_1 IF EXISTS

Restrictions on Properties with Constraints

Renaming Properties

Properties with the NOT NULL constraints can be renamed. However, renaming properties with an EDGE KEY constraint is not allowed.

Dropping Properties

A property with a constraint cannot be dropped until all the related constraints are deleted.