MySQL - 每个表都应该包含它自己的 id/主列吗? [英] MySQL - Should every table contain it's own id/primary column?

查看:62
本文介绍了MySQL - 每个表都应该包含它自己的 id/主列吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在整理一个问答应用程序 - 只要存在与其相关的问题,答案就会存在.

I'm putting together a question and answers application - the answers are only going to exist as long as there is a question that relates to it.

所以我决定不给答案表提供它自己的 id 列,并将主键设为与 question_id 相关的外键.

So I've decided not to give the answers table it's own id column and have made the primary key a foreign key that relates to the question_id.

问题表:

id | title

答案表:

question_id | title

我应该保持这种方式还是给答案表它自己的 id 列?

Should I keep it this way or give the answers table it's own id column?

推荐答案

如果一个问题可能有多个答案,那么最好在 answer 表上也有一个主键如果我们得到重复的答案,则唯一地标识每一行

If there is possibility of multiple answers for a single question then it will be better to have a primary key on answer table too to identify each row uniquely if we get duplicate answers as follows

id |question_id |标题1 1 52 1 53 2 真

但是,如果您预计每个问题只有一个答案,那么最好将其合并到 question 表中,因为问题和答案都直接依赖于一个主键.

But, in case you are anticipating only a single answer for each question then it is better to merge it to the question table as both question and answer are directly dependent on a single primary key.

id |问题 |回答1 任务 1 ?52 任务 2 ?53 任务 3 ?真的4 任务 4 ?空值

我希望,这可以澄清您的疑问.

I hope, this clarifies your doubt.

这篇关于MySQL - 每个表都应该包含它自己的 id/主列吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