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

900 questions
663
votes
9 answers

Sqlite primary key on multiple columns

What is the syntax for specifying a primary key on more than 1 column in SQLITE ?
Bogdan Gavril MSFT
  • 18,632
  • 10
  • 50
  • 73
405
votes
12 answers

Can I have multiple primary keys in a single table?

Can I have multiple primary keys in a single table?
vaithi
206
votes
6 answers

ALTER TABLE to add a composite primary key

I have a table called provider. I have three columns called person, place, thing. There can be duplicate persons, duplicate places, and duplicate things, but there can never be a dupicate person-place-thing combination. How would I ALTER TABLE to…
David542
  • 96,524
  • 132
  • 375
  • 637
200
votes
8 answers

How to properly create composite primary keys - MYSQL

Here is a gross oversimplification of an intense setup I am working with. table_1 and table_2 both have auto-increment surrogate primary keys as the ID. info is a table that contains information about both table_1 and table_2. table_1 (id, field) …
filip
  • 2,756
  • 3
  • 19
  • 19
134
votes
4 answers

How can I define a composite primary key in SQL?

How can I define a composite primary key consisting of two fields in SQL? I am using PHP to create tables and everything. I want to make a table name voting with fields QuestionID, MemeberID, and vote. And the Composite primary key consists of the…
Zeeshan Rang
  • 17,525
  • 27
  • 65
  • 96
111
votes
4 answers

How to create and handle composite primary key in JPA

I want to have versions from the same data entry. In other words, I want to duplicate the entry with another version number. id - Version will be the primary key. How should the entity look like? How can I duplicate it with another version? id…
Kayser
  • 6,116
  • 17
  • 49
  • 86
65
votes
5 answers

NULL value in multi-column primary key

I've got a table with several columns making up the primary key. The nature of the data stored allows some of these fields to have NULL values. I have designed my table as such: CREATE TABLE `test` ( `Field1` SMALLINT(5) UNSIGNED NOT NULL, …
simbabque
  • 50,588
  • 8
  • 69
  • 121
62
votes
4 answers

MongoDB and composite primary keys

I'm trying to determine the best way to deal with a composite primary key in a mongo db. The main key for interacting with the data in this system is made up of 2 uuids. The combination of uuids is guaranteed to be unique, but neither of the…
herbrandson
  • 2,157
  • 2
  • 30
  • 43
59
votes
8 answers

How I can put composite keys in models in Laravel 5?

I have in my DataBase a table with two primary keys (id and language_id) and I need put it in my models. The default primaryKey in Models (Model.php in Laravel 5) is id, and I want that the primaryKeys will be id and id_language. I tried put it with…
Sergioh Lonet
  • 751
  • 1
  • 6
  • 9
53
votes
2 answers

Foreign key relationship with composite primary keys in SQL Server 2005

I have two tables Table1( FileID, BundledFileID, Domain) and Table2( FileID, FileType, FileName) In Table2 FileID and FileType are the composite primary key. I want to create a foreign key relationship from Table1.FileID to Table2.…
43
votes
1 answer

How to delete multiple rows with 2 columns as composite primary key in MySQL?

My innodb table has the following structure: 4 columns (CountryID, Year, %Change, Source), with the 2 columns (CountryID, Year) as the primary key. How do I delete multiple rows other than using a for-loop to delete each row? I'm looking for…
Ana Ban
  • 1,295
  • 4
  • 19
  • 28
42
votes
1 answer

Laravel Model with Two Primary Keys update

I'm trying to update Model which has two primary keys. Model namespace App; use Illuminate\Database\Eloquent\Model; class Inventory extends Model { /** * The table associated with the model. */ protected $table =…
Ugnius Malūkas
  • 2,151
  • 6
  • 26
  • 37
42
votes
2 answers

Primary key for multiple column in PostgreSQL?

How to provide primary key for multiple column in a single table using PostgreSQL? Example: Create table "Test" ( "SlNo" int not null primary key, "EmpID" int not null, /* Want to become primary key */ "Empname" varchar(50) null, …
Meem
  • 4,007
  • 12
  • 47
  • 78
42
votes
3 answers

PostgreSQL composite primary key

In MySQL, when I create a composite primary key, say with columns X, Y, Z, then all three columns become indexes automatically. Does the same happen for Postgres?
user1467855
  • 3,193
  • 5
  • 25
  • 29
37
votes
7 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,002
  • 1
  • 11
  • 24
1
2 3
59 60