繁体   English   中英

将数据放入规范化数据库中

[英]Putting data in a normalized database

我正在建立一个数据库系统,用户可以在其中投票表决自己可以创建的问题。 我正在努力地正确地填充数据库,特别是因为我尝试对其进行规范化,并且这极大地困扰了我! 我对SQL和MySQLi非常陌生。

在下面的数据库中,当某人在我的网页上创建个人资料时,如何最好地输入用户信息,然后当我想将其用户ID与QuestionID和上下投票联系起来时再次使用该信息他们做了?

用于投票,用户和问题的数据库系统。

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

我会尝试一些更简单的方法,并将其一次标准化。

用户表好

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将来出现的问题? 等等

在脑海中进行这些问答时,您会发现应该在哪里进行标准化以及在多大程度上进行标准化。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM