Existence constraints for relationship properties
You may want to ensure that some relationship types always have a particular property. For example, a RATED relationship must have a property, rating.
Enterprise Edition Only
Existence constraints are only available in Enterprise Edition.
Syntax for creating an existence constraint for a relationship
Here is the syntax for creating an existence constraint on a relationship property:
CREATE CONSTRAINT <constraint_name> IF NOT EXISTS
FOR ()-[x:<RELATIONSHIP_TYPE>]-()
REQUIRE x.<property_key> IS NOT NULL
You specify the name of the constraint, the relationship type it will be associated with, and the name of the property that must exist for relationships of that type.
-
If a constraint already exists in the graph with the same name, no constraint is created.
-
If a constraint does not exist in the graph with the same name:
-
No constraint is created if there already is an existence constraint for that relationship type and property key.
-
Otherwise, the constraint is created.
-
Creating the existence constraint
Suppose we want to enforce that all RATED relationships must have a value for the rating property. Execute this code to create an existence constraint for this property:
CREATE CONSTRAINT RATED_rating_exists IF NOT EXISTS
FOR ()-[x:RATED]-()
REQUIRE x.rating IS NOT NULL
This code creates the existence constraint named RATED_rating_exists.
When to create a constraint
You typically create constraints before the data is loaded into the graph, but in our case, we already have the data in the graph.Constraint creation failure
Not all people in the graph have an ACTED_IN relationship with a role property set. Execute this code. The creation of this constraint should fail:
CREATE CONSTRAINT ACTED_IN_role_exists IF NOT EXISTS
FOR ()-[x:ACTED_IN]-()
REQUIRE x.role IS NOT NULL
Property update failure
Suppose we have created the RATED_rating_exists constraint in the graph.
If we attempt to remove all rated properties from existing relationships, an error will occur. Execute this code:
MATCH ()-[x:RATED]->()
SET x.rating = null
Check your understanding
1. Creating an existence constraint on a relationship property
Suppose we have a graph that contains Company and Person nodes.
Some Person nodes have the relationship, :WORKS_AT
a Company node.
We want to ensure that the :WORKS_AT
relationships in the graph always have a property with key, .since
.
Before we load the Company data, we want to create an existence constraint for :WORKS_AT
relationships we will be creating.
What is the correct statement to create this constraint?
-
✓
CREATE CONSTRAINT WORKS_AT_since_exists IF NOT EXISTS FOR ()-[x:WORKS_AT]-() REQUIRE x.since IS NOT NULL
-
❏
CREATE EXISTENCE CONSTRAINT WORKS_AT_since_exists IF NOT EXISTS FOR (WORKS_AT.since)
-
❏
CREATE EXISTENCE CONSTRAINT WORKS_AT_since_exists IF NOT EXISTS FOR ()-[x:WORKS_AT]-() WITH (x.since)
-
❏
CREATE CONSTRAINT WORKS_AT_since_exists IF NOT EXISTS FOR ()-[x:WORKS_AT]-() EXISTS x.since
Hint
You specify the type of constraint at the end of the statement. You must specify a path that contains the relationship type.
Solution
The correct statement for creating this constraint is:
CREATE CONSTRAINT WORKS_AT_since_exists IF NOT EXISTS FOR ()-[x:WORKS_AT]-() REQUIRE x.since IS NOT NULL
2. Type for a constraint
When you list the constraints in the graph. What constraint type is returned for an existence constraint on a relationship?
-
❏ RELATIONSHIP_PROPERTY_EXISTS
-
✓ RELATIONSHIP_PROPERTY_EXISTENCE
-
❏ EXISTENCE
-
❏ EXISTS
Hint
Type the statement SHOW CONSTRAINTS
to view the constraints and their types in the graph.
Solution
The correct type for an existence constraint defined for a relationship type is:
RELATIONSHIP_PROPERTY_EXISTENCE
Summary
In this lesson, you learned how to create an existence constraint for a relationship type property. In the next challenge, you will create another existence constraint.