MySQL insertion when the record does not exist:
Example: Insert multiple records
Assume there is a clients table with the primary key client_id. You can use the following statement:
INSERT INTO clients
(client_id, client_name, client_type)
SELECT supplier_id, supplier_name, 'advertising'
FROM suppliers
WHERE not exists (select * from clients
where clients.client_id = suppliers.supplier_id);
Example: Insert a single record
INSERT INTO clients
(client_id, client_name, client_type)
SELECT 10345, 'IBM', 'advertising'
FROM dual
WHERE not exists (select * from clients
where clients.client_id = 10345);
Using dual as the table name allows you to use it after the select statement Directly follows the values of the fields to be inserted, even if those values do not yet exist in the current table.