Overview
Constraints enforce rules for data that can go into a graph. Any data modification that violates a constraint will result in an error.
The following constraints are supported:
Showing Constraints
To retrieve all constraints created on node properties in the current graph:
SHOW NODE CONSTRAINT
To retrieve all constraints created on edge properties in the current graph:
SHOW EDGE CONSTRAINT
The information about constraints is organized into the _nodeConstraint
or _edgeConstraint
table, including the following fields:
Field |
Description |
---|---|
name |
Constraint name. |
type |
Constraint type. |
schema |
The node or edge schemas where the constraint applies. |
properties |
The node or edge properties where the constraint applies. |
status |
Constraint status, which can be DONE or CREATING . |
Creating Constraints
A constraint is created with the ALTER NODE
or ALTER EDGE
statement. The constraint creation is executed as a job, you may run SHOW JOB <id?>
afterward to verify the success of the creation.
Creating a constraint can take some time, as the system needs to scan all existing data for compliance with the constraint. The creation process will fail if any existing data does not meet the constraint. Additionally, to ensure data consistency, other data modifications are put on hold during the constraint creation process.
EDGE KEY
An EDGE KEY
constraint designates either a single edge property or a combination of edge properties as the unique identifier for all edges in the graph. When multiple properties are specified, it is also referred to as a composite EDGE KEY
.
Each property in the EDGE KEY
must be present in every edge schema and use a consistent value type. For a single-property EDGE KEY
, that property must contain unique, non-null values across all edges. For a composite EDGE KEY
, each property must be non-null, and the combination of their values must be unique across all edges.
Details
- A graph can have only one
EDGE KEY
- either single-property or composite. - Properties of the
list
type cannot be used in theEDGE KEY
. - Properties involved in the
EDGE KEY
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.
To create an EDGE KEY
constraint on the edge property eID
. This constraint can only be successfully created if the following conditions are met:
- All edge schemas have a property named
eID
, and it must be of the typeint32
. - The
eID
property must not contain duplicate values in each shard ornull
values.
ALTER EDGE * ADD CONSTRAINT EDGE KEY ON eID INT32
Default Property Value Type
When the edge property value type is not specified, it defaults to string
:
ALTER EDGE * ADD CONSTRAINT EDGE KEY ON tag
In this case, all edge schemas must have a tag
property of the type string
.
Composite EDGE KEY Constraint
A composite EDGE KEY
constraint involves multiple edge properties. The requirements remain the same as those for a single-property EDGE KEY
, except that uniqueness is enforced based on the combined values of the specified properties.
To create a composite EDGE KEY
constraint on the edge properties eID
and tag
:
ALTER EDGE * ADD CONSTRAINT EDGE KEY ON eID INT32, tag STRING
NOT NULL
The NOT NULL
constraint enforces a property cannot have null
values, ensuring that a value is always provided. Note that a NOT NULL
constraint can be defined only on a single property of a single schema.
To create a NOT NULL
constraint on the property name
of the user
nodes:
ALTER NODE user ADD CONSTRAINT NOT NULL ON name
To create a NOT NULL
constraint on the property weight
of the link
edges:
ALTER EDGE link ADD CONSTRAINT NOT NULL ON weight
These constraints can only be successfully created when there is no null
values exist in the specified property.
Using IF NOT EXISTS
The IF NOT EXISTS
clause is used to prevent errors when attempting to create a constraint that already exists. It allows the statement to be safely executed.
ALTER NODE user ADD CONSTRAINT IF NOT EXISTS NOT NULL ON name
This creates the constraint only if there is no existing NOT NULL
constraint on the name
property of user
nodes. If such a constraint already exists, the statement is ignored without throwing an error.
Dropping Constraints
A constraint can be dropped with the ALTER NODE
or ALTER EDGE
statement.
To drop the EDGE KEY
constraint from the current graph:
ALTER EDGE * DROP EDGE KEY
To drop the NOT NULL
constraint on the name
property of user
nodes:
ALTER NODE user DROP CONSTRAINT NOT NULL ON name
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.