Replicating SQL to Cypher


(Varun Sharma) #1

Hi All,

I am replicating SQL code to cypher, can any one help me identifying the way how I can start as below SQL query format.

Insert into stgTable
Table A Left outer Join Table B;
Table A right outer Join table B;

Update stgTable(With some condition);

Insert into MainTable( from stgTable)

Update Maintable(With some condition)

I am looking for better approach to start the data model and cypher.

Thanks in advance for your support.

Varun Sharma

(Andrew Bowman) #2

Can you provide what you've tried so far, and what labels you have in your graph?

Keep in mind that when doing a straightforward translation from SQL to Cypher, you'll use node labels in place of tables, and that outer joins are usually performed using OPTIONAL MATCH.

In the case where you originally had foreign keys, you'd create relationships between nodes (so joins on values are represented as expansions via relationships).

In this particular case, you might represent this by creating some new node that has relationships to each pairing of nodes:

MATCH (a:A), (b:B) // this creates a cartesian product of all :A and :B nodes
WHERE  // add your predicate on which pairings to keep 
CREATE (c:C) // entry pointing to each pair that passes the above filter
CREATE (c)-[:REL_A]->(a)
CREATE (c)-[:REL_B]->(b)

You would use whatever relationship types make sense to you.

This approach assumes that you just need nodes that reference the nodes in the other two labels, no data is being copied between the nodes for this approach. If you do need data copied, you'd copy whatever properties you need from :A and/or :B into the new :C nodes.

In any case it may be useful to review the documentation on Neo4j and Cypher to better understand nodes, labels, and relationships in general.

(Michael Hunger) #3

What does your domain look like right now and your relational model?
Start with that.

And what's the goal for this statement?