{"id":39112,"date":"2024-09-02T06:16:17","date_gmt":"2024-09-01T20:16:17","guid":{"rendered":"https:\/\/database.guide\/?p=39112"},"modified":"2026-01-21T07:17:40","modified_gmt":"2026-01-20T21:17:40","slug":"compound-keys-explained","status":"publish","type":"post","link":"https:\/\/database.guide\/compound-keys-explained\/","title":{"rendered":"Compound Keys Explained"},"content":{"rendered":"\n<p class=\"\">In SQL databases, a compound key is a type of <a href=\"https:\/\/database.guide\/what-is-a-primary-key\/\" data-type=\"post\" data-id=\"215\">primary key<\/a> that consists of two or more columns combined to uniquely identify each row in a table. The key columns are used together as a single unit to ensure uniqueness.<\/p>\n\n\n\n<p class=\"\">Some within the SQL community define compound keys as <a href=\"https:\/\/database.guide\/what-is-a-composite-primary-key\/\" data-type=\"post\" data-id=\"34893\">composite primary keys<\/a> comprising of <a href=\"https:\/\/database.guide\/what-is-a-foreign-key\/\" data-type=\"post\" data-id=\"217\">foreign keys<\/a> from other tables, so there doesn&#8217;t seem to be an agreed consensus on the precise definition.<\/p>\n\n\n\n<p class=\"\">With that in mind, let&#8217;s explore these definitions of compound keys.<\/p>\n\n\n\n<!--more-->\n\n\n\n<h2 class=\"wp-block-heading\">Example of a Compound Key<\/h2>\n\n\n\n<p class=\"\">Here&#8217;s an example that encapsulates both definitions mentioned:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>-- Create the Courses table\nCREATE TABLE Courses (\n    CourseID VARCHAR(10),\n    CourseName VARCHAR(100),\n    PRIMARY KEY (CourseID)\n);\n\n-- Create the Students table\nCREATE TABLE Students (\n    StudentID INT,\n    StudentName VARCHAR(100),\n    PRIMARY KEY (StudentID)\n);\n\n-- Create the Enrollments table with a compound key\nCREATE TABLE Enrollments (\n    StudentID INT,\n    CourseID VARCHAR(10),\n    EnrollmentDate DATE,\n    Grade CHAR(1),\n    PRIMARY KEY (StudentID, CourseID),\n    FOREIGN KEY (StudentID) REFERENCES Students(StudentID),\n    FOREIGN KEY (CourseID) REFERENCES Courses(CourseID)\n);\n\n-- Insert some sample data\nINSERT INTO Courses (CourseID, CourseName) VALUES\n('CS101', 'Introduction to Programming'),\n('MATH201', 'Calculus I');\n\nINSERT INTO Students (StudentID, StudentName) VALUES\n(1, 'Harry Heckle'),\n(2, 'Mandy Mint');\n\nINSERT INTO Enrollments (StudentID, CourseID, EnrollmentDate, Grade) VALUES\n(1, 'CS101', '2023-09-01', 'A'),\n(1, 'MATH201', '2023-09-01', 'B'),\n(2, 'CS101', '2023-09-02', 'B');<\/code><\/pre>\n\n\n\n<p class=\"\">In this example, we have three tables: <code>Courses<\/code>, <code>Students<\/code>, and <code>Enrollments<\/code>. <\/p>\n\n\n\n<p class=\"\">The <code>Enrollments<\/code> table uses a compound key consisting of <code>StudentID<\/code> and <code>CourseID<\/code>. This compound key ensures that a student can only be enrolled once in a particular course.<\/p>\n\n\n\n<p class=\"\">Therefore, the following <code><a href=\"https:\/\/database.guide\/sql-insert-for-beginners\/\" data-type=\"post\" data-id=\"11415\">INSERT<\/a><\/code> statement will fail if we run it after the previous <code>INSERT<\/code> statement:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>INSERT INTO Enrollments (StudentID, CourseID, EnrollmentDate, Grade) VALUES\n(1, 'CS101', '2024-01-15', 'A');<\/code><\/pre>\n\n\n\n<p class=\"\">It will fail because it tries to insert duplicate data into the compound key.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Compound Keys vs Composite Keys<\/h2>\n\n\n\n<p class=\"\">Compound keys are a form of composite key. But they&#8217;re not necessarily the same thing &#8211; there is a difference between them (depending on who you ask). First of all, let&#8217;s look at the difference based on our initial definition:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li class=\"\"><strong>Composite key<\/strong>: A <a href=\"https:\/\/database.guide\/what-is-a-composite-key-in-sql\/\" data-type=\"post\" data-id=\"39096\">composite key<\/a> is any key that consists of multiple columns. It&#8217;s a broader term that encompasses both compound keys and other multi-column keys.<\/li>\n\n\n\n<li class=\"\"><strong>Compound key<\/strong>: A compound key is a specific type of composite key used as the primary key of a table. All compound keys are composite keys, but not all composite keys are compound keys.<\/li>\n<\/ul>\n\n\n\n<p class=\"\">In essence, compound keys are a subset of composite keys. The main distinction is that compound keys are always used as primary keys, while composite keys can be used in other contexts, such as <a href=\"https:\/\/database.guide\/what-is-a-unique-constraint\/\" data-type=\"post\" data-id=\"37785\">unique constraints<\/a> or foreign keys.<\/p>\n\n\n\n<p class=\"\">However, as mentioned, there appears to be some disagreement within the SQL community about the precise definition of a compound key. Some professionals define a compound key as being a composite key with all its columns being foreign keys of other tables.<\/p>\n\n\n\n<p class=\"\">Let&#8217;s break it down:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li class=\"\"><strong>Composite Key<\/strong>:\n<ul class=\"wp-block-list\">\n<li class=\"\">This term is widely accepted and consistently used across database systems and literature.<\/li>\n\n\n\n<li class=\"\">It simply means a key composed of two or more columns.<\/li>\n\n\n\n<li class=\"\">These columns can be any combination of <a href=\"https:\/\/database.guide\/what-is-an-attribute-in-a-database\/\" data-type=\"post\" data-id=\"47216\">attributes<\/a> (columns), whether they&#8217;re foreign keys or not.<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n\n\n\n<ul class=\"wp-block-list\">\n<li class=\"\"><strong>Compound Key<\/strong>:\n<ul class=\"wp-block-list\">\n<li class=\"\">This term is less consistently defined across the database community.<\/li>\n\n\n\n<li class=\"\">In many contexts, especially in relational database design, it is used to refer to a composite key where the constituent parts are foreign keys to other tables.<\/li>\n\n\n\n<li class=\"\">However, some folk use &#8220;compound key&#8221; interchangeably with &#8220;composite key,&#8221; without the specific foreign key connotation.<\/li>\n\n\n\n<li class=\"\">The &#8220;foreign key&#8221; use of the term is common in the context of modeling relationships between tables, particularly for junction tables in many-to-many relationships.<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n\n\n\n<p class=\"\">Perhaps the reason that the &#8220;foreign key&#8221; use of the term (as composite keys made of foreign keys) is common is that it often arises in the context of representing relationships between entities. When you&#8217;re linking two or more tables together, you frequently end up with a key that&#8217;s composed of foreign keys from those tables.<\/p>\n\n\n\n<p class=\"\">However, it&#8217;s important to note that:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li class=\"\">Not all database professionals or systems make this distinction.<\/li>\n\n\n\n<li class=\"\">A compound key could theoretically include both foreign key columns and non-foreign key columns.<\/li>\n\n\n\n<li class=\"\">Some might use &#8220;compound key&#8221; to specifically refer to a composite key that serves as the primary key of a table, regardless of whether its components are foreign keys.<\/li>\n\n\n\n<li class=\"\">C.J. Date, a prominent figure in relational database theory, and known for his work on the relational model, typically does not differentiate between the terms composite key and compound key.<\/li>\n<\/ol>\n\n\n\n<p class=\"\">So to put it in a nutshell, it&#8217;s not a universally agreed-upon distinction. When discussing these concepts, it&#8217;s often helpful to clarify the specific meaning intended, as interpretations can vary depending on the context or the background of the individuals involved.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">When to Use a Compound Key<\/h2>\n\n\n\n<p class=\"\">OK, now that we&#8217;ve established the vagueness around the definition of a compound key, let&#8217;s take a look at when to use one.<\/p>\n\n\n\n<p class=\"\">When to use a compound key:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li class=\"\"><strong>Natural relationships<\/strong>: When the combination of two or more existing attributes naturally identifies a unique record (like in our <code>Enrollments<\/code> example).<\/li>\n\n\n\n<li class=\"\"><strong>Lack of a single unique identifier<\/strong>: When no single column can guarantee uniqueness across all records.<\/li>\n\n\n\n<li class=\"\"><strong>Enforcing <a href=\"https:\/\/database.guide\/what-is-data-integrity\/\" data-type=\"post\" data-id=\"483\">data integrity<\/a><\/strong>: To prevent duplicate combinations of certain fields.<\/li>\n\n\n\n<li class=\"\"><strong>Representing many-to-many relationships<\/strong>: As seen in the <code>Enrollments<\/code> table, which links <code>Students<\/code> and <code>Courses<\/code>.<\/li>\n\n\n\n<li class=\"\"><strong>Avoiding synthetic keys<\/strong>: When you want to use meaningful business data rather than introducing an artificial identifier.<\/li>\n<\/ul>\n\n\n\n<p class=\"\">However, it&#8217;s important to note that compound keys can sometimes make querying and <a href=\"https:\/\/database.guide\/introduction-to-indexing-in-sql\/\" data-type=\"post\" data-id=\"37679\">indexing<\/a> more complex, and they may be less flexible if the data model changes. In some cases, using a single <a href=\"https:\/\/database.guide\/what-is-a-surrogate-key-in-sql\/\" data-type=\"post\" data-id=\"38766\">surrogate key<\/a> (like an auto-incrementing integer) as the primary key and creating a unique constraint on the compound of columns might be a more flexible approach.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Summary<\/h2>\n\n\n\n<p class=\"\">So to summarize, while there isn&#8217;t a clear consensus around the precise definition of a compound key, it&#8217;s generally accepted that it&#8217;s a primary key consisting of multiple columns. Some go a step further and add that those columns should be foreign keys in other tables.<\/p>\n\n\n\n<p class=\"\">There are also some other nuanced arguments in addition to what I&#8217;ve discussed here.<\/p>\n\n\n\n<p class=\"\">In any case, it pays to clarify the specific meaning intended, so that all parties are on the same page when discussing compound keys.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In SQL databases, a compound key is a type of primary key that consists of two or more columns combined to uniquely identify each row in a table. The key columns are used together as a single unit to ensure uniqueness. Some within the SQL community define compound keys as composite primary keys comprising of &#8230; <a title=\"Compound Keys Explained\" class=\"read-more\" href=\"https:\/\/database.guide\/compound-keys-explained\/\" aria-label=\"Read more about Compound Keys Explained\">Read more<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[17,7],"tags":[171,50,18,20],"class_list":["post-39112","post","type-post","status-publish","format-standard","hentry","category-database-concepts","category-sql","tag-constraints","tag-database-design","tag-database-terms","tag-what-is"],"_links":{"self":[{"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/posts\/39112","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=39112"}],"version-history":[{"count":5,"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/posts\/39112\/revisions"}],"predecessor-version":[{"id":47510,"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/posts\/39112\/revisions\/47510"}],"wp:attachment":[{"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/media?parent=39112"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/categories?post=39112"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/tags?post=39112"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}