有时对数据库进行插入操作时,需要数据库不存在时重复数据才进行插入。代码实现如下
--表结构
CREATE TABLE person (person_id int PRIMARY KEY, name varchar(20));
--插入操作
INSERT INTO person (person_id, name)
SELECT 1, 'Me'
FROM DUAL
WHERE NOT EXISTS (SELECT 1 FROM person WHERE person_id = 1);
有时对数据库进行插入操作时,需要数据库不存在时重复数据才进行插入。代码实现如下
--表结构
CREATE TABLE person (person_id int PRIMARY KEY, name varchar(20));
--插入操作
INSERT INTO person (person_id, name)
SELECT 1, 'Me'
FROM DUAL
WHERE NOT EXISTS (SELECT 1 FROM person WHERE person_id = 1);