【问题标题】:Putting data in a normalized database将数据放入规范化数据库
【发布时间】:2015-12-06 19:06:45
【问题描述】:

我正在构建一个数据库系统,用户可以在其中对他们自己可以创建的问题进行投票。我正在努力正确地填充数据库,特别是因为我已经尝试对其进行规范化,这让我非常困惑!我对 SQL 和 MySQLi 非常陌生。

在下面的数据库中,当有人在我的网页上创建个人资料时,我将如何最好地输入用户信息,然后当我想将他们的 UserID 与 QuestionID 和 Up- 或他们投了反对票?

我正在寻求有关数据库设计的帮助,尤其是有关将数据放入数据库并将其“捆绑”在一起的问题。

【问题讨论】:

    标签: php mysql sql database mysqli


    【解决方案1】:

    我会尝试一些更简单的方法,然后一步一步规范化它。

    Users 表不错

    create table users (
      userid int not null auto_increment primary key,
      email varchar(100) not null,
      password varchar(255) not null
    );
    

    问题表

    我只是在问题表中标记谁提出了问题

    create table questions (
      questionid int not null auto_increment primary key,
      question mediumtext or whatever,
      userid int not null,
      add fk here for userid
    );
    

    问题投票 - 多对多/联结表

    create table question_votes (
      question_votesid int not null auto_increment primary key,
      questionid int not null,
      voterid int not null,
      votedate timestamp not null default current_timestamp,
      score int not null,
      add fk here for questionid and voterid
    );
    

    答案表

    create table answers (
      answerid int not null auto_increment primary key,
      answer mediumtext or whatever,
      questionid int not null,
      userid int not null,
      add fk here for questionid, userid
    );
    

    您可以选择创建只包含答案的答案和一个名为 question_answers 的连接表,其中包含 questionid、answerid 列。您也可以选择将 question_userid 和 answer_userid 放在此表中。阅读最后一段以自己获得答案。

    答案投票表

    create table answer_votes (
      answer_votesid int not null auto_increment primary key,
      answerid int not null,
      voterid int not null,
      votedate timestamp not null default current_timestamp,
      score int not null,
      add fk here for answerid and voterid
    );
    

    将一些虚拟数据放入其中,然后开始问自己一些问题——我需要从中得到什么样的信息。如果 X 发生了怎么办?当 X 发生时,这个模式可以适应我将来会被问到的答案吗?等等

    当您在脑海中进行这些问答时,您会发现应该在哪里规范化以及规范化到什么程度。

    【讨论】:

    • 非常感谢您回答 Zedfoxus。这个数据库让我很头疼,因为它需要做很多事情,而且我对它很陌生。我做了一个完整的帖子试图解释我想用它做什么,我想知道我是否可以让你看一下? stackoverflow.com/questions/34111419/…如果没有,那谢谢你的回答,我会尝试一次做几个简单的表格,看看能不能让自己更轻松!
    • 嗨@user2304993,我对你其他问题的回答与此类似。首先规范化两个表。运行一些查询并考虑您必须回答问题的场景。然后添加下一个表格并重复练习。添加另一个表并重复练习等
    • 哦,另外,如果您发现此答案有用,您可以通过接受它来结束您的问题吗?谢谢。
    猜你喜欢
    • 2012-10-08
    • 2012-06-21
    • 2013-11-10
    相关资源
    最近更新 更多