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

Why is my EmbeddedId in hibernate not working?

I have a compound Primary Key (IDHOLIDAYPACKAGE, IDHOLIDAYPACKAGEVARIANT) in table HolidayPackageVariant where IDHOLIDAYPACKAGE refers to entity HolidayPackage with a Many to One relationship between HolidayPackageVariant and HolidayPackage. When I…
brainydexter
  • 19,826
  • 28
  • 77
  • 115
21
votes
1 answer

How to implement composite primary keys in rails

I have a User model that looks like this: class User < ApplicationRecord belongs_to :organization belongs_to :department end The users table in the database has the two foreign keys organization_id and department_id. How can I make these two…
19
votes
3 answers

How do I add a Composite primary key with Knex.js?

I have 2 tables. 1 called events with an event ID and another table called tickets which I want to have primary keys of event ID and ticket ID. I am also using a PostgreSQL database. At the moment ,I have it as a foreign key but would like to have…
Coder1234
  • 389
  • 1
  • 3
  • 9
18
votes
3 answers

Referencing a composite primary key

I have two tables, with each table having a composite primary key. One attribute is in both composite primary keys. How am i supposed to reference the common attribute?? Do i just reference it as a FK in both tables as below? The cust_id and…
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
17
votes
2 answers

How to address entity that uses composite identity key in OData Url?

I have an entity OrderItem that has OrderId and ProductId integer fields and these two fields form the identity key/primary key for this table. I would like to use OData/Web API to expose such entities through a service and to be able to select…
ViktorZ
  • 901
  • 1
  • 10
  • 26
16
votes
2 answers

How to specify a composite primary key using EFCore Code First Migrations

I'm using Asp.Net Core 2.1, Mvc, c#, EF Core with Code First and Migrations. I'm trying to build a table that has a composite primary key in the Migration.Up() method: migrationBuilder.CreateTable( name: "TagValueAttributes", columns: table…
16
votes
1 answer

Change Primary Key to Composite Key (Primary Key already exists)

I am trying to change the primary key of a table in my SQL database from the existing key to a composite key, which does not include the existing column. The following code is not working due to the following error messages: DROP PRIMARY KEY:…
NealR
  • 10,189
  • 61
  • 159
  • 299
14
votes
10 answers

"Cannot insert explicit value for identity column in table when IDENTITY_INSERT is set to OFF" with composite key

We have recently added a new "level" to our database - added a key "Company_ID" to be above/before the existing ID Identity field in the tables throughout the database. For example, if a Table had ID then fields, it now has Company_ID, then ID, then…
and_E
  • 293
  • 1
  • 3
  • 11
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
13
votes
3 answers

DiscriminatorColumn as part of primary key / id

Situation I have an Entity with a DiscriminatorColumn, configured for single table inheritance: @Entity @Inheritance(strategy=InheritanceType.SINGLE_TABLE) @DiscriminatorColumn(name="TYPE") public class…
Spycho
  • 7,698
  • 3
  • 34
  • 55
12
votes
2 answers

How to define composite keys in MS Access?

I am new to MS Access. Could anybody tell me how to define a composite primary key in MS Access 2007.
Ahsan Iqbal
  • 1,422
  • 5
  • 20
  • 39
12
votes
2 answers

Composite Primary Key equivalent in Redis

I'm new to nosql databases so forgive my sql mentality but I'm looking to store data that can be 'queried' by one of 2 keys. Here's the structure: {user_id, business_id, last_seen_ts, first_seen_ts} where if this were a sql DB I'd use the user_id…
AIntel
  • 1,087
  • 5
  • 14
  • 27
12
votes
1 answer

Laravel: How to add a composite key (2 or more columns) as the $primaryKey in the related model?

Name of the primary key column in any model in Laravel framework is id protected $primaryKey = 'id'; And I know I can change that default name like this: protected $primaryKey = 'new_name'; My question is: What if I have a composite key (2 or more…
Amr
  • 4,809
  • 6
  • 46
  • 60
11
votes
3 answers

spring data rest with composite primary key

I use spring data rest for crud. But when the entity has composite primary keys, I dont know how to to get an entity by giving the primary key. River class: @Entity public class River { private RiverPK id; private Double length; private…
kidfruit
  • 364
  • 1
  • 2
  • 9
1 2
3
65 66