Delete and relocate index

Asked

Viewed 171 times

2

How should I associate data to have a db SQL Server 2017 with a good performance. I am doing a data association and being new in this area I feel a little lost.

I have the following structure as an example:

imagem 1.1

Let’s say the structures above have the following data:

imagem 1.2 imagem 1.3

Delete and relocate

Using the example above I look for a solution to for example, if the "U2" on the table Account is excluded, the u3 be relocated to index 2 and the same change happens in the table Account_Data.

Doubts

This type of action is possible with pure SQL?

Should I use another language to manage these events?

  • simply, don’t do it. Id is the key to that record and should not be changed

2 answers

4


I’m finding it odd to have these two tables separated, but since there’s no description of the case, you might actually need it. By the name and structure of the tables something tells me you don’t need.

If you want an action in a table to trigger action in another table you need to use triggers. But if there’s only one table You don’t even need this :)

The primary key should never be changed in the database. It doesn’t matter that there will be holes. The only exception is if you create a routine that modifies all references to it throughout the database and can ensure that these keys were not used anywhere outside of DB. But I wouldn’t do that. Practically zero advantages and there are risks.

What you call index is this key, index is a set of keys.

Trying to do what you want will not make the database faster, this is micro-optimization and those that do not bring gains. There’s a chance it could get worse.

To tell the truth in most scenarios I know nothing should be effectively erased unless one has a strategy of how to do this properly.

Even if I wanted to do it, I don’t see the link between one table and another. If it’s the id then you really don’t need two tables and then the description of the question is wrong.

Model right, create the right indexes, make the right queries, configure the server correctly, make the application the right way and all the architecture of the solution is adequate, make tests, analyze the use, see the bottlenecks, all this is what will make it perform.

  • the answer suits me well, if possible tell me if in case the ID is not a primary index, how to do the relocation, vlw!

  • id is always primary, or at least should. Even if it is not, it will serve for some foreign key, then also can not move.

  • thanks, helped too much, along with the code of Rovann Linhalis that I can use in cases of need, I will take the tips of not change id and pk.

2

Responding to your comment in Maniero’s reply:

Suppose you have multiple records:

n
1
2
3
4
5
6

You will delete n 3. After deletion, simply perform a command:

update tabela set n = n-1 where n > 3;

But, also as Marisol said, I don’t see anything that that solves, and something totally unnecessary.

If you want the line number to come sequentially in a select without jumping, there are functions that do this.

  • 1

    you filled in the gaps, kkkkkk vlw bro, not being in primary indexes, your answer helps me a lot.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.