Insert SQL Server row into specific position in formatted table

1.8k Views Asked by At

I'm new at creating database and currently trying to accomplish something that is really necessary for me.

Lets say I have a Database "Customer" with 300 rows all with a unique identifier called Id_.

Id_ | Customer | Postal | Country |
200 | Mica Sa. | 99582 | USA
201 | Shum Jr. | 10258 | USA
202 | Carl Ro. | 45697 | USA
203 | Brad Mi. | 24761 | USA

If i delete a row number 202 using:

DELETE FROM Customer
WHERE Id_ = 202;

I Get:

Id_ | Customer | Postal | Country |
200 | Mica Sa. | 99582 | USA
201 | Shum Jr. | 10258 | USA
203 | Brad Mi. | 24761 | USA

But when I try to insert a row using:

INSERT INTO Customer (Id_, Customer, Postal, Country)
VALUES (202, 'Peter R.', 08574, 'USA');

I get the row randomly inserted in the database, so my question is how do I insert this row exactly after 201(Id_) and before 203(Id_)?

1

There are 1 best solutions below

4
On BEST ANSWER

To help you clear some things up:

  • ID field seems to be int and not unique identifier
  • Insert statements are not made randomly in the DB, they go to the last record. E.G if you have 201,203 and you insert 202 it will go after 203.
  • The way you select the records (and thus they fetched and displayed) is another thing. You may run a query that return 202 before 203 but this doesn't mean that this is the way that are stored in the DB
  • If ID are actually of type int I recon you make them auto incremental

    Select * from Customer Order by Id_ Desc