Example 1
Add new subscribers but skip rows whose email already exists
The incoming batch contains id 3 ([email protected]) and id 4 ([email protected]). Alice's email already exists in the table, so that row is silently dropped. Carol's email is new, so id 4 is inserted. The final table has three rows: the original two plus Carol. The original Alice row (id 1) is unchanged — this is an ignore, not an update.
CREATE TABLE subscribers (id INT, email VARCHAR(100), UNIQUE (email));
INSERT INTO
subscribers
VALUES
(1, '[email protected]'),
(2, '[email protected]');INSERT IGNORE INTO subscribers (id, email)
VALUES
(3, '[email protected]'),
(4, '[email protected]');
SELECT
id,
email
FROM
subscribers
ORDER BY
id;CREATE TABLE subscribers (id INT, email VARCHAR(100), UNIQUE (email));
INSERT INTO
subscribers
VALUES
(1, '[email protected]'),
(2, '[email protected]');INSERT INTO
subscribers (id, email)
SELECT
v.id,
v.email
FROM
(
VALUES
(3, '[email protected]'),
(4, '[email protected]')
) AS v (id, email)
WHERE
NOT EXISTS (
SELECT
1
FROM
subscribers s
WHERE
s.email = v.email
);
SELECT
id,
email
FROM
subscribers
ORDER BY
id;CREATE TABLE subscribers (id INT, email VARCHAR(100), UNIQUE (email));
INSERT INTO
subscribers
VALUES
(1, '[email protected]'),
(2, '[email protected]');INSERT INTO
subscribers (id, email)
VALUES
(3, '[email protected]'),
(4, '[email protected]') ON CONFLICT (email) DO NOTHING;
SELECT
id,
email
FROM
subscribers
ORDER BY
id;CREATE TABLE subscribers (id INT, email VARCHAR(100), UNIQUE (email));
INSERT INTO
subscribers
VALUES
(1, '[email protected]'),
(2, '[email protected]');INSERT
OR IGNORE INTO subscribers (id, email)
VALUES
(3, '[email protected]'),
(4, '[email protected]');
SELECT
id,
email
FROM
subscribers
ORDER BY
id;MySQL and MariaDB use `INSERT IGNORE INTO`. PostgreSQL uses `ON CONFLICT (email) DO NOTHING`. SQLite uses `INSERT OR IGNORE INTO`. SQL Server uses a `WHERE NOT EXISTS` subquery to pre-filter conflicting rows. All produce identical results.