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
);
Can anyone make it more simpler(shorter) and meaningful than this for polls and quiz table ?
Here is another idea, which is better ? =>
| 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 |
Firstly, always name your tables in the singular. This is because a “table definition” is actually the definition for each row – one row (ie singular). Also, it reads more naturally:
quiz.idmakes sense – the id of the quiz, butquizzes.idis what? The id of the quizzes? No.To answer your question, you should definitely merge poll and quiz into one table. The tip off is they have the same definition. If you need to distinguish between the two, have a boolean column. Let’s call it quiz.
You should also name your foreign keys as ‘table_id’, eg ‘quiz_id’.
You should probably give a name to your quizzes.
In mysql, the
timestampdata type automatically updates whenever you change something in the row. It’s pretty handy – saves addingnow()to every update/insert value set.IMHO your tables should be: