使用auto_increment
create table table_name (
id int unsigned not null auto_increment,primary key (id),
name varchar(10) nut null
)engine=innodb auto_increment=100 charset=utf8;
设置序列的开始值:
alter table table_name auto_increment=100;
可以在mysql数据表中设置指定的字段为primary key或unique索引来保证数据的唯一性。
如果想设置表中字段first_name,last_name数据不能重复,可以设置双主键模式来设置数据的唯一性,如果设置的双主键,那么那个键的默认值不能为null,可设置为not null。
create table table_name (
first_name char(20) not null,
last_name char(20) not null,
sex char(10),
premary key (last_name,first_name)
);
如果设置了唯一索引,在插入重复数据时,sql语句将无法执行成功,并抛出错误。
INSERT IGNORE INTO 与 INSERT INTO 的区别就是 INSERT IGNORE 会忽略数据库中已经存在的数据,如果数据库没有数据,就插入新的数据,如果有数据的话就跳过这条数据。这样就可以保留数据库中已经存在数据,达到在间隙中插入数据的目的。
group by having count(*)>1;
distinct
create table table_tmp select first_name,last_name,sex from persion group by (last_name,first_name ,sex);
drop table persion;
alter table table_tmp rename to persion;
方法二:也可以在数据表中添加 INDEX(索引) 和 PRIMAY KEY(主键)这种简单的方法来删除表中的重复记录
alter ingore table persion add primary key (last_name,first_name);
原文:https://www.cnblogs.com/myheart-new/p/11950705.html