Current location: Hot Scripts Forums » Programming Languages » PHP » Many tables or "sub" tables?


Many tables or "sub" tables?

Reply
  #1 (permalink)  
Old 12-02-03, 01:06 AM
rhunter007 rhunter007 is offline
New Member
 
Join Date: Dec 2003
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Many tables or "sub" tables?

Here's my situation. I have a set of users. Each user has a number of "threads" of data. Each thread has a number of "entries". I want to know how to represent this in MySQL.

One option I see is to create a users database. Then each user has a table with threads. And also, each individual thread has its own table to house the entries. But this seems strange to me. I would be putting everything at the "top level" and it seems like it would be very cluttered.

Another option might be to have a top-level database, with a singe user table. Each element of the user table is a "thread". Each thread points to a subtable of entry tables. Conceptually this seems cleaner to me.

Which way should I do this and why? And, if the second way, then how?

Thanks.
Reply With Quote
  #2 (permalink)  
Old 12-02-03, 07:27 AM
hyjacked hyjacked is offline
Wannabe Coder
 
Join Date: Nov 2003
Location: New Brunswick, Canada
Posts: 174
Thanks: 0
Thanked 0 Times in 0 Posts
I would suggest:
User table
-user_id, name, etc

Thread table
-thread_id, user_id, entry_id, thread_title

Entry table
-entry_id, thread_id, entry_title, entry_data

User table contains all users.
Thread table contains all threads.
Entry table contains all entries.

If you need more information to understand this, feel free to post back.
Reply With Quote
  #3 (permalink)  
Old 12-02-03, 08:23 AM
YourPHPPro's Avatar
YourPHPPro YourPHPPro is offline
Community VIP
 
Join Date: Aug 2003
Posts: 430
Thanks: 0
Thanked 0 Times in 0 Posts
Quote:
Originally Posted by rhunter007
then how?
Adding to what hyjacked said, here's the SQL for what he said:

Quote:
# Database : `test_user`
#

# --------------------------------------------------------

#
# Table structure for table `entry`
#

DROP TABLE IF EXISTS `entry`;
CREATE TABLE `entry` (
`entry_id` int(3) NOT NULL auto_increment,
`thread_id` int(3) NOT NULL default '0',
`entry_title` varchar(50) NOT NULL default '',
`entry_date` date NOT NULL default '0000-00-00',
PRIMARY KEY (`entry_id`),
KEY `thread_id` (`thread_id`,`entry_date`)
) TYPE=MyISAM AUTO_INCREMENT=1 ;

#
# Dumping data for table `entry`
#


# --------------------------------------------------------

#
# Table structure for table `thread`
#

DROP TABLE IF EXISTS `thread`;
CREATE TABLE `thread` (
`thread_id` int(3) NOT NULL auto_increment,
`user_id` int(3) NOT NULL default '0',
`entry_id` int(3) NOT NULL default '0',
`thread_title` varchar(50) NOT NULL default '',
PRIMARY KEY (`thread_id`),
KEY `user_id` (`user_id`),
KEY `entry_id` (`entry_id`)
) TYPE=MyISAM AUTO_INCREMENT=1 ;

#
# Dumping data for table `thread`
#


# --------------------------------------------------------

#
# Table structure for table `users`
#

DROP TABLE IF EXISTS `users`;
CREATE TABLE `users` (
`user_id` int(3) NOT NULL auto_increment,
`name` varchar(50) NOT NULL default '',
PRIMARY KEY (`user_id`)
) TYPE=MyISAM AUTO_INCREMENT=1 ;

#
# Dumping data for table `users`
#
Reply With Quote
  #4 (permalink)  
Old 12-02-03, 11:02 AM
rhunter007 rhunter007 is offline
New Member
 
Join Date: Dec 2003
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Thanks for both of the previous replies!

One thing I don't understand, is why would the "thread" table contain a column for entryid? I may have explained it poorly, but remember that each thread has potentially many entries.

Thanks again.
Reply With Quote
  #5 (permalink)  
Old 12-02-03, 11:16 AM
YourPHPPro's Avatar
YourPHPPro YourPHPPro is offline
Community VIP
 
Join Date: Aug 2003
Posts: 430
Thanks: 0
Thanked 0 Times in 0 Posts
Quote:
Originally Posted by rhunter007
why would the "thread" table contain a column for entryid? I may have explained it poorly, but remember that each thread has potentially many entries.
Oops, you are right. The entries relate to the threads, not the other way around. Remove:

`entry_id` int(3) NOT NULL default '0',

from the thread table.
Reply With Quote
  #6 (permalink)  
Old 12-02-03, 12:37 PM
hyjacked hyjacked is offline
Wannabe Coder
 
Join Date: Nov 2003
Location: New Brunswick, Canada
Posts: 174
Thanks: 0
Thanked 0 Times in 0 Posts
yea that does make more sense, not sure what I was thinking at the time.

Nice catch

ps. that was just to keep you on your toes...haha
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

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

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Linking two tables within a db Ported Valhalla PHP 2 10-14-03 10:36 AM
Change the tables in a postnuke theme Marko PHP 0 08-18-03 02:41 PM
sorting multiple tables elmeto PHP 1 08-13-03 09:07 AM
Making tables.. Kylevision PHP 2 08-10-03 11:12 AM
script that measures height of tables and vertically aligns nested tables amj01 Script Requests 1 07-04-03 01:20 PM


All times are GMT -5. The time now is 06:07 AM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.