Let's say you have a database with a single table like...
---------------------------------------------
| Name | FavoriteFood |
---------------------------------------------
| Alice | Pizza |
| Mark | Sushi |
| Jack | Pizza |
---------------------------------------------
Would it be more space-efficient to have an additional table called "Strings" that stores strings, and change the FavoriteFood column to an index in the string table. In the above example, "Pizza" looks like it is stored twice, but with the additional table, it would appear to be stored only once. Of course, please assume there are 1,000,000 rows and 1,000 unique strings instead of just 3 rows and 2 unique strings.
Edit: We don't know what the FavoriteFoods are beforehand: they are user-supplied. The programmatic interface to the string table would be something like...
String GetString(int ID) { return String at with Row-ID == ID }
int GetID(String s) {
if s exists, return row-id;
else {
Create new row;
return new row id;
}
}
So the string-table seems more efficient, but do modern databases already do that in the background, so I can just do the simple one table approach and be efficient?