Database Management
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsDatabasesDatabase Management

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old November 1st, 2002, 04:52 AM
johnn johnn is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: May 2001
Location: Westminster, CA USA
Posts: 54 johnn User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 3 h 12 m 7 sec
Reputation Power: 8
table and select

Hello,

Say I have a table FRUIT defined as:
f_id primary key autoincrement
f_field1
f_field2
..
..
f_fruit_type
..

I could do:
"select * from table FRUIT where f_fruit_type = 'apple'";
and that is FIRST WAY to do it.

Now another way is whenever someone input and insert a row into table FRUIT that has fruit type = 'apple', I will insert also into TABLE APPL, defined as
ap_id primary key autoincrement
ap_fruit_id //foreign key is f_id as above

Then to list all rows that fruit type = 'apple' in table FRUIT, I do:

"SELECT f.*
FROM table FRUIT f, APPL a
WHERE f.f_id = a.ap_fruit_id "

OK, so this is the SECOND WAY to find fruit type = 'apple', but it requires 2 tables.
Which way is more efficient and better?

Thanks,
John

Last edited by johnn : November 1st, 2002 at 05:05 AM.

Reply With Quote
  #2  
Old November 1st, 2002, 12:02 PM
jharnois's Avatar
jharnois jharnois is offline
mod_dev_shed
Dev Shed God 20th Plane (14500 - 14999 posts)
 
Join Date: Sep 2002
Location: Atlanta, GA
Posts: 14,565 jharnois User rank is Lieutenant General (80000 - 90000 Reputation Level)jharnois User rank is Lieutenant General (80000 - 90000 Reputation Level)jharnois User rank is Lieutenant General (80000 - 90000 Reputation Level)jharnois User rank is Lieutenant General (80000 - 90000 Reputation Level)jharnois User rank is Lieutenant General (80000 - 90000 Reputation Level)jharnois User rank is Lieutenant General (80000 - 90000 Reputation Level)jharnois User rank is Lieutenant General (80000 - 90000 Reputation Level)jharnois User rank is Lieutenant General (80000 - 90000 Reputation Level)jharnois User rank is Lieutenant General (80000 - 90000 Reputation Level)jharnois User rank is Lieutenant General (80000 - 90000 Reputation Level)jharnois User rank is Lieutenant General (80000 - 90000 Reputation Level)jharnois User rank is Lieutenant General (80000 - 90000 Reputation Level)jharnois User rank is Lieutenant General (80000 - 90000 Reputation Level)jharnois User rank is Lieutenant General (80000 - 90000 Reputation Level)jharnois User rank is Lieutenant General (80000 - 90000 Reputation Level) 
Time spent in forums: 1 Month 1 Week 2 Days 22 h 35 m 18 sec
Reputation Power: 835
I've always looked at it as "how can I store the same information as little as possible?" and it's usually based on a one to many relation. You have one Fruit Type which relates to many Fruits.

In you current schema, you would store 'apple' many times in table Fruit, then again in table Apple. It would be better to store fruit types in a table with a unique id, then reference that id in your fruit table.

table.fruit
f_id
f_t_id // from table.fruit_type
f_1
f_2

table.fruit_type
f_t_id
t_label

In table.fruit_type you might have the following rows:
1 | Apple
2 | Pear
3 | Orange

In table.fruit, you'd have
1 | 1 | field1 | field2
2 | 3 | field1 | field2
3 | 1 | field1 | field2

In table.fruit, you have two apples, one orange, and zero pears.
__________________
# Jeremy

Explain your problem instead of asking how to do what you decided was the solution.

Reply With Quote
  #3  
Old November 1st, 2002, 01:54 PM
johnn johnn is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: May 2001
Location: Westminster, CA USA
Posts: 54 johnn User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 3 h 12 m 7 sec
Reputation Power: 8
Thanks, jharnois. So it's more efficient to break into 2 tables.
One thing, suppose this table:
1 | Apple
2 | Pear
3 | Orange

and there are exactly only 3 types of fruits like that, that's it. If when searching, the user wants to find a store in a city in the United States that sells apple (there are hundreds of thousands of stores), a detail about that store will be displayed. So in a query, it may contains several joins, do you think it's still efficient to have 2 table as above?

Thanks,
John

Reply With Quote
  #4  
Old November 1st, 2002, 02:10 PM
jharnois's Avatar
jharnois jharnois is offline
mod_dev_shed
Dev Shed God 20th Plane (14500 - 14999 posts)
 
Join Date: Sep 2002
Location: Atlanta, GA
Posts: 14,565 jharnois User rank is Lieutenant General (80000 - 90000 Reputation Level)jharnois User rank is Lieutenant General (80000 - 90000 Reputation Level)jharnois User rank is Lieutenant General (80000 - 90000 Reputation Level)jharnois User rank is Lieutenant General (80000 - 90000 Reputation Level)jharnois User rank is Lieutenant General (80000 - 90000 Reputation Level)jharnois User rank is Lieutenant General (80000 - 90000 Reputation Level)jharnois User rank is Lieutenant General (80000 - 90000 Reputation Level)jharnois User rank is Lieutenant General (80000 - 90000 Reputation Level)jharnois User rank is Lieutenant General (80000 - 90000 Reputation Level)jharnois User rank is Lieutenant General (80000 - 90000 Reputation Level)jharnois User rank is Lieutenant General (80000 - 90000 Reputation Level)jharnois User rank is Lieutenant General (80000 - 90000 Reputation Level)jharnois User rank is Lieutenant General (80000 - 90000 Reputation Level)jharnois User rank is Lieutenant General (80000 - 90000 Reputation Level)jharnois User rank is Lieutenant General (80000 - 90000 Reputation Level) 
Time spent in forums: 1 Month 1 Week 2 Days 22 h 35 m 18 sec
Reputation Power: 835
Yes, check out the manual for information on JOIN and INDEX.

Reply With Quote
  #5  
Old November 1st, 2002, 02:29 PM
johnn johnn is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: May 2001
Location: Westminster, CA USA
Posts: 54 johnn User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 3 h 12 m 7 sec
Reputation Power: 8
Thanks again, I'm kinda concerned when breaking down to more tables may create huge temporary table for database to work in a query, and if not then it may slow things down.

BTW, I posted same topic at forum http://www.sitepointforums.com/foru...p?s=&forumid=88

and jofa has about the same view as yours.

Reply With Quote
Reply

Viewing: Dev Shed ForumsDatabasesDatabase Management > table and select


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 5 hosted by Hostway
Stay green...Green IT