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
14
votes
5 answers

Eclipse error on mapping with @EmbeddedId

I have an entity with composite key so I use the @Embeddable and @EmbeddedId annotations. Embeddable class looks like this : @Embeddable public class DitaAdminAccountSkillPK implements Serializable { @ManyToOne @JoinColumn(name = "admin_id") …
user1622058
  • 453
  • 3
  • 7
  • 16
14
votes
1 answer

Foreign key mapping inside Embeddable class

I am using eclipselink for JPA. I have an entity, which has a composite key fabricated out of two fields. Following is my Embeddable primary key class' fields(members). @Embeddable public class LeavePK { @ManyToOne(optional = false) …
Ahamed
  • 39,245
  • 13
  • 40
  • 68
13
votes
2 answers

How to use NOT EXISTS with COMPOSITE KEYS in SQL for inserting data from POJO

I am using DB2 DBMS. Scenario 1: myTable has a composite key (key1, key2) where both key1 and key2 are foreign keys from yourTable. I want to insert new data from yourTable into myTable, but only if the key1, key2 combination does not already exist…
Vicky
  • 16,679
  • 54
  • 139
  • 232
13
votes
4 answers

Oracle composite primary key / foreign key question

I have a composite primary key in 1 table in oracle. I want to create a foreign key for one table entry in my second table that references the composite primary key in the first table. I am getting the error ORA-02256. Any thoughts on how I can…
Christopher
  • 671
  • 3
  • 10
  • 18
13
votes
9 answers

Why do I read so many negative opinions on using composite keys?

I was working on an Access database which loved auto-numbered identifiers. Every table used them except one, which used a key made up of the first name, last name and birthdate of a person. Anyways, people started running into a lot of problems with…
Jeff
  • 1,153
  • 2
  • 15
  • 35
12
votes
1 answer

How to create a composite primary key which contains a @ManyToOne attribute as an @EmbeddedId in JPA?

I'm asking and answering my own question, but i'm not assuming i have the best answer. If you have a better one, please post it! Related questions: How to set a backreference from an @EmbeddedId in JPA hibernate mapping where embeddedid (?) JPA…
Tom Anderson
  • 46,189
  • 17
  • 92
  • 133
12
votes
2 answers

Composite DB keys with Entity Framework 4.0

The re-design for a large database at our company makes extensive use of composite primary keys on the database. Forgetting performance impacts, will this cause any difficulties when working with this db in Entity Framework 4.0? The database…
Steve Ward
  • 1,207
  • 3
  • 16
  • 42
12
votes
2 answers

Hadoop - composite key

Suppose I have a tab delimited file containing user activity data formatted like this: timestamp user_id page_id action_id I want to write a hadoop job to count user actions on each page, so the output file should look like this: user_id …
Jacek Chmielewski
  • 1,763
  • 4
  • 16
  • 18
11
votes
2 answers

JPA table with 2 primary key fields

I have a table which contains only 2 fields. The table has a composite PK formed by these two fields. When using Netbeans for creating entity beans from database, the entity bean is not created automatically like other tables that have more than 2…
user2046810
  • 393
  • 1
  • 8
  • 21
10
votes
3 answers

Indexing individual fields of SQL Server composite keys

I'm upsizing a Jet database to SQL Server Express 2008 R2 and before doing so, I'm re-evaluating the schema (it was designed in 1997-98, and the guy who designed it (i.e., me) was something of a moron!). My question is about N:N join tables with a…
David-W-Fenton
  • 22,871
  • 4
  • 45
  • 58
10
votes
1 answer

Entity Framework - DB-First - Composite Foreign Keys

I have a database that has a table with a 2-column primary composite key (one int, one bigint.) I have two tables that have a composite foreign key, referencing the first table's composite primary key. The relationships are (as far as I know,)…
10
votes
3 answers

hibernate composite key

Is it necessary that composite-id should be mapped to class ?? can it be like this ? or should be
Senthilnathan
  • 1,157
  • 2
  • 10
  • 10
10
votes
2 answers

Entity Framework Database First - Composite Foreign Keys

I have a database that contains a couple of composite foreign keys. For example, here is the generation script for the foreign key: ALTER TABLE [dbo].[WorkingRosters] WITH NOCHECK ADD CONSTRAINT [FK_WorkingRoster_ShiftLeaveCode] FOREIGN…
10
votes
3 answers

How to make a composite key to be unique?

I am making a database of students in one school.Here is what I have so far: If you don't like reading jump to the "In short" part The problem is that I'm not happy with this design. I want the combination of grade, subgrade and id_class to be…
Bosak
  • 2,103
  • 4
  • 24
  • 43
9
votes
1 answer

Creating composite foreign key constraint

I am trying to create a composite foreign key relationship/constraint. All tables are empty. I have this table: CREATE TABLE [dbo].[ChemSampleValueTest]( [SampleNumber] [int] NOT NULL, [ParameterID] [int] NOT NULL, [Value]…
sennett
  • 8,014
  • 9
  • 46
  • 69
1 2
3
49 50