Home >Database >Mysql Tutorial >oracle 防止插入重复数据

oracle 防止插入重复数据

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOriginal
2016-06-07 15:08:102128browse

有时候用oracle的数据库,插入数据的时候需要判断一下该条数据是否已经存在。 我们的第一思路如下,首先执行下面这个sql: select count(*) isExists from t_test_lll; 然后判断isExists等于0与否,如果等于0,则执行insert。 上面这样写,也可以,但是多写

有时候用oracle的数据库,插入数据的时候需要判断一下该条数据是否已经存在。

我们的第一思路如下,首先执行下面这个sql:

 

select count(*) isExists from t_test_lll;

 

然后判断isExists等于0与否,如果等于0,则执行insert。

上面这样写,也可以,但是多写很多代码,不利于后期维护。

其实oracle可以内置在insert语句中进行判断,如下sql:

 

insert
when (not exists (select 1 from t_test_lll where id = '111')) then
into t_test_lll(id) select '111' from dual;


这样,当数据库里有一个id='111'的记录的时候,就不会进行insert操作了。

 

 

 

 

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