如何在狭窄的MySQL表中存储答案

时间:2019-03-07 16:52:25

标签: mysql data-structures

我正在设计一个数据库来存储问题和答案。我计划有5个表:测验,问题,userAnswer和multipleChoice

测验:

/** represents the store when logged out */
interface LoggedOutStore {
  type: 'LOGGED_OUT';
  login: LoggedOut;
}

/** represents the store when logged in */
interface LoggedInStore {
  type: 'LOGGED_IN';
  login: LoggedIn;
}

type LogState = LoggedInStore | LoggedOutStore; // the part of the store with login state
type AppState = LogState & OtherStateFields; // the actual redux store

// dummy components w/ typing
const LoggedInComponent = ({ login }: { login: LoggedIn }) => <></>;
const LoggedOutComponent = ({ logout }: { logout: LoggedOut }) => <></>;

function LogStateComponent(props: LogState) {
  switch (props.type) {
    case 'LOGGED_IN':
      return <LoggedInComponent login={props.login} />;
    case 'LOGGED_OUT':
      return <LoggedOutComponent logout={props.login} />;
  }
}
// Pick<LogState, never> === {}
type ConnectedLogStateComponentType = ConnectedComponentClass<
  typeof LogStateComponent,
  Pick<LogState, never>>;

// this should work in theory; but unfortunately, it needs casting
const ConnectedLogStateComponent = connect((s: AppState): object => s)
  (LogStateComponent) as any as ConnectedLogStateComponentType;

const initState: AppState = {
  login: { isLoading: false, lastAttemptFailed: false },
  type: 'LOGGED_OUT',
};

export const App = () => <Provider store={createStore((s = initState) => s)}>
  <ConnectedLogStateComponent />
</Provider>;

问题:

quizID          (int)(primary key) Auto Increment
quizName        (varchar)

用户:

questionID      (int)(primary key) Auto Increment
quizID          (Foreign Key)
question        (varchar)
questionType    (enum) textbox, multiple choice, int, date, decimal, email, ...

userAnswers:

userID          (int)(primary key) Auto Increment
firstname       (varchar)
surname         (varchar)
...
...

multipleChoice

questionID    (Foreign Key)
userID        (Foreign Key)
answerText
answerDec
answerDate
answerInt

PRIMARY KEY(questionID, userID)

我的问题出在答案表上,一些答案将是自由格式的文本,一些小数和一些整数(外键)

我该如何处理?我是否有4列,每种类型1列?如果是这样,我在检索答案时如何有效地选择3个中的正确者之一,我想尽可能多地执行SQ​​L中的“繁重任务”,而不必使用PHP来生成动态查询。

1 个答案:

答案 0 :(得分:0)

如果您不需要按答案进行搜索,那么我建议您将它们存储在JSON对象或类似对象中,而不必为单独的表烦恼。

quiz | question   | answers     | multipleChoice
---- | ---------- | ----------- | --------------
1    | "Country?" | [           | true
     |            |   "France", |
     |            |   "Germany" |
     |            | ]           |