0% found this document useful (0 votes)
32 views10 pages

Neo 4 J

Neo4j is a leading NoSQL graph database management system designed for efficiently storing and querying highly interconnected data using graph structures instead of traditional tables. It utilizes its own query language, Cypher, to create nodes, relationships, and manage data properties, making it suitable for applications like social network analysis and fraud detection. The document outlines the syntax for various operations in Neo4j, including creating nodes, relationships, and viewing or deleting data.

Uploaded by

pes1202203627
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
32 views10 pages

Neo 4 J

Neo4j is a leading NoSQL graph database management system designed for efficiently storing and querying highly interconnected data using graph structures instead of traditional tables. It utilizes its own query language, Cypher, to create nodes, relationships, and manage data properties, making it suitable for applications like social network analysis and fraud detection. The document outlines the syntax for various operations in Neo4j, including creating nodes, relationships, and viewing or deleting data.

Uploaded by

pes1202203627
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 10

Neo4j : The Graph Database

Neo4j is the most famous database management system and it is also a NoSQL
database system. Neo4j is different from Mysql or MongoDB it has its own
features and it is designed to efficiently store and query highly interconnected
data that’s makes it special compared to other Database Management System.

Neo4j is a cutting-edge database designed to handle and analyze connected data


more efficiently than traditional databases. Instead of using tables, it uses graph
structures to store and query data, making it ideal for applications with complex
relationships. Neo4j is known for its high performance, scalability, and
flexibility, and is used in various fields like social network analysis, fraud
detection, recommendation systems, and knowledge graphs.

Neo4j structure
Neo4j stores and present the data in the form of graph not in tabular format or not
in a Json format. Here the whole data is represented by nodes and there you can
create a relationship between nodes. That means the whole database collection
will look like a graph, that’s why it is making it unique from other database
management system.
What is a Graph Database?

A graph database uses graph theory to store, map, and query relationships. It
consists of nodes, edges, and properties, where:

 Nodes represent entities such as people, businesses, or any data item.

 Edges (or relationships) connect nodes and illustrate how entities are related.

 Properties provide additional information about nodes and relationships.

This structure allows graph databases to model real-world scenarios more naturally
and intuitively than traditional relational databases.

The Neo4j has its own query language that called Cypher Language. It is similar to
SQL, remember one thing Neo4j does not work with tables, row or columns it
deals with nodes. It is more satisfied to see the data in a graph format rather than in
a table format.
Neo4j Syntax and Rules
Below is the syntax and explanation for the commands provided earlier:

1. Syntax for Creating Nodes

CREATE (alias:Label {property1: value1, property2: value2, ...});

alias: Optional, used to reference the node later in the query.



 Label: Used to categorize the node (e.g., Person, City).
 Properties: Key-value pairs inside curly braces {}.
Example:

CREATE (n:Person {name: "Alice", age: 30, city: "New York"});

Rules:
 Node labels should start with an uppercase letter.
 Properties should be defined as key-value pairs, with string values in quotes
(" ").
 Multiple properties are separated by commas.

2. Syntax for Creating Multiple Nodes

CREATE

(alias1:Label {property1: value1, ...}),

(alias2:Label {property1: value1, ...}),

...;

Example:

CREATE

(p1:Person {name: "Bob", age: 25, city: "Chicago"}),

(p2:Person {name: "Charlie", age: 35, city: "San Francisco"});


Rules:
 Each node creation is separated by a comma.
 Properties can vary across nodes.

3. Syntax for Creating Relationships

MATCH (alias1:Label1 {property: value}), (alias2:Label2 {property: value})

CREATE (alias1)-[:RELATIONSHIP_TYPE]->(alias2);

MATCH: Finds the nodes to create relationships between them.


 RELATIONSHIP_TYPE: Name of the relationship (e.g., FRIEND,
KNOWS).
 Direction is specified using --> or <--.
Example:

MATCH (p1:Person {name: "Alice"}), (p2:Person {name: "Bob"})

CREATE (p1)-[:FRIEND]->(p2);

Rules:
 Relationships are always directed.
 Relationship type must be enclosed in [:RELATIONSHIP_TYPE].
 MATCH is used to find existing nodes before creating the relationship.

4. Syntax for Adding Properties to Nodes

MATCH (alias:Label {property: value})

SET alias.new_property = new_value;

Example:

MATCH (p:Person {name: "Alice"})

SET p.hobby = "Painting";

Rules:
 MATCH is used to locate the node.
 SET adds or updates properties dynamically.

5. Syntax for Adding Properties to Relationships

MATCH (alias1:Label1)-[alias2:RELATIONSHIP_TYPE]->(alias3:Label2)

SET alias2.property = value;

Example:

MATCH (p1:Person {name: "Alice"})-[r:FRIEND]->(p2:Person {name:


"Bob"})

SET r.since = 2020;

Rules:
 Properties on relationships are stored in the relationship object.
 Use the relationship alias (r in the example) for referencing.

6. Syntax for Creating Nodes and Relationships Together

CREATE (alias1:Label1 {property1: value1})-[:RELATIONSHIP_TYPE]-


>(alias2:Label2 {property2: value2});

Example:

