Check your understanding

Review the following CSV data files and answer the questions.

airports.csv

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

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.

cypher
/*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.

cypher
`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.

cypher
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.

cypher
SET f.airline = row.airline

3. Creating relationship properties

The airport_code property is unique. Select the correct Cypher statement to create the constraint.

cypher
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:

cypher
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:

cypher
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.