Home >Database >Mysql Tutorial >sql怎么避免插入重复数据三种解决方法

sql怎么避免插入重复数据三种解决方法

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOriginal
2016-06-07 17:48:492408browse

本文章介绍了三种关于如何解决重复数据处理方法,前二种有一写的局限性,最后一种方法相对比较好,有需要的朋友可以参考一下。


第一种方法:设置为主键

第二种方法:设置唯一性约束

 代码如下 复制代码
alter table 你的表名 add constraint cons_01 unique (重复列名一,重复列名二,重复列名三,重复列名四,重复列名五)

第三种方法:加SQL语句判断

 代码如下 复制代码

if not exists( 1 from tb where 重复列名一='xx' and 重复列名二='xx' and 重复列名三='xx' and 重复列名四='xx')
begin
    insert into ...
end

需要避免插入一样的数据,保证数据的唯一性,比如大学学生编号,肯定是唯一的,不能重复。

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn