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).
Asked
Active
Viewed 61 times
-1

Max
- 1,810
- 3
- 26
- 37

user3863717
- 3
- 1
-
Why do the id values matter? – jarlh May 28 '15 at 12:38
-
Why do you need to update those values? Pay attention if Student Table is referred somewhere else as a foreign key – Galma88 May 28 '15 at 12:45
-
Which DBMS are you using? Postgres? Oracle? – May 28 '15 at 12:59
2 Answers
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