1、创建表
CREATE TABLE `trace_test` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(255) DEFAULT NULL,
PRIMARY KEY (`id`) // 最后一行不能有符号
) ENGINE=InnoDB AUTO_INCREMENT 10000 DEFAULT CHARSET=utf8 ;
建立远程连接 mysql -h *** -u root -p
2、数据库设置自增起始值
(1)创建表时设置AUTO_INCREMENT 10000参数
drop table if exists `trace_test`;
CREATE TABLE `trace_test` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(255) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT 10000 DEFAULT CHARSET=utf8 ;
insert into `trace_test`(`name`)values('name2');
select * from `trace_test`;
(2)使用alter table tablename
AUTO_INCREMENT=10000
drop table if exists `trace_test`;
CREATE TABLE `trace_test` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(255) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 ;
alter table `trace_test` AUTO_INCREMENT=10000;
insert into `trace_test`(`name`)values('name2');
select * from `trace_test`;
(3)如果表已有数据,truncate 之后设置auto_increment=10000,可行。
drop table if exists `trace_test`;
CREATE TABLE `trace_test` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(255) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 ;
insert into `trace_test`(`name`)values('name1');
select * from `trace_test`;
truncate table `trace_test`;
alter table `trace_test` AUTO_INCREMENT=10000;
insert into `trace_test`(`name`)values('name2');
select * from `trace_test`;
Truncate是SQL中的一个删除数据表内容的语句,用法是:
语法
TRUNCATE TABLE name
参数
name
`TRUNCATE TABLE` 在功能上与不带 WHERE 子句的 DELETE 语句相同:二者均删除表中的全部行。但 TRUNCATE TABLE 比 DELETE 速度快,且使用的系统和事务日志资源少。
DELETE 语句每次删除一行,并在事务日志中为所删除的每行记录一项。TRUNCATE TABLE 通过释放存储表数据所用的数据页来删除数据,并且只在事务日志中记录页的释放。
TRUNCATE TABLE 删除表中的所有行,但表结构及其列、约束、索引等保持不变。新行标识所用的计数值重置为该列的种子。如果想保留标识计数值,请改用 DELETE。如果要删除表定义及其数据,请使用 DROP TABLE 语句。
对于由 FOREIGN KEY 约束引用的表,不能使用 TRUNCATE TABLE,而应使用不带 WHERE 子句的 DELETE 语句。由于 TRUNCATE TABLE 不记录在日志中,所以它不能激活触发器。
(4)如果表已有数据,delete from之后设置auto_increment=10000,可行。
drop table if exists trace_test;
CREATE TABLE trace_test (
id int(20) NOT NULL AUTO_INCREMENT,
name varchar(255) DEFAULT NULL,
PRIMARY KEY (id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 ;
insert into trace_test(name)values('name1');
select * from trace_test;
delete from `trace_test`;
alter table trace_test AUTO_INCREMENT=10000;
insert into trace_test(name)values('name2');
select * from trace_test;
注:auto_increment再次设num的时候,mysql的auto_increment会变成目前num基础上最大
学习链接:https://www.cnblogs.com/yy3b2007com/p/11674844.html
https://www.cnblogs.com/yxdmoodoo/p/9728226.html