Forum Moderators: open

Message Too Old, No Replies

Lists in Mysql

How to

         

CWebguy

7:20 pm on Jan 18, 2008 (gmt 0)

10+ Year Member



Hello,
Is there a way to do a list in a Mysql database field? For instance, if I want to have a field that stores a list of questions for instance for a poll.

Table questions:

Id: Int not null primary key auto_increment
Questions: (List of questions here)
Date: Date
Category: Varchar(50)
Etc. Etc.

Thanks!

[edited by: CWebguy at 7:20 pm (utc) on Jan. 18, 2008]

ZydoSEO

7:59 pm on Jan 18, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Typically you wouldn't store a 'list' in a single column. It's a pain when you want to extract the individual elements using SQL only. Typically, you'd create a new table to hold the questions and relate them back to their parent poll.

So instead of having a single Table (say, tPoll) with columns such as:

PollID INT NOT NULL Primary Key Auto-Increment
Questions VARCHAR(?) NOT NULL
PollDate DATETIME
etc...

typically, you would have two tables (say, tPoll and tPollQuestion) with columns such as:

TABLE: tPoll:

PollID INT NOT NULL Primary Key Auto-Increment
PollDate DATE
etc... (other poll related attributes)

and

TABLE: tPollQuestion:

PollID (not sure of syntax in mysql to say foreign key back to tPoll.PollID)
QuestionID INT NOT NULL Auto-Increment
QuestionText VARCHAR(128)
QuestionOrder INT NOT NULL
etc... (other question related attributes)

[edited by: ZydoSEO at 8:01 pm (utc) on Jan. 18, 2008]

phranque

2:31 am on Jan 19, 2008 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



heed ZydoSEO, but you should be aware of the ENUM type [dev.mysql.com].

CWebguy

5:17 am on Jan 19, 2008 (gmt 0)

10+ Year Member



Thanks Zydo. I see what you are saying. Another question is, is let's say that I have a large database (50,000+ rows), would it still be wise to do it this way, even though it will create about 200,000+ rows in the questions table (estimating approximatly four questions per poll).

I know about the enum type, but if I'm not mistaken, doesn't that only let you insert one value into each field (which is limited to one of a number of predetermined values)?

Thanks!