As a general rule, you should just try to insert. The dbms will return an error if they key exists. Trap that error.
You have to trap errors anyway, because there are a lot of things besides primary key constraints that can prevent a row from being inserted. Among them
- Any other kind of constraint, like a foreign key constraint, a check constraint, etc.
- Disk error.
- Network error.
- Hurricane.
You don't lose any efficiency by trapping the error.
- Inserting (no error) requires one round-trip to the database.
- Inserting (with error, which you trap) requires one round-trip to the database, not counting whatever you might have to do to correct that error. You'd have to do all the correcting if you checked first anyway.
- Check-then-insert requires two round-trips to the database.
Some platforms can merge data if you try to insert a row for which a primary key already exists. The most famous (or most notorious, depending on your point of view) is MySQL's ON DUPLICATE KEY UPDATE.