|
2018-01-22, 09:34 PM | #1 |
Lonewolf Internet Sales
|
Database structure advice needed
I hope I can explain the situation well enough that those of you that are database peeps can offer recommendations.
Situation: A large list of primary names, each with up to three aliases, along with other info. Desired output: An alphabetic list by name that shows the info including the aliases, plus within the list in the appropriate places alphabetically, the alias names with the associated primary name. So... would it be best to put it all in one table with fields for name, alias1, alias2, alias3, etc. OR Would two tables, one with the primary names and info, then a second table with aliases and the associated primary name? I'm using PHP and a Mysql database. Thx |
2018-01-23, 12:19 PM | #2 |
Shift Out / X-On
|
I would definitely use one table and sort the required outpud by just changing the variables by AND and OR LIKE in mysql query.
But if you want to use same db and call by aliases in other places other table just for aliases sound like best bet. You just need to join tables by id for same person. I guess id would numerical. Then its easy to join tables by id and get the values from other table. Option number 2 sounds better but its more code writing. But then your db would be very flexible for other projects. |
2018-01-23, 02:48 PM | #3 |
Lonewolf Internet Sales
|
Thanks, solution 1 sounds like the right fit for this situation.
|
2018-01-23, 03:58 PM | #4 |
Shift Out / X-On
|
|
2018-01-23, 09:50 PM | #5 |
Lonewolf Internet Sales
|
I've run across a few things from StackOverflow when searching Google. Thanks for the tip.
|
2018-01-30, 12:48 PM | #6 |
Asleep at the switch? I wasn't asleep, I was drunk
Join Date: Apr 2005
Posts: 214
|
2 tables.
1 with the primary name and an associated key. 2nd table with the primary key and a field for alias. That way you are not limited on number of aliases and do not have to change structure when you decide you need 4 instead of 5. Primary table: Id - auto-increment primary key Primary_name Varchar how ever long you need it to be Alias table: id - auto-increment key primary_id - points at key in primary table alias_name varchar however long you need Example: Primary table: 1 Primary Name 1 2 Primary Name 2 3 Primary Name 3 Alias Table: 1 1 Alias for Primary 1 1 2 1 Alias for Primary 1 2 3 1 Alias for Primary 1 3 4 2 Alias for Primary 2 1 5 2 Alias for Primary 2 2 Pull primary with their aliases Select a.primary, b.alias from primary_table a left outer join alias_table b on a.id=b.primary_id order by primary, alias Hope that makes sense |
2018-01-30, 10:43 PM | #7 |
Lonewolf Internet Sales
|
Thanks Sarettah, I was part way there already. You've confirmed I was headed in the right direction.
|
2018-01-31, 11:38 AM | #8 |
Asleep at the switch? I wasn't asleep, I was drunk
Join Date: Apr 2005
Posts: 214
|
You are quite welcome
|
|
|