{"id":17258,"date":"2024-05-28T02:18:51","date_gmt":"2024-05-28T09:18:51","guid":{"rendered":"https:\/\/www.pingcap.com\/?post_type=article&#038;p=17258"},"modified":"2024-05-28T02:18:54","modified_gmt":"2024-05-28T09:18:54","slug":"creating-a-knowledge-graph-with-mysql","status":"publish","type":"article","link":"https:\/\/www.pingcap.com\/ko\/article\/creating-a-knowledge-graph-with-mysql\/","title":{"rendered":"Creating a Knowledge Graph with MySQL: A Beginner\u2019s Guide"},"content":{"rendered":"<p>In today\u2019s 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.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"What_is_a_Knowledge_Graph\"><\/span>What is a Knowledge Graph?<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>A knowledge graph represents a network of interlinked descriptions of entities \u2014 objects, events, or concepts \u2014 where each entity is connected to others through relationships. It&#8217;s widely used in various applications, such as semantic search, recommendation systems, and AI.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Setting_Up_the_MySQL_Database\"><\/span>Setting Up the MySQL Database<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>To begin, we\u2019ll need a <a href=\"https:\/\/tidb.cloud\/\">MySQL database<\/a>. This tutorial assumes you have MySQL installed and have basic familiarity with SQL operations. We will set up two primary tables: <code>entities<\/code> \uadf8\ub9ac\uace0 <code>relationships<\/code>.<\/p>\n\n\n\n<p class=\"has-medium-font-size\"><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-cyan-blue-color\"><strong>Spin up a MySQL database in seconds with TiDB Serverless. <\/strong><\/mark><\/p>\n\n\n\n<p><a href=\"https:\/\/tidbcloud.com\/free-trial\/\" class=\"button\" target=\"_blank\" data-gtag=\"event:go_to_cloud_signup,product_type:serverless,button_name:Start Instantly,position:article_middle_cta\" rel=\"noopener\">Start Instantly<\/a><\/p>\n\n\n\n<p><strong>Step 1: Designing the Table Schema<\/strong><\/p>\n\n\n\n<p><strong>1. Entities Table<\/strong><\/p>\n\n\n\n<p>This table will store information about the entities. Each entity has a unique <code>id<\/code>, a <code>name<\/code>, and a <code>description<\/code> to explain what the entity is about. Here\u2019s the SQL code to create the <code>entities<\/code> table:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>CREATE TABLE `entities` (\n  `id` int(11) NOT NULL AUTO_INCREMENT,\n  `name` varchar(512) DEFAULT NULL,\n  `description` text DEFAULT NULL,\n  PRIMARY KEY (`id`)\n) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin;<\/code><\/pre>\n\n\n\n<p><strong>2. Relationships Table<\/strong><\/p>\n\n\n\n<p>The <code>relationships<\/code> table captures how entities are related. It includes a <code>source_entity_id<\/code> \uadf8\ub9ac\uace0 <code>target_entity_id<\/code> to denote the start and end of a relationship, and a <code>relationship_desc<\/code> which describes the nature of the relationship. Here\u2019s the SQL code for this table:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>CREATE TABLE `relationships` (\n  `id` int(11) NOT NULL AUTO_INCREMENT,\n  `source_entity_id` int(11) DEFAULT NULL,\n  `target_entity_id` int(11) DEFAULT NULL,\n  `relationship_desc` text DEFAULT NULL,\n  PRIMARY KEY (`id`),\n  KEY `fk_1` (`source_entity_id`),\n  KEY `fk_2` (`target_entity_id`),\n  CONSTRAINT `fk_1` FOREIGN KEY (`source_entity_id`) REFERENCES `entities` (`id`),\n  CONSTRAINT `fk_2` FOREIGN KEY (`target_entity_id`) REFERENCES `entities` (`id`)\n) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin;<\/code><\/pre>\n\n\n\n<p><strong>Step 2: Inserting Data<\/strong><\/p>\n\n\n\n<p>With the tables created, the next step is to populate them with data.<\/p>\n\n\n\n<p><strong>Insert Entities<\/strong><\/p>\n\n\n\n<p>Here\u2019s how you can add some entities into your database:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>INSERT INTO entities (name, description) VALUES\n('Elon Reeve Musk', 'Elon Reeve Musk is a businessman and investor, born in Pretoria, South Africa.'),\n('Tesla, Inc.', 'American electric vehicle and clean energy company based in Palo Alto, California.');<\/code><\/pre>\n\n\n\n<p><strong>Insert Relationships<\/strong><\/p>\n\n\n\n<p>Once entities are inserted, we can define relationships between them:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>INSERT INTO relationships (source_entity_id, target_entity_id, relationship_desc) VALUES\n(1, 2, 'CEO of');<\/code><\/pre>\n\n\n\n<p><strong>Step 3: Querying the Knowledge Graph<\/strong><\/p>\n\n\n\n<p>To see how entities are interconnected through relationships, you can execute the following SQL query:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT e1.name AS `Entity`, r.relationship_desc AS `Relationship`, e2.name AS `Related Entity`\nFROM relationships r\nJOIN entities e1 ON r.source_entity_id = e1.id\nJOIN entities e2 ON r.target_entity_id = e2.id;<\/code><\/pre>\n\n\n\n<p>This query will help you visualize the relationships in a tabular format, showing how entities are linked.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Conclusion\"><\/span>Conclusion<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>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.<\/p>\n\n\n\n<p class=\"has-medium-font-size\"><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-cyan-blue-color\"><strong>Spin up a MySQL database in seconds with TiDB Serverless. <\/strong><\/mark><\/p>\n\n\n\n<p><a href=\"https:\/\/tidbcloud.com\/free-trial\/\" class=\"button\" target=\"_blank\" data-gtag=\"event:go_to_cloud_signup,product_type:serverless,button_name:Start Instantly,position:article_bottom_cta\" rel=\"noopener\">Start Instantly<\/a><\/p>","protected":false},"excerpt":{"rendered":"<p>In today\u2019s 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? [&hellip;]<\/p>\n","protected":false},"author":8,"featured_media":0,"template":"","class_list":["post-17258","article","type-article","status-publish","hentry"],"acf":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.9 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Creating a Knowledge Graph with MySQL: A Beginner\u2019s Guide<\/title>\n<meta name=\"description\" content=\"Explore how to create and manage a basic knowledge graph using MySQL, focusing on designing table schemas and inserting example data.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.pingcap.com\/ko\/article\/creating-a-knowledge-graph-with-mysql\/\" \/>\n<meta property=\"og:locale\" content=\"ko_KR\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Creating a Knowledge Graph with MySQL: A Beginner\u2019s Guide\" \/>\n<meta property=\"og:description\" content=\"Explore how to create and manage a basic knowledge graph using MySQL, focusing on designing table schemas and inserting example data.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.pingcap.com\/ko\/article\/creating-a-knowledge-graph-with-mysql\/\" \/>\n<meta property=\"og:site_name\" content=\"TiDB\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/facebook.com\/pingcap2015\" \/>\n<meta property=\"article:modified_time\" content=\"2024-05-28T09:18:54+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/static.pingcap.com\/files\/2024\/09\/11005522\/Homepage-Ad.png\" \/>\n\t<meta property=\"og:image:width\" content=\"1440\" \/>\n\t<meta property=\"og:image:height\" content=\"714\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:site\" content=\"@PingCAP\" \/>\n<meta name=\"twitter:label1\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data1\" content=\"3\ubd84\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.pingcap.com\/article\/creating-a-knowledge-graph-with-mysql\/\",\"url\":\"https:\/\/www.pingcap.com\/article\/creating-a-knowledge-graph-with-mysql\/\",\"name\":\"Creating a Knowledge Graph with MySQL: A Beginner\u2019s Guide\",\"isPartOf\":{\"@id\":\"https:\/\/www.pingcap.com\/#website\"},\"datePublished\":\"2024-05-28T09:18:51+00:00\",\"dateModified\":\"2024-05-28T09:18:54+00:00\",\"description\":\"Explore how to create and manage a basic knowledge graph using MySQL, focusing on designing table schemas and inserting example data.\",\"breadcrumb\":{\"@id\":\"https:\/\/www.pingcap.com\/article\/creating-a-knowledge-graph-with-mysql\/#breadcrumb\"},\"inLanguage\":\"ko-KR\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.pingcap.com\/article\/creating-a-knowledge-graph-with-mysql\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.pingcap.com\/article\/creating-a-knowledge-graph-with-mysql\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/www.pingcap.com\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Articles\",\"item\":\"https:\/\/www.pingcap.com\/article\/\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"Creating a Knowledge Graph with MySQL: A Beginner\u2019s Guide\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.pingcap.com\/#website\",\"url\":\"https:\/\/www.pingcap.com\/\",\"name\":\"TiDB\",\"description\":\"TiDB | SQL at Scale\",\"publisher\":{\"@id\":\"https:\/\/www.pingcap.com\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.pingcap.com\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"ko-KR\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/www.pingcap.com\/#organization\",\"name\":\"PingCAP\",\"url\":\"https:\/\/www.pingcap.com\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"ko-KR\",\"@id\":\"https:\/\/www.pingcap.com\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/static.pingcap.com\/files\/2021\/11\/pingcap-logo.png\",\"contentUrl\":\"https:\/\/static.pingcap.com\/files\/2021\/11\/pingcap-logo.png\",\"width\":811,\"height\":232,\"caption\":\"PingCAP\"},\"image\":{\"@id\":\"https:\/\/www.pingcap.com\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/facebook.com\/pingcap2015\",\"https:\/\/x.com\/PingCAP\",\"https:\/\/linkedin.com\/company\/pingcap\",\"https:\/\/youtube.com\/channel\/UCuq4puT32DzHKT5rU1IZpIA\"]}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Creating a Knowledge Graph with MySQL: A Beginner\u2019s Guide","description":"Explore how to create and manage a basic knowledge graph using MySQL, focusing on designing table schemas and inserting example data.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.pingcap.com\/ko\/article\/creating-a-knowledge-graph-with-mysql\/","og_locale":"ko_KR","og_type":"article","og_title":"Creating a Knowledge Graph with MySQL: A Beginner\u2019s Guide","og_description":"Explore how to create and manage a basic knowledge graph using MySQL, focusing on designing table schemas and inserting example data.","og_url":"https:\/\/www.pingcap.com\/ko\/article\/creating-a-knowledge-graph-with-mysql\/","og_site_name":"TiDB","article_publisher":"https:\/\/facebook.com\/pingcap2015","article_modified_time":"2024-05-28T09:18:54+00:00","og_image":[{"width":1440,"height":714,"url":"https:\/\/static.pingcap.com\/files\/2024\/09\/11005522\/Homepage-Ad.png","type":"image\/png"}],"twitter_card":"summary_large_image","twitter_site":"@PingCAP","twitter_misc":{"Est. reading time":"3\ubd84"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.pingcap.com\/article\/creating-a-knowledge-graph-with-mysql\/","url":"https:\/\/www.pingcap.com\/article\/creating-a-knowledge-graph-with-mysql\/","name":"Creating a Knowledge Graph with MySQL: A Beginner\u2019s Guide","isPartOf":{"@id":"https:\/\/www.pingcap.com\/#website"},"datePublished":"2024-05-28T09:18:51+00:00","dateModified":"2024-05-28T09:18:54+00:00","description":"Explore how to create and manage a basic knowledge graph using MySQL, focusing on designing table schemas and inserting example data.","breadcrumb":{"@id":"https:\/\/www.pingcap.com\/article\/creating-a-knowledge-graph-with-mysql\/#breadcrumb"},"inLanguage":"ko-KR","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.pingcap.com\/article\/creating-a-knowledge-graph-with-mysql\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.pingcap.com\/article\/creating-a-knowledge-graph-with-mysql\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.pingcap.com\/"},{"@type":"ListItem","position":2,"name":"Articles","item":"https:\/\/www.pingcap.com\/article\/"},{"@type":"ListItem","position":3,"name":"Creating a Knowledge Graph with MySQL: A Beginner\u2019s Guide"}]},{"@type":"WebSite","@id":"https:\/\/www.pingcap.com\/#website","url":"https:\/\/www.pingcap.com\/","name":"\ud2f0DB","description":"TiDB | SQL at Scale","publisher":{"@id":"https:\/\/www.pingcap.com\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.pingcap.com\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"ko-KR"},{"@type":"Organization","@id":"https:\/\/www.pingcap.com\/#organization","name":"PingCAP","url":"https:\/\/www.pingcap.com\/","logo":{"@type":"ImageObject","inLanguage":"ko-KR","@id":"https:\/\/www.pingcap.com\/#\/schema\/logo\/image\/","url":"https:\/\/static.pingcap.com\/files\/2021\/11\/pingcap-logo.png","contentUrl":"https:\/\/static.pingcap.com\/files\/2021\/11\/pingcap-logo.png","width":811,"height":232,"caption":"PingCAP"},"image":{"@id":"https:\/\/www.pingcap.com\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/facebook.com\/pingcap2015","https:\/\/x.com\/PingCAP","https:\/\/linkedin.com\/company\/pingcap","https:\/\/youtube.com\/channel\/UCuq4puT32DzHKT5rU1IZpIA"]}]}},"card_markup":"        <a class=\"card-article\" href=\"https:\/\/www.pingcap.com\/ko\/article\/creating-a-knowledge-graph-with-mysql\/\">            <h3>Creating a Knowledge Graph with MySQL: A Beginner\u2019s Guide<\/h3>            <p>In today\u2019s 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? [&hellip;]<\/p>        <\/a>","_links":{"self":[{"href":"https:\/\/www.pingcap.com\/ko\/wp-json\/wp\/v2\/article\/17258","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.pingcap.com\/ko\/wp-json\/wp\/v2\/article"}],"about":[{"href":"https:\/\/www.pingcap.com\/ko\/wp-json\/wp\/v2\/types\/article"}],"author":[{"embeddable":true,"href":"https:\/\/www.pingcap.com\/ko\/wp-json\/wp\/v2\/users\/8"}],"wp:attachment":[{"href":"https:\/\/www.pingcap.com\/ko\/wp-json\/wp\/v2\/media?parent=17258"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}