Overview
Constraints enforce additional rules on the node and edge properties in the graph. Any attempt to insert or update data that violates these rules will result in an error.
Ultipa supports the following constraints in typed graphs:
- NOT NULL: Ensures that a property never contains
nullvalues. - UNIQUE: Ensures that a property contains no duplicate values.
- EDGE KEY: Specifies a property as the unique identifier for all edges in the graph.
Showing Constraints
To show node constraints in the current graph:
SHOW NODE CONSTRAINT
To show edge constraints in the current graph:
SHOW EDGE CONSTRAINT
The plural form SHOW NODE|EDGE CONSTRAINTS is also supported.
Each constraint provides the following essential metadata:
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, creating a graph type, or within an existing graph. When creating constraints in an existing graph, it execute as a job, you may run SHOW JOB <id?> afterward to verify its success.
Note that creating a constraint in a large 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 ensures that a property never contains null values.
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
The NOT NULL constraint can only be successfully created when there is no null values exist in the specified property.
You can apply the NOT NULL constraint to any property when creating a typed graph:
CREATE GRAPH g1 {
NODE User ({name STRING NOT NULL, age UINT32}),
EDGE Follows ()-[{createdOn LOCAL DATETIME NOT NULL}]->()
}
You can also apply the NOT NULL constraint to any property when creating a graph type:
CREATE GRAPH TYPE gType {
NODE User ({name STRING NOT NULL, age UINT32}),
EDGE Follows ()-[{createdOn LOCAL DATETIME NOT NULL}]->()
}
UNIQUE
The UNIQUE constraint ensures that a property contains no duplicate values. A UNIQUE constraint can be defined on either a single property or multiple properties.
Single-Property UNIQUE
To create a UNIQUE constraint on the property name of the User nodes:
ALTER NODE User ADD CONSTRAINT UNIQUE ON name
To create a UNIQUE constraint on the property weight of the link edges:
ALTER EDGE link ADD CONSTRAINT UNIQUE ON weight
The UNIQUE constraint can only be successfully created when there is no duplicated values exist in the specified property.
Composite UNIQUE
To create a composite UNIQUE constraint on the properties name and uid of the User nodes:
ALTER NODE User ADD CONSTRAINT UNIQUE ON name, uid
To create a composite UNIQUE constraint on the properties weight and eid of the link edges:
ALTER EDGE link ADD CONSTRAINT UNIQUE ON weight, eid
The UNIQUE constraint can be created successfully only when the combined values of all specified properties contain no duplicates.
UNIQUE During Graph Creation
You can apply the UNIQUE constraint to any property when creating a typed graph:
CREATE GRAPH g1 {
NODE User ({name STRING UNIQUE, age UINT32}),
EDGE Follows ()-[{createdOn LOCAL DATETIME UNIQUE}]->()
}
UNIQUE During Graph Type Creation
You can also apply the UNIQUE constraint to any property when creating a graph type:
CREATE GRAPH TYPE gType {
NODE User ({name STRING UNIQUE, age UINT32}),
EDGE Follows ()-[{createdOn LOCAL DATETIME UNIQUE}]->()
}
EDGE KEY
The EDGE KEY constraint specifies a property as the unique identifier for all edges in the graph, ensuring that its values are both non-null and unique. An EDGE KEY constraint can be defined on either a single property or multiple properties.
Details
- Only one
EDGE KEYconstraint can be defined per graph, either a single-propertyEDGE KEYor a compositeEDGE KEY. EDGE KEYdoesn't apply to properties of the typeLIST.EDGE KEYproperties are automatically cached to accelerate query performance.- When the
EDGE KEYis 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 specify the edge property eID as EDGE KEY:
ALTER EDGE * ADD CONSTRAINT EDGE KEY ON eID INT32
To successfully create the EDGE KEY:
- All edges must possess an
eIDproperty of typeINT32. - The
eIDproperty doesn’t contain existingnullor 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 edges must have a tag property of type STRING.
Composite EDGE KEY
To specify the edge properties eID and tag as EDGE KEY:
ALTER EDGE * ADD CONSTRAINT EDGE KEY ON eID INT32, tag STRING
To successfully create the EDGE KEY:
- All edges must possess an
eIDproperty of typeINT32and atagproperty of typeSTRING. - Neither the property
eIDnortagmay contain existingnullvalues. - The combination of the values of
eIDandtagmust not contain any duplicated values.
EDGE KEY During Graph Creation
You can apply the EDGE KEY constraint when creating a typed graph:
CREATE GRAPH g1 {
NODE User ({name STRING , age UINT32}),
NODE Club ({name STRING}),
EDGE Follows ()-[{createdOn LOCAL DATETIME}]->(),
EDGE Joins ()-[]->()
}
EDGE KEY eID INT64, tag STRING
The specified EDGE KEY properties eID and tag will be automatically created for all edge schemas.
EDGE KEY During Graph Type Creation
You can apply the EDGE KEY constraint when creating a graph type:
CREATE GRAPH TYPE gType {
NODE User ({name STRING, age UINT32}),
NODE Club ({name STRING}),
EDGE Follows ()-[{createdOn LOCAL DATETIME}]->(),
EDGE Joins ()-[]->()
}
EDGE KEY eID INT64
The specified EDGE KEY property eID will be automatically created for all edge schemas when this graph type is used.
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.
Naming Convetions
Constraint names must be unique. Each constraint name must:
- Contain 2 to 64 characters.
- Begin with a letter.
- Allowed characters: letters (A-Z, a-z), numbers (0-9) and underscores (
_).
Dropping Constraints
To drop the NOT NULL constraint on the name property of User nodes from the current graph:
ALTER NODE User DROP CONSTRAINT NOT NULL ON name
To drop the UNIQUE constraint on the name property of User nodes from the current graph:
ALTER NODE User DROP CONSTRAINT UNIQUE ON name
To drop the EDGE KEY constraint from the current graph:
ALTER EDGE * DROP EDGE KEY
Restrictions on Properties with Constraints
Renaming Properties
Properties with the NOT NULL or UNIQUE 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.