查看db.table表的下一条记录auto_increment的值:
show table status from db like 'table'
例如:
mysql>show table status from mailbox like 'mailbox'
+---------+--------+---------+------------+------+----------------+-------------+-----------------+--------------+-----------+----------------+---------------------+-
--------------------+---------------------+-------------------+----------+----------------+---------+
| Name| Engine | Version | Row_format | Rows | Avg_row_length | Data_length | Max_data_length | Index_length | Data_free |Auto_increment| Create_time |
Update_time | Check_time | Collation | Checksum | Create_options | Comment |
+---------+--------+---------+------------+------+----------------+-------------+-----------------+--------------+-----------+----------------+---------------------+-
--------------------+---------------------+-------------------+----------+----------------+---------+
| mailbox | MyISAM | 10 | Dynamic| 1681 |148 | 249688 | 281474976710655 | 104448 | 0 | 15355| 2012-03-13 11:19:10 |
2012-03-13 11:19:10 | 2012-03-13 11:19:10 | latin1_swedish_ci | NULL || |
+---------+--------+---------+------------+------+----------------+-------------+-----------------+--------------+-----------+----------------+---------------------+-
--------------------+---------------------+-------------------+----------+----------------+---------+
1 row in set (0.00 sec)
Auto_increment:15355
这个就是下一条记录会使用的自增ID;
修改db.table表下一条记录使用的自增ID值为20000:
alter table db.table auto_increment=20000
例如:
mysql>alter table mailbox.mailbox auto_increment=20000
Query OK, 1681 rows affected (0.05 sec)
这时再进行:
show table status from mailbox like 'mailbox'
auto_increment已经变成20000;
新插入的记录,自增字段就会从20000开始;
设置mysql 自动插入id,需要设置id为整形自动增长的主键。下面介绍设置方法,
通过一个例子说明:
创建一张表student
create table student(
id int(5) not null auto_increatment,
name varchar(20) not null,
age int(3) not null,
primary key(id))
在插入数据可以不用插入id,可以这样:
insert into student(name,age) values('xiaobai',12)第一条插入数据id默认是1
insert into student (name,age) values('xiaoming',11)第二条插入数据id默认是2,第三条,第四条,······,依次自动增加。
由以上例子可知表插入数据的时候,无需插入id,即可自动插入。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)