CREATE (p1:Person {name: "Eve", age: 40})-[:KNOWS]->(p2:Person


{name: "Frank", age: 45});

Rules:
 Both nodes and the relationship are created in a single query.
 No need for MATCH when creating new nodes.

7. Syntax for Viewing Data


Viewing All Nodes:
MATCH (n)

RETURN n;

Viewing Nodes and Relationships:

MATCH (alias1)-[alias2:RELATIONSHIP_TYPE]->(alias3)

RETURN alias1, alias2, alias3;

Rules:
 MATCH retrieves nodes or relationships.
 RETURN specifies what to display.

8. Syntax for Deleting Nodes and Relationships


Deleting a Node:

MATCH (alias:Label {property: value})

DELETE alias;

Deleting a Node and Its Relationships:

MATCH (alias:Label {property: value})

DETACH DELETE alias;

Rules:
 DELETE removes nodes without relationships.
 DETACH DELETE removes a node along with all its relationships.

General Rules for Neo4j Syntax


1. Labels and Relationship Types:
o Labels and relationship types are case-sensitive and should follow
consistent naming conventions.
o Use uppercase for relationship types (e.g., FRIEND).
2. Properties:
o Property keys are case-sensitive and must be strings or primitives.
o Use : between property keys and values.
3. Node and Relationship Aliases:
o Aliases (e.g., n, p1, r) are optional but useful for referencing nodes or
relationships in queries.
4. Keywords:
o Keywords like MATCH, CREATE, RETURN, SET, DELETE are
case-insensitive but often written in uppercase for clarity.
5. Directionality:
o Relationships must specify direction (-->, <--, or -- for bidirectional).
These rules ensure consistent, predictable interaction with the Neo4j database.

Neo4j Create Node

In the Neo4j to create node you will have to state CREATE statement. With the
help of cypher language it is easy to create nodes, properties and relation between
nodes.

CREATE

(Alice:Person {name: "Alice", age: 30, city: "New York"}),

(Bob:Person {name: "Bob", age: 25, city: "Chicago"}),

(Charlie:Person {name: "Charlie", age: 35, city: "San Francisco"}),

(Diana:Person {name: "Diana", age: 28, city: "Boston"}),

(Alice)-[:FRIEND]->(Bob),

(Alice)-[:COLLEAGUE]->(Charlie),

(Diana)-[:FRIEND]->(Alice);

1. Creating Nodes

// Create a single node

CREATE (n:Person {name: "Alice", age: 30, city: "New York"});

2. Creating Multiple Nodes

// Create multiple nodes


CREATE

(p1:Person {name: "Bob", age: 25, city: "Chicago"}),

(p2:Person {name: "Charlie", age: 35, city: "San Francisco"}),

(p3:Person {name: "Diana", age: 28, city: "Boston"});

3. Creating Relationships

Relationships in Neo4j are directional. You can use --> or <-- to define the
direction.

Example 1: Creating a relationship between two nodes

MATCH (p1:Person {name: "Alice"}), (p2:Person {name: "Bob"})

CREATE (p1)-[:FRIEND]->(p2);

Example 2: Creating multiple relationships

MATCH (p1:Person {name: "Alice"}), (p2:Person {name: "Charlie"})

CREATE (p1)-[:COLLEAGUE]->(p2);

MATCH (p3:Person {name: "Diana"}), (p1:Person {name: "Alice"})

CREATE (p3)-[:FRIEND]->(p1);

4. Adding Properties to Nodes

You can add properties to existing nodes using the SET command.

Example:

MATCH (p:Person {name: "Alice"})

SET p.hobby = "Painting";


5. Adding Properties to Relationships

Properties can also be added to relationships.

Example:

MATCH (p1:Person {name: "Alice"})-[r:FRIEND]->(p2:Person {name: "Bob"})

SET r.since = 2020;

6. Creating Nodes and Relationships Together

You can create nodes and relationships in a single query.

Example:

CREATE (p1:Person {name: "Eve", age: 40, city: "Los Angeles"})-[:KNOWS]-


>(p2:Person {name: "Frank", age: 45, city: "Seattle"});

7. Viewing Data

To view all nodes and their properties:

MATCH (n)

RETURN n;

To view nodes and relationships:

MATCH (p1)-[r]->(p2)

RETURN p1, r, p2;

8. Deleting Nodes and Relationships

Example 1: Deleting a single node


MATCH (p:Person {name: "Eve"})

DELETE p;

Example 2: Deleting a node and its relationships

MATCH (p:Person {name: "Alice"})

DETACH DELETE p;

Sample Dataset for Practice

To create a sample graph:

CREATE

(Alice:Person {name: "Alice", age: 30, city: "New York"}),

(Bob:Person {name: "Bob", age: 25, city: "Chicago"}),

(Charlie:Person {name: "Charlie", age: 35, city: "San Francisco"}),

(Diana:Person {name: "Diana", age: 28, city: "Boston"}),

(Alice)-[:FRIEND]->(Bob),

(Alice)-[:COLLEAGUE]->(Charlie),

(Diana)-[:FRIEND]->(Alice);

This creates nodes with properties and relationships among them.

You might also like