-1

I want to update Student table where student_id is an auto-increment field. But I have deleted some students from the table, now I want to assign the ids again (i.e from 1 to last row value).

Max
  • 1,810
  • 3
  • 26
  • 37

2 Answers2

0

You need to reset the auto increment for the students table.

ALTER TABLE Student AUTO_INCREMENT = 1
Danish Bhayani
  • 425
  • 3
  • 7
0

I don't think it is a good practise to update CD of a table. Anyway try yhis:

UPDATE YOUR_TABLE
SET ID = T.NEW_ID 
FROM (SELECT [ID_STUDENT] AS OLD_ID, 
             NEW_ID = ROW_NUMBER() OVER (PARTITION BY [ID_STUDENT] ORDER BY        [ID_STUDENT])) AS T
WHERE ID_STUDENT = T.OLD_ID
Galma88
  • 2,398
  • 6
  • 29
  • 50