Thursday, October 29, 2020

Neo4j - Create a Constraint using Cypher

A constraint allows you to place restrictions over the data that can be entered against a node or a relationship.


Constraints help enforce data integrity, because they prevent users from entering the wrong kind of data. If a someone tries to enter the wrong kind of data when a constraint has been applied, they will receive an error message.


Constraint Types


In Neo4j, you can create uniqueness constraints and property existence constraints.


Uniqueness Constraint

Specifies that the property must contain a unique value (i.e. no two nodes with an Artist label can share a value for the Name property.)

Property Existence Constraint

Ensures that a property exists for all nodes with a specific label or for all relationships with a specific type. Property existence constraints are only available in the Neo4j Enterprise Edition.


Create a Uniqueness Constraint


To create a uniqueness constraint in Neo4j, use the CREATE CONSTRAINT ON statement. Like this:



CREATE CONSTRAINT ON (a:Artist) ASSERT a.Name IS UNIQUE

In the above example, we create a uniqueness constraint on the Name property of all nodes with the Artist label.


When the statement succeeds,the following message is displayed:




View the Constraint


Constraints (and indexes) become part of the (optional) database schema.


We can view the constraint we just created by using the :schema command. Like this:


:schema



References:

https://www.quackit.com/neo4j/tutorial/neo4j_create_a_constraint_using_cypher.cfm

No comments:

Post a Comment