Is there anything in cypher equivalent to sequence.nextval in Oracle

Hi,

I want to create employee table in Neo4j. I will create a node for each employee with his name, address, phone, email. when I insert each employee data i should get employee id generated in database. It means employee id is also a property of employee node which is auto generated. Is there a way how to generate integer sequence while creating node in cypher.

Does it need to be an auto incremented id, or are you looking for a way to create a primary key for nodes? If the latter perhaps the UUID function in the APOC standard library would help?

https://neo4j.com/docs/labs/apoc/current/graph-updates/uuid/

1 Like

Hi,

I am looking for auto increment id.

Thanks & Regards,
Vijayalakshmi

I think the best was is to use apoc.atomic.add:
1st, create the sequence node:
CREATE (s:Seq {key:"my_seq", value: 0})

Then, use this to get new values:
MATCH (s:Sequence {key:"my_seq"})
CALL apoc.atomic.add(s,'value',1,10)
YIELD newValue as seq
RETURN seq

1 is the number you want to add to your sequence, 10 is how many times to retry if value is locked.
Or you can use the sequence in your query when creating/updating a value, and use the seq as a variable.
return p

Note: do NOT rely long term stability of a node's internal id. The id of a deleted node can get reused.

1 Like