-1

I have one existing table called 'x' contains 'n' columns and trying to add one more column called ID which should have value with 1 to 128 (because i have 128 rows) in table.how can i do this using sql.

Michael Berkowski
  • 267,341
  • 46
  • 444
  • 390
Veeresh Honnaraddi
  • 1,011
  • 1
  • 9
  • 20
  • 1
    If you add additional rows, should it continue incrementing? (I assume so) See [Insert auto increment primary key into existing table](http://stackoverflow.com/questions/9070764/insert-auto-increment-primary-key-to-existing-mysql-database/9070808#9070808) – Michael Berkowski Feb 11 '15 at 05:13
  • thanks for hint Michael,since i am using SQL server IDENTITY do work of AUTO_INCREMENT hence fallowing command works fine . ALTER TABLE tb1 ADD id INT PRIMARY KEY IDENTITY – Veeresh Honnaraddi Feb 11 '15 at 05:34
  • Retagging from mysqli to sql server then. – Michael Berkowski Feb 11 '15 at 05:41

1 Answers1

1

You can ALTER query with IDENTITY(1,1) for the new column.

ALTER TABLE x ADD ID INT IDENTITY(1,1) PRIMARY KEY

Here you get the fiddler help.

Veera
  • 3,412
  • 2
  • 14
  • 27