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

Primary Key MySQL Error

I'm trying to make an database and one of my tables use composite primary keys. This works fine until I try to make a foreign key to a separate table using the 2nd composite key. Error given is "ERROR 1005 (HY000): Can't create table…
Nimila Hiranya
  • 4,842
  • 10
  • 35
  • 52
0
votes
1 answer

One-to-Many With Composite Keys and Different Column Names in Grails

This is a syntax question. I want a one-to-many relationship between Foo -> Bar (simplified here): class Foo { String fooPK1, fooPK2 static mapping = { id composite: ["fooPK1", "fooPK2"] } static hasMany = [bars:…
Alex Beardsley
  • 20,988
  • 15
  • 52
  • 67
0
votes
1 answer

JPA: Retrieve a list from secondary table using a composite key

well, here I have an example of what I'm trying to do with no success. Sorry for the dummy example. I have the following tables: CREATE TABLE HOUSE( HOUSE_NAME VARCHAR2(255), OWNER_ID VARCHAR2(255), PRIMARY KEY(FOLDER_NAME, USER_ID) ); CREATE…
Tomarto
  • 2,755
  • 6
  • 27
  • 37
0
votes
3 answers

sql server, composite keys - ignoring duplicate

Is there a way to prevent sql from throwing an error when I try to save a record that already exists. I've got a composite key table for a many-to-many relationship that has only the two values, when I update a model from my application, it tries to…
Daniel
  • 34,125
  • 17
  • 102
  • 150
0
votes
2 answers

cfwheels - removing unused composite key pairs

I've got an issue with a nested property that uses a composite key. When I'm editing a model that has multiple instances of nested properties (with a composite key) and want to update it to have fewer by leaving them blank, cfWheels does not remove…
Daniel
  • 34,125
  • 17
  • 102
  • 150
0
votes
1 answer

Is it possible to map a composite key in fluent nhibernate where half of the key is an identity field in the database?

As the question states, is it possible to map a composite key in fluent nhibernate (or non fluent i suppose) where one of the two fields used in the composite is an identity field? I have a table where one part of the primary key is an identity…
Chris Conway
  • 16,269
  • 23
  • 96
  • 113
0
votes
1 answer

Client generated ID, PK is a composite key with parent. NHibernate isn't happy. Alternatives?

I am developing a client-side app which passes data back to a database. The client has the ability to create an object of type PlaylistItem. I do not wait for the database to respond with a generated ID when creating a PlaylistItem. Instead, I let…
Sean Anderson
  • 27,963
  • 30
  • 126
  • 237
0
votes
1 answer

Many-to-One w/ Composite key- Foreign key must have same number of columns as he referenced PK

Problem Summary I am trying to map a many-to-one with a composite key. So far I haven't been able to locate another question that helps. I know how to map to a composite key with one-to-one, but it is not allowing me to map many-to-one. Below you…
zach
  • 1,281
  • 7
  • 27
  • 41
0
votes
1 answer

Which Hibernate ( or NHiberate) tags and/or mapping classes would I use to create mappings to the bridge table called contract?

Let's say I have a bridge table with more than two foreign keys that make up it's composite key. For Example, Goverment table gov_id int primary key cournty_name String NonProfitOrg Table nonProfit_id int primary key NonProfit_name…
crazyTech
  • 1,379
  • 3
  • 32
  • 67
0
votes
1 answer

database design link 2 tables to 1 that is linked to several

I currently have 3 main tables animal, food and medicine. From animal table I get tables: species (cat,dog, bird, fish....) size(small,medium,large, big...) age(puppy, young adult, old...) color (brown, black, gray... ) I want to store useful…
edgarmtze
  • 24,683
  • 80
  • 235
  • 386
0
votes
1 answer

Exception using Compsite Keys on EclipseLink

Im having trouble using composite primary keys with JPA EclipseLink. The problem is when I theres a foreign key that is the primary key of another table. I have this simple scenario. User public class Users implements Serializable { ... private…
Daniel Rojas
  • 407
  • 2
  • 5
  • 16
0
votes
1 answer

Vertical partitioning of composite keys

I have to track values for a combination of different resources everyday. So a table to do this might look like: CREATE TABLE `data` ( `id` INT UNSIGNED NULL PRIMARY KEY AUTO_INCREMENT, `datetime` DATETIME NOT NULL, `res1` INT UNSIGNED NOT…
Courtney Miles
  • 3,756
  • 3
  • 29
  • 47
0
votes
1 answer

Why do I get a foreign key violation when I try to use NHibernate Session to save to a bridge table containing composite key?

Our office team is working and ASP.NET project that uses .NET Framework 4 and NHibernate 3.3.1 I have a POCO domain class called AllResourcesInfo ( which ultimately maps to a Resource table in the database that is inner joined to some other tables,…
crazyTech
  • 1,379
  • 3
  • 32
  • 67
0
votes
3 answers

Composite Foreign Key from multiple related tables

Just beginning to learn about SQL and had a question I couldn't figure out. I have a setup based on the following tables and their primary keys, the columns with the same name between tables are constrained by foreign…
Jake
  • 733
  • 8
  • 23
0
votes
1 answer

Hibernate sequence generation not working for element of a composite key

I have a Hibernate entity with a composite key where the key is made up of a Long ID and a Timestamp. The ID should use a sequence generator. MyEntity: @Entity @Table(name="MY_ENTITY") public class MyEntity { private MyEntityPk myEntityPk; …
Chris Williams
  • 11,647
  • 15
  • 60
  • 97