Reply
Table Organization.
Old 10-15-2007, 08:46 PM Table Organization.
l3lueMage's Avatar
Skilled Talker

Posts: 68
Location: San Jose, California
Okay, soemone asked me to setup a 'bookstore' type thing for their site, where users can comment/buy books(the purchasing will be done through paypal lucky me lol).

Anyways basically what I need help with is how to do the comments

Bookstore Table BookID BookTitle Author Pages Published ISBN Number Description
That is the first table, and this is the comments table?

Bookstore Comments CommentID Comments Author CommentNumber
But I'm unsure of how to link them together. I mean this is my problem.

In a single table if you say select all from books where BookID = 1
You can't have 2 descriptions for that 1 book if the descriptions is in the same table right?

So my problem is How do I make it so it loads the comments, I mean same thing you could say select bookIs and comments where ID = 1, but wont it only load 1 comment? How do you basically store multiple of the same "item"....eh I'm confusing myself now XD I hope you guys get what I'm trying to say.
l3lueMage is offline
Reply With Quote
View Public Profile
 
When You Register, These Ads Go Away!
Old 10-16-2007, 03:44 AM Re: Table Organization.
chrishirst's Avatar
Super Moderator

Posts: 13,576
Location: Blackpool. UK
Quote:
You can't have 2 descriptions for that 1 book if the descriptions is in the same table right?
Comments/reviews would go in a separate table and you would use a JOIN query
__________________
Chris. ->> Links are advertising NOT optimising!! <<-
Indifference will be the downfall of mankind, but who cares?
Code Samples | People Counting System
chrishirst is offline
Reply With Quote
View Public Profile Visit chrishirst's homepage!
 
Old 10-16-2007, 09:02 AM Re: Table Organization.
l3lueMage's Avatar
Skilled Talker

Posts: 68
Location: San Jose, California
yes but I dont understand where comments for the book with ID2 would go? or when you use JOIN it creates a blank table of comments for the item its being joined with? D;
l3lueMage is offline
Reply With Quote
View Public Profile
 
Old 10-16-2007, 05:49 PM Re: Table Organization.
chrishirst's Avatar
Super Moderator

Posts: 13,576
Location: Blackpool. UK
Ok

As a simple example, the table structures could be;

Bookstore Table
BookID (Primary Key)
BookTitle
Author
Pages
Published
ISBN_Number (No spaces are allowed in column names)
Description

Comments table
BookID (Foreign Key) (New Column )
CommentID
Comments
Author
CommentNumber (this column is not required)

the query to join these tables would be

Code:
SELECT BookID,BookTitle,b.Author,Pages,Published,ISBN_Number,Description,CommentID,Comments,c.Author FROM bookstore as b INNER JOIN comments as c ON b.BookID = c.BookID WHERE b.BookID = ID_For_requested_book ORDER BY CommentID DESC ;
no idea what database you are using but the above is plain ANSII SQL so should work on any DB Server
__________________
Chris. ->> Links are advertising NOT optimising!! <<-
Indifference will be the downfall of mankind, but who cares?
Code Samples | People Counting System
chrishirst is offline
Reply With Quote
View Public Profile Visit chrishirst's homepage!
 
Old 10-16-2007, 06:34 PM Re: Table Organization.
l3lueMage's Avatar
Skilled Talker

Posts: 68
Location: San Jose, California
Yes yes I understand that much, But what I dont understand is where the comments are stored for each book, obviously in the comments table, but for each book you see.

Basically what I'm seeing it as is this, if you join BookID 1 and show on the page, and then you get another page and join BookID 2, wont the comments be the same? I guess what I'm trying to understand is how does it know which comments are for which book, or is that all in the BookID key? :S
l3lueMage is offline
Reply With Quote
View Public Profile
 
Old 10-16-2007, 06:51 PM Re: Table Organization.
chrishirst's Avatar
Super Moderator

Posts: 13,576
Location: Blackpool. UK
BookID is the PK/FK connection for both tables

Primary Key in Bookstore and a Foreign Key in comments
Tables & Data
Code:
BookID       Title                          Author        ....       more fields
0001    Treasure Island            R.L. Stevenson   .....
0002    The Ascent of Man         Jacob Bronowski   ....

Comments
BookID     Comment    .... 
0001      Good Read
0002      Heavy Stuff
0001      Aaah Jim Lad!
0002      Investigation into modern civilisation
So by using the BookID to link both tables, only comment records where the BookID is a match are extracted.
__________________
Chris. ->> Links are advertising NOT optimising!! <<-
Indifference will be the downfall of mankind, but who cares?
Code Samples | People Counting System
chrishirst is offline
Reply With Quote
View Public Profile Visit chrishirst's homepage!
 
Old 10-16-2007, 07:53 PM Re: Table Organization.
l3lueMage's Avatar
Skilled Talker

Posts: 68
Location: San Jose, California
Exactly what I thought, so how are you supposed to allow multiple comments on a book?
l3lueMage is offline
Reply With Quote
View Public Profile
 
Old 10-16-2007, 08:07 PM Re: Table Organization.
tripy's Avatar
Fetchez la vache!

Posts: 2,055
Name: Thierry
Location: In the void
Look closer at what Chris showed you...
It shows exactly that.

A table don't need to have a unique constraint on it's primary key, or on any key. An index can even by composed of multiples row.
You should always take care that the cardinality of your datas is maintained, but in the case of the comments here, you could/should do that by the date the comment was left.

It would result that you would have no primary key on the comments table, but a composite index made of bookId and commentDt with bookId being a foreign key from the books table.

Nothings stops you to say that they can be multiples comment that got the same foreign key bookId. As long as the cardinality defined by bookId+commentDt is maintained.
__________________
Listen to the ducky: "This is awesome!!!"

tripy is offline
Reply With Quote
View Public Profile
 
Old 10-16-2007, 08:18 PM Re: Table Organization.
l3lueMage's Avatar
Skilled Talker

Posts: 68
Location: San Jose, California
I think I sort of get it, will try something and come back if I have problems haha :P
l3lueMage is offline
Reply With Quote
View Public Profile
 
Reply     « Reply to Table Organization.
 

Thread Tools

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
Trackbacks are Off
Pingbacks are Off
Refbacks are Off




   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML

 


Page generated in 0.15126 seconds with 12 queries