Question

I would like to create a table that can store, say, the Title or Name of something in the first column, and then have associated people or objects in the next column. The problem is that there may be multiple people associated with the same Title or Name. If that first column is my primary key, I can't have duplicates for each row.

Name1 | Jim
      | John
      | Jill

Name2 | Mike
      | Mary

Name3 | Jeff

Does this need to be done with intermediary tables, and if so, I'm fuzzy on how to actually code them (in sqlite). Do I just create them with foreign keys referencing the appropriate attribute in the main table? Any help would be appreciated.

Was it helpful?

Solution

Yes, in SQLite (or any relational database) you would model this by creating separate tables. It is a capital offense in the relational model to ever store two pieces of information in one column.

It's difficult to give a more precise answer to your question because you don't include any specifics, but most likely you will need one table to store the "things" you're interested in, one table to store information about people (one row per person), and a third "linking" table to associate people with "things".

This third table contains only the columns that make up the primary key of the "things" table and the columns that make up the primary key of the people table. The primary key of this table is made up of all the columns (probably two columns if "things" and people are each identified by a single column in the source table), and contains two foreign keys, one back to "things" and one to people.

One row is added to associate a person with a thing, but additional rows can be added to associate more people with the specified thing and more things with each person.

OTHER TIPS

Another option is to use a DBMS specifically designed to support multiple values in a single field. There are several to choose from including at least one open source flavor. Look at d3, rocket software (U2 DBMS), Ladybridge (open qm) and search Google for multivalued DBMS for more.

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