View Single Post

   
  #7 (permalink)  
Old 02-28-2008, 09:35 AM
Jerry Stuckle
 
Posts: n/a
Default Re: Problem with very long 'create table as select' query

Tomasz Sokólski wrote:
> Jerry Stuckle napisał(a):
>
>>> That's not my design - I just work with problems with such designs
>>> BTW - how to store data from forms - where each form has 1000
>>> questions. Each record is one filled form which contains 1000 answers.

>>
>> Read up on database normalization. A much better way would be to have
>> a second table with three columns - userid, questionid and answer.

>
>
> Very interesting.
>
> So we have table:
>
> userid questionid answer
>
> paul 1 0.33333
> paul 2 0.45454
> paul 3 0.23232
> john 1 0.55555
> john 2 0.44444
> john 3 0.22222
>
> I want to insert new records:
>
> userid questionid answer
>
> paul 4 answer=questionid[1].answer(where userid = paul) +
> questionid[2].answer(where userid = paul)
> john 4 answer=questionid[1].answer(where userid = john) +
> questionid[2].answer(where userid = paul)
>
> Is there a possibility to insert this records in one sql command?
>
> (sorry - I'm not an SQL programmer - I would like to know if this is
> hard to write in one sql command)


Not a problem at all.

INSERT INTO answers (name, questionid, answer) VALUES ('paul',
questionid, answer);

Of course, you would also be better off using id's - for instance, what
if you have two "paul"s? Something like:


Table TestTakers
id name

Table Answers

id question answer

Your TestTakers table might look like:

1 paul
2 John

And your Answers table might look like

1 1 0.33333
1 2 0.45454
1 3 0.23232
2 1 0.55555
2 2 0.44444
2 3 0.22222

Then to get a specific answer you would join the two tables, i.e.

SELECT answer FROM Answers
JOIN TestTakers ON Answers.id = TestTakers.id
WHERE questionid = 2;

You really need to read up on SQL and database normalization. SQL is a
very powerful language, and proper normalization will make you life much
easier.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================
Reply With Quote