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
5
votes
4 answers

Mysql Select record where PRIMARY key = x

I have a primary key in my mysql table which is comprised of three columns. CREATE TABLE IF NOT EXISTS `bb_bulletin` ( `OfficeCode` int(5) NOT NULL, `IssuerId` int(11) NOT NULL, `BulletinDtm` datetime NOT NULL, `CategoryCode` varchar(4) NOT…
andrew
  • 5,096
  • 10
  • 43
  • 57
5
votes
2 answers

MongoDB Composite Key: InvalidOperationException: {document}.Identity is not supported

I am having issues with hydrating a class which consists of a composite ID which in turn has a base class, I am getting an error saying InvalidOperationException: {document}.Identity is not supported. The class i am trying to write to the database…
morleyc
  • 2,169
  • 10
  • 48
  • 108
5
votes
2 answers

How can I set multiple fields as primary key in MongoDB?

I am trying to create a collection with 50+ fields. I understand that the purpose of the primary key is to uniquely identify a record. Since the primary key is the _id in MongoDB that gets created automatically, isn't it obvious that all my records…
Kavitha Madhavaraj
  • 562
  • 1
  • 6
  • 23
5
votes
3 answers

Composite primary Key and Data truncation error

I'm using Hibernate and MySql and today I setted a composite primary key in one of my table, so below: DefSelfLearning And this entity is OneToMany with SelfLearning: This is my java entity: @Entity @Table(name = "defselflearning", catalog =…
luca
  • 3,248
  • 10
  • 66
  • 145
5
votes
3 answers

T-SQL Unique Identity Column as Part of Composite Primary Key

Suppose that you have a table with the following structure: CREATE TABLE [log] ( [type] int NOT NULL, [stat] nvarchar(20) NOT NULL, [id] int IDENTITY (1, 1) NOT NULL, descr nvarchar(20), PRIMARY KEY ([type], [stat], [id]) ) Is it possible…
ax1mx2
  • 654
  • 1
  • 11
  • 23
5
votes
2 answers

Foreign key in MySql as composite primary key

I have the database with the name Shop with this 3 tables: create table usr( id_usr varchar(20) not null, primary key(id_usr) ); create table product( id_product varchar(20) not null, id_size varchar(20) not null, price…
5
votes
1 answer

EF Composite key fluent API

I am trying to map a composite key for an entity. public class Customer { public int CustomerId { get; set; } public virtual List CustomerImages { get; set; } } And its Map: public class CustomerMap :…
Tomer
  • 4,382
  • 5
  • 38
  • 48
5
votes
1 answer

SQL composite primary key without ordering (pair of integers in either order must be unique)

I have a MySQL table for Users with the primary key _id, and I want to represent friendships (visibility on friends lists of other users) as a table with pairs of userId foreign keys. I'm thinking something like: CREATE TABLE UserFriendships ( …
Mike Turley
  • 1,172
  • 1
  • 9
  • 26
5
votes
3 answers

Many to many relationship with a composite key on SQLAlchemy

Let's say I have the following model: class Molecule(Base): db = Column(Integer, primary_key=True) id = Column(Integer, primary_key=True) data = Column(Integer) class Atom(Base): id = Column(Integer, primary_key=True) weight =…
Christian Teijon
  • 456
  • 4
  • 12
5
votes
2 answers

Composite primary keys in ruby on rails

I am trying to use http://compositekeys.rubyforge.org/ in order to have composite primary keys in my activerecord models. I already added gem 'composite_primary_keys', '=3.1.0' to my Gemfile. Now I am trying to setup my first modelclass as…
Coxer
  • 1,694
  • 2
  • 26
  • 44
5
votes
1 answer

jpa composite primary key and jointable with unique key

here are the sql tables: option primary key is name + value CREATE TABLE `option` ( id int(11) NOT NULL AUTO_INCREMENT, `name` varchar(45) NOT NULL, `value` varchar(45) NOT NULL, PRIMARY KEY (`name`, `value`), UNIQUE KEY `id_UNIQUE`…
user1800633
  • 59
  • 1
  • 3
5
votes
1 answer

Insert cassandra column family with composite key

I have a column family AllLog create that create column family LogData with column_type = 'Standard' and comparator = 'CompositeType(org.apache.cassandra.db.marshal.UTF8Type,org.apache.cassandra.db.marshal.UTF8Type)' and…
tnk_peka
  • 1,525
  • 2
  • 15
  • 25
4
votes
0 answers

Entity Framework and MVC Scaffolding with Composite Primary Keys

I'm curious about how Entity Framework and MVC scaffolding handle primary keys. When I create a controller in an MVC3 project and choose the scaffolding template of 'Controller with read/write actions and views, using Entity Framework' and specify…
Alex
  • 1,576
  • 2
  • 15
  • 28
4
votes
5 answers

SQL Primary Key - is it necessary?

I have a list of items. Most of these items will not be in stock. The item table has id, name, description. The quantities of items are stored in another table named inventory. The inventory table has item_id and quantity of items that are in stock.…
deadghost
  • 5,017
  • 3
  • 34
  • 46
4
votes
1 answer

Doctrine 2 one-to-one via composite key

I am trying to set up a relationship as shown below. Each car can have one review. A car has a primary key on 2 columns. Review is referenced back to the car via the composite primary key. Simple, in theory. class Car { /** * @ORM\Id …
iBiryukov
  • 1,730
  • 4
  • 19
  • 30