Questions regarding usage of compound key (specific composite key) in database
Questions tagged [compound-key]
76 questions
198
votes
5 answers
SQLite table constraint - unique on multiple columns
I can find syntax "charts" on this on the SQLite website, but no examples and my code is crashing. I have other tables with unique constraints on a single column, but I want to add a constraint to the table on two columns. This is what I have that…

Rich
- 36,270
- 31
- 115
- 154
58
votes
5 answers
dplyr issues when using group_by(multiple variables)
I want to start using dplyr in place of ddply but I can't get a handle on how it works (I've read the documentation).
For example, why when I try to mutate() something does the "group_by" function not work as it's supposed to?
Looking at…

Marc Tulla
- 1,751
- 2
- 20
- 34
33
votes
2 answers
Compound/Composite primary/unique key with Django
How can you create models (and thus tables) with a compound (composite) primary/unique key using Django?

Viet
- 17,944
- 33
- 103
- 135
17
votes
6 answers
In a junction table, should I use a Primary key and a unique constraint, or a compound/composite key?
I have read through handfuls of what would seem to make this a duplicate question. But reading through all of these has left me uncertain. I'm hoping to get an answer based on the absolute example below, as many questions/answers trail off into…

user1447679
- 3,076
- 7
- 32
- 69
9
votes
2 answers
Compound keys in JPA
I want to make an entity that has an autogenerated primary key, but also a unique compound key made up of two other fields. How do I do this in JPA?
I want to do this because the primary key should be used as foreign key in another table and making…

homaxto
- 5,428
- 8
- 37
- 53
9
votes
1 answer
Can not set java.lang.Integer field id to org.hibernate.id.IdentifierGeneratorHelper
I need to store some data in a MySQL-database using Jpa 2/Hibernate 3.5.1. For legacy reasons the table I want to store data in has a compound primary key. The first part of the primary key is of type INTEGER (auto-increment-value), the second part…

user506600
- 91
- 1
- 2
9
votes
2 answers
In SQLAlchemy, how do I query composite primary keys?
I'm using SQLAlchemy to programmatically query a table with a composite foreign key. e.g.:
CREATE TABLE example (
id INT NOT NULL,
date TIMESTAMP NOT NULL,
data VARCHAR(128)
PRIMARY KEY (id, date)
)
I'd like to take a list of values and get rows…

DaveA
- 1,227
- 2
- 14
- 19
7
votes
1 answer
How to handle compound keys with Spring Data JDBC
I'm trying to use Spring Data JDBC to access a table that has a compound key, and although I can find lots of info on how Spring Data JPA handles this, the same doesn't seem to apply to Spring Data JDBC.
Can anyone give a quick example of how you…

Ozzy
- 71
- 1
- 2
7
votes
1 answer
SQLAlchemy: Querying compound primary key with `IN` operator
Assuming a table foo with compound primary key (a,b), How I can generate following sql query with SQLAlchemy (postgresql dialect)?
SELECT * FROM foo WHERE (a,b) IN ((1,2), (2,3));

Taha Jahangir
- 4,774
- 2
- 42
- 49
7
votes
1 answer
How to define data.table keys for fastest aggregation using multiple keys
I am trying to better understand utilizing keyd data.tables. After reading the documentation I think I understand how to speed up subsetting when using one key. For example:
DT = data.table(x=rep(c("ad","bd","cd"),each=3), y=c(1,3,6), v=1:9)
Option…

dayne
- 7,504
- 6
- 38
- 56
6
votes
2 answers
Map with multiple keys in C++
I want to store data by both, their name and their index. In other words, I want to map string names to objects and also give them a custom order.
What I came up with first is a std::vector of pairs of the string key and the object. The order was…

danijar
- 32,406
- 45
- 166
- 297
5
votes
2 answers
How to merge two dataframes using multiple columns as key?
Say I have the following dataframes:
DF1 <- data.frame("A" = rep(c("A","B"), 18),
"B" = rep(c("C","D","E"), 12),
"NUM"= rep(rnorm(36,10,1)),
"TEST" = rep(NA,36))
DF2 <- data.frame("A" =…

goldisfine
- 4,742
- 11
- 59
- 83
3
votes
1 answer
Clustering order does not work with compound partition key
With the following table definition:
CREATE TABLE device_by_create_date (
year int,
comm_nr text,
created_at timestamp,
PRIMARY KEY ((year, comm_nr), created_at)
) WITH CLUSTERING ORDER BY (created_at DESC)
Comm_nr is a unique…

Alex Tbk
- 2,042
- 2
- 20
- 38
3
votes
1 answer
Compound key in Realm with lazy property
I found this great solution for using Realm with compound primary key in Swift: https://github.com/realm/realm-cocoa/issues/1192
public final class Card: Object {
public dynamic var id = 0 {
didSet {
compoundKey =…

Marco
- 1,057
- 1
- 19
- 36
3
votes
1 answer
MySQL SELECT LAST_INSERT_ID() for compound key. Is it possible?
Can I get the LAST INSERT ID() for a compound key in MySQL?
atlacatl