---
title: "Tutorial: tables and graphs in one database"
description: "Step-by-step CoraDB tutorial: create tables and a graph in one database, query the graph with MATCH and combine both in one query."
url: "https://www.coradb.ai/manual/graph/graph_getting_started.html"
markdown_url: "https://www.coradb.ai/manual/markdown/graph/graph_getting_started.md"
product: "CoraDB"
version: "1.0"
keywords: "coradb tutorial, hybrid relational graph tutorial, coradb graph tutorial, create vertex table, create edge table, insert vertex, insert edge, graph database tutorial"
---

# Tutorial: tables and graphs in one database

## Start with a new database

CoraDB is a unified database engine that blends relational SQL with native graph capabilities: you store, update and query graph structures directly alongside relational data, with the same SQL engine and the same transactions.

In this tutorial you build one database that holds both kinds of data, and then query them together:

- A **transportation network** as a graph: **cities**, **companies** and **routes** are vertex tables, **connects** and **operates** are edge tables;
- **passengers** and their **tickets** as ordinary relational tables, which refer to the graph by ID.

The goal is the hybrid queries of step 4: one `SELECT` that joins the passengers and tickets to the companies and routes of the graph. Steps 1 to 3 build and explore the data; graph and relational tables are created, filled and queried with the same SQL.

The following steps show how to quickly set up and connect to a CoraDB database.

**Step 1: create the database**

Use the **coradb createdb** utility to create a new database. The **--db-volume-size** option sets the initial data volume size. The example below creates a database named *testdb* with a 20 MB volume using the **en_US** locale.

``` bash
% coradb createdb --db-volume-size=20M testdb en_US
```

**Step 2: start the server**

Start the CoraDB service and then start the *testdb* database server.

``` bash
% coradb service start
% coradb server start testdb
```

**Step 3: connect to the database**

Use the **corasql** interpreter to connect to *testdb* as the **dba** user.

``` bash
% corasql -u dba testdb
```

### 1. Defining the schema

Let’s start by defining the graph and relational schema.

``` sql
-- Vertex labels
CREATE VERTEX TABLE City (
    id INTEGER PRIMARY KEY,
    Name STRING,
    Country STRING,
    Population INTEGER
);

CREATE VERTEX TABLE Company (
    id INTEGER PRIMARY KEY,
    Name STRING,
    FleetSize INTEGER,
    Headquarters STRING
);

CREATE VERTEX TABLE Route (
    id INTEGER PRIMARY KEY,
    Code STRING,
    Type STRING,        -- "bus", "train", "flight"
    Distance INTEGER,
    Duration INTEGER
);

-- Edge labels
CREATE EDGE TABLE Connects (
    Since INTEGER
);

CREATE EDGE TABLE Operates (
    Since INTEGER
);

-- Relational tables (non-graph)
CREATE TABLE Passenger (
    id INTEGER PRIMARY KEY,
    Name STRING,
    HomeCity STRING
);

CREATE TABLE Ticket (
    id INTEGER PRIMARY KEY,
    PassengerID INTEGER,
    CompanyID INTEGER,
    RouteID INTEGER,
    Price INTEGER
);
```

### 2. Inserting data

**Insert vertices**

``` sql
INSERT INTO City VALUES
    (1, 'Bucharest', 'Romania', 1800000),
    (2, 'Sofia', 'Bulgaria', 1300000),
    (3, 'Belgrade', 'Serbia', 1200000),
    (4, 'Budapest', 'Hungary', 1750000),
    (5, 'Vienna', 'Austria', 1900000);

INSERT INTO Company VALUES
    (10, 'EuroTrans', 150, 'Vienna'),
    (11, 'Danube Lines', 80, 'Budapest'),
    (12, 'Carpathia Express', 120, 'Bucharest');

INSERT INTO Route VALUES
    (100, 'EUX-11', 'train', 240, 180),
    (101, 'EUX-12', 'bus', 370, 280),
    (102, 'EUX-13', 'bus', 380, 300),
    (103, 'EUX-14', 'train', 400, 310);
```

