1、打开Xshell,连接服务器并登陆到服务器,输入连接到MySQL
2、创建测试表,并指定字段为auto_increment,如下图所示create table test( id int not null auto_increment primary key, name varchar(10));
3、MySQL的auto_increment的基本特性;1.te衡痕贤伎st表的自动增长列可以insert手工插入,但如果插入的值是空或者是0,则实醪撅此饕际插入的将会是自动增长之后的值,请看下图insert into test values(0,'zhanghui');insert into test values(null,'liubei');select * from test;
4、可以使用并通过alter table test auto_incremenrt=n语句来强制设置自动增长列的初始值。例如使用如下语句强制设置自增长初始值alter table test auto_incremenrt=4;insert into test values(null,'guanyu');
5、我们使用last_insert_id()查询当前线程下的最后插入记录所使用的值。select last_insert_id();
6、一个表有且只能有一个auto_increment属性,自增长列必须是索引5.如果表中的auto_increment最大值被删除,不会被重用。即会巳鹞届蟑跳号mysql> select * from test;+----+------------+| id | name |+----+------------+| 1 | zhanghui || 2 | liubei || 3 | guanyu || 5 | shjhsdshjd |+----+------------+4 rows in set (0.00 sec)mysql> delete from test where id=5;Query OK, 1 row affected (0.01 sec)mysql>mysql> insert into test values(null,'machao');Query OK, 1 row affected (0.01 sec)mysql> select * from test;+----+----------+| id | name |+----+----------+| 1 | zhanghui || 2 | liubei || 3 | guanyu || 6 | machao |+----+----------+4 rows in set (0.00 sec)