使用 ALTER 命令。让我们先创建一个表-mysql> create table DemoTable
-> (
-> StudentName varchar(100)
-> );
Query OK, 0 rows affected (0.46 sec)
以下是插入AUTO_INCREMENT−的查询alter table DemoTable ADD COLUMN StudentId int NOT NULL;
alter table DemoTable ADD PRIMARY KEY(StudentId);
alter table DemoTable CHANGE StudentId StudentId int NOT NULL AUTO_INCREMENT;
使用INSERT命令−在表中插入一些记录mysql> insert into DemoTable(StudentName) values('Chris');
Query OK, 1 row affected (0.17 sec)
mysql> insert into DemoTable(StudentName) values('David');
Query OK, 1 row affected (0.15 sec)
使用SELECT语句显示表中的所有记录mysql> select *from DemoTable;
输出结果+-------------+-----------+
| StudentName | StudentId |
+-------------+-----------+
| Chris | 1 |
| David | 2 |
+-------------+-----------+
2 rows in set (0.00 sec)