Question

More of a question concerning the database model for a specific problem. The problem is as follows:

I have a number of objects that make up the rows in a fixed table, they are all distinct (of course). One would like to create sets that contain a variable amount of these stored objects. These sets would be user-defined, therefore no hard-coding. Each set will be characterized by a number.

My question is: what advice can you experienced SQL programmers give me to implement such a feature. My most direct approach would be to create a table for each such set using table-variables or temporary tables. Finally, an already present table that contains the names of the sets (as a way to let the user know what sets are currently present in the database).

If not efficient, what direction would I be looking in to solve this?

Thanks.

Was it helpful?

Solution

Table variables and temporary tables are short lived, narrow of scope and probably not what you want to use for this. One table for each Set is also not a solution I would choose.

By the sound of it you need three tables. One for Objects, one for Sets and one for the relationship between Objects and Sets.

Something like this (using SQL Server syntax to describe the tables).

create table [Object]
(
  ObjectID int identity primary key,
  Name varchar(50)
  -- more columns here necessary for your object.
)

go

create table [Set]
(
  SetID int identity primary key,
  Name varchar(50)
)

go

create table [SetObject]
(
  SetID int references [Object](ObjectID),
  ObjectID int references [Set](SetID),
  primary key (SetID, ObjectID)
)

Here is the m:m relation as a pretty picture: enter image description here

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top