Greenguy's Board

Greenguy's Board (http://www.greenguysboard.com/board/index.php)
-   General Business Knowledge (http://www.greenguysboard.com/board/forumdisplay.php?f=10)
-   -   Database structure advice needed (http://www.greenguysboard.com/board/showthread.php?t=69206)

Toby 2018-01-22 09:34 PM

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 |crazy|

pc 2018-01-23 12:19 PM

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.

Toby 2018-01-23 02:48 PM

Thanks, solution 1 sounds like the right fit for this situation. :)

pc 2018-01-23 03:58 PM

Quote:

Originally Posted by Toby (Post 549577)
Thanks, solution 1 sounds like the right fit for this situation. :)


For strictly coding querries/solutions I usually browse StackOverflow. It's a plethora of knowledge.

Toby 2018-01-23 09:50 PM

Quote:

Originally Posted by pc (Post 549578)
For strictly coding querries/solutions I usually browse StackOverflow. It's a plethora of knowledge.

I've run across a few things from StackOverflow when searching Google. Thanks for the tip.

sarettah 2018-01-30 12:48 PM

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

Toby 2018-01-30 10:43 PM

Thanks Sarettah, I was part way there already. You've confirmed I was headed in the right direction.

sarettah 2018-01-31 11:38 AM

Quote:

Originally Posted by Toby (Post 549736)
Thanks Sarettah, I was part way there already. You've confirmed I was headed in the right direction.

You are quite welcome |bananna|


All times are GMT -4. The time now is 01:04 PM.

Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
© Greenguy Marketing Inc