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
35
votes
3 answers

No default constructor for entity for inner class in Hibernate

I have two classes. One is the entity class, the other serves as a composite key class. The code is as followed. @Entity public class Supply { @Embeddable class Id implements Serializable { @Column(name = "supplier_id") …
xiaohan2012
  • 9,870
  • 23
  • 67
  • 101
32
votes
5 answers

In a join table, what's the best workaround for Rails' absence of a composite key?

create_table :categories_posts, :id => false do |t| t.column :category_id, :integer, :null => false t.column :post_id, :integer, :null => false end I have a join table (as above) with columns that refer to a corresponding categories table and a…
pez_dispenser
  • 4,394
  • 7
  • 37
  • 47
30
votes
7 answers

Defining Composite Key with Auto Increment in MySQL

Scenario: I have a table which references two foreign keys, and for each unique combination of these foreign keys, has its own auto_increment column. I need to implement a Composite Key that will help identify the row as unique using combination of…
Nirav Zaveri
  • 687
  • 1
  • 9
  • 28
26
votes
7 answers

What is the difference between candidate key and composite key?

I am reading about candidate keys and composite keys. I came to know that a candidate key can qualify as a primary key and it can be a single column or combination of columns and a composite key is also a combination of columns. For composite key…
Shailesh Jaiswal
  • 3,606
  • 13
  • 73
  • 124
26
votes
2 answers

composite key resource REST service

I've come across a problem at work where I can't find information on the usual standard or practice for performing CRUD operations in a RESTful web service against a resource whose primary key is a composite of other resource ids. We are using MVC…
19
votes
2 answers

Composite Key/Id Mapping with NHibernate

I have the following tables in my database: Announcements: - AnnouncementID (PK) - Title AnouncementsRead (composite PK on AnnouncementID and UserID): - AnnouncementID (PK) - UserID (PK) - DateRead Users: - UserID (PK) - UserName Usually I'd map…
19
votes
1 answer

REST - how design an URI with composite keys?

I have a question about how design a resource URI with composite keys. I have a resource called freight that has 4 keys/ids: partner ID, initial zipcode, final zipcode and weight. Actually my resource was designed to have an incremental ID…
Krock
  • 395
  • 3
  • 10
18
votes
4 answers

JPA Composite Key + Sequence

Is it possible in plain JPA or JPA+Hibernate extensions to declare a composite key, where an element of the composite key is a sequence? This is my composite class: @Embeddable public class IntegrationEJBPk implements Serializable { //... …
Miguel Ping
  • 18,082
  • 23
  • 88
  • 136
17
votes
2 answers

Hibernate foreign key with a part of composite primary key

I have to work with Hibernate and I am not very sure how to solve this problem, I have 2 tables with a 1..n relationship like this: ------- TABLE_A ------- col_b (pk) col_c (pk) [other fields] ------- TABLE_B ------- col_a (pk) col_b (pk) (fk…
Jagger
  • 10,350
  • 9
  • 51
  • 93
16
votes
2 answers

Duplicate columns when using EmbeddedId with a ManyToOne mapping with Ebean

I have a model called "EventCheckin" which has a ManyToOne mapping to an "Event" and a "User". The PrimaryKey of the "EventCheckin" table is the id of the user and the id of the event. I'm trying to represent this using an "EmbeddedId" in my…
InPursuit
  • 3,245
  • 24
  • 20
16
votes
2 answers

How can I use generated value within composite keys?

I have two classes documentlog and documentversion(with primary keys: int doc_id and int docVersionID) with a many-to-one relationship. I used a composite key class called CompundKey to manage the compound primary key. I need to auto increment…
Nick
  • 161
  • 1
  • 1
  • 3
15
votes
1 answer

Composite primary key, foreign key. Reference to object or key?

I have two classes Foo and Bar. The tables in the database look like this: |Foo| |id : INT (PK) | bar_id : INT (PK, FK) | |Bar| |id : INT (PK) | Normally I would map it like this: @Entity public class Bar { @Id @Column(name = "id") …
siebz0r
  • 18,867
  • 14
  • 64
  • 107
14
votes
5 answers

MySQL Question - Unique Key Not functioning correctly, or am I misunderstanding?

I'm trying to create a relation where any of four different parts may be included, but any collection of the same parts should be handled as unique. Example: An assignment must have an assigned company, may optionally have an assigned location,…
Drew Arrigoni
14
votes
4 answers

Should Hibernate be able to handle overlapping foreign keys?

I have a table that has two foreign keys to two different tables with both foreign keys sharing one column: CREATE TABLE ZipAreas ( country_code CHAR(2) NOT NULL, zip_code VARCHAR(10) NOT NULL, state_code VARCHAR(5) NOT NULL, city_name…
Kawu
  • 13,647
  • 34
  • 123
  • 195
14
votes
1 answer

Hibernate: No default constructor for entity Inner Class

I realize there are many similar questions, but none have helped me along on this. I'm using a CRUD framework called Tynamo, which in turn relies on Apache Tapestry and Hibernate. This all works fine, except for when I try to use it for CRUD on a…
liltitus27
  • 1,670
  • 5
  • 29
  • 46
1
2
3
49 50