Creating Existence Constraints for Node properties

Property must exist

In our Movie data model, we want to ensure every Movie in the graph has a value for its title property and every User has a value for the name property. In your real application, you would create existence constraints for nodes prior to loading the data. For this course, we already have the data loaded, but we still want to constrain the graph so that all movies have titles and all users have names.

In the previous lesson, you executed code to create an existence constraint for the Person nodes to ensure they always have a value for the name property. In this Challenge, you will create two more uniqueness constraints in the graph.

In the sandbox on the right, modify the code to create these existence constraints in the graph:

  1. For the Person nodes:

    • constraint_name: User_name_exists

    • node_label: User

    • property_key: name

  2. For the Movie nodes:

    • constraint_name: Movie_title_exists

    • node_label: Movie

    • property_key: title

After you have created the constraints, list them:

cypher
SHOW CONSTRAINTS

Creating Multiple Constraints

If you create incorrect constraints with different names or property_key names, do not worry. You can create new ones, provided the constraint_name or property_key is unique. Later in this course you will learn how to remove constraints from the graph.

If you reload this page, the graph will be reset to what it should be at the beginning of the challenge.

Validate Results

Once you have created the two constraints, click the Check Database button and we will check the database for you.

Hint

Constraint names, label names, and property key names are all case-sensitive. The node labels you should specify are for Movie and User nodes (case-sensitive). The property keys you should specify are for title and name (case-sensitive).

If you mis-specify a label or property key, the constraint will not be created.

You can type SHOW CONSTRAINTS after you have created each constraint.

If you mess up, you can reload this Challenge page and you should be where you need to be at the beginning of this Challenge.

Solution

Here are the statements to create these two constraints:

cypher
CREATE CONSTRAINT Movie_title_exists IF NOT EXISTS FOR (x:Movie) REQUIRE x.title IS NOT NULL;
CREATE CONSTRAINT User_name_exists IF NOT EXISTS FOR (x:User) REQUIRE x.name IS NOT NULL

Summary

In this challenge, you demonstrated that you can create existence constraints for node properties in the graph. In the next lesson, you will learn how to create existence constraints for relationship properties.

Chatbot

Hi, I am an Educational Learning Assistant for Intelligent Network Exploration. You can call me E.L.A.I.N.E.

How can I help you today?