**Insert edges**

Each `INSERT EDGE` must connect exactly one source and one destination vertex. Subqueries that return multiple rows are not allowed.

Connect routes to the cities they serve:

``` sql
-- Route EUX-12: Belgrade ↔ Budapest
INSERT EDGE
    FROM (SELECT r FROM Route r WHERE r.Code='EUX-12')
    TO   (SELECT c FROM City c WHERE c.Name='Belgrade')
    INTO Connects VALUES (2020);

INSERT EDGE
    FROM (SELECT r FROM Route r WHERE r.Code='EUX-12')
    TO   (SELECT c FROM City c WHERE c.Name='Budapest')
    INTO Connects VALUES (2020);

-- Route EUX-13: Bucharest ↔ Sofia
INSERT EDGE
    FROM (SELECT r FROM Route r WHERE r.Code='EUX-13')
    TO   (SELECT c FROM City c WHERE c.Name='Bucharest')
    INTO Connects VALUES (2021);

INSERT EDGE
    FROM (SELECT r FROM Route r WHERE r.Code='EUX-13')
    TO   (SELECT c FROM City c WHERE c.Name='Sofia')
    INTO Connects VALUES (2021);

-- Route EUX-14: Sofia ↔ Belgrade
INSERT EDGE
    FROM (SELECT r FROM Route r WHERE r.Code='EUX-14')
    TO   (SELECT c FROM City c WHERE c.Name='Sofia')
    INTO Connects VALUES (2022);

INSERT EDGE
    FROM (SELECT r FROM Route r WHERE r.Code='EUX-14')
    TO   (SELECT c FROM City c WHERE c.Name='Belgrade')
    INTO Connects VALUES (2022);

-- Route EUX-11: Budapest ↔ Vienna
INSERT EDGE
    FROM (SELECT r FROM Route r WHERE r.Code='EUX-11')
    TO   (SELECT c FROM City c WHERE c.Name='Budapest')
    INTO Connects VALUES (2023);

INSERT EDGE
    FROM (SELECT r FROM Route r WHERE r.Code='EUX-11')
    TO   (SELECT c FROM City c WHERE c.Name='Vienna')
    INTO Connects VALUES (2023);
```

Assign operating companies to specific routes:

``` sql
INSERT EDGE
    FROM (SELECT comp FROM Company comp WHERE comp.Name='EuroTrans')
    TO   (SELECT r FROM Route r WHERE r.Code='EUX-11')
    INTO Operates VALUES (2015);

INSERT EDGE
    FROM (SELECT comp FROM Company comp WHERE comp.Name='Danube Lines')
    TO   (SELECT r FROM Route r WHERE r.Code='EUX-12')
    INTO Operates VALUES (2018);

INSERT EDGE
    FROM (SELECT comp FROM Company comp WHERE comp.Name='Carpathia Express')
    TO   (SELECT r FROM Route r WHERE r.Code='EUX-13')
    INTO Operates VALUES (2020);
```

**Insert relational data**

``` sql
INSERT INTO Passenger VALUES
    (1, 'Alice', 'Bucharest'),
    (2, 'Boris', 'Sofia'),
    (3, 'Clara', 'Vienna');

INSERT INTO Ticket VALUES
    (101, 1, 12, 102, 120),
    (102, 2, 11, 101, 90),
    (103, 3, 10, 100, 140);
```

### 3. Querying the graph on its own

The examples below show the results for the data inserted above. Table and column names are not case-sensitive; CoraDB stores them in lowercase.

**Example 1 – show all routes and their cities**

```sql
SELECT r.Code, r.Type, c.Name AS City
FROM MATCH (r:Route)-[e:Connects]->(c:City)
ORDER BY r.Code, City;
```

| Code | Type | City |
|---|---|---|
| EUX-11 | train | Budapest |
| EUX-11 | train | Vienna |
| EUX-12 | bus | Belgrade |
| EUX-12 | bus | Budapest |
| EUX-13 | bus | Bucharest |
| EUX-13 | bus | Sofia |
| EUX-14 | train | Belgrade |
| EUX-14 | train | Sofia |

