I have
CREATE TABLE Persons (
name varchar(255) NOT NULL UNIQUE
);
How do I run an INSERT query only if the name is UNIQUE?
I have
CREATE TABLE Persons (
name varchar(255) NOT NULL UNIQUE
);
How do I run an INSERT query only if the name is UNIQUE?
You would just attempt to run the insert query. If the value doesn’t exist, a row will be inserted. If the value already exists, the query will produce a duplicate index/unique constraint error number that you can test for in your application code.
@mabismad has the correct answer
however, you could also just use MySQL’s INSERT IGNORE syntax, or use REPLACE instead of INSERT – assuming you’re using MySQL, which you never mentioned
thanks
This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.