Review the following CSV data files and answer the questions.
airports.csv
airport_code|name|city|country
SFO|San Francisco International Airport|San Francisco|United States
NYC|John F. Kennedy International Airport|New York City|United States
LHR|London Heathrow Airport|London|United Kingdom
flights.csv
flight_number,airline,departure_airport,arrival_airport
SF_4863,Swift Flights,SFO,NYC
SF_4864,Swift Flights,NYC,SFO
AA_9135,America Airways,SFO,LHR
AA_9136,America Airways,LHR,SFO
BA_2945,Britain Atlantic,LHR,NYC
BA_2946,Britain Atlantic,NYC,LHR
Check Your Understanding
1. Load the CSV file
Select the correct Cypher statement to load the data from the airports.csv
file.
/*select:LOAD CSV FROM 'file:///airports.csv' AS row FIELDTERMINATOR '|'*/
MERGE (a:Airport {airport_code: row.airport_code))
SET a.name = row.name, a.city = row.city, a.country = row.country
-
❏
LOAD CSV FROM 'file:///airports.csv' AS row FIELDTERMINATOR '|'
-
✓
LOAD CSV WITH HEADERS FROM 'file:///airports.csv' AS row FIELDTERMINATOR '|'
-
❏
LOAD CSV WITH HEADERS FROM 'file:///airports.csv' AS row
Hint
The airports.csv
CSV file contains headers and the field terminator is |
.
Solution
As the CSV file contains headers, we need to use the LOAD CSV WITH HEADERS
clause. The field terminator is |
and should be specified.
`LOAD CSV WITH HEADERS FROM 'file:///airports.csv' AS row FIELDTERMINATOR '|'`
2. Creating relationship properties
The flights.csv
data file contains information about flights between airports. The following Cypher statement will create FLIGHT
relationships between Airport
nodes based on the data in the flights.csv
file.
Select the correct Cypher statement to add the airline
property to the relationship.
LOAD CSV WITH HEADERS 'file:///flights.csv' AS row
MATCH (d:Airport {airport_code: row.departure_airport})
MATCH (a:Airport {airport_code: row.arrival_airport})
MERGE (d)-[f:FLIGHT {flight_number: row.flight_number}]->(a)
/*select:SET d.airline = row.airline*/
-
❏ SET d.airline = row.airline
-
✓ SET f.airline = row.airline
-
❏ SET a.airline = row.airline
Hint
The airline
property is against the relationship, not the departure or arrival airport.
Solution
The FLIGHT
relationship is between the Airport
nodes d
and a
, and the airline
property should be set against the relationship f
.
SET f.airline = row.airline
3. Creating relationship properties
The airport_code
property is unique. Select the correct Cypher statement to create the constraint.
CREATE CONSTRAINT Airport_code
FOR (x:Airport)
/*select:REQUIRE x.airport_code IS UNIQUE*/
-
✓ REQUIRE x.airport_code IS UNIQUE
-
❏ REQUIRE x.id IS UNIQUE
-
❏ REQUIRE id(x.airport_code) IS UNIQUE
Hint
The syntax to create a unique property constraint is:
CREATE CONSTRAINT [constraint_name] [IF NOT EXISTS]
FOR (n:LabelName)
REQUIRE n.propertyName IS UNIQUE
Solution
This is the correct syntax to create a unique property constraint on the airport_code
property:
REQUIRE x.airport_code IS UNIQUE
4. Why have a Unique property?
Select why having a unique property value for each node is a Neo4j best practice:
-
✓ Help ensure duplicate data is not created
-
❏ Make sure data is not accidentally deleted
-
✓ Avoid potential problems when creating relationships between nodes
-
❏ Improve the performance of the data import
Hint
Unique property values help ensure the data integrity of nodes and relationships.
Solution
Unique property values:
-
Help ensure duplicate data is not created
-
Avoid problems when creating relationships between nodes.
Summary
In this quiz you checked your understanding of using Cypher to load CSV files, create nodes, relationships and constraints.