1

I am now concerned that I have not normalised the tables correctly as now I see no way to join them;

I have 1 table with 2 columns called Questions, and another table called answers with 10 columns, one for the userId then 9 columns which hold all the answers (int) for each user.

Everything is working well for inserts and updates, however I am having a heck of a time trying to create a view which will show all questions, each user and each of their responses to each question.

Question table;

CREATE TABLE [dbo].[Questions](
[questionId] [int] IDENTITY(1,1) NOT NULL,
[question] [nvarchar](max) NULL,
CONSTRAINT [PK_Questions] PRIMARY KEY CLUSTERED 
(
[questionId] ASC
 )WITH (
        PAD_INDEX  = OFF, 
        STATISTICS_NORECOMPUTE  = OFF, 
        IGNORE_DUP_KEY = OFF,       
        ALLOW_ROW_LOCKS  = ON, 
        ALLOW_PAGE_LOCKS  = ON
       ) ON [PRIMARY]
 ) ON [PRIMARY]

Answer table;

CREATE TABLE [dbo].[Answers](
[empID] [nvarchar](10) NOT NULL,
[q1] [int] NULL,
[q2] [int] NULL,
[q3] [int] NULL,
[q4] [int] NULL,
[q5] [int] NULL,
[q6] [int] NULL,
[q7] [int] NULL,
[q8] [int] NULL,
[q9] [int] NULL,
 CONSTRAINT [PK_Answers] PRIMARY KEY CLUSTERED 
(
[empID] ASC
)WITH (
       PAD_INDEX  = OFF, 
       STATISTICS_NORECOMPUTE  = OFF, 
       IGNORE_DUP_KEY = OFF,   
       ALLOW_ROW_LOCKS  = ON, 
       ALLOW_PAGE_LOCKS  = ON
      ) ON [PRIMARY]
) ON [PRIMARY]

Nothing I have tried works, so any ideas from you all that can help me avoid re-doing the tables would be so greatly appreciated!

Thanks in advance for any help.

Alex

2
  • 2
    Will you always only have 9 questions? Is the the answer to the question with QuestionID = 1 stored in column q1? Aug 12, 2011 at 16:48
  • 2
    Whether or not you can make this work, which I could imagine something on querying the system's columns for this table, ordering them, finding the appropriate column by its row number,turning the one you need into a string... The best answer here is to fix this. Your answer table should be [employeeID] [questionID] [answer], if you want this functionality.
    – Sam DeHaan
    Aug 12, 2011 at 16:57

2 Answers 2

2

Seems to me that your answer table should have a foreign key to the question table. Unless an answer can refer to multiple questions, in which case I would say you should consider something like a mapping/relation table which has FKs to both answer and question tables.

0

I think you might need to look into pivot tables to accomplish your task.

2
  • Hmm, after reading all the comments, I think you're right, that pivot tables are my answer - but I am still a bit confused by how to turn columns into rows. I have 20 sections of questions and I have a simple code routine to update and insert answers;
    – Alex
    Aug 15, 2011 at 11:34
  • int i = 1; var q = new int[31]; var objDB01 = new dbconn(); const string strSQL = "insertInvestigationsResults"; objDB01.objCommand.Parameters.AddWithValue("@empId", logonName); foreach (GridViewRow gvr in GridView1.Rows) { var rb = gvr.FindControl("investigate_list") as RadioButtonList; var quest = rb.SelectedValue; if (quest == "") { quest = "0"; } int question = Convert.ToInt32(quest); objDB01.objCommand.Parameters.AddWithValue("@q" + i, question); i++; }
    – Alex
    Aug 15, 2011 at 11:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.