Let's cover the basics of Cypher Query Language:
1. Nodes and Relationships:
In Cypher, nodes represent entities in the graph, while relationships represent connections between nodes. Nodes are enclosed in parentheses () and relationships are represented by arrows --> or <-- indicating the direction of the relationship.
Example:
Creating a node
CREATE (n:Person {name: 'Alice', age: 30})
Creating another node
CREATE (m:Person {name: 'Bob', age: 35})
Creating a relationship between nodes
CREATE (n)-[:FRIENDS_WITH]->(m)
2. MATCH clause:
The MATCH clause is used to retrieve data from the graph by specifying patterns of nodes and relationships.
Example:
Matching nodes and their relationships
MATCH (p:Person)-[:FRIENDS_WITH]->(q:Person)
WHERE p.name = 'Alice'
RETURN p, q
3. RETURN clause:
The RETURN clause is used to specify what data to retrieve from the query.
Example:
Returning specific properties of nodes
MATCH (p:Person)
RETURN p.name, p.age
4. WHERE clause:
The WHERE clause is used to filter query results based on conditions.
Example:
Filtering nodes based on properties
MATCH (p:Person)
WHERE p.age > 30
RETURN p.name
5. CREATE clause:
The CREATE clause is used to create nodes and relationships in the graph.
Example:
Creating a node with properties
CREATE (n:Person {name: 'Charlie', age: 25})
Creating a relationship between existing nodes
MATCH (p:Person), (q:Person)
WHERE p.name = 'Alice' AND q.name = 'Bob'
CREATE (p)-[:WORKS_WITH]->(q)
6. OPTIONAL MATCH clause:
The OPTIONAL MATCH clause is used to perform a pattern match where the pattern is optional. It returns null values for missing patterns.
Example:
Optional match to find nodes with or without specific relationships
OPTIONAL MATCH (p:Person)-[:LIKES]->(q:Product)
RETURN p.name, q.name
These are some of the basic elements and clauses of Cypher Query Language. With these fundamentals, you can start querying and exploring graph data in a Neo4j database.
No comments:
Post a Comment