Managing constraints
Managing constraints in the graph includes:
-
Creating constraints (
CREATE CONSTRAINT…
) -
Listing constraints (
SHOW CONSTRAINTS
) -
Dropping constraints
You have already learned how to create and list the constraints in the graph. In this lesson, you will learn how to drop a constraint.
Syntax for dropping a constraint
You use this statement to drop a constraint by its name:
DROP CONSTRAINT <constraint_name>
Dropping a constraint
Execute this statement to list the constraints in the graph so you can view the names of the constraints:
SHOW CONSTRAINTS
Now, drop the constraint that you created in the last Challenge named Person_name_url_nodekey.
DROP CONSTRAINT Person_name_url_nodekey
Creating list of constraints to drop
You can use SHOW CONSTRAINTS
to create a list of constraints to drop:
SHOW CONSTRAINTS YIELD name RETURN collect('DROP CONSTRAINT ' + name + ';') AS Statements
Using APOC for dropping all indexes and constraints
The APOC library has a number of procedures you can use to manage indexes.
Here is the code that will drop all indexes and constraints in the graph. Give it a try:
CALL apoc.schema.assert({},{},true)
Recreate All Constraints
Execute this code to recreate the constraints created in this module:
CREATE CONSTRAINT Movie_movieId_unique IF NOT EXISTS FOR (x:Movie) REQUIRE x.movieId IS UNIQUE;
CREATE CONSTRAINT Movie_released_title_unique IF NOT EXISTS FOR (x:Movie) REQUIRE (x.released,x.title) IS UNIQUE;
CREATE CONSTRAINT Person_tmdbId_unique IF NOT EXISTS FOR (x:Person) REQUIRE x.tmdbId IS UNIQUE;
CREATE CONSTRAINT User_userId_unique IF NOT EXISTS FOR (x:User) REQUIRE x.userId IS UNIQUE;
CREATE CONSTRAINT Genre_name_unique IF NOT EXISTS FOR (x:Genre) REQUIRE x.name IS UNIQUE;
CREATE CONSTRAINT Person_name_exists IF NOT EXISTS FOR (x:Person) REQUIRE x.name IS NOT NULL;
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;
CREATE CONSTRAINT RATED_timestamp_exists IF NOT EXISTS FOR ()-[x:RATED]-() REQUIRE x.timestamp IS NOT NULL;
CREATE CONSTRAINT Movie_imdbId_nodekey IF NOT EXISTS FOR (x:Movie) REQUIRE x.imdbId IS NODE KEY;
CREATE CONSTRAINT Person_name_url_nodekey IF NOT EXISTS FOR (x:Person) REQUIRE (x.name,x.url) IS NODE KEY
Check your understanding
1. Dropping a constraint
We have created a Node key constraint named Person_name_url_nodekey for all Person nodes in the graph on the name and url properties.
What is the correct statement to drop this constraint?
-
❏
DELETE CONSTRAINT Person_name_url_nodekey
-
✓
DROP CONSTRAINT Person_name_url_nodekey
-
❏
DROP NODE KEY Person_name_url_nodekey`
-
❏
DROP CONSTRAINT FOR (x:Person) ON (x.name,x.url)
Hint
DROP CONSTRAINT
is used to drop a constraint.
Solution
The correct code for dropping this constraint is:
DROP CONSTRAINT Person_name_url_nodekey
2. Drop all constraints from the graph
What statement will drop all constraints from the graph?
-
❏
DROP CONSTRAINTS
-
✓
CALL apoc.schema.assert({},{},true)
-
❏
DROP CONSTRAINT *
-
❏
CALL apoc.schema.dropConstraints({},{},true)
Hint
There is one APOC function you can use to drop all constrants and indexes in the graph.
Solution
The correct answer is CALL apoc.schema.assert({},{},true)
Summary
In this lesson, you learned how to drop constraints from the graph. In next Challenge, you will drop some more constraints.