Creating Existence Constraints for Node Properties

Existence constraints for nodes

Depending on your application needs, you may want to ensure that some nodes with a given label always have a particular property. For example, a Person node must have a name property. The name need not be unique for all Person nodes, but the application does not permit a Person node without a name property.

Enterprise Edition Only Feature

Existence constraints are only available in Enterprise Edition.

Syntax for creating an existence constraint on a node

Here is the syntax for creating an existence constraint on a node property:

cypher
CREATE CONSTRAINT <constraint_name> IF NOT EXISTS
FOR (x:<node_label>)
REQUIRE x.<property_key>  IS NOT NULL

You specify the name of the constraint, the node label it will be associated with, and the name of the property that must exist for nodes with that label.

  • 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 node label and property key.

    • Otherwise, the constraint is created.

Creating the existence constraint

Suppose we want to enforce that all Person nodes must have a value for a name property. Execute this code to create an existence constraint for this property:

cypher
CREATE CONSTRAINT Person_name_exists IF NOT EXISTS
FOR (x:Person)
REQUIRE x.name IS NOT NULL

This code creates the existence constraint named Person_name_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 movies in the graph for this course contain a value for the plot property. Execute this code. The creation of this constraint should fail:

cypher
CREATE CONSTRAINT Movie_plot_exists IF NOT EXISTS
FOR (x:Movie)
REQUIRE x.plot IS NOT NULL

Node creation or update failure

We have created the Person_name_exists constraint in the graph. If we attempt to create a Person node without a name, an error will occur and the node will not be created. Execute this code:

cypher
MERGE (x:Person {tmdbId:9999999})
SET x.born = 2022

If we attempt to remove a name property from an existing node, an error will occur. Execute this code:

cypher
MATCH (x:Person {name: 'Tom Hanks'})
SET x.name = null

Listing constraints in the graph

At this point, you should have multiple uniqueness constraints and a single existence constraint in the graph:

cypher
SHOW CONSTRAINTS

Check your understanding

1. Creating an existence constraint on a node property

Suppose we have a graph that contains Company nodes. One of the properties of a Company node is name. Every Company in the graph must have a value for the name property. Before we load the Company data, we want to create an existence constraint for the Company.name property. What is the correct statement to create this constraint?

  • CREATE EXISTENCE CONSTRAINT Company_name_exists IF NOT EXISTS FOR (Company.name)

  • CREATE EXISTENCE CONSTRAINT Company_name_exists IF NOT EXISTS FOR (x:Company) WITH (x.name)

  • CREATE CONSTRAINT Company_name_exists IF NOT EXISTS FOR (x:Company) REQUIRE x.name IS NOT NULL

  • CREATE CONSTRAINT Company_name_exists IF NOT EXISTS FOR (x:Company) EXISTS x.name

Hint

You specify the type of constraint at the end of the statement

Solution

The correct statement for creating this constraint is:

CREATE CONSTRAINT Company_name_exists IF NOT EXISTS FOR (x:Company) REQUIRE x.name 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 node?

  • ❏ NODE_PROPERTY_EXISTS

  • ✓ NODE_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 node label is:

NODE_PROPERTY_EXISTENCE

Summary

In this lesson, you learned how to create an existence constraint for node properties. In the next challenge, you will create more existence constraints.