I use a specific database table, such as the "Install" data structure, i.e. you can try to insert the same row several times, but it will only contain one instance. The primary key is the natural key. For example, I want the following series of operations to work fine and result in only one line for Oklahoma:
insert into states_visited (state_name) values ('Oklahoma');
insert into states_visited (state_name) values ('Texas');
insert into states_visited (state_name) values ('Oklahoma');
Of course, I get an error due to duplication of the primary key on subsequent inserts of the same value. Is there a way to make the insert conditional so that these errors are not thrown? That is, only insert if the natural key does not exist yet?
I know I can make a where clause and a subquery to check for the existence of a string first, but it seems to be expensive. These are 2 physical operations for one logical conditional insert operation. Anything like that in SQL?
FYI I am using HSQLDB 1.8
source
share