Load csv: create a node and a relation

how can i create a node and create a relationship in the same statement when loading csv?

company(short_name) ← :employed - employee (emp_id)

emp_id, name
Desired behavior:

LOAD CSV FROM "o/people_nodes.csv" WITH HEADER AS row
CREATE (e:employee {emp_id: row.emp_id, name: row.name})
CREATE (c:Company {short_name: $short_name }) <- [:EMPLOYED] - (emp: Employee {emp_id : row.emp_id})

thank you.

1 Like

Hi @azs :grinning_face_with_smiling_eyes:
The query you submitted in your question should be able to create both nodes and relationships while importing from a CSV file. You could even express it with one CREATE clause:

LOAD CSV FROM "o/people_nodes.csv" WITH HEADER AS row
CREATE (c:Company {short_name: $short_name })<-[:EMPLOYED]-(e:employee {emp_id: row.emp_id, name: row.name})

Are you maybe getting an error or experiencing some unexpected behavior?

@idespot :
no issues :). Just learning the memgraph/cypher at this point.
Based on the example above, does it mean that I can have an “unlimited” number of “create” statements with the same “LOAD” clause?

The way i read the manual : all clauses ( create, merge, union etc) are on the same “level”. But, if I look at the examples, e.g WITH, it clearly shows that WITH is part of MERGE and is not an operator on its own. The way the manual describes the operands/clauses is confusing, to be honest.

1 Like

Good point @azs! This could be better described in the documentation and we will update it.

To answer your question, yes, you can have multiple create statements in the same LOAD CSV query.
For example, if you had 5 columns in your CSV file and wanted to create nodes out of all of them, you could just define 5 create statements.

1 Like

wow! this is actually very cool!

What about :

  1. does it mean that i can combine MERGE with CREATE and LOAD in one “statement” too?
  2. Can i refer to the results from a MERGE in CREATE?
    Load CSV … WITH HEADER as row
    Merge (n:City)
    Where n.population > 100
    Create ( big:Metro {ppl : n.population }) < - [:REL] - (c:contry)

so that METRO node(s) will be created with the “population” returned by MERGE

  1. Can I have several MERGEs and can i refer to the results of those MERGES in all CREATEs?
  2. Can I read several files simultaneously?