- Это должно быть все, что вам нужно.
- У вас может быть 9 миллионов обследований.
- У одного может быть 200 вопросов, а у другого может быть 4. Неважно.
create table users
(
userId int auto_increment primary key,
fName varchar(100) not null, -- Stan
lName varchar(100) not null -- Smith
)ENGINE=MyIsam;
create table surveys
(
surveyId int auto_increment primary key,
sTitle varchar(255) not null,
dtCreated date not null -- date created in system
)ENGINE=MyIsam;
-- create table questions
-- ( -- to keep things simple to start, assume questions are not in mysql
-- that can be for version Beta 0.02
--);
create table UserTakesSurvey
( -- A user takes a survey (this is going to be useful)
-- 1 row for each combo kinda obvious
userId int not null,
surveyId int not null,
dtTaken date not null,
-- Foreign Key constraints go here:
-- to Users Table:
FOREIGN KEY (userId) REFERENCES users(userId),
-- to Surveys Table:
FOREIGN KEY (surveyId) REFERENCES surveys(surveyId),
-- so no row can exist here with a wrong (non-existing) userId or surveyId
-- addition indexes here:
primary key (userid,surveyid),
key (surveyid,userid)
)ENGINE=MyIsam;
create table answers
( id int auto_increment primary key,
userId int not null,
surveyId int not null, -- survey id
qNum int not null, -- question number
answer int not null, -- 5 to 1
-- Foreign Key constraints go here:
-- to Users Table:
FOREIGN KEY (userId) REFERENCES users(userId),
-- to Surveys Table:
FOREIGN KEY (surveyId) REFERENCES surveys(surveyId),
-- so no row can exist here with a wrong (non-existing) userId or surveyId
-- Additional indexes go here:
-- create a composite (combo) key incorporating two columns
KEY (userId,surveyId),
KEY (surveyId,userId)
)ENGINE=MyIsam;
insert users (fName,lName) values ('Stan','Smith'),('Sally','Higgins');
-- first survey will have 7 questions hypothetically, 2nd 101 questions
insert surveys (sTitle,dtCreated) values ('Feelings about Milk','2013-01-17');
insert surveys (sTitle,dtCreated) values ('Thoughts on Global Warming',curdate());
.
-- we now have 2 users, 2 surveys
-- Stan is id=1, Sally is id=2. Ditto for survey numbers starting at 1 and up.
-- in web loop, say PHP or whatever, you are collecting question answers
-- loop thru upon Final Submit and do your inserts into table 'answers' (one row each)
-- Assuming for now surveys themselves for simplicity exist on paper/webpage without questions embedded in database (which is not a problem).
-- But trying to simplify the example. Also note that answers.answer is not null but it can be null (no user answer).
-- One table contains all answers without the need to monkey around with creating columns as you were.
-- That monkeying around would be an answers table for each survey, bad idea.
-- This will perform very fast, and data is easily accessible as if in the format you desire.
-- It may not be readily obvious how, but then again what are followup questions for :>
-- So that question is something like "How do I get at my data" !
Дрю, большое вам спасибо за помощь. У меня возникли проблемы с ответом. Я начинающий пользователь MySQL, и я был бы признателен за вашу помощь, пройдя через это немного. – Alexander
Без проблем позвольте мне добавить еще займет 15 минут – Drew
Спасибо. Если это помогает, и в случае, если это неясно, каждая запись в таблице, в которой я работаю, представляет собой ответчика на опрос, а столбцы представляют собой сами опросные вопросы. – Alexander