Question

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"?

Was it helpful?

Solution

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 person's 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.

OTHER TIPS

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).

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)

I'd think of a couple of options:

  • Create a one-to-many Survey table and Question table, where each Question has a column DataType that has a code of the response data-type we're expecting, and optionally some other columns / related table of available options, a column that specifies if this question is multi/single choice, mandatory/optional question etc. The Answer table's Answer is serialized/desieralized/indexed/formatted etc. according to the question data-type
  • Consider using NoSql database to store the survey data in a similar manner
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top