create table polls (
id integer not null auto_increment primary key,
created datetime,
modified datetime
);
create table quizzes (
id integer not null auto_increment primary key,
created datetime,
modified datetime
);
create table questions (
id integer not null auto_increment primary key,
model varchar(255) not null, -- Poll or Quiz, in this scenario
foreign_key integer not null,
question varchar(255) not null,
created datetime,
modified datetime
);
create table answers (
id integer not null auto_increment primary key,
question_id integer not null,
answer varchar(255) not null,
created datetime,
modified datetime
);
对于民意调查和测验表,谁能使它比这更简单(更短)和更有意义?
这是另一个想法,哪个更好? =>
| polls | CREATE TABLE `polls` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`question` varchar(300) NOT NULL,
`mark` tinyint(4) NOT NULL,
`created` datetime DEFAULT NULL,
`modified` datetime DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 |
| pollanswers | CREATE TABLE `pollanswers` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`poll_id` int(11) NOT NULL,
`answer` varchar(300) NOT NULL,
`percentage` int(11) NOT NULL,
`correct` tinyint(4) NOT NULL,
`mark` tinyint(4) NOT NULL,
`created` datetime DEFAULT NULL,
`modified` datetime DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 |
最佳答案
首先,始终以单数命名您的表格。这是因为“表定义”实际上是每一行的定义 - 一个 行(即单数)。此外,它读起来更自然:quiz.id
有意义 - 测验的 id,但是 quizzes.id
是什么?测验的id?没有。
要回答您的问题,您绝对应该将投票和测验合并到一张表中。提示是它们具有相同的定义。如果您需要区分两者,请使用 bool 列。我们称之为测验。
您还应该将外键命名为“table_id”,例如“quiz_id”。
您可能应该为测验命名。
在 mysql 中,timestamp
数据类型自动在您更改行中的内容时更新。这非常方便 - 节省了将 now()
添加到每个更新/插入值集。
恕我直言,您的表格应该是:
create table quiz (
id integer not null auto_increment primary key,
created datetime,
modified timestamp,
name text,
is_poll boolean not null default false
);
create table question (
id integer not null auto_increment primary key,
quiz_id integer not null references quiz,
question varchar(255) not null,
created datetime,
modified timestamp
);
create table answer (
id integer not null auto_increment primary key,
question_id integer not null references question,
answer varchar(255) not null,
created datetime,
modified timestamp
);
关于mysql - 任何人都可以简化这个用于民意调查的 MySQL 表关系吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7312579/