April 23, 2024 at 8:14 am
Hello All,
I have recently joined a project, where main, core tables with millions of records have FKs to the columns within the table itself like in the example below:
DROP TABLE IF EXISTS test_table;
CREATE TABLE test_table (
table_id INT NOT NULL,
parent_id INT NOT NULL,
top_parent_id INT NOT NULL);
ALTER TABLE test_table
ADD CONSTRAINT test_table_PK PRIMARY KEY (table_id);
ALTER TABLE test_table
ADD CONSTRAINT test_table_R01 FOREIGN KEY (parent_id)
REFERENCES test_table (table_id);
ALTER TABLE test_table
ADD CONSTRAINT test_table_R02 FOREIGN KEY (top_parent_id)
REFERENCES test_table (table_id);
I never saw this approach before and would like to know if it is reasonable, if there is any performance issues one would expect, please? Any pros and cons will be truly appreciated.
April 23, 2024 at 8:21 am
Obviously a hierarchy of some sort. What sort of data do the rows in the table represent?
The absence of evidence is not evidence of absence.
Martin Rees
You can lead a horse to water, but a pencil must be lead.
Stan Laurel
April 23, 2024 at 8:30 am
List of various Ids: document ids, booking id etc. Those in turn have their respective tables.
April 23, 2024 at 1:37 pm
April 23, 2024 at 2:34 pm
Take a look at Hierarchies on Steroids #1: Convert an Adjacency List to Nested Sets & Hierarchies on Steroids #2: A Replacement for Nested Sets Calculations from Jeff Moden.
Completely forgot about those "Steroids". Thank you for reminding.
April 23, 2024 at 6:57 pm
Take a look at Hierarchies on Steroids #1: Convert an Adjacency List to Nested Sets & Hierarchies on Steroids #2: A Replacement for Nested Sets Calculations from Jeff Moden.
Thanks for the referral, Ratbak.
--Jeff Moden
Change is inevitable... Change for the better is not.
April 24, 2024 at 10:14 pm
ALTER TABLE test_table
ADD CONSTRAINT test_table_R01 FOREIGN KEY (parent_id)
REFERENCES test_table (table_id);
Seems to me that the first row can only reference itself, so would have to watch the order in which you do your inserts.
----------------------------------------------------
Viewing 7 posts - 1 through 6 (of 6 total)
You must be logged in to reply to this topic. Login to reply