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
8
votes
2 answers

Sequences with composite primary key

Using PostgreSQL, how do I manage a sequence (auto incremented Integer id) for the following scenario- Table named 'businesses' which has 'id' and some more columns. Table named 'users' which has: a composite primary key, composed of a…
Alon Amir
  • 4,913
  • 9
  • 47
  • 86
8
votes
1 answer

Why does primary key order matter?

I recently set up a class in an EntityFramework project which designated a couple of its members as a composite key. However, when it came to time to create the database from this it gave the error Unable to determine composite primary key ordering…
Stephen Holt
  • 2,360
  • 4
  • 26
  • 34
8
votes
3 answers

SQL Multiple Foreign Keys as Primary Keys

If I declare the table below does it implicitly imply that both the foreign keys make a unique primary key or do I need to do something more to make both attributes as a primary key? CREATE TABLE Report_has_Items ( ReportID int REFERENCES…
Kairan
  • 5,342
  • 27
  • 65
  • 104
8
votes
1 answer

Composite key in SQLite

How to put a composite key in SQLite? In SQL Server, we select multiple columns and then Set Primary Key. What is the method in SQLite? I'm using SQLite Database Browser to manage my DB structure, I don't find the option for putting a composite key…
Ibad Baig
  • 2,276
  • 5
  • 22
  • 27
7
votes
1 answer

How do I join on two columns in the hibernate mapping file using the tag?

I need to map a single class to two tables (both with multiple columns primary key). Let's say TABLE1 has id1, id2, id3 and TABLE2 has id1, id2 as primary keys. Now when writing the mapping file I would do something like the…
despot
  • 7,167
  • 9
  • 44
  • 63
7
votes
2 answers

Creating index/pk in a huge table is taking too long. I am using Oracle. How do I know if it is going well?

I have a really huge table, with ~200 million rows. It had no index/pk at all. Selects in this table were (obviously) running slow. I decided to create a PK using 3 columns. I did it in a test environment that has a smaller version of this table and…
Michael
  • 477
  • 1
  • 5
  • 12
7
votes
3 answers

Cassandra - How to retrieve most recent value

I have the following table defined in Cassandra 2.0.9: CREATE TABLE history ( histid uuid, ddate text, -- Day Date, i.e. 2014-11-20 valtime timestamp, -- value time val text, --value …
iamtheoracle
  • 317
  • 2
  • 11
7
votes
1 answer

SQLAlchemy joins with composite foreign keys (with flask-sqlalchemy)

I'm trying to understand how to do joins with composite foreign keys on SQLAlchemy and my attempts to do this are failing. I have the following model classes on my toy model (I'm using Flask-SQLAlchemy, but I'm not sure this has anything to do with…
7
votes
4 answers

Scala's Slick with multiple PK insertOrUpdate() throws exception ERROR: syntax error at end of input

I am using Scala' Slick and PostgreSQL. And I am working well with tables with single PK. Now I need to use a table with multiple PKs: case class Report(f1: DateTime, f2: String, f3: Double) class Reports(tag: Tag) extends…
7
votes
1 answer

Symfony2 Doctrine ORM Composite Primary Keys

I am developing an application in Symfony 2.3 with Doctrine 2.4 as ORM. The database engine I use is PostgreSQL. I'm having problems when mapping entities with composite primary keys in other tables. These keys are foreign keys in the related key.…
Guillermo
  • 71
  • 2
  • 7
7
votes
2 answers

Turnoff mysql unsafe statement warning

I am using log-error to write warning/errors into a file. When I perform INSERT IGNORE..SELECT statement, it just keep write this warning messages. 120905 3:01:23 [Warning] Unsafe statement written to the binary log using statement format since…
user1640242
  • 221
  • 1
  • 3
  • 5
7
votes
4 answers

MySQL: Multiple Primary Keys and Auto Increment

I'm quite new to setting up tables in MySQL and there is something I'd like to do which is a bit more advance than I'm able to do. I have two columns as part of a composite primary key, one is a Date and an ID I would like to be an auto increment…
AdmiralJonB
  • 2,038
  • 3
  • 23
  • 27
6
votes
1 answer

Composite primary key with not primitive attributes

I am new to Java and Hibernate. I've got problem with composite key. I am trying to do something like that: @Entity class A { @Id int id; } @Entity class B { @Id int id; } @Entity class C { @EmbeddedId C_PK…
svobol13
  • 1,842
  • 3
  • 25
  • 40
6
votes
2 answers

MySQL+PHP: getting last_id of multiple/composite primary key

I need to get the last inserted id of table that have multi-column primary keys. Those tables does not have AUTOCOUNT column. I'm using parametrized queries (arbitrary order) Using PHP (5.3) and MySQLi module Arbitrary INSERT SQL Query. (In any…
lepe
  • 24,677
  • 9
  • 99
  • 108
6
votes
0 answers

Sqlalchemy: Resolve cartesian product warning for many-to-many relationship with custom primaryjoin

Let's say I have the following database schema: class A(Base): __tablename__ = "a_table" id = Column(Integer, primary_key=True, autoincrement=True) version = Column(Integer, primary_key=True, default=1) # More columns... bs =…