zoukankan      html  css  js  c++  java
  • 10、mysql序列使用、数据去重

    一、mysql序列使用

    使用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处理重复数据

    1、防止出现重复数据

    可以在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 会忽略数据库中已经存在的数据,如果数据库没有数据,就插入新的数据,如果有数据的话就跳过这条数据。这样就可以保留数据库中已经存在数据,达到在间隙中插入数据的目的。

    2、统计重复数据

    group by having count(*)>1;

    3、过滤重复数据

    distinct

    4、删除重复数据

    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);

  • 相关阅读:
    树状数组——求和问题题解
    java 基础01
    java jdk 配置
    一位资深程序员大牛给予Java初学者的学习路线建议
    汇编 OD 标志位 置位相关指令
    汇编 SETG,SETL ,SETGE, SETLE指令
    汇编 指令lodsb,lodsw,lodsd
    汇编 STOSB, STOSW, STOSD指令
    汇编 LOOP,LOOPD指令
    汇编 STD和CLD指令
  • 原文地址:https://www.cnblogs.com/myheart-new/p/11950705.html
Copyright © 2011-2022 走看看