Questions tagged [composite-primary-key]

Composite primary key is a primary key, which consists of more than one column. The column combination guarantees the uniqueness of the PK.

A Composite Primary Key is a Composite Key, which is assigned as Primary.

Primary key is a single field or the least combination of fields that uniquely identifies each record in table. When the primary key consists of more than one field, when we are talking about composite primary key. Composite or not, is the most appropriate key to be main key of reference for the table.

The primary key must not be null and must contain only unique values.

Primary keys are mandatory for every table. When choosing a primary key from the pool of candidate keys, always choose a single simple key over a composite key.

Links

984 questions
48
votes
6 answers

How to update primary key

Here is my problem - I have 2 tables: WORKER, with columns |ID|OTHER_STAF| , where ID is primary key FIRM, with columns |FPK|ID|SOMETHING_ELSE| , where combination FPK and ID make primary key, and also ID is a foreign key referenced to WORKER.ID…
47
votes
1 answer

SQL join with composite primary key

I have to join two tables. But in one table primary key is not there,composite primary key is there,means three columns put together uniquely define a row of that table. I have those three columns in the other table too.rest nothing is common. Is…
Anubh
  • 481
  • 1
  • 4
  • 3
47
votes
1 answer

How to define composite primary key in SQLAlchemy

I'm trying to use SQLAlchemy with MySQL to create a table mapping for a table with a composite primary key, and I'm unsure if I'm doing it right. The existing table is defined with the composite primary key. Here's the mapping class…
Charlie Carwile
  • 837
  • 2
  • 7
  • 9
39
votes
6 answers

Define a unique primary key based on 2 columns

I would like to define a unique key for records based on 2 columns : 'id' and 'language' to let the user submits the following strings : id=1 language=en value=blabla english id=1 language=fr value=blabla french I tried to use set_primary_key…
Stéphane V
  • 1,094
  • 2
  • 11
  • 25
38
votes
9 answers

WHERE col1,col2 IN (...) [SQL subquery using composite primary key]

Given a table foo with a composite primary key (a,b), is there a legal syntax for writing a query such as: SELECT ... FROM foo WHERE a,b IN (SELECT ...many tuples of a/b values...); UPDATE foo SET ... WHERE a,b IN (SELECT ...many tuples of a/b…
Phrogz
  • 296,393
  • 112
  • 651
  • 745
37
votes
2 answers

Hibernate: Where do insertable = false, updatable = false belong in composite primary key constellations involving foreign keys?

When implementing composite primary keys in Hibernate or other ORMs there are up to three places where to put the insertable = false, updatable = false in composite primary key constellations that use identifying relationships (FKs that are part of…
Kawu
  • 13,647
  • 34
  • 123
  • 195
33
votes
1 answer

JPA COUNT with composite primary key query not working

In my db, I have a table (Defaults), and when I generate an entity from table, I get these two classes: @Entity public class Defaults implements Serializable { private static final long serialVersionUID = 1L; @EmbeddedId protected…
victorio
  • 6,224
  • 24
  • 77
  • 113
31
votes
2 answers

alter composite primary key in cassandra CQL 3.0

I'm in a situation where I need to change the the composite primary key as follows: Old Primary Key: (id, source, attribute_name, updated_at); New Primary Key I want: (source, id, attribute_name, updated_at); I issued the following (mysql like)…
aroyc
  • 890
  • 2
  • 13
  • 26
29
votes
3 answers

How to make Primary key Auto increment while using Composite Primary keys in Room persistent library?

I am using Room persistent library. I have requirement to add two primary keys in one table and one of the primary key should be auto increment. I don't know exact syntax to achieve this. Below is my Model class: @Entity(tableName = "newsPapers",…
27
votes
3 answers

JPA/Hibernate: What's better for composite primary keys, @IdClass or @EmbeddedId implementations and why?

what's better for JPA/Hibernate composite primary keys, @IdClass or @EmbeddedId implementations and why? This is an intentionally naive question. I decided to use @EmbeddedId (for whatever reason) and I feel like I made the wrong choice.…
Kawu
  • 13,647
  • 34
  • 123
  • 195
27
votes
2 answers

How to create an indexeddb composite key

I spent hours and hours searching for this one, and just by trial and error was I able to finally find the solution. Logging this in Stack Overflow for future searchers. Q: How do I create a composite key in indexeddb? Keys are created in indexeddb…
SnareChops
  • 13,175
  • 9
  • 69
  • 91
27
votes
3 answers

Cassandra: choosing a Partition Key

I'm undecided whether it's better, performance-wise, to use a very commonly shared column value (like Country) as partition key for a compound primary key or a rather unique column value (like Last_Name). Looking at Cassandra 1.2's documentation…
25
votes
8 answers

What are the pros and cons of using multi column primary keys?

I would like to see an example of: When this is appropriate When this is not appropriate Is there a time when the choice of database would make a difference to the above examples?
Curtis Inderwiesche
  • 4,940
  • 19
  • 60
  • 82
24
votes
3 answers

Room @Relation with composite Primary Key

my question is an extension of this question (also mine :) ) -> Room composite Primary Key link to Foreign Key So, if I have this class: public class FoodWithIngredients extends Food{ @Relation(parentColumn = "id", entityColumn = "food_id",…
MrVasilev
  • 1,503
  • 2
  • 17
  • 34
23
votes
4 answers

Composite primary key or not?

Here's what's confusing me. I often have composite primary keys in database tables. The bad side of that approach is that I have pretty extra work when I delete or edit entries. However, I feel that this approach is in the spirit of database…
sandalone
  • 41,141
  • 63
  • 222
  • 338
1
2
3
65 66