Sql代码
- mysql> create database ssh2;
- Query OK, 1 row affected (0.04 sec)
- mysql> use ssh2;
- Database changed
- mysql> create table user(
- -> id integer primary key,
- -> firstname varchar(200) not null,
- -> lastname varchar(200) not null,
- -> age integer
- -> );
- Query OK, 0 rows affected (0.46 sec)
Java代码
- mysql> alter table user modify id integer auto_increment ;
- Query OK, 1 row affected (0.28 sec)
- Records: 1 Duplicates: 0 Warnings: 0
Java代码
- mysql> alter table user modify id integer auto_increment ;
- Query OK, 0 rows affected (0.39 sec)
- Records: 0 Duplicates: 0 Warnings: 0
- mysql> alter table user modify id integer default '1';
- Query OK, 0 rows affected (0.16 sec)
- Records: 0 Duplicates: 0 Warnings: 0
- mysql> alter table user modify id integer auto_increment ;
- Query OK, 1 row affected (0.28 sec)
- Records: 1 Duplicates: 0 Warnings: 0
Java代码
- mysql> alter table user add createtime timestamp default current_timestamp;
- Query OK, 2 rows affected (0.17 sec)
- Records: 2 Duplicates: 0 Warnings: 0
如果想要从一个表中创建相同数据结构的表:
create table newtbname select * from tab where 0;
这个新表和原表数据结构相同但是没有设置主键自动增长
alter table modify id integer primary key auto_increment
这样就保证了数据库中主键id自动增长