**Example 2 – companies and routes they operate**

```sql
SELECT comp.Name AS Company, r.Code AS RouteCode, r.Type
FROM MATCH (comp:Company)-[:Operates]->(r:Route)
ORDER BY comp.Name;
```

| Company | RouteCode | Type |
|---|---|---|
| Carpathia Express | EUX-13 | bus |
| Danube Lines | EUX-12 | bus |
| EuroTrans | EUX-11 | train |

**Example 3 – following connections**

Edges point from a route to the cities it serves, so two cities are linked when one route points to both. The cities reachable from Bucharest with one route:

```sql
SELECT dest.Name AS Destination, r.Code AS Route
FROM MATCH (r:Route)-[:Connects]->(start:City),
           (r:Route)-[:Connects]->(dest:City)
WHERE start.Name = 'Bucharest' AND dest.id <> start.id;
```

| Destination | Route |
|---|---|
| Sofia | EUX-13 |

With a change of route, the pattern grows by one route and one intermediate city:

```sql
SELECT DISTINCT dest.Name AS Destination, r1.Code AS FirstRoute, mid.Name AS ChangeAt, r2.Code AS SecondRoute
FROM MATCH (r1:Route)-[:Connects]->(start:City),
           (r1:Route)-[:Connects]->(mid:City),
           (r2:Route)-[:Connects]->(mid:City),
           (r2:Route)-[:Connects]->(dest:City)
WHERE start.Name = 'Bucharest' AND mid.id <> start.id AND r2.id <> r1.id AND dest.id <> mid.id
ORDER BY 1;
```

| Destination | FirstRoute | ChangeAt | SecondRoute |
|---|---|---|---|
| Belgrade | EUX-13 | Sofia | EUX-14 |

### 4. Combining the tables and the graph

This is what the database was built for. A **MATCH** pattern can be used like a table in the **FROM** clause and joined with the relational tables in the same statement; see [Relational and graph in one query](https://www.coradb.ai/manual/sql/hybrid.html) for more examples.

**Example 1 – tickets with their passenger, route and operator**

```sql
SELECT t.id AS TicketID, p.Name AS Passenger, r.Code AS RouteCode, comp.Name AS Operator, t.Price
FROM Ticket t
     JOIN Passenger p ON p.id = t.PassengerID
     JOIN MATCH (comp:Company)-[:Operates]->(r:Route)
         ON comp.id = t.CompanyID AND r.id = t.RouteID
ORDER BY Passenger;
```

| TicketID | Passenger | RouteCode | Operator | Price |
|---|---|---|---|---|
| 101 | Alice | EUX-13 | Carpathia Express | 120 |
| 102 | Boris | EUX-12 | Danube Lines | 90 |
| 103 | Clara | EUX-11 | EuroTrans | 140 |

**Example 2 – companies and the routes serving their headquarters city**

```sql
SELECT comp.Name, comp.Headquarters, r.Code AS Route
FROM Company comp
     LEFT OUTER JOIN MATCH (r:Route)-[:Connects]->(c:City)
         ON c.Name = comp.Headquarters
ORDER BY comp.Name, Route;
```

| Name | Headquarters | Route |
|---|---|---|
| Carpathia Express | Bucharest | EUX-13 |
| Danube Lines | Budapest | EUX-11 |
| Danube Lines | Budapest | EUX-12 |
| EuroTrans | Vienna | EUX-11 |

**Example 3 – MATCH with a relational table in FROM**

Routes that reach Vienna:

```sql
SELECT rt.Code, rt.Type, rt.Distance, c2.Name AS Destination
FROM Route rt,
     MATCH (r:Route)-[:Connects]->(c2:City)
WHERE r.id = rt.id AND c2.Name = 'Vienna';
```

| Code | Type | Distance | Destination |
|---|---|---|---|
| EUX-11 | train | 240 | Vienna |

**Example 4 – several patterns with a property filter**

City pairs connected by a route of an operator with more than 100 vehicles:

```sql
SELECT c1.Name AS FromCity, c2.Name AS ToCity, comp.Name AS Operator
FROM MATCH (comp:Company)-[:Operates]->(r:Route),
           (r:Route)-[:Connects]->(c1:City),
           (r:Route)-[:Connects]->(c2:City)
WHERE comp.FleetSize > 100 AND c1.id < c2.id
ORDER BY Operator;
```

| FromCity | ToCity | Operator |
|---|---|---|
| Bucharest | Sofia | Carpathia Express |
| Budapest | Vienna | EuroTrans |

**Example 5 – relational rows filtered by a graph pattern**

Passengers living in a city served by EuroTrans:

```sql
SELECT p.Name AS Passenger, p.HomeCity
FROM Passenger p,
     MATCH (comp:Company)-[:Operates]->(r:Route), (r:Route)-[:Connects]->(c:City)
WHERE comp.Name = 'EuroTrans' AND c.Name = p.HomeCity;
```

| Passenger | HomeCity |
|---|---|
| Clara | Vienna |

### 5. Inspecting the graph

The [JSON graph functions](https://www.coradb.ai/manual/sql/function/graph_fn.html#json-graph-functions) return nodes, edges and subgraphs as JSON, for applications and visualisation tools:

```sql
SELECT LABEL(v) AS vertex_label, JSON_NODE_PROPERTIES(v) AS props
FROM MATCH (v:Route)
WHERE v.Type = 'train'
ORDER BY v.id;
```

| vertex_label | props |
|---|---|
| route | `{"id":"(0\|6465\|1)","properties":{"duration":180,"distance":240,"id":100,"type":"train","code":"EUX-11"}}` |
| route | `{"id":"(0\|6465\|4)","properties":{"duration":310,"distance":400,"id":103,"type":"train","code":"EUX-14"}}` |

`JSON_GRAPH` collects nodes and edges into one subgraph:

```sql
SELECT JSON_GRAPH(comp, o, r) AS subgraph
FROM MATCH (comp:Company)-[o:Operates]->(r:Route)
WHERE comp.Name = 'Danube Lines';
```

```json
{"nodes":[{"id":"(0|6337|2)","label":"company","properties":{"fleetsize":80,"id":11,"headquarters":"Budapest","name":"Danube Lines"}},
          {"id":"(0|6465|2)","label":"route","properties":{"duration":280,"distance":370,"id":101,"type":"bus","code":"EUX-12"}}],
 "edges":[{"id":"(0|6529|1)","startNodeId":"(0|6337|2)","endNodeId":"(0|6465|2)","label":"operates","properties":{"since":2018}}]}
```

The ids are internal object ids and differ from database to database.

### 6. Summary

You have now:

1.  Created relational tables and a graph (vertex and edge tables) in one database
2.  Inserted rows, nodes and edges with ordinary SQL
3.  Queried the graph with `MATCH`
4.  Joined graph patterns with relational tables in single queries
5.  Exported nodes and subgraphs as JSON

CoraDB keeps tabular and connected data in one engine and one query language. Try next:

- [Relational and graph in one query](https://www.coradb.ai/manual/sql/hybrid.html) for more hybrid queries on the e-commerce database
- [MATCH clause (graph patterns)](https://www.coradb.ai/manual/graph/match.html) for the complete syntax
- Explore `ANY_SHORTEST`, `ALL_SHORTEST` and the [graph functions](https://www.coradb.ai/manual/sql/function/graph_fn.html)
- Build more complex hybrid analytic queries

## Explore a ready-made graph

For a larger dataset, load the [e-commerce sample database](https://www.coradb.ai/manual/sample-database.html): 1,000 customers, 2,115 products, 106 brands and nine kinds of relationships, plus relational order tables. All examples of the [SELECT](https://www.coradb.ai/manual/sql/query/select.html) and [MATCH](https://www.coradb.ai/manual/graph/match.html) pages run on it.
