1. mysql> show create table user\G
    2. *************************** 1. row ***************************
    3. Table: user
    4. Create Table: CREATE TABLE `user` (
    5. `id` int(11) DEFAULT NULL COMMENT '表的ID',
    6. `username` varchar(20) COLLATE utf8_icelandic_ci DEFAULT NULL,
    7. `password` varchar(20) COLLATE utf8_icelandic_ci DEFAULT NULL
    8. ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_icelandic_ci
    9. 1 row in set (0.00 sec)
    10. ## 添加user表 将ID设值为主键
    11. mysql> alter table user add primary key (id);
    12. Query OK, 0 rows affected (0.06 sec)
    13. Query OK, 0 rows affected (0.06 sec)
    14. Records: 0 Duplicates: 0 Warnings: 0
    15. mysql> show create table user\G;
    16. *************************** 1. row ***************************
    17. Table: user
    18. Create Table: CREATE TABLE `user` (
    19. `id` int(11) NOT NULL COMMENT '表的ID',
    20. `username` varchar(20) COLLATE utf8_icelandic_ci DEFAULT NULL,
    21. `password` varchar(20) COLLATE utf8_icelandic_ci DEFAULT NULL,
    22. PRIMARY KEY (`id`)
    23. ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_icelandic_ci
    24. 1 row in set (0.00 sec)
    25. ## 修改表 将ID设值为自动增长类型 并且为非空
    26. mysql> alter table user change id id int not null auto_increment;
    27. Query OK, 2 rows affected (0.03 sec)
    28. Records: 2 Duplicates: 0 Warnings: 0
    29. ## 设值自增ID的起始值
    30. mysql> alter table user auto_increment = 3;
    31. Query OK, 0 rows affected (0.01 sec)
    32. Records: 0 Duplicates: 0 Warnings: 0
    33. mysql> show create table user\G;
    34. *************************** 1. row ***************************
    35. Table: user
    36. Create Table: CREATE TABLE `user` (
    37. `id` int(11) NOT NULL AUTO_INCREMENT,
    38. `username` varchar(20) COLLATE utf8_icelandic_ci DEFAULT NULL,
    39. `password` varchar(20) COLLATE utf8_icelandic_ci DEFAULT NULL,
    40. PRIMARY KEY (`id`)
    41. ) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8 COLLATE=utf8_icelandic_ci
    42. 1 row in set (0.00 sec)