更好的关系数据库模式的建议
我来这里是为了设计一个对轮询和用户进行建模的关系数据库模式。每个 类别可以有一个或多个问题。每个用户都可以参与 每个类别只能轮询一次,并且可以轮询(或不轮询)一次 每个问题。每次民意调查都是赞成、反对或弃权(不投票)。
我用四个表设计了我的架构:
users, [userId, IP]
category, [catId, catTitle]
question, [queId, queTitle]
polls [pollId, queId, userId, answer]
更好,或者
users, [userId, IP]
category, [catId, catTitle]
question, [queId, queTitle]
polls [pollId, catId, userId]
pollAnswers [pollAndId, queId, pollId, answer]
我想知道哪一个更好,为什么?
根据我的说法,因为我不知道有关民意调查的任何额外信息,所以我直接交叉连接用户和问题与答案。
我还需要找出有多少用户放弃了 a.) 所有类别问题 b.) 特定类别 c.) 特定问题
我对选择一种模式有自己的看法:
select U1.*, Q1.*, P2.*, C1.*
from
( users U1,
questions Q1 )
Left outer Join polls P2 on
Q1.queId = P2.queId AND U1.userId = P2.userId
Left Outer Join category C1 on
Q1.catId = C1.catId
我试图担心使用上述查询与之间的交叉连接用户和问题是否会影响我的表现?
如果第二个模式更好,您可以为我的结果提出建议吗?
I am here to design a relational database schema that models polling and users. Each
category can have one or many questions. Each user can participate
only once at each category, and can poll (or not) exactly once on
each question. Each poll is yes, no, or abstain (no vote).
I have designed my schema with four tables:
users, [userId, IP]
category, [catId, catTitle]
question, [queId, queTitle]
polls [pollId, queId, userId, answer]
is better or
users, [userId, IP]
category, [catId, catTitle]
question, [queId, queTitle]
polls [pollId, catId, userId]
pollAnswers [pollAndId, queId, pollId, answer]
I would like to know which one is better and why?
as per me since i dun have any extra information about polls i am directly cross joining users and questions with answer.
I also do need to find how many users were abstain for a.) all category question b.) particular category c.) particular question
I have my view as for choice one schema :
select U1.*, Q1.*, P2.*, C1.*
from
( users U1,
questions Q1 )
Left outer Join polls P2 on
Q1.queId = P2.queId AND U1.userId = P2.userId
Left Outer Join category C1 on
Q1.catId = C1.catId
I am trying to worried using above query with cross join between users and question will loose my performance or not?
If second schema is better can u suggest options for my results?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
users:重命名为 user,以便所有实体都是单数(或将单数重命名为复数)
category:好的
问题:您需要一个从问题到类别的外键
pollAnswers:重命名为 pollAnswer,并使其成为包含三行的引用表:是的,不,弃权
polls:重命名为 poll,并使其成为用户轮询问题的事务,并使用 user、question 和 pollAnswer 的外键。
(选项:要么在插入时验证用户对该类别中的问题没有现有的民意调查,要么使用问题的类别外键对 PollAnswers 进行非规范化,并对用户/问题施加唯一的约束。我个人不喜欢此选项,因为它如果更改问题的类别,则会出现问题。)
然后针对特定类别的查询可能如下所示:
users: rename to user so all entities are singular (or rename the singulars to plural)
category: OK
question: You need a foreign key from question to category
pollAnswers: Rename to pollAnswer, and make it a reference table with three rows: Yes, No, Abstain
polls: Rename to poll, and make this a transaction of a user polling a question, with foreign keys to user, question, and pollAnswer.
(Options: Either validate on insert that the user has no existing polls for questions in the category, or denormalize PollAnswers with the question's foreign key to category and put a unique constraint on user/question. I don't personally like this option because it presents issues if you change the category of a question.)
Then a query for a particular category could look like this: