Book a Demo Start Instantly

In today’s data-driven world, knowledge graphs are becoming increasingly important for understanding complex relationships between entities and extracting valuable insights. This guide will demonstrate how to create and manage a basic knowledge graph using MySQL, focusing on designing table schemas and inserting example data without delving into advanced vector operations.

What is a Knowledge Graph?

A knowledge graph represents a network of interlinked descriptions of entities — objects, events, or concepts — where each entity is connected to others through relationships. It’s widely used in various applications, such as semantic search, recommendation systems, and AI.

Setting Up the MySQL Database

To begin, we’ll need a MySQL database. This tutorial assumes you have MySQL installed and have basic familiarity with SQL operations. We will set up two primary tables: entities and relationships.

Spin up a MySQL database in seconds with TiDB Serverless.

Start Instantly

Step 1: Designing the Table Schema

1. Entities Table

This table will store information about the entities. Each entity has a unique id, a name, and a description to explain what the entity is about. Here’s the SQL code to create the entities table:

CREATE TABLE `entities` (
  `name` varchar(512) DEFAULT NULL,
  `description` text DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin;

2. Relationships Table

The relationships table captures how entities are related. It includes a source_entity_id and target_entity_id to denote the start and end of a relationship, and a relationship_desc which describes the nature of the relationship. Here’s the SQL code for this table:

CREATE TABLE `relationships` (
  `source_entity_id` int(11) DEFAULT NULL,
  `target_entity_id` int(11) DEFAULT NULL,
  `relationship_desc` text DEFAULT NULL,
  PRIMARY KEY (`id`),
  KEY `fk_1` (`source_entity_id`),
  KEY `fk_2` (`target_entity_id`),
  CONSTRAINT `fk_1` FOREIGN KEY (`source_entity_id`) REFERENCES `entities` (`id`),
  CONSTRAINT `fk_2` FOREIGN KEY (`target_entity_id`) REFERENCES `entities` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin;

Step 2: Inserting Data

With the tables created, the next step is to populate them with data.

Insert Entities

Here’s how you can add some entities into your database:

INSERT INTO entities (name, description) VALUES
('Elon Reeve Musk', 'Elon Reeve Musk is a businessman and investor, born in Pretoria, South Africa.'),
('Tesla, Inc.', 'American electric vehicle and clean energy company based in Palo Alto, California.');

Insert Relationships

Once entities are inserted, we can define relationships between them:

INSERT INTO relationships (source_entity_id, target_entity_id, relationship_desc) VALUES
(1, 2, 'CEO of');

Step 3: Querying the Knowledge Graph

To see how entities are interconnected through relationships, you can execute the following SQL query:

SELECT AS `Entity`, r.relationship_desc AS `Relationship`, AS `Related Entity`
FROM relationships r
JOIN entities e1 ON r.source_entity_id =
JOIN entities e2 ON r.target_entity_id =;

This query will help you visualize the relationships in a tabular format, showing how entities are linked.


This tutorial provided a foundational approach to creating a knowledge graph using MySQL. By understanding how to set up and query relationships between entities, you can begin to model complex networks and enhance your applications with rich, interconnected data. As your requirements grow, consider extending this model with more sophisticated attributes and tables to capture detailed aspects of your data universe.

Spin up a MySQL database in seconds with TiDB Serverless.

Start Instantly

Last updated May 28, 2024

Spin up a Serverless database with 25GiB free resources.

Start Now