Database Reference
In-Depth Information
DELETE
Deleting a record in a relational database and in Neo4j are nearly identical in terms of syntax, the one exception being
that the record search is performed before the DELETE command in Cypher, as shown in Listings 4-13 and 4-14.
Listing 4-13. SQL Query to DELETE a User
DELETE FROM User
WHERE username="greg"
Listing 4-14. Cypher Query to DELETE a User
MATCH (u:User {username: "greg"} )
DELETE u
If you delete a node that has relationships, you need to be sure to remove the relationships as well. The good
news is that all of those steps can be done in the same command, as shown in Listing 4-15. A demonstration of how to
remove specific relationships from nodes will be given later in this chapter.
Listing 4-15. Cypher Query to DELETE a User and Its Relationships
MATCH (u:User {username: "greg"} )-[r]-()
DELETE u
Cypher Clauses
Beyond the basics of Cypher covered through a CRUD comparison with SQL in the preceding section are many more
commands and functions at your disposal. This section starts out with a look at some useful Cypher clauses.
Return
The RETURN clause simply returns the parts of the graph that are necessary for display or further analysis within your
application. The RETURN is similar to the SELECT statement found in SQL. Typically, applications concerned with
domains such as social networks want to analyze the relationships but only return properties for display. However,
you can include nodes as well as relationships in your operations when necessary. Listings 4-16, 4-17, and 4-18 show
various combinations available when using the RETURN clause.
Listing 4-16. RETURN the Nodes Found in the MATCH
MATCH (u:User {username: "greg"} )
RETURN u
Listing 4-17. RETURN a Property Using an Alias
MATCH (u:User {username: "greg"} )
RETURN u.username AS uname
Listing 4-18. RETURN All Elements Found in the MATCH
MATCH (u:User {username: "greg"} ) -[r]-()
RETURN u,r
 
Search WWH ::




Custom Search