Unique IDs and Constraints

A Neo4j best practice is to use an ID as a unique property value for each node.

Unique IDs help ensure duplicate data is not created. When you load data from CSV files, you rely heavily upon the IDs specified in the file. If the IDs in your CSV file are not unique for the same entity (node), you could create duplicate data. You may also have problems loading the data and creating relationships between nodes.

You can add constraints to your database to stop the creation of nodes with duplicate IDs.

Create a unique constraint

The syntax for creating a unique constraint on a property is:

cypher
CREATE CONSTRAINT [constraint_name] [IF NOT EXISTS]
FOR (n:LabelName)
REQUIRE n.propertyName IS UNIQUE

The constraint is for a property on all nodes with a specified label.

The constraint_name is optional, but it is good practice to specify one. If you do not specify a constraint name, Neo4j will create one for you.

The IF NOT EXISTS clause is also optional - if not used Neo4j will generate an error if the constraint already exists.

Person node constraint

The Person nodes you created should all have a unique tmbdId property.

You can create a constraint for the tmdbId property to ensure that all Person nodes have a unique tmdbId property value.

Review the following Cypher statement.

cypher
CREATE CONSTRAINT Person_tmdbId IF NOT EXISTS
FOR (x:Person)
REQUIRE x.tmdbId IS UNIQUE

You should note that:

  • The constraint name is Person_tmdbId.

  • The optional clause IF NOT EXISTS is used - without which Neo4j would raise an error if the constraint exists.

  • It applies to all nodes with the Person label.

  • It requires the tmdbId property to be unique.

Run the Cypher statement to create the constraint:

cypher
CREATE CONSTRAINT Person_tmdbId
FOR (x:Person)
REQUIRE x.tmdbId IS UNIQUE

You can check that the constraint has been created by running the following Cypher statement:

cypher
SHOW CONSTRAINTS

You should see the constraint named Person_tmdbId in the results.

If you try to create a Person node with a duplicate tmdbId property value, Neo4j will raise an error.

cypher
CREATE (p:Person {tmdbId: 3}) RETURN p
Node(0) already exists with label `Person` and property `tmdbId` = 3

Drop a constraint

If you need to drop a constraint, use the following Cypher statement.

cypher
DROP CONSTRAINT [constraint_name]

Check Your Understanding

Duplicate Constraint

True or False - Neo4j will allow you to create duplicate unique ID constraints.

  • ❏ True

  • ✓ False

Hint

Using the 'IF NOT EXISTS' clause will prevent Neo4j from generating an error if the constraint already exists.

Solution

The statement is False. Neo4j will generate an error if a constraint exists - unless IF NOT EXISTS has been specified.

Summary

In this lesson, you learned how to create a unique constraint on a property.

In the next lesson, you will load a CSV file of movies and create Movie nodes.