Open In App

Neo4j Create Constraint

Improve
Improve
Like Article
Like
Save
Share
Report

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:

    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:

  • View the Constraint:
    :schema

    Output:

  • 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:

    Property Existence Constraints:
    Property existence constraints is used to ensure all nodes with a certain label have a certain property. For example, you could specify that all nodes labelled with Language must contain a id property.

  • Query:

    CREATE CONSTRAINT ON (label) ASSERT exists(node)
  • Note:
    Property existence constraints are only available in the Neo4j Enterprise Edition.


    Last Updated : 03 Sep, 2019
    Like Article
    Save Article
    Previous
    Next
    Share your thoughts in the comments
Similar Reads