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

  • 相关阅读:
    Nginx教程(三) Nginx日志管理
    Nginx教程(二) Nginx虚拟主机配置
    官方解析Cookies和Session的区别
    J2EE十三个技术规范
    J2EE十三个规范小结
    tomcat -web.xml里的内容
    tcp协议和udp协议的使用场景
    IntelliJ IDEA创建maven web项目(IDEA新手适用)
    Maven安装与配置
    X86、X64和X86_64区别
  • 原文地址:https://www.cnblogs.com/myheart-new/p/11950705.html
Copyright © 2011-2022 走看看