MySQL优化表结构
如果我有一个食品列表,每个食品都有一个“课程字段”,那么以文本形式输入课程(“主菜”)是否有意义,或者我最好创建一个课程表然后使用外键参考?这是课程和食物之间的一对多关系,但是食物和课程之间是一对一的关系,对吗?
感谢您的任何建议。
If I have a list of food items, each with a "course field", does it make sense to enter the course in as text ("Entree"), or would I be better served to create a course table and then use foreign key referencing? This is a one-to-many relationship between the course and food, but a one-one relationship between the food and course correct?
Thanks for any advice.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我曾经为所有内容创建单独的表,但有时这比它的价值更麻烦。所以就看你的保养情况了。如果您要更改这些值并且不希望进行大量操作,例如:
如果您需要更改这些值,那么外部表也许不是一个坏主意。当您需要查询数据库时,问题就出现了......您需要内部或外部连接其他表。
一位 DBA 曾经告诉我,MSSQL 在索引 text/varchar 值方面做得很糟糕,所以这就是外部表方法诞生的原因......不确定我是否完全相信这一点,但同时他说 MySQL 做得更好这项工作,并使查询变得更加容易。
我确信您会因为 10 个不同的原因看到 10 个不同的答案,说明为什么您应该或不应该使用外部表,但很多时候这取决于您的具体需求。希望这会有所帮助。
I used to create separate tables for everything, but sometimes it's more hassle than it's worth. So it depends on your maintenance. If you are going to be changing these values and do not wish to have to do a massive such as:
if you need to change the values, then perhaps an external table isn't such a bad idea. The problem comes when you need to query the database...you'll need to inner or outer join the other tables.
A DBA once told me that MSSQL did a terrible job with indexing text/varchar values so that's why the external tables method was born...not sure if I completely believe that, but in the same breath he said that MySQL did a much better job of this, and makes queries much easier.
I'm sure you'll see 10 different answers for 10 different reasons why you should or shouldn't use an external table, but many times it comes down to what your specific needs are. Hope this helps a bit.
我对课程和食物坚持1:M。如果是这种情况,我将为课程创建一个单独的表,为食物创建一个单独的表,并使用 course_id 作为食物表上的外键。
无论如何,作为字符串输入课程仍然有效,但问题是当您要对特定课程进行更新时,您仍然会继续使用该课程来更新每个食品,有时某些餐厅可能最终会更改其课程,或者是他们可以添加、编辑或删除课程。 :)
I stick to 1:M for the course and food. If this is the scenario I would be creating a separate table for the course and separate table for the food and use the course_id as a foreign key on food table.
anyway entering the course as a string would still work but the problem is when your going to do an update for a particular course you would still go on each food item having that course to update it and sometimes some restaurant may eventually change their course it's either they may add, edit or delete a course. :)