When I delete the data in the middle of SQL as delete, I want to arrange the entire default key accordingly.

Asked 6 days ago, Updated 6 days ago, 1 views

Hello, I'm a beginner at coding.

When I deleted the value in the middle of the SQL syntax while studying the database, I thought the default key would be sorted on its own, but the default key remained the same as before.

For example, if you delete data number 3 with the default key of 1,2,3,4,5,6 respectively, the default key of the remaining data remains 1,2,4,5,6.

I want to automatically sort these basic keys in order like 1, 2, 3, 4, 5, but I can't find a way to search on the Internet, so I'm asking you a question.

If this is the case, is there any other way than deleting the data and reloading the default key from scratch?

I'd appreciate it if you could answer.

sql delete primary-key database

2022-09-20 15:45

2 Answers

Yes. I don't know why you need to rearrange, but PK key is the only value, so there should be no change.


2022-09-20 15:45

First of all, if you think you can change the default key, you can't do .At the level of the database, the data in 3 must/have existed only once in human history, can only be generated once, and can only be deleted once. If that doesn't work, history distortion will occur regarding what was the data of 3 or whether there was such a thing. Be sure to understand the concepts of unity, minimality, etc. .

Anyway, what you want to do is eventually to rank the default keys in ascending order It's a different concept that has nothing to do with the basic key. If it's MySQL, you can do it like this.

SELECT
    (@row_number := @row_number + 1) AS rnum,
    t.PK, foo, t.bar
FROM table t,
(SELECT @row_number := 0) as X
ORDER BY t.PK


2022-09-20 15:45

If you have any answers or tips


© 2022 pinfo. All rights reserved.