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
739
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
  • 20,615
  • 10
  • 53
  • 74
470
votes
12 answers

Can I have multiple primary keys in a single table?

Can I have multiple primary keys in a single table?
vaithi
226
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
  • 104,438
  • 178
  • 489
  • 842
226
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
  • 3,036
  • 4
  • 22
  • 20
164
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 want to make a table name voting with fields QuestionID, MemberID, and vote. And the Composite primary key consists of the fields QuestionID and MemberID. How should I do…
Zeeshan Rang
  • 19,375
  • 28
  • 72
  • 100
134
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,544
  • 19
  • 53
  • 86
128
votes
6 answers

How can I alter a primary key constraint using SQL syntax?

I have a table that is missing a column in its primary key constraint. Instead of editing it through SQL Server, I want to put this in a script to add it as part of our update scripts. What syntax can I use to do this? Must I drop and recreate the…
Jason Rae
  • 2,583
  • 5
  • 25
  • 34
80
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
  • 53,749
  • 8
  • 73
  • 136
79
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,357
  • 2
  • 30
  • 44
77
votes
3 answers

Primary key for multiple columns 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, …
67
votes
9 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
  • 831
  • 1
  • 7
  • 9
63
votes
3 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.…
57
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,649
  • 7
  • 29
  • 42
52
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,843
  • 7
  • 28
  • 29
50
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,395
  • 4
  • 21
  • 29
1
2 3
65 66