开发者

add values to mysql new field

开发者 https://www.devze.com 2023-01-29 05:57 出处:网络
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

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


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 |
+----+----+----+


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)
0

精彩评论

暂无评论...
验证码 换一张
取 消