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:
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
You can define constraints when creating a graph, defining a graph type, or within an existing graph. The examples below demonstrate how to create constraints in an existing graph using the ALTER NODE
or ALTER EDGE
statements. In these cases, constraint creations execute as a job, you may run SHOW JOB <id?>
afterward to verify its success. To learn how to include constraints during graph or graph type creation, see Graph.
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 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.
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-propertyEDGE KEY
or a compositeEDGE KEY
. EDGE KEY
doesn't apply to properties of the typeLIST
.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
ALTER EDGE * ADD CONSTRAINT EDGE KEY ON eID INT32
To successfully create the EDGE KEY
:
- All edge schemas in the graph must possess an
eID
property of typeINT32
. eID
doesn’t contain existingnull
or duplicated values.
When the 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 type STRING
.
Composite EDGE KEY
ALTER EDGE * ADD CONSTRAINT EDGE KEY ON eID INT32, tag STRING
To successfully create the EDGE KEY
:
- All edge schemas in the graph must possess an
eID
property of typeINT32
and atag
property of typeSTRING
. - Neither
eID
nortag
may contain existingnull
values. - The combination of
eID
andtag
must not contain any duplicated values.
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.