views:

431

answers:

3

I'm a total newbie when it comes to SQL. I'm making a site in ASP.NET for doing surveys. A privileged admin user will be able to build a survey. Then lots of other people will respond to the survey.

Once I have the set of questions for a new survey, what is the best way to automatically construct a table and an INSERT query to store the responses?

UPDATE

Thanks for the quick responses! I'm still a little unclear on how survey responses would best be stored in this scenario. Would each respondent get a row of the table? And if so, would the columns be "answer given for question k of the survey identified by the entry in column 1"?

+1  A: 

You probably don't want to be programatically generating the schema.

Instead, have a table for surveys, for questions (which belong to surveys), for response sets (one per user), and question responses (which belong to the response sets).

Ben Alpert
+8  A: 

You dont want to build a new table for each survey.

Create a surveys table (SurveyID, UserID, Name, etc)

Create a Questions Table (QuestionID, SurveyID, QuestionText, SortOrder, etc)

optionally, Create an Options table, assuming you have multiple choice type questions (OptionID, QuestionID, OptionText, etc)

Then when someone creates a survey, you insert into the Surveys Table, referencing that persons UserID as the foriegn key, then get the newly inserted SurveyID,

Then for each question they add, insert it into the Questions table, using the above SurveyID as the foriegn key.. and so on.

EDIT to answer your edit:

Sorry I should have followed through to cover storing answers as well.

You would want another table called SurveyResponses (ResponseID, Name, etc) And another table I'll call ResponseAnswers(ResponseAnswerID, SurveyID, ResponseID, QuestionID, AnswerText/AnswerID) Where SurveyID and ResponseID are foriegn keys to thier respective tables, and depending on wether users have multiple choice, or enter an answer, you would either store thier answer as text(varchar) or as another foriegn key to the options table.

Neil N
Also, you should establish up front how long responses last, whether or not you anticipate editing your surveys and other such things. There is a legacy system similar in concept to this that allows editing of surveys. When a question is edited, all the existing responses to that question are broken
Drew
once a question is answered, other than spelling mistakes, it should never be changed. If you want to "change" it, you set a flag to make that question inactive, then replace it with a new one. But in most cases you would really just want to start a new survey, and deprecate the old one.
Neil N
Thanks - this helps a lot!
Mike Kantor
A: 

You wouldn't automatically construct the table. The table would be predefined, and the answers would be added to them. I think the best way to get started with ASP.NET these days is to dive into MVC & Linq (http://asp.net/learn)

Jason