Questions tagged [shared-primary-key]

Shared Primary Key is a technique used in relational database design when it is desired to enforce a one-to-one relationship between rows in two or more tables (relations).

One-to-one relationships are typically of the IS-A variety of relationships. IS-A relationships are known in object modeling as class/subclass designs. IS-A relationships are known in ER modeling as generalization/specialization designs.

Shared primary key comes with a cost. In general, DBMS products support part but not all of the shared primary key concept. In the main table, the primary key is just declared in the usual way. In any secondary tables, the key is declared both as a primary key and as a foreign key, referencing the primary key in the main table. This is enough to enforce the one-to-one relationship.

When new entries are to be made in the main table and in one or more secondary tables, the program doing the insert has to insert the row into the main table first, and then propagate the value of the main table's primary key to the secondary table(s). This propagation can be thought of as "poor man's inheritance".

Note that the entry in the secondary table is optional, while the entry in the main table is mandatory in order to maintain referential integrity. Also note that you shouldn't use the autonumber feature of the DBMS in the primary keys in the secondary table.

Here are the nice features you get from using a shared primary key. Most DBMS products will create indexes on all columns (fields) that are declared as primary keys. And most DBMS products will use a very fast algorithm when performing joins where there is an index on both sides of the join condition. Also, a join between the main table and a secondary table will automatically drop out entries in the main table that do not pertain to the given secondary table.

A foreign key used in some third table that references the main table will automatically reference the related rows in relevant secondary tables. This makes the resulting data structure more flexible.

Shared Primary Key is very often used in conjunction with a design pattern described under the tag . The relationship between classes and subclasses is an IS-A variety of relationship.

39 questions
0
votes
1 answer

Missing column: id on a one-to-one mapping with shared PK

I'm sure this is a duplicate question, but I can't make it work. I have a one-to-zero-or-one relationship with shared primary key between an entity Home and an entity Address. My problem is that I keep getting an error Missing column: id I'm…
user3748908
  • 885
  • 2
  • 9
  • 26
0
votes
1 answer

Database design for relating a table to 1 of 2 other tables

I have a database with some users and agents and want to add a simple message system. So I have the following simple set of tables: [users] - user_id (PK) [agents] - agent_id (PK) [message_threads] - message_thread_id (PK) [message] - message_id…
0
votes
0 answers

how to make a relationship 1-1 in mysql

My question is What is correct to make a relationship 1-1 in MySQL? The data in the child table must be unique Exists confusing information on the internet about doing this. In my opinion the first form is correct because it is easier. DROP TABLE IF…
0
votes
1 answer

Primary key generate from a table for multiple table

I have a settings table 'SettingTab' and a Number column 'nSlNo (int)' to generate primary key of two tables 'Tab1' and 'Tab2'. How to implement this without any number conflict when access setting table at same time by two tables.
rgb
  • 143
  • 3
  • 15
0
votes
1 answer

Design considerations to using Foreign Key as Primary Key

Are there any general design considerations (good/bad/neutral) for using a foreign key of one table as the primary key in another table? For example, assume the following tables as part of a film catalogue: titles ------ id episodes -------- …
Mr Mikkél
  • 2,577
  • 4
  • 34
  • 52
0
votes
1 answer

Creating a combinational Primary Mysql

I am trying to create a very custom primary key, example in a [table_1] having columns tble_id, date_entered, time_entered, card_number, name, address. I would like to have the primary key (tble_id) to be a combination of the columns: date_entered,…
dames
  • 1,421
  • 8
  • 35
  • 55
0
votes
2 answers

SQLite error when executing delete on 2 primary keys

I created a table with 2 primary keys and when I tried to delete few of the records at once, but the whole table gets wiped. Here is the code I tried and I am not sure why it does that. Any help will be most welcomed. BEGIN TRANSACTION; DROP TABLE…
user3629316
  • 1,245
  • 2
  • 10
  • 8
0
votes
1 answer

OneToOne shared Primary Key, Pure JPA 2.0 Solution works with EclipseLink but fails with with Hibernate Provider

Please refer OneToOne between two tables with shared primary key for the original problem. And my answer in the same thread about a solution in Pure JPA 2.0 way (using EclipseLink Provider). Now the issue I am facing is that once I switched the JPA…
0
votes
3 answers

Foreign key as the primary key or just separate surrogate primary key different from foreign key in JPA context

What is the best practice to use FK as PK, or use surrogate PK, and FK just as FK in JPA context? I saw few times people said that they had to map FK as PK, because they had legacy database. So does it mean that for new tables, if you have control…
1 2
3