Ask Your Question
1

Possible to update primary key on delete in Base?

asked 2012-07-20 14:57:40 +0200

anonymous user

Anonymous

Say I have a table with 4 rows and the id column (primary key) has the values 1,2,3,4.

When I delete row 2, is it possible in Base to update the primary keys so that the remaining 3 rows get the id values of 1,2,3 and not 1,3,4?

delete close flag offensive retag edit

Comments

I hope someone answers this. I'd like to know also.

DonLow ( 2012-08-14 04:00:55 +0200 )edit

1 Answer

Sort by » oldest newest most voted
0

answered 2013-03-06 10:50:42 +0200

qubit gravatar image qubit flag of United States
5816 3 50 43

Hi Anon, @DonLow,

In most databases, primary key values are not shuffled around upon deletion of rows. There are a couple of reasons for doing this:

  1. It takes time
  2. (more importantly) the database doesn't necessarily know how to update all uses of that primary key id across the database
  3. The primary key might even be used/referenced outside of the database

What's the use case in which you'd want to renumber all of the primary keys?

If you really need to have that kind of id in your table, instead of changing all of the primary keys, why not just put a new column "order_id" into your table. Whenever you delete a row from the table, iterate over the remaining rows (in ascending order by primary key) and set order_id to the appropriate value.

link delete flag offensive edit

Login/Signup to Answer

Donate

LibreOffice is made available by volunteers around the globe, backed by a charitable Foundation. Please support our efforts: Your donation helps us to deliver a better product!

Question tools

Follow

subscribe to rss feed

Stats

Asked: 2012-07-20 14:57:40 +0200

Seen: 112 times

Last updated: Mar 06