Difference between revisions of "Space Race/Relationships Tutorial"
ChrisHouston (talk | contribs) |
ChrisHouston (talk | contribs) |
||
(One intermediate revision by the same user not shown) | |||
Line 1: | Line 1: | ||
− | <p>Relationships are the most powerful part of graph-based databases. While some of the functionality these provide is possible under SQL using join tables for many to many relationships, certain queries can be constructed which would require many joins in SQL.</p> | + | <p>Relationships are the most powerful part of graph-based databases. While some of the functionality these provide is possible under SQL using join tables for many to many relationships, certain queries can be constructed which would require many joins in SQL. A good example is the number of connections required to link one node to another.</p> |
<p>All relationships in Neo4j are directional, that is they have a start and end node. They can only possess a single <code>TYPE</code>, rather than multiple labels, but they can also possess properties similar to nodes</p> | <p>All relationships in Neo4j are directional, that is they have a start and end node. They can only possess a single <code>TYPE</code>, rather than multiple labels, but they can also possess properties similar to nodes</p> | ||
Line 6: | Line 6: | ||
[http://nosqlzoo.net/mw/visproject/visualisation.php Click here for 3d graph visualisation] | [http://nosqlzoo.net/mw/visproject/visualisation.php Click here for 3d graph visualisation] | ||
+ | |||
==Relationship types== | ==Relationship types== | ||
Line 11: | Line 12: | ||
<p>The <code>Type()</code> function can return the type of a relationship</p> | <p>The <code>Type()</code> function can return the type of a relationship</p> | ||
<p class='strong'>Return the type of the relationship between Astronaut and Mission</p> | <p class='strong'>Return the type of the relationship between Astronaut and Mission</p> | ||
− | <pre class="def"><nowiki>MATCH( | + | <pre class="def"><nowiki>MATCH(:Astronaut)-[r]->(:Country) RETURN DISTINCT type(r);</nowiki> |
</pre> | </pre> | ||
− | <pre class="ans"><nowiki>MATCH( | + | <pre class="ans"><nowiki>MATCH(:Astronaut)-[r]->(:Mission) RETURN DISTINCT type(r);</nowiki> |
</pre> | </pre> | ||
</div> | </div> | ||
Line 42: | Line 43: | ||
</div> | </div> | ||
+ | ==Saturn V Missions== | ||
+ | <div class="q nonum" data-lang="neo4j"> | ||
+ | <p>Aggregate functions can also be used on the result of relationships queries</p> | ||
+ | <p class='strong'>Show how many missions were launched on the Saturn V.</p> | ||
+ | <pre class="def"><nowiki>MATCH(n:LaunchVehicle{name:"Saturn V"})RETURN DISTINCT n; </nowiki> | ||
+ | </pre> | ||
+ | <pre class="ans"><nowiki>MATCH(:LaunchVehicle{name:"Saturn V"})<-[r:LAUNCHED_ON]-(:Mission) RETURN count(r);</nowiki> | ||
+ | </pre> | ||
+ | </div> | ||
{{Acknowledgements}} | {{Acknowledgements}} |
Latest revision as of 08:43, 9 December 2019
Relationships are the most powerful part of graph-based databases. While some of the functionality these provide is possible under SQL using join tables for many to many relationships, certain queries can be constructed which would require many joins in SQL. A good example is the number of connections required to link one node to another.
All relationships in Neo4j are directional, that is they have a start and end node. They can only possess a single TYPE
, rather than multiple labels, but they can also possess properties similar to nodes
Contents
Visualisation
Click here for 3d graph visualisation
Relationship types
The Type()
function can return the type of a relationship
Return the type of the relationship between Astronaut and Mission
MATCH(:Astronaut)-[r]->(:Country) RETURN DISTINCT type(r);
MATCH(:Astronaut)-[r]->(:Mission) RETURN DISTINCT type(r);
Born in Russia
The item in []
represents a relationship between nodes, in this case the relationship possessing the label "BORN_IN"
Note the (startNode)-[:LABEL_NAME]->(endNode)
format. The direction the arrow points shows which node is the end point.
It is perfectly valid to express this relationship as (endNode)<-[:LABEL_NAME]-(startNode)
If you are not concerned about the direction of the relationships, you can also MATCH
based on (node1)-[:LABEL_NAME]-(node2)
Show the Surnames of Astronauts born in Russia, and the full name of Russia rather than the USA. Hint: Use the ISO 3166-1 Alpha-2 code in all caps
MATCH(n:Astronaut)-[:BORN_IN]->(c:Country) WHERE c.abbrev ='USA' RETURN n.surname, c.name;
MATCH(n:Astronaut)-[:BORN_IN]->(c:Country) WHERE c.abbrev= 'RU' RETURN n.surname, c.name;
Flight Colleagues
Numerous relationships can be strung together. DISTINCT
can be used to only display a result once if that node is linked to multiple times
Show the names of everyone that James Lovell flew with.
MATCH(n:Astronaut{surname:"Armstrong"})-[:CREWED]->(a:Mission)<-[:CREWED]-(m:Astronaut) RETURN DISTINCT m.surname, m.first_name;
MATCH(n:Astronaut{surname:"Lovell"})-[:CREWED]->(a:Mission)<-[:CREWED]-(m:Astronaut) RETURN DISTINCT m.surname, m.first_name;
Saturn V Missions
Aggregate functions can also be used on the result of relationships queries
Show how many missions were launched on the Saturn V.
MATCH(n:LaunchVehicle{name:"Saturn V"})RETURN DISTINCT n;
MATCH(:LaunchVehicle{name:"Saturn V"})<-[r:LAUNCHED_ON]-(:Mission) RETURN count(r);
Acknowledgements
NoSQLZoo is made possible by the following open-source technologies: