{"id":14815,"date":"2021-06-21T07:55:32","date_gmt":"2021-06-20T21:55:32","guid":{"rendered":"https:\/\/database.guide\/?p=14815"},"modified":"2025-12-25T11:44:39","modified_gmt":"2025-12-25T01:44:39","slug":"database-relationships-explained","status":"publish","type":"post","link":"https:\/\/database.guide\/database-relationships-explained\/","title":{"rendered":"Database Relationships Explained"},"content":{"rendered":"\n<p class=\"\">If you&#8217;re new to relational databases, and you&#8217;re trying to get your head around this concept of a &#8220;relationship&#8221; in your database, I hope this article helps.<\/p>\n\n\n\n<!--more-->\n\n\n\n<h2 class=\"wp-block-heading\">What is a Database Relationship?<\/h2>\n\n\n\n<p class=\"\">When it comes to databases, a <dfn><a href=\"https:\/\/database.guide\/what-is-a-relationship\/\" data-type=\"post\" data-id=\"195\">relationship<\/a><\/dfn> is where two or more tables contain related data, and you&#8217;ve configured your database to recognise (and enforce) that fact.<\/p>\n\n\n\n<p class=\"\">The reason you would do this is to ensure that the <a href=\"https:\/\/database.guide\/what-is-data-integrity\/\" data-type=\"post\" data-id=\"483\">integrity of the data<\/a> is not compromised.<\/p>\n\n\n\n<p class=\"\">Consider the following database diagram.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"434\" src=\"https:\/\/database.guide\/wp-content\/uploads\/2021\/02\/Relationship-Diagram.png\" alt=\"Diagram of a database relationship\" class=\"wp-image-14817\" srcset=\"https:\/\/database.guide\/wp-content\/uploads\/2021\/02\/Relationship-Diagram.png 1024w, https:\/\/database.guide\/wp-content\/uploads\/2021\/02\/Relationship-Diagram-300x127.png 300w, https:\/\/database.guide\/wp-content\/uploads\/2021\/02\/Relationship-Diagram-768x326.png 768w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p class=\"\">The dashed lines represent a relationship between the tables. <\/p>\n\n\n\n<p class=\"\">I have a relationship between the <strong>Artists<\/strong> and <strong>Albums<\/strong> tables because the <strong>Albums<\/strong> table contains the <strong>ArtistId<\/strong> (so that I know which artist released each album). The reason I established this relationship is because I don&#8217;t want anyone to enter a non-existent <strong>ArtistId<\/strong>. That is, I don&#8217;t want the <strong>Albums<\/strong> table to contain an <strong>ArtistId<\/strong> that doesn&#8217;t exist in the <strong>Artists<\/strong> table.<\/p>\n\n\n\n<p class=\"\">You can see that I&#8217;ve also established a relationship between the <strong>Genres<\/strong> table and the <strong>Albums<\/strong> table. And I&#8217;ve done this for the same reason &#8211; I don&#8217;t want the <strong>Albums<\/strong> table to contain a <strong>GenreId<\/strong> that doesn&#8217;t exist in the <strong>Genres<\/strong> table. If an album is from a genre that doesn&#8217;t exist in the database, someone will need to first enter that genre into the <strong>Genres<\/strong> table. Once they&#8217;ve done that, they can go ahead and enter the album from that genre.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Types of Relationships<\/h2>\n\n\n\n<p class=\"\">You&#8217;ll often hear of <a href=\"https:\/\/database.guide\/the-3-types-of-relationships-in-database-design\/\" data-type=\"post\" data-id=\"196\">3 types of relationships<\/a> that can be applied to a relational database. Here&#8217;s an overview of each one.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">One-to-One<\/h3>\n\n\n\n<p class=\"\">A <a href=\"https:\/\/database.guide\/what-is-a-one-to-one-relationship\/\" data-type=\"post\" data-id=\"39266\">one-to-one relationship<\/a> is a relationship between two tables where each table can have only one matching row in the other table.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"303\" src=\"https:\/\/database.guide\/wp-content\/uploads\/2021\/02\/Relationship-One-to-One.png\" alt=\"Diagram of a one to one relationship\" class=\"wp-image-14818\" srcset=\"https:\/\/database.guide\/wp-content\/uploads\/2021\/02\/Relationship-One-to-One.png 1024w, https:\/\/database.guide\/wp-content\/uploads\/2021\/02\/Relationship-One-to-One-300x89.png 300w, https:\/\/database.guide\/wp-content\/uploads\/2021\/02\/Relationship-One-to-One-768x227.png 768w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><figcaption class=\"wp-element-caption\">One-to-One Relationship<\/figcaption><\/figure>\n\n\n\n<p class=\"\">Using the above screenshot as an example, the business case is that each employee&#8217;s pay details must be stored in a separate table to the employee&#8217;s contact details. In such a case, there can only be one row in the <strong>Pay<\/strong> table that matches a given employee in the <strong>Employees<\/strong> table. This is a good candidate for a one-to-one relationship.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">One-to-Many<\/h3>\n\n\n\n<p class=\"\">The <a href=\"https:\/\/database.guide\/what-is-a-one-to-many-relationship\/\" data-type=\"post\" data-id=\"39209\">one-to-many relationship<\/a> is similar to the one-to-one relationship, except that it allows multiple matching rows in one of the tables.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"288\" src=\"https:\/\/database.guide\/wp-content\/uploads\/2021\/02\/Relationship-One-to-Many.png\" alt=\"Diagram of a one to many relationship\" class=\"wp-image-14819\" srcset=\"https:\/\/database.guide\/wp-content\/uploads\/2021\/02\/Relationship-One-to-Many.png 1024w, https:\/\/database.guide\/wp-content\/uploads\/2021\/02\/Relationship-One-to-Many-300x84.png 300w, https:\/\/database.guide\/wp-content\/uploads\/2021\/02\/Relationship-One-to-Many-768x216.png 768w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><figcaption class=\"wp-element-caption\">One-to-Many Relationship<\/figcaption><\/figure>\n\n\n\n<p class=\"\">In the above example, each author can have many books, but each book can only have one author. <\/p>\n\n\n\n<p class=\"\">Therefore, the <strong>Books<\/strong> table is allowed to contain multiple rows with the same <strong>AuthorId<\/strong> value. If an author has released five books, then there would be one row in <strong>Authors<\/strong> for that author, and five rows in <strong>Books<\/strong>, each with that author&#8217;s <strong>AuthorId<\/strong>.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Many-to-Many<\/h3>\n\n\n\n<p class=\"\">In a <a href=\"https:\/\/database.guide\/what-is-a-many-to-many-relationship\/\" data-type=\"post\" data-id=\"39180\">many-to-many relationship<\/a>, each side of the relationship can contain multiple rows.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"384\" src=\"https:\/\/database.guide\/wp-content\/uploads\/2021\/02\/Relationship-Many-to-Many.png\" alt=\"Diagram of a many to many relationship\" class=\"wp-image-14820\" srcset=\"https:\/\/database.guide\/wp-content\/uploads\/2021\/02\/Relationship-Many-to-Many.png 1024w, https:\/\/database.guide\/wp-content\/uploads\/2021\/02\/Relationship-Many-to-Many-300x113.png 300w, https:\/\/database.guide\/wp-content\/uploads\/2021\/02\/Relationship-Many-to-Many-768x288.png 768w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><figcaption class=\"wp-element-caption\">Many-to-Many Relationship<\/figcaption><\/figure>\n\n\n\n<p class=\"\">In this example, each book is allowed to have multiple authors. Therefore, I created a lookup table (also known as a &#8220;junction table&#8221;) that stores both the <strong>AuthorId<\/strong> and the <strong>BookId<\/strong>. <\/p>\n\n\n\n<p class=\"\">These two columns could be configured to be the <a rel=\"noreferrer noopener\" aria-label=\"primary key (opens in a new tab)\" href=\"https:\/\/database.guide\/what-is-a-primary-key\/\" target=\"_blank\">primary key<\/a> of the table (in which case they would be a &#8220;composite primary key&#8221; or simply &#8220;composite key&#8221;), or you could create a separate column to be the primary key. <\/p>\n\n\n\n<p class=\"\">Note that the Books table doesn&#8217;t have <strong>AuthorId<\/strong> in this case. That column has been moved to the <strong>AuthorBooks<\/strong> table so that we can have many <strong>AuthorId<\/strong>s for the same <strong>BookId<\/strong>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Creating a Database Relationship<\/h2>\n\n\n\n<p class=\"\">Database relationships can be created via a GUI tool (such as <a href=\"https:\/\/database.guide\/what-is-sql-server-management-studio\/\" title=\"What Is SQL Server Management Studio (SSMS)?\">SQL Server Management Studio<\/a>, Microsoft Access, etc) or by running an <a href=\"https:\/\/database.guide\/what-is-sql\/\" title=\"What is SQL?\">SQL<\/a> statement.<\/p>\n\n\n\n<p class=\"\">Examples:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li class=\"\"><a title=\"How to Create a Relationship in Access\" href=\"https:\/\/database.guide\/how-to-create-a-relationship-in-access\/\">Create a Relationship in Access<\/a><\/li>\n\n\n\n<li class=\"\"><a title=\"How to Create a Relationship in MySQL Workbench\" href=\"https:\/\/database.guide\/how-to-create-a-relationship-in-mysql-workbench\/\">Create a Relationship in MySQL Workbench<\/a><\/li>\n<\/ul>\n\n\n\n<p class=\"\">When creating a relationship with SQL, you do it by applying a <a aria-label=\" (opens in a new tab)\" href=\"https:\/\/database.guide\/what-is-a-foreign-key\/\" target=\"_blank\" rel=\"noreferrer noopener\">foreign key<\/a> constraint against the child table. This foreign key references the primary key in the parent table.<\/p>\n\n\n\n<p class=\"\">Examples<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li class=\"\"><a href=\"https:\/\/database.guide\/create-a-foreign-key-in-sqlite\/\">Create a Foreign Key in SQLite<\/a><\/li>\n\n\n\n<li class=\"\"><a title=\"How to Create a Foreign Key in SQL Server (T-SQL Examples)\" href=\"https:\/\/database.guide\/how-to-create-a-foreign-key-in-sql-server-t-sql-examples\/\">Create a Foreign Key in SQL Server<\/a><\/li>\n\n\n\n<li class=\"\"><a title=\"How to Create a Composite Foreign Key in SQL Server (T-SQL Example)\" href=\"https:\/\/database.guide\/how-to-create-composite-foreign-key-sql-server-t-sql-example\/\">Create a Composite Foreign Key in SQL Server<\/a><\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>If you&#8217;re new to relational databases, and you&#8217;re trying to get your head around this concept of a &#8220;relationship&#8221; in your database, I hope this article helps.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[17],"tags":[19,20],"class_list":["post-14815","post","type-post","status-publish","format-standard","hentry","category-database-concepts","tag-relationships","tag-what-is"],"_links":{"self":[{"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/posts\/14815","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/comments?post=14815"}],"version-history":[{"count":4,"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/posts\/14815\/revisions"}],"predecessor-version":[{"id":47190,"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/posts\/14815\/revisions\/47190"}],"wp:attachment":[{"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/media?parent=14815"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/categories?post=14815"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/tags?post=14815"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}