mysql> show create table user\G
*************************** 1. row ***************************
Table: user
Create Table: CREATE TABLE `user` (
`id` int(11) DEFAULT NULL COMMENT '表的ID',
`username` varchar(20) COLLATE utf8_icelandic_ci DEFAULT NULL,
`password` varchar(20) COLLATE utf8_icelandic_ci DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_icelandic_ci
1 row in set (0.00 sec)
## 添加user表 将ID设值为主键
mysql> alter table user add primary key (id);
Query OK, 0 rows affected (0.06 sec)
Query OK, 0 rows affected (0.06 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> show create table user\G;
*************************** 1. row ***************************
Table: user
Create Table: CREATE TABLE `user` (
`id` int(11) NOT NULL COMMENT '表的ID',
`username` varchar(20) COLLATE utf8_icelandic_ci DEFAULT NULL,
`password` varchar(20) COLLATE utf8_icelandic_ci DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_icelandic_ci
1 row in set (0.00 sec)
## 修改表 将ID设值为自动增长类型 并且为非空
mysql> alter table user change id id int not null auto_increment;
Query OK, 2 rows affected (0.03 sec)
Records: 2 Duplicates: 0 Warnings: 0
## 设值自增ID的起始值
mysql> alter table user auto_increment = 3;
Query OK, 0 rows affected (0.01 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> show create table user\G;
*************************** 1. row ***************************
Table: user
Create Table: CREATE TABLE `user` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`username` varchar(20) COLLATE utf8_icelandic_ci DEFAULT NULL,
`password` varchar(20) COLLATE utf8_icelandic_ci DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8 COLLATE=utf8_icelandic_ci
1 row in set (0.00 sec)