Questions tagged [composite-key]

A composite key is a database key whose value is made up of multiple typed values

A Composite Key is a Key that is made up of multiple columns

Any column(s) that can guarantee uniqueness is called a candidate key; however a composite key is a special type of candidate key that is only formed by a combination of two or more columns. Sometimes the candidate key is just a single column, and sometimes it's formed by joining multiple columns.

A composite key can be defined as the primary key. This is done using SQL statements at the time of table creation. It means that data in the entire table is defined and indexed on the set of columns defined as the primary key.

Dr E F Codd's Relational Model demands that:

  1. Data is organised into table and columns

  2. Rows (as distinct from records) are unique

  3. A Key is made up from the data (ID, GUID, etc. columns are not data).

In any given table, this naturally leads to multiple columns being used to provide row uniqueness, and to identify each row. That is a composite Key.

Composite Keys are the hallmark of a Relational database (those that conform to Relational Model), without them the database does not comply, and is therefore non-relational.

SQL-compliant platforms provide complete support for composite Keys.

Non-compliant platforms have partial support for composite keys, and usually necessitate single column keys.

Further reading

747 questions
1037
votes
14 answers

How do I specify unique constraint for multiple columns in MySQL?

I have a table: table votes ( id, user, email, address, primary key(id), ); Now I want to make the columns user, email, address unique (together). How do I do this in MySql? Of course the example is just... an example. So please…
Niyaz
  • 53,943
  • 55
  • 151
  • 182
242
votes
8 answers

How to map a composite key with JPA and Hibernate?

In this code, how to generate a Java class for the composite key (how to composite key in hibernate): create table Time ( levelStation int(15) not null, src varchar(100) not null, dst varchar(100) not null, distance int(15) not…
kaaf
  • 2,421
  • 3
  • 15
  • 4
226
votes
8 answers

How to properly create composite primary keys - MYSQL

Here is a gross oversimplification of an intense setup I am working with. table_1 and table_2 both have auto-increment surrogate primary keys as the ID. info is a table that contains information about both table_1 and table_2. table_1 (id, field) …
filip
  • 3,036
  • 4
  • 22
  • 20
146
votes
2 answers

Postgres: How to do Composite keys?

I cannot understand the syntax error in creating a composite key. It may be a logic error, because I have tested many varieties. How do you create composite keys in Postgres? CREATE TABLE tags ( (question_id, tag_id) NOT NULL, …
Léo Léopold Hertz 준영
  • 134,464
  • 179
  • 445
  • 697
110
votes
3 answers

composite key as foreign key

I am using Entity framework 4.1 in MVC 3 application. I have an entity where I have primary key consists of two columns ( composite key). And this is being used in another entity as foreign key. How to create the relationship ? In normal scnerios we…
DotnetSparrow
  • 27,428
  • 62
  • 183
  • 316
105
votes
2 answers

Composite Key with EF 4.1 Code First

I am trying to figure out how to have a composite key using EF code First 4.1 RC. Currently, I am using the [Key] Data Annotation, but I am unable to specify more than one key. how would one specify a composite key? Here is my Example: public class…
bugnuker
  • 3,918
  • 7
  • 24
  • 31
61
votes
1 answer

Creating Composite Key Entity Framework

Shortly, I want to create composite keys on my table remaining with the primary key in order to improve sql server search performance. The performance issue occurs on 200k data table whenever I search an entity without primary key (i.e a string of…
kkocabiyik
  • 4,246
  • 7
  • 30
  • 40
58
votes
2 answers

How to make composite primary key on phpmyadmin

How to make a composite primary key on phpmyadmin. such that student id is combination of year and roll number. sid = 112132 , 11 of year 2011 and 2132 is roll number?? anybody help..
user3427877
  • 589
  • 1
  • 4
  • 4
54
votes
3 answers

How to write JPQL SELECT with embedded id?

I'm using Toplink essentials (JPA) + GlassFish v3 + NetBean 6.9 I have one table with composite primary key: table (machine) ---------------- |PK machineId | |PK workId | | | |______________| I created 2 entity classes one for…
Meow
  • 18,371
  • 52
  • 136
  • 180
53
votes
3 answers

MySQL - Make a pair of values unique

I have a table with two int values that are IDs. On their own these IDs can show up any number of times in the table, but together they should only ever appear once. Is there a way to make a pair of values unique and still allow the individual…
Josh Brittain
  • 2,162
  • 7
  • 31
  • 54
51
votes
3 answers

Composite Primary Key performance drawback in MySQL

We have a table with a composite Primary key consisting of three fields (and it is in MySQL 5.1). There are near 200 inserts and 200 selects per second on this table, and the size of the table is around 1 million rows and it is increasing. My…
Ahmad
  • 513
  • 1
  • 4
  • 6
49
votes
4 answers

How do I map a composite primary key in Entity Framework 4 code first?

I'm getting to grips with EF4 code first, and liking it so far. But I'm having trouble mapping an entity to a table with a composite primary key. The configuration I've tried looks like this: public SubscriptionUserConfiguration() { …
jamesfm
  • 869
  • 1
  • 9
  • 17
41
votes
6 answers

Why are composite keys discouraged in hibernate?

This is from Hibernate official tutorial: There is an alternative declaration that allows access to legacy data with composite keys. Its use is strongly discouraged for anything else. Why are composite keys discouraged? I am…
Isaac
  • 2,701
  • 4
  • 30
  • 47
39
votes
3 answers

How to give a unique constraint to a combination of columns in Oracle?

I have a Table with 4 Columns Each Column will be A,B,C,D Column A is the Primary key. Column B has unique name constraint. Now I want to remove the unique constraint for column B and give a unique constraint by combining the columns B, C and D. So…
Nigel Thomas
  • 1,881
  • 8
  • 30
  • 50
37
votes
9 answers

@OneToMany and composite primary keys?

I'm using Hibernate with annotations (in spring), and I have an object which has an ordered, many-to-one relationship which a child object which has a composite primary key, one component of which is a foreign key back to the id of the parent…
Kris Pruden
  • 3,280
  • 4
  • 25
  • 30
1
2 3
49 50