Skip to main content

All Questions

Tagged with
Filter by
Sorted by
Tagged with
2 votes
3 answers
1k views

Foreign key reference to column that is no primary key

I know, foreign keys should uniquely identify rows and therefore reference a primary key. However, consider my example: There are 4 pre-defined categories, and for some combinations of values for ...
David's user avatar
  • 121
2 votes
0 answers
56 views

Design for a child to have a single parent `A` OR `B` with FK cascade constraint?

I'm assuming this is not possible with PK and FK... Let's say I have tables A, B, and C. A and B are "parents", and B and C are "children" A is the root B can be the child of either A or B C ...
xtratic's user avatar
  • 121
1 vote
1 answer
110 views

How to add foreign key in a table where column value can come from two different tables?

I have three tables create table main_comments ( id serial primary key, comment text not null ); create table sub_comments ( id serial primary key, main_id integer not null, comment ...
gaurav's user avatar
  • 113
4 votes
1 answer
1k views

Surrogate vs composite key in hierarchical data structure

I'm currently evaluating a schema for a hierarchical data structure. My main problem is how I should design the schema to prevent inconsistent data (reference of foreign key in another hierarchy). The ...
Dave's user avatar
  • 173
7 votes
2 answers
2k views

Nested table hierarchy: foreign key duplication vs. join

I have a nested table hierarchy, similar to the following one (this is an example but demonstrates the schema well enough): +---------+ +---------+ +---------+ +-------------+ | users | | ...
shevron's user avatar
  • 214
4 votes
1 answer
3k views

Foreign key with multiple table options

I'm not sure if the title is very good, feel free to suggest a better one. People can "comment" on different objects of my site,similarly to StackExchange (for example they can comment on a Question, ...
nute's user avatar
  • 313