{"id":616,"date":"2016-09-27T21:00:39","date_gmt":"2016-09-28T04:00:39","guid":{"rendered":"http:\/\/www.1keydata.com\/blog\/?p=616"},"modified":"2016-09-27T21:12:31","modified_gmt":"2016-09-28T04:12:31","slug":"composite-key-in-sql","status":"publish","type":"post","link":"https:\/\/www.1keydata.com\/blog\/composite-key-in-sql.html","title":{"rendered":"Composite Key In SQL"},"content":{"rendered":"<p>Composite key, or composite primary key, refers to cases where more than one column is used to specify the primary key of a table. In such cases, all foreign keys will also need to include all the columns in the composite key. Note that the columns that make up a composite key can be of different data types.<\/p>\n<p>Below is the SQL syntax for specifying a composite key:<\/p>\n<p><!--more-->CREATE TABLE TABLE_NAME<br \/>\n(COLUMN_1 DATA_TYPE_1,<br \/>\nCOLUMN_2 DATA_TYPE_2,<br \/>\n&#8230;<br \/>\nPRIMARY KEY (COLUMN_1, COLUMN_2, &#8230;));<\/p>\n<p>Some database-specific examples are shown below.  In all cases the composite key created consists of COL1 and COL2.<\/p>\n<h3>MySQL<\/h3>\n<p>CREATE TABLE SAMPLE_TABLE<br \/>\n(COL1 integer,<br \/>\nCOL2 varchar(30),<br \/>\nCOL3 varchar(50),<br \/>\nPRIMARY KEY (COL1, COL2));<\/p>\n<h3>Oracle<\/h3>\n<p>CREATE TABLE SAMPLE_TABLE<br \/>\n(COL1 integer,<br \/>\nCOL2 varchar(30),<br \/>\nCOL3 varchar(50),<br \/>\nPRIMARY KEY (COL1, COL2));<\/p>\n<h3>SQL Server<\/h3>\n<p>CREATE TABLE SAMPLE_TABLE<br \/>\n(COL1 integer,<br \/>\nCOL2 nvarchar(30),<br \/>\nCOL3 nvarchar(50),<br \/>\nPRIMARY KEY (COL1, COL2));<\/p>\n<h3>Composite Key Example<\/h3>\n<p>An example is when we want to record the history of student class registration and grade history. For this, we want to record which student took what class during what quarter, along with the date of registration, date of drop (if the class was dropped), and the final grade. Our table will look like the following:<\/p>\n<p>Table <i><b>Class_History<\/b><\/i><\/p>\n<table border=1>\n<tr>\n<td>Quarter_ID<\/td>\n<\/tr>\n<tr>\n<td>Student_ID<\/td>\n<\/tr>\n<tr>\n<td>Course_ID<\/td>\n<\/tr>\n<tr>\n<td>Date_Registered<\/font><\/tr>\n<tr>\n<td>Date_Dropped<\/td>\n<\/tr>\n<tr>\n<td>Final_Grade<\/td>\n<\/tr>\n<\/table>\n<p>In this table, <b>the first three fields, Quarter_ID, Student_ID, Course_ID, together form a composite key.<\/b> Each of them individually cannot uniquely identify each record, but together the combination of all three does uniquely identify each record.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Discusses the concept of composite key in SQL, and shows examples of creating a composite key in MySQL, Oracle, and SQL Server.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[247],"tags":[297,299,298],"_links":{"self":[{"href":"https:\/\/www.1keydata.com\/blog\/wp-json\/wp\/v2\/posts\/616"}],"collection":[{"href":"https:\/\/www.1keydata.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.1keydata.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.1keydata.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.1keydata.com\/blog\/wp-json\/wp\/v2\/comments?post=616"}],"version-history":[{"count":8,"href":"https:\/\/www.1keydata.com\/blog\/wp-json\/wp\/v2\/posts\/616\/revisions"}],"predecessor-version":[{"id":1136,"href":"https:\/\/www.1keydata.com\/blog\/wp-json\/wp\/v2\/posts\/616\/revisions\/1136"}],"wp:attachment":[{"href":"https:\/\/www.1keydata.com\/blog\/wp-json\/wp\/v2\/media?parent=616"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.1keydata.com\/blog\/wp-json\/wp\/v2\/categories?post=616"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.1keydata.com\/blog\/wp-json\/wp\/v2\/tags?post=616"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}