Skip to Main Content
  • Questions
  • Update primary key column or update other columns in a hierarchy

Breadcrumb

Question and Answer

Chris Saxon

Thanks for the question.

Asked: June 08, 2019 - 9:13 am UTC

Last updated: June 11, 2019 - 8:36 am UTC

Version: 11.2.0.4.0

Viewed 10K+ times! This question is

You Asked

I hava a table like the following structure:

create table table_a
(
"ID_TABLE_A" NUMBER, --primary key column
"PARENT_ID" NUMBER,
"CONTENT_1" VARCHAR2(32),
"CONTENT_2" NUMBER,
"CONTENT_3" NUMBER,
"CONTENT_4" VARCHAR2(256),
"CONTENT_5" varchar2(128)
)


There are about 200 records in the table_a which composite one logical collection, and is hierarchy structure. There may be many many such collections.

for the purpose of building a tree from such a collection which has about 200 records, I choose
manually generating primary keys, and they are continuous integers. So they can easily be ordered by sql, and built into a tree by java code(not related with oracle db).

Problems occurs when it comes that sometimes we need insert some extra records at some position into such collection, or delete some records at some position from such collection.
To keep the primary key column continuous integers. I may choose update primary key or update other content columns.

To make it clear, suppose we have records:
"ID_TABLE_A", "PARENT_ID", "CONTENT_1", "CONTENT_2", "CONTENT_3", "CONTENT_4", "CONTENT_5"
 1, null, null, null, null, null, null
 2,  1, null, null, null, null, null
 3,  1, null, null, null, null, null
 4,  1, null, null, null, null, null
 5,  2, null, null, null, null, null
 6,  2, null, null, null, null, null
 7,  3, null, null, null, null, null
 8,  3, null, null, null, null, null
 9,  4, null, null, null, null, null
10,  4, null, null, null, null, null
11,  5, null, null, null, null, null
12,  6, null, null, null, null, null
13,  7, null, null, null, null, null
14,  8, null, null, null, null, null
15,  9, null, null, null, null, null
16, 10, null, null, null, null, null


the above 16 records is a collection.

now we need add another child for 1, so its "ID_TABLE_A" should be 5, which already existed.
so I can choose:
A:

update ID_TABLE_A set ID_TABLE_A = ID_TABLE_A + 1 where ID_TABLE_A >= 5;
   insert into ID_TABLE_A (ID_TABLE_A) values(5);


B:

update ID_TABLE_A set PARENT_ID = #{}, CONTENT_1 = #{}, CONTENT_2 = #{}, CONTENT_3 = #{}, 
   CONTENT_4 = #{}, CONTENT_5 = #{}; -- for every record "ID_TABLE_A" >= 5
   insert into ID_TABLE_A (ID_TABLE_A, PARENT_ID, CONTENT_1, CONTENT_2, CONTENT_3, CONTENT_4, 
   CONTENT_5) values(17, 10, null, null, null, null, null);


way A need update primary key column, and I don't know the performance behavior.
way B need copy parent_id column and other content columns.

way A only need 2 SQL,
way B need many SQLs.

I really don't know which way is better.
Could you guys shine me some light? Sorry for my poor English.








and Chris said...

Updating a table's primary key is a bad idea!

You have to propagate this change to all child rows. Which Oracle Database doesn't support automagically. But even if it did, in extreme cases you could end up cascading updates to a huge number of tables.

My suggestion:

Abandon the idea of trying to have fully sequential primary keys, which somehow match the order of the tree.

Use a sequence/identity to generate the values. Once you've inserted the row, never change the primary.

And use SQL to generate the tree!

If you're not sure how to do this, I discuss various techniques in this video:



If you want to look into other ways of storing your tree, Bill Karwin has a great slide share discussing various ways to store hierarchies with the pros and cons:

http://www.slideshare.net/billkarwin/models-for-hierarchical-data

Rating

  (2 ratings)

Is this answer out of date? If it is, please let us know via a Comment

Comments

A reader, June 11, 2019 - 7:30 am UTC

Thank you so much for responding so soon!
But the youtube or slideshare.net link are not accessible for me as I cannot across GFW. I will read them maybe other time once I can 🙂.
I do understand the origin of the problem.
As I insist the primary key column sequential integers.
Maybe I should share more of the background.
There is annother table "TABLE_A_DEF", and "TABLE_A" is an instance table of "TABLE_A_DEF". In "Table_A_DEF", I don't rely on sequential integers of primary key column, the primary key is sys_guid().
Anyhow I am really looking forward to read the contents of links as this is the first time I use tree structure in sql. Thanks so much!
Chris Saxon
June 11, 2019 - 8:36 am UTC

Ack, that's a shame.

Bill Karwin also has a version of the hierarchy presentation at:

https://www.percona.com/live/17/sites/default/files/slides/Recursive%20Query%20Throwdown.pdf

Which hopefully you can reach. It's specific to MySQL, but the first ~20 slides discuss the storage concepts which you could use in Oracle Database.

And Tim Hall has a few articles discussing how to write hierarchical SQL:

https://oracle-base.com/articles/11g/recursive-subquery-factoring-11gr2
https://oracle-base.com/articles/misc/hierarchical-queries

A reader, June 11, 2019 - 8:57 am UTC

ah, yes, they can all be reached!!!
And they are really valuable!
Thank you so so much!

More to Explore

SQL

The Oracle documentation contains a complete SQL reference.