0

I have two tables as mentioned below. The point is to have 'n' choice for my question. I have designed to have composite key on the second table instead of having one more column for primary key. With this approach i have two questions.

  1. Is this a good approach in table design? i mean TABLE QUESTIONCHOICE has composite key rather than a primary key.

  2. How do i make one to many mapping in Hibernate between QUESTIONBANK and QUESTIONCHOICE tables using annotations?

Any points and suggestions will be of great help.

Thanks,

-Vijay Selvaraj

CREATE TABLE QUESTIONBANK(
    QUESTIONID INT NOT NULL AUTO_INCREMENT(10001, 1),
    QUESTION VARCHAR(200) NOT NULL,
    TOPIC VARCHAR(20) NOT NULL,
    SUBTOPIC VARCHAR(20) NOT NULL,
    COMPLEXITY SMALLINT  NOT NULL DEFAULT 1,
    QUESTIONTYPE SMALLINT  NOT NULL,
    VERSION INT NOT NULL DEFAULT 0,
    CONSTRAINT QUESTIONBANK_PK PRIMARY KEY (QUESTIONID)
);

CREATE TABLE QUESTIONCHOICE(
   QID INT NOT NULL,
   CHOICE VARCHAR(100) NOT NULL,
   CORRECT_CHOICE BOOLEAN, 
   VERSION INT NOT NULL DEFAULT 0,
   CONSTRAINT QUESTIONCHOICE_PK PRIMARY KEY (QID, Choice),
   CONSTRAINT QUESTIONCHOICE_FK FOREIGN KEY (QID) REFERENCES QUESTIONBANK (QUESTIONID)
);
4

1 回答 1

0

让我回答你问题的前半部分......

  1. 这是表格设计的好方法吗?我的意思是 TABLE QUESTIONCHOICE 具有复合键而不是主键。

这不是一个非此即彼的命题。您有一个键,它恰好是复合键和主键。

您的设计是否有效取决于相同的值是否CHOICE应该能够存在于多QUESTIONCHOICE行中(连接到不同的问题):

  • 如果是,那么您的设计是正确的。
  • 如果不是,那是不正确的,你应该把它CHOICE单独作为一个键。

是否还要添加代理键是一个平衡问题,这里有一些标准可以帮助您做出决定。

于 2012-08-01T14:52:15.130 回答