I have a similar table layout:
create table data ( id int identity primary key
, value int not null);
create table relation (id int identity primary key
, someid int not null
, dataid int foreign key references data(id));
When inserting data into the tables, in most of the cases I need to insert something in data
and also in relation
.
Currently I do it like this:
-- insert data.value = 1337 and relation.someid = 42 + relation.dataid = 1337
if not exists (select id from data where value = 1337)
insert into data (value)
values (1337);
insert into relation (someid, dataid)
values (42, (select id from data where value = 1337));
Is this the proper solution?
What can I do if I need to insert many of these lines?
Currently I insert all data into data
and then do the queries for relation
.