The
neo4j constraint helps user to nor enter in wrong kind of data. When the constraint is applied and the user by mistake entering the wrong kind of data then it will show an error message. In the neo4j there are two kind of constraint one is
uniqueness constraints and other one is
property existence constraints.
Below is the example of both the constraint with the example:
Suppose there is already below database is exist.
CREATE(CPP:Language{id:001, Designer: "Bjarne Stroustrup", YOE: 1985})
CREATE(C:Language {id:002, Designer: "Dennis Ritchie", YOE: 1972})
CREATE(Python:Language {id:003, Designer: "Guido van Rossum ", YOE: 1990})
CREATE(Java:Language {id:004, Designer: "James Gosling", YOE: 1995})
CREATE(CSharp:Language {id:005, Designer: "Microsoft", YOE: 2000})
RETURN CPP, C, Python, Java, CSharp
Output:
👁 Image
Uniqueness Constraints:
This Constraint contain a unique value, in his constraint two same lebel can't share a same id.
- Query:
CREATE CONSTRAINT ON(l:Language) ASSERT l.id IS UNIQUE
Output:
👁 Image
- View the Constraint:
:schema
Output:
👁 Image
- Test the Constraint:
Here we will try to add another node into that database with a redundant id value(004).
Query:
CREATE(Neo4j:Language {id:004, Designer: "Neo4j", YOE: 2012})
Output:
👁 Image