Creating Uniqueness Constraints

Uniqueness constraints

A best practice is to identify a property for all nodes in the graph that make them unique. In our movie graph, these constraints are:

  • Movie nodes use movieId.

  • Person nodes use tmdbId.

  • User nodes use userId.

  • Genre nodes use name.

Syntax for creating a uniqueness constraint for a single property

Here is the syntax for creating a uniqueness constraint for a single property:

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

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

    • Otherwise, the constraint is created.

Creating the uniqueness constraint for a single property

Execute this code to create a uniqueness constraint for the Movie.movieId property:

cypher
CREATE CONSTRAINT Movie_movieId_unique IF NOT EXISTS
FOR (x:Movie)
REQUIRE x.movieId IS UNIQUE

This code creates the uniqueness constraint named Movie_movieId_unique. It is always a best practice to name constraints.

When to create constraints

You typically create constraints before the data is loaded into the graph, but in our case, we already have the data in the graph.

Syntax for creating a uniqueness constraint for multiple properties

Here is the syntax for creating a uniqueness 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 UNIQUE

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

Creating the uniqueness constraint for multiple properties

Execute this code to create a uniqueness constraint for the Movie.released and Movie.title properties:

cypher
CREATE CONSTRAINT Movie_released_title_unique IF NOT EXISTS
FOR (x:Movie)
REQUIRE (x.released,x.title) IS UNIQUE

This code creates the uniqueness constraint named Movie_released_title_unique. Every Movie node in the graph will have a unique value for the released and title properties (both of them). That is two movies with the same title cannot have the same released value in the graph.

Listing constraints in the graph

You can view the existing constraints in the graph as follows:

cypher
SHOW CONSTRAINTS

Uniqueness in node creation

Now that you have created a uniqueness constraint, we will see it in action.

In our graph, we already have a Movie with the movieId of "1". Try to execute this code to create a Movie node with the same value for movieId:

cypher
MERGE (m:Movie {movieId: '1'})

This code does not modify the graph. It first finds the Movie node with this value for movieId and because the node already exists, does not create the node.

Uniqueness constraint creation failure

Suppose we have already loaded our data into the graph and we want to create a uniqueness constraint. In the previous example, our data is clean and we know that the Movie.movieId values in the graph are unique. Because the values are unique, the creation of the constraint was successful.

What if we are unsure if the property values for a node are unique? If we attempt to create a uniqueness constraint on a property that is not unique for all nodes with that label, the constraint creation will fail.

Execute this code that attempts to create a uniqueness constraint on the Movie.year property:

cypher
CREATE CONSTRAINT Movie_year_unique IF NOT EXISTS
FOR (x:Movie)
REQUIRE x.year IS UNIQUE

This code will fail because the values for the year property are not unique across all Movie nodes.

Check your understanding

1. Creating a uniqueness constraint

Suppose we have a graph that contains Company nodes. One of the properties of a Company node is taxId. Every Company in the graph must have a unique taxId. Before we load the Company data, we want to create a uniqueness constraint for the Company.taxId property.

What is the correct statement to create this constraint?

  • CREATE UNIQUENESS CONSTRAINT Company_taxId_unique IF NOT EXISTS FOR (Company.taxId)

  • CREATE UNIQUENESS CONSTRAINT Company_taxId_unique IF NOT EXISTS FOR (x:Company) WITH (x.taxId)

  • CREATE CONSTRAINT Company_taxId_unique IF NOT EXISTS FOR (x:Company) REQUIRE x.taxId IS UNIQUE

  • CREATE CONSTRAINT Company_taxId_unique IF NOT EXISTS FOR (x:Company) UNIQUE x.taxId

Hint

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

Solution

The correct code for creating the uniqueness constraint is:

CREATE CONSTRAINT Company_taxId_unique IF NOT EXISTS FOR (x:Company) REQUIRE x.taxId IS UNIQUE

2. Listing constraints

What statement lists the constraints in the graph?

  • SHOW UNIQUENESS

  • CONSTRAINTS?

  • LIST CONSTRAINTS

  • SHOW CONSTRAINTS

Hint

The command is SHOW and you want to list all constraints in the graph.

Solution

The correct statement for listing constraints is:

SHOW CONSTRAINTS

Summary

In this lesson, you learned how to create a uniqueness constraint for a node. In the next challenge, you will create more uniqueness constraints.