Database Reference
In-Depth Information
SQL to Cypher
If you understand and use SQL, moving into Cypher requires only a small conceptual adjustment. Using a CRUD
(Create, Read, Update, Delete) comparison of some common SQL commands with how they would be written in
Cypher, this section introduces the basics of Cypher through a prior knowledge of SQL. Later sections in this chapter
cover Cypher in greater depth.
INSERT and CREATE
We start with a simple SQL command to add a User to a relational database and its counterpart in Cypher, as shown
in Listings 4-3 and 4-4. In both examples, we employ the User part of the “schema”, but the CREATE command in the
Cypher example implies that values are going to be added and does not need an explicit VALUES command.
Listing 4-3. SQL Query to INSERT a User
INSERT INTO User (username) VALUES ("greg")
Listing 4-4. Cypher Query to CREATE a User
CREATE (u:User {username:"greg"})
Two unique and amazingly powerful advantages of Neo4j that can be realized through Cypher are adding
additional schema descriptors to Node entities through labels and adding new properties without having to use an
equivalent to the SQL ALTER TABLE command. In a relational database, if you needed another column, then you
would need to run a SQL similar to that shown in Listing 4-5.
Listing 4-5. ALTER TABLE Statement in SQL
ALTER TABLE table_name
ADD my_new_column_name datatype
In Neo4j, if you wanted to add a new property to a node, then you would just add the property as a part of
executing the cypher, as shown in Listing 4-6.
Listing 4-6. Add a New Property to a Node
CREATE (u:User {username:"greg", business: "Graph Story"})
SELECT and START / MATCH
Listing 4-7 is the simple command to retrieve a User from a relational database; Listing 4-8 is its counterpart in
Cypher. Some additional SELECT-style operations will be covered later in this chapter.
Listing 4-7. SQL Query to SELECT a User
SELECT *
FROM User
WHERE username = "greg"
Listing 4-8. Cypher Query to START with a Node of Type User
START user=node:User(username="greg")
RETURN user
 
Search WWH ::




Custom Search