Creating Node Key Constraints

Node key

A Node key is a specialized type of constraint for the properties of a node. It combines existence with uniqueness.

For example, a Person node’s tmdbId value must be unique and exist.

Enterprise Edition Only

Node key constraints are only available in Enterprise Edition.

Syntax for creating a Node key constraint for a single property

Here is the syntax for creating a Node key constraint for a single property:

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

You specify the name of the constraint, the node label it will be associated with, and the name of the property.

  • 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 a Node key constraint for that node label and property key.

    • Otherwise, the constraint is created.

Creating the Node key constraint for a single property

Execute this code to create a Node key constraint for the Movie.imdbId property:

cypher
CREATE CONSTRAINT Movie_imdbId_nodekey IF NOT EXISTS
FOR (x:Movie)
REQUIRE x.imdbId IS NODE KEY

This code creates the Node key constraint named Movie_imdbId_nodekey. Every Movie node in the graph must have a imdbId property and it must be unique.

At this point in this course, you should have the following constraints on the Movie nodes:

  • The movieId value must be unique

  • The value for released and title properties combined must be unique

  • The title property must exist

  • The imdbId property must exist and must have a unique value

With these constraints, the following code will fail based upon the latest Node key constraint that you created:

cypher
MERGE (x:Movie {title: "No Place Like Home", imdbId: "0113497", movieId: "9999999"})
RETURN x

The title and movieId values are unique, but there already is a Movie node in the graph with the same value for imdbId. If you remove the specification for imdbId, it will still fail because it is required per the Node key constraint.

Syntax for creating a Node key constraint for multiple properties

Here is the syntax for creating a Node key constraint for a set of properties:

cypher
CREATE CONSTRAINT <constraint_name> IF NOT EXISTS
FOR (x:<node_label>)
REQUIRE (x.<property_key1>, x.<property_key2>)  IS NODE KEY

This constraint ensures that a set of values for a node’s properties will be unique and must also exist.

Check your understanding

1. Creating a Node key constraint

Suppose we have a graph that contains Company nodes. Every Company in the graph must have a unique name and location. These properties must be set in every Company node. Before we load the Company data, we want to create a Node key constraint for the name and location properties. What is the correct statement to create this constraint?

  • CREATE UNIQUENESS CONSTRAINT Company_name_location_nodekey IF NOT EXISTS FOR (Company.name, Company.location)

  • CREATE UNIQUENESS CONSTRAINT Company_name_location_nodekey IF NOT EXISTS FOR (x:Company) WITH (x.name,x.location)

  • CREATE CONSTRAINT Company_name_location_nodekey IF NOT EXISTS FOR (x:Company) UNIQUE x.name, x.location

  • CREATE CONSTRAINT Company_name_location_nodekey IF NOT EXISTS FOR (x:Company) REQUIRE (x.name,x.location) IS NODE KEY

Hint

You specify the type of constraint at the end of the clause for creating the constraint.

This Node key requires two properties.

Solution

The correct code for creating the uniqueness constraint is:

CREATE CONSTRAINT Company_name_location_nodekey IF NOT EXISTS FOR (x:Company) REQUIRE (x.name,x.location) IS NODE KEY

2. Type for a constraint

When you list the constraints in the graph. What constraint type is returned for a Node key constraint?

  • ❏ NODE_EXISTENCE_UNIQUENESS

  • ✓ NODE_KEY

  • ❏ EXISTENCE_UNIQUENESS

  • ❏ NODE_CONSTRAINT

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:

NODE_KEY

Summary

In this lesson, you learned how to create a Node key constraint for a node that ensures uniqueness and existence. In the next challenge, you will create a Node key constraint.