mt0s
mt0s

Reputation: 5821

add values to mysql new field

i have a table with some fields

i add one more an 'id' field and i want to automatic take values 0,1,2...etc

whats the command to do this?

thnx

Upvotes: 0

Views: 188

Answers (2)

Ronnis
Ronnis

Reputation: 12833

create table t(
   a varchar(10) not null
  ,b varchar(10) not null
);

insert into t(a,b) values('a1', 'b1');
insert into t(a,b) values('a2', 'b2');
insert into t(a,b) values('a3', 'b3');

alter table t add id int not null auto_increment primary key;

select * from t;


+----+----+----+
| a  | b  | id |
+----+----+----+
| a1 | b1 |  1 |
| a2 | b2 |  2 |
| a3 | b3 |  3 |
+----+----+----+

Upvotes: 1

Svisstack
Svisstack

Reputation: 16616

Set AUTO_INCREMENT to this column.

mysql> CREATE TABLE example_autoincrement (
         id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
         data VARCHAR(100)
       );
Query OK, 0 rows affected (0.01 sec)

Upvotes: 0

Related Questions