Overview
UQL constraints enforces rules for data that can go into the current graphset. If there is any violation between the constraint and the data modification action, the action is aborted.
The following constraints are used in UQL:
- EDGE KEY: Ensures the existence of properties and the uniqueness of their values.
- NOT NULL: Ensures that a property cannot have a
null
value.
Creating Constraints
A constraint is created for one or multiple properties with the CREATE CONSTRAINT
statement. The constraint creation is executed as a job, you may run show().job(<id?>)
afterward to verify the success of the creation.
CREATE CONSTRAINT <constraintName> <ifNotExistsFlag?>
FOR <aliasDecl>
REQUIRE <alias>.<property> <constraintType>
OPTIONS {
<item?>: {<key?>: <value?>}
}
Details
<constraintName>
is the unique name provided for the constraint. The creation will fail if a constraint with the same name already exists.- With the optional
<ifNotExistsFlag?>
, i.e.,IF NOT EXISTS
, if the specified<constraintName>
already exists, or if a constraint already exists with a type that allows only one constraint, no creation will occur, and the operation will complete without throwing errors. - The optional
OPTIONS
clause can be used to provide additional configurations.
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.
Once a constraint is applied to a property, any data modification that violates the constraint will result in an error.
EDGE KEY
The EDGE KEY
constraint applies exclusively to edge properties. It ensures that the specified property:
- Exists for all edge schemas.
- Has unique values across the graph.
- Does not allow
null
values.
For a composite EDGE KEY
constraint involving multiple properties, the combination of their values must be unique.
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 applied must adhere to global uniqueness.
Only one EDGE KEY
constraint can be created in a graphset and it applies to all edge schemas. The EDGE KEY
constraint establishes a mapping between the specified properties and _uuid
of edges. Meanwhile, the edge key properties are loaded to caches for query acceleration.
// Creates a single-property edge key constraint
CREATE CONSTRAINT <constraintName> <ifNotExistsFlag?>
FOR ()-[<alias>]-()
REQUIRE <alias>.<property> IS EDGE KEY
OPTIONS {
type: {<property?>: <type?>}
}
// Creates a composite edge key constraint
CREATE CONSTRAINT <constraintName> <ifNotExistsFlag?>
FOR ()-[<alias>]-()
REQUIRE (<alias>.<property1>, <alias>.<property2>, ...) IS EDGE KEY
OPTIONS {
type: {<property1?>: <type1?>, <property2?>: <type2?>, ...}
}
Details
- The
OPTIONS
clause defines the desired type for all specified properties, which must match their corresponding types in the database. If theOPTIONS
clause is omitted, all edge key properties default to thestring
type. - The
EDGE KEY
constraint does not support the following property types:decimal
,list
, andset
.
To create an EDGE KEY
constraint for edge properties named notes
while the type of each notes
property must be string
:
CREATE CONSTRAINT edge_key
FOR ()-[e]-() REQUIRE e.notes IS EDGE KEY
To create an EDGE KEY
constraint for edge properties named score
while the type of each score
property must be float
:
CREATE CONSTRAINT edge_key
FOR ()-[e]-() REQUIRE e.score IS EDGE KEY
OPTIONS {
type: {score: "float"}
}
To create an EDGE KEY
constraint for edge properties named notes
and score
while the types of each notes
andscore
properties must be string
and float
respectively:
CREATE CONSTRAINT edge_key
FOR ()-[e]-() REQUIRE (e.notes, e.score) IS EDGE KEY
OPTIONS {
type: {notes: "string", score: "float"}
}
To create an EDGE KEY
constraint only if no EDGE KEY
constraint currently exists, preventing an error if it does:
CREATE CONSTRAINT edge_key IF NOT EXIST
FOR ()-[e]-() REQUIRE e.notes IS EDGE KEY
NOT NULL
By default, a property can hold null
values. The NOT NULL
constraint enforces a property to NOT accept null
values, ensuring that a value is always provided. Only one property can be specified with each NOT NULL
constraint.
// Creates a not null constraint for a node property
CREATE CONSTRAINT <constraintName> <ifNotExistsFlag?>
FOR (<alias>:<schema>)
REQUIRE <alias>.<property> IS NOT NULL
// Creates a not null constraint for an edge property
CREATE CONSTRAINT <constraintName> <ifNotExistsFlag?>
FOR ()-[<alias>:<schema>]-()
REQUIRE <alias>.<property> IS NOT NULL
To create a NOT NULL
constraint for the node property @user.name
:
CREATE CONSTRAINT not_null_user_name
FOR (u:user) REQUIRE u.name IS NOT NULL
To create a NOT NULL
constraint for the edge property @likes.weight
named not_null_weight
only if this name does not exist, preventing an error if it does exist:
CREATE CONSTRAINT not_null_weight IF NOT EXIST
FOR ()-[e:likes]-() REQUIRE e.weight IS NOT NULL
Showing Constriants
To retrieves information about all constraints created in the current graphset:
// Shows all constraints
show().constraint()
// Shows all constraints created for node properties
show().node_constraint()
// Shows all constraints created for edge properties
show().edge_constraint()
The information about constraints is organized into _nodeConstraint
and _edgeConstraint
tables. Each table includes fields that provide essential details about each constraint:
Field |
Description |
---|---|
name |
The name of the constraint. |
type |
The type of the constraint. |
schema |
The node or edge schemas where the constraint applies. |
properties |
The node edge properties where the constraint applies. |
status |
The current state of the constraint, which can be DONE or CREATING . |
Dropping Constraints
A constraint can be dropped with the DROP CONSTRAINT
statement.
DROP CONSTRAINT <constraintName> <ifExistsFlag?>
Details
<constraintName>
is the constraint name. The constraint deletion will fail if the specified name does not exist.- With the optional
<ifExistsFlag?>
, i.e.,IF EXISTS
flag, if the specified<constraintName>
is not found, no deletion will occur, and the operation will complete without throwing errors.
To drop a constraint named not_null_user_name
:
DROP CONSTRAINT not_null_user_name
To drop a constraint named user_name
only if it exists, preventing an error if it doesn't exist:
DROP CONSTRAINT not_null_user_name IF EXISTS
Other Restrictions
Renaming Properties
Properties with applied NOT NULL
constraints can be renamed. However, renaming properties with an EDGE KEY
constraint is not allowed.
Dropping Properties
A property with any applied constraints cannot be dropped until all the related constraints are removed.