# CREATE clause

The `CREATE` clause is used to create nodes and relationships in a graph. Additionally `CREATE` is also used for additional database definitions, such as enums.

> **Info**
>
> Indexing can increase performance when executing queries. Please take a look at
> our [documentation on indexing](https://memgraph.com/docs/fundamentals/indexes) for
> more details.

1. [Creating nodes](#1-creating-nodes) 

    1.1. [Creating a single node](#11-creating-a-single-node)

    1.2. [Creating a node with properties](#12-creating-a-node-with-properties)

    1.3. [Creating multiple nodes](#13-creating-multiple-nodes)

    1.4. [Creating node labels dynamically](#14-creating-node-labels-dynamically)

2. [Creating relationships](#2-creating-relationships)

    2.1. [Creating a relationship between two nodes](#21-creating-a-relationship-between-two-nodes)

    2.2. [Creating a relationship with properties](#22-creating-a-relationship-with-properties)

    2.3. [Creating relationship types dynamically](#15-creating-relationship-types-dynamically)

3. [Creating a path](#3-creating-a-path)
4. [Creating an enum](#4-creating-an-enum)

## 1. Creating nodes

### 1.1. Creating a single node

Use the following query to create a single node.
The `RETURN` clause is used to return results. A newly created node can be returned in the same query.

```cypher
CREATE (n)
RETURN n;
```

Output:
```nocopy
+----+
| n  |
+----+
| () |
+----+
```

You can also specify a label while creating a node:

```cypher
CREATE (n:Country)
RETURN n;
```

Output:
```nocopy
+------------+
| n          |
+------------+
| (:Country) |
+------------+
```

If you wish to add multiple labels to a node, use the following syntax:

```cypher
CREATE (n:Country:City)
RETURN n;
```

Output:
```nocopy
+-----------------+
| n               |
+-----------------+
| (:Country:City) |
+-----------------+
```

### 1.2. Creating a node with properties

A node can be created with initial properties.

```cypher
CREATE (n:Country {name: 'San Marino', continent: 'Europe'})
RETURN n;
```

Output:
```nocopy
+------------------------------------------------------+
| n                                                    |
+------------------------------------------------------+
| (:Country {continent: "Europe", name: "San Marino"}) |
+------------------------------------------------------+
```

### 1.3. Creating multiple nodes

To create multiple nodes, separate them with a comma.

```cypher
CREATE (n:Country), (m:City)
RETURN n,m;
```

Output:
```nocopy
+------------+------------+
| n          | m          |
+------------+------------+
| (:Country) | (:City)    |
+------------+------------+
```

### 1.4. Creating node labels dynamically

Node labels can be created dynamically from variable values. The functionality only works with CREATE.
Matching and merging of dynamic node labels is not supported since query plan and scanning indices are created upfront.

```cypher
WITH {label_value: "Label"} as x
CREATE (n:x.label_value) RETURN n;
```

Output:
```nocopy
+------------+
| n          |
+------------+
| (:Label)   |
+------------+
```

This functionality can especially be useful when importing data from CSV or other sources, since at that point you can inject the arbitrary labels
into the graph.

## 2. Creating relationships

### 2.1. Creating a relationship between two nodes

To create a relationship between two nodes, we need to specify which nodes 
either by creating them or filtering them with the `WHERE` clause.

```cypher
CREATE (c1:Country {name: 'Belgium'}), (c2:Country {name: 'Netherlands'})
CREATE (c1)-[r:BORDERS_WITH]->(c2)
RETURN r;
```

Output:
```nocopy
+----------------+
| r              |
+----------------+
| [BORDERS_WITH] |
+----------------+
```

If the nodes already exist, the query would look like this:

```cypher
MATCH (c1:Country),(c2:Country)
WHERE c1.name = 'Belgium' AND c2.name = 'Netherlands'
CREATE (c1)-[r:NEIGHBOURS]->(c2)
RETURN r;
```

Output:
```nocopy
+--------------+
| r            |
+--------------+
| [NEIGHBOURS] |
+--------------+
```

### 2.2. Creating a relationship with properties

You can add properties to a relationship at the time of creation.

```cypher
MATCH (c1:Country),(c2:Country)
WHERE c1.name = 'Belgium' AND c2.name = 'Netherlands'
CREATE (c1)-[r:BORDERS_WITH {length: '30KM'}]->(c2)
RETURN r;
```

Output:
```nocopy
+---------------------------------+
| r                               |
+---------------------------------+
| [BORDERS_WITH {length: "30KM"}] |
+---------------------------------+
```

### 2.3. Creating relationship types dynamically

Relationship types can be created dynamically from variable values. The functionality only works with CREATE.
Matching and merging of dynamic relationship types is not supported since query plan and scanning indices are created upfront.

```cypher
WITH {edge_type_value: "EDGE_TYPE"} as x
CREATE ()-[r:x.edge_type_value]->() RETURN r;
```

Output:
```nocopy
+--------------+
| n            |
+--------------+
| [:EDGE_TYPE] |
+--------------+
```

This functionality can especially be useful when importing data from CSV or other sources, since at that point you can inject the arbitrary 
edge types into the graph.

## 3. Creating a path

When creating a path all the entities of the pattern will be created.

```cypher
CREATE p=((n:Country {name: 'Belgium'})-[r:BORDERS_WITH {length: '30KM'}]->(m:Country {name: 'Netherlands'}))
RETURN p;
```

Output:
```nocopy
+------------------------------------------------------------------------------------------------+
| p                                                                                              |
+------------------------------------------------------------------------------------------------+
| (:Country {name: "Belgium"})-[BORDERS_WITH {length: "30KM"}]->(:Country {name: "Netherlands"}) |
+------------------------------------------------------------------------------------------------+
```

## 4. Creating an enum

To use enums in queries (as values, properties, etc) they first need to be created.

```cypher
CREATE ENUM Suit VALUES { Hearts, Diamonds, Clubs, Spades };
```

[ALTER](https://memgraph.com/docs/querying/clauses/alter) can be used to modify the enums afterwards.
