Do I need foreign keys?


(Samjblackburn) #1

Hello,

I'm new to neo4j and I'm setting up my first schema. I have what I'm sure is a dumb question but I'm going to ask it anyway. Do I need foreign keys? To give an example...

I'm setting up a simple graph consisting of customer nodes and customer bank card nodes and I want to relate them using a [:CARD_BELONGS_TO] relationship.

Do I need to include the customer card number in my customer nodes or is it enough just to establish the relationship? Is there any advantage in terms of indexing the card number on the customer node when it comes to searching?

Thanks in advance for any help!

S


(Benoit Simard) #2

Hi,

You don't need any foreign key, the relationship is sufficient.

In Neo4j, it's really cheap to traverse a relationship, it is not computed like in SQL, we have it on the disk.
So when you have your Customer node, you can retrieve really fast your BankCard node(s).

This is the key concept of a graph database. A graph database works locally, you find a node and then expand in the graph by following relationships.

On the other side, SQL database works globally (on tables) with the concept of join that comes from the set theory.

Hope it's more clear now for you.

Cheers.


(Samjblackburn) #3

That's excellent, thanks for confirming!

S