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:
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
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
The EDGE KEY
constraint specifies an edge property effectively as the unique identifier for edges. Thus, the specified property must be present in all edge schemas with a consistent value type and must contain unique, non-null
values across the graph.
Details
- A graph can have only one
EDGE KEY
constraint. - The
EDGE KEY
constraint cannot be applied to an edge property of thelist
type. - The edge property involved in the
EDGE KEY
constraint is automatically cached to accelerate query performance. - The
*
must be used to apply theEDGE KEY
constraint across all edge schemas. - The
EDGE KEY
constraint creation enforces uniqueness among property values within each shard, allowing duplicates across different shards. However, any data modifications made after theEDGE KEY
constraint is created must adhere to global uniqueness.
To create an EDGE KEY
constraint named eUID
on the edge property createdOn
:
ALTER EDGE * ADD CONSTRAINT eUID EDGE KEY (createdOn)
OPTIONS {
type: {createdOn: "datetime"}
}
This constraint can only be successfully created if the following conditions are met:
- All edge schemas have a property named
createdOn
, and it must be of the typedatetime
. - The
createdOn
property must not contain duplicate values in each shard ornull
values.
Default Property Value Type
When the property value type is not specified, it defaults to string
:
ALTER EDGE * ADD CONSTRAINT eUID EDGE KEY (createdOn)
In this case, all edge schemas must have a createdOn
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 named eUIDs
on the edge properties createdOn
and weight
:
ALTER EDGE * ADD CONSTRAINT eUIDs EDGE KEY (createdOn, weight)
OPTIONS {
type: {createdOn: "datetime", weight: "float"}
}
NOT NULL
The NOT NULL
constraint enforces a property cannot have null
values, ensuring 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:
ALTER NODE user ADD CONSTRAINT nn_1 NOT NULL (name)
To create a NOT NULL
constraint named nn_2
on the property weight
of the link
edges:
ALTER EDGE link ADD CONSTRAINT nn_2 NOT NULL (weight)
These constraints can only be successfully created when there is no null
values exist in the specified property.
Using the IF NOT EXISTS Flag
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.
With the IF NOT EXISTS
flag, the job will complete 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:
ALTER NODE user ADD CONSTRAINT IF NOT EXISTS nn_1 NOT NULL (age)
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. Each table provides essential details about each constraint:
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 . |
Dropping Constraints
A constraint can be dropped with the ALTER NODE
or ALTER EDGE
statement.
To drop an EDGE KEY
constraint named eUID
from all edge schemas:
ALTER EDGE * DROP CONSTRAINT eUID
To drop a NOT NULL
constraint named nn_1
from the node schema user
:
ALTER NODE user DROP CONSTRAINT nn_1
Using the IF EXISTS Flag
The constraint deletion will fail and output an error message if the specified constraint name does not exist.
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
from the node schema user
. If the constraint name does not exist, skip the deletion without returning an error message:
ALTER NODE user DROP CONSTRAINT nn 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.