1. Index
1.1 Creating an Index
CREATE INDEX ON:label (node)
eg.
CREATE (Dhawan:player{name: "shikar Dhawan", YOB: 1995, POB: "Delhi"})
Following is a sample Cypher Query to create an index on the node Dhawan in Neo4j.
CREATE INDEX ON:player(Dhawan)
1.2 Deleting an Index
Neo4j CQL provides a "DROP INDEX" command to drop an existing index of a Node or Relationshis property.
DROP INDEX ON:label(node)
Following is a sample Cypher Query to create an index on the node named “Dhawan” in Neo4j.
DROP INDEX ON:player(Dhawan)
2. Create Unique Constraint
MATCH (root {name: "Dhawan"})
CREATE UNIQUE (root)-[:LOVES]-(someone)
RETURN someone
eg.
CREATE(Dhawan:player{id:001, name: "shikar Dhawan", YOB: 1995, POB: "Delhi"})
CREATE(Jonathan:player {id:002, name: "Jonathan Trott", YOB: 1981, POB: "CapeTown"})
CREATE(Sangakkara:player {id:003, name: "Kumar Sangakkara", YOB: 1977, POB: "Matale"})
CREATE(Rohit:player {id:004, name: "Rohit Sharma", YOB: 1987, POB: "Nagpur"})
CREATE(Virat:player {id:005, name: "Virat Kohli", YOB: 1988, POB: "Delhi"})
Then
CREATE CONSTRAINT ON (n:player) ASSERT n.id IS UNIQUE
Then, input:
CREATE (Jadeja:player {id:002, name: "Ravindra Jadeja", YOB: 1988, POB: "NavagamGhed"})
Error:
3. Drop Unique
DROP CONSTRAINT ON (node:label)
ASSERT node.id IS UNIQUE
eg.
Following is a sample Cypher Query to remove the UNIQUE constraint on the property id.
DROP CONSTRAINT ON (n:player)
ASSERT n.id IS UNIQUE
Result: