Tutorial: tables and graphs in one database
Start with a new database
Section titled “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.
% coradb createdb --db-volume-size=20M testdb en_USStep 2: start the server
Start the CoraDB service and then start the testdb database server.
% coradb service start% coradb server start testdbStep 3: connect to the database
Use the corasql interpreter to connect to testdb as the dba user.
% corasql -u dba testdb1. Defining the schema
Section titled “1. Defining the schema”Let’s start by defining the graph and relational schema.
-- Vertex labelsCREATE 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 labelsCREATE 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
Section titled “2. Inserting data”Insert vertices
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:
-- Route EUX-12: Belgrade ↔ BudapestINSERT 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 ↔ SofiaINSERT 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 ↔ BelgradeINSERT 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 ↔ ViennaINSERT 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:
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
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
Section titled “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
SELECT r.Code, r.Type, c.Name AS CityFROM 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
SELECT comp.Name AS Company, r.Code AS RouteCode, r.TypeFROM 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:
SELECT dest.Name AS Destination, r.Code AS RouteFROM 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:
SELECT DISTINCT dest.Name AS Destination, r1.Code AS FirstRoute, mid.Name AS ChangeAt, r2.Code AS SecondRouteFROM 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.idORDER BY 1;| Destination | FirstRoute | ChangeAt | SecondRoute |
|---|---|---|---|
| Belgrade | EUX-13 | Sofia | EUX-14 |
4. Combining the tables and the graph
Section titled “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 for more examples.
Example 1 – tickets with their passenger, route and operator
SELECT t.id AS TicketID, p.Name AS Passenger, r.Code AS RouteCode, comp.Name AS Operator, t.PriceFROM 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.RouteIDORDER 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
SELECT comp.Name, comp.Headquarters, r.Code AS RouteFROM Company comp LEFT OUTER JOIN MATCH (r:Route)-[:Connects]->(c:City) ON c.Name = comp.HeadquartersORDER 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:
SELECT rt.Code, rt.Type, rt.Distance, c2.Name AS DestinationFROM 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:
SELECT c1.Name AS FromCity, c2.Name AS ToCity, comp.Name AS OperatorFROM MATCH (comp:Company)-[:Operates]->(r:Route), (r:Route)-[:Connects]->(c1:City), (r:Route)-[:Connects]->(c2:City)WHERE comp.FleetSize > 100 AND c1.id < c2.idORDER 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:
SELECT p.Name AS Passenger, p.HomeCityFROM 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
Section titled “5. Inspecting the graph”The JSON graph functions return nodes, edges and subgraphs as JSON, for applications and visualisation tools:
SELECT LABEL(v) AS vertex_label, JSON_NODE_PROPERTIES(v) AS propsFROM 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:
SELECT JSON_GRAPH(comp, o, r) AS subgraphFROM MATCH (comp:Company)-[o:Operates]->(r:Route)WHERE comp.Name = 'Danube Lines';{"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
Section titled “6. Summary”You have now:
- Created relational tables and a graph (vertex and edge tables) in one database
- Inserted rows, nodes and edges with ordinary SQL
- Queried the graph with
MATCH - Joined graph patterns with relational tables in single queries
- 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 for more hybrid queries on the e-commerce database
- MATCH clause (graph patterns) for the complete syntax
- Explore
ANY_SHORTEST,ALL_SHORTESTand the graph functions - Build more complex hybrid analytic queries
Explore a ready-made graph
Section titled “Explore a ready-made graph”For a larger dataset, load the e-commerce sample database: 1,000 customers, 2,115 products, 106 brands and nine kinds of relationships, plus relational order tables. All examples of the SELECT and MATCH pages run on it.