Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
Name: FBI Agent
im making an online address book using a database (mysql) and i have a few question about it.
one, i have everything made and working almost 100% but it seems very inefficient, uses way too much code that does almost the same thing.
i have a database called "address" and four tables. they are:
`names` => ID, FIRST_NAME LAST_NAME
`location` => ID, CITY, STATE
`contact_info` => ID, EMAIL, PHONE
`date` => ID, DATEall of the IDs are primary keys and i dont have any unique keys or the other ones (i dont really understand what they are, i just made the IDs primary because i red a little in a book about them).
well... my question is. is that how i should be setting up my database? if not how? and secondly, the reason i dont think it's that way is because when i try to get all of the information from one person (ID) it doesnt work. it says it's ambiugous.
SELECT * FROM `names`, `location`, `contact_info`, `date` where `ID` = 2 (for instance)
i would greatly appreciate any help on this. thanks
FBI Agent
AIM: EliteAssassin187

There is too much info to go into proper dsatabase structure or to teach you how to properly form databaser queries here - at least it would take more time than I am willing to devote.
The main problem I see is that you have four tables. Why? The only reason to do that (in this situation) is if the values in one table would have a many-to-one relationship witht he values in another table. Are you expecting that people will have multiple addresses, or multiple emails? It's entirely possible, but if this is your first project I would keep it simple and have just one table. Then you just need to Select * from "table"
The reason your query doesn't work is that you need to join the tables. For instance the ID last three tables should match up with the releveant ID of the names table or they should include a namesID column. Then in your select statement you need to include a JOIN statement that joins the tables based upon the common ID.

Michael's right on in saying this should all go into one table, unless you are trying to account for many to one or many to many relationships.
To be clear, the reason your query doesn't work is that you are joining the tables, but you aren't filtering the results. When you join two tables (which happens when you list them after the select statement), the result is every possible combination of rows from each table. For example, if you had two tables, A and B, both with one field named ID, select * from A,B this is what would happen:
A B
1 1
2 2
select * from A,B:
ID ID
1 1
1 2
2 1
2 2So you need to filter the results by specifying that you only want the ones where the id is the same. But if you just say WHERE id=1, the DB doesn't know which ID field you're talking about, since there are two. What you really want to do is WHERE A.id=B.id AND A.id=2.
Good luck,
-SN

ohhhh. i get it SN. that's pretty sweet. sounds like it'll work.
im still confused on how the JOIN works though.
and no micheal, this isnt my first project using MySQL, it's my second ;) i think that i did very good on my first project though, didnt have any problems but i only used two tables but they were used separatly from each other and i didnt add too much to it.
so now that i know how i could be doing this, should i really put this all in one table? on one query that i have it only needs the data from the `names` table, would it be more efficient to just use one table and take it all from there or just take it from three different tables?
thanks for the advice the both of ya ;)
FBI Agent
AIM: EliteAssassin187

SELECT * FROM `names`, `location`, `contact_info`, `date` WHERE `names`.ID = `location`.ID = `contact_info`.ID = `date`.ID AND `names`.ID = 2
lmao, that's one big ass query. thanks SN ;) but still let me know about that other question
FBI Agent
AIM: EliteAssassin187

"would it be more efficient to just use one table and take it all from there or just take it from three different tables?"
When I was first learning about databases, I was obsessed with efficiency. Honestly, forget about efficiency - that's a complicated topic. Once you know SQL, the RDMS you are using, etc. you can think about that and will know enough to handle it. For now, focus on how you want to access your data - what queries you will want to perform.
In your case, the data is simple. Breaking up across tables doesn't make querying any more or less difficult. Is there a query you can't do with one table? Assuming everything else is the same, querying many tables does not give you any advantages over querying one. Or maybe it does, I don't know what you are doing with the data.
When you have a dataset, think about what you want do with it. After a few projects, you will understand SQL enough to know when to break up tables, because it will be obvious. A book on the basics of normalization and ACID will show you when to do this. Look for some complicated datasets and try to query useful information. Once you see the relationships with real data, things will make a lot more sense.

well the thing is, i can do it either way, with one table or more. the thing is, sometimes i just need to extract the data from only one table (meaning it would be faster, right?) instead of takeing all of the data... but now that i think about it, i can put it all inot one table and then extract only certain parts, but then that makes big long complicated queries. i think i may do it this way for now and then think about it more later and then see how i would like it if i used one table and queries from there.
thanks for the bit about efficiency. it's just hard fr me to not think about that, i always write my code to the best of my ability and if i dont know the best way to do something, it really messes with me because im doing it 'wrong'.
FBI Agent
AIM: EliteAssassin187

FBI,
When designing your database, table structure should be based upon how the data fits together - not on how you plan on extracting the data. In your example above you are making the tables more complex and making the queries needed when you need all the data more complex.
For example if you were creating a DB for your music collection you could use three tables to properly hold the data. One table would be for artists and the data that was specific to them, another table for each of their albums with an id linking back to the artist table and a third table for tracks with an id linking back to the album table (and perhaps an id to link back to the artist table - but not really necessary since you can indirectly link through the album table).
In your example, it would only make sense to have a separate location table if you want to allow for entities in the names table to have multiple locations associated with them.
As for having complecated quries when only extracting certain data, you just need to create a function that will create the queries for you based upon the criteria you send it. For example, if you pass the function an asterisk have the function pull all of the data or you can pass it a comma separated list of the columns you want to get. It will depend upon the tables you are using and how you plan to use the data that will determine how you will write the function, but this is much easier to use than hard coding the queries in every place that you need to pull data.
Michael J

well i get that, but there isnt a HUGE difference in that as i just copy and past. if i did make a function like that, should i make it absolutly reconfigurable and set it in a root type directory or should i just have one simple function in the directory of the script that im doing?
FBI Agent
AIM: EliteAssassin187

Guys please help me with this php and mysql prob I just installed a phpdev5 bundle and when I tried to use phpmyadmin at first I was able to create a database then after I restart it gave me a error
Database running on localhost
ErrorSQL-query :
CREATE DATABASE
MySQL said:
You have an error in your SQL syntax near '' at line 1what should I do I`m really a newbie here I just want to branch out from graphic design to web developing please help thanks please reply
I`m really sorry if I`m out of the topic here guys
The Bold and the beautiful

FBI Agent,
The difference is that when you need to make changes to the database or the calls to the database, it becomes much more difficult the more complex that you design the database. By creating two tables when you do not need to, you are only adding complexity.
As for a database function, it will depend entirely on the project you are working on. For example, if you want a function that will either pull all data or only partial data from a SINGLE table, you could make it very flexible where you pass the table name and either * or the column names. However, if you need to pull data from tables that will be joined it becomes more complicated. But, if you have certain linked tables that you will pull different information from many times, then you could create a function for those querys.
Michael J

yeah, i recently (just yesterday) redid the whole thing using one table instead of four. it's sooo much easier to do queries and stuff.
so i finished the creating the entry, deleting, and editing parts. the only thing i really need to do now is make it all auto for the newbs that dont know how to change stuff or the busy people that dont have time to do it.
anyway, thanks for all of your guys help. it made this a whole lot easier and more knowledgable for me
FBI Agent
AIM: EliteAssassin187

![]() |
![]() |
![]() |

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.
| Ads by Google |