简体   繁体   English

MySQL-何时向用户表添加新行以及何时创建新表?

[英]MySQL - When to add new rows to user table and when to create new table?

So I am creating a database and I have a few questions because I am not too sure if it's best to just add this to the users table or create a new table for it. 因此,我正在创建一个数据库,但我有几个问题,因为我不太确定是否最好将其添加到users表或为其创建一个新表。

So my users table has the following fields: 因此,我的用户表具有以下字段:

id, shop_name, address, city, state, zip_code, email_address, username, password, last_login, forgot_password

So those are the basics of each shop. 因此,这些是每家商店的基本知识。 Now I need to add 3 fields: 现在,我需要添加3个字段:

timezone_id which pulls from another table. 从另一个表中提取的timezone_id
primary_color which is going to be used as a hex color code primary_color将用作十六进制颜色代码
secondary_color which is also a hex color, for the main template colors. secondary_color ,也是主模板颜色的十六进制颜色。

From my understanding, if every single user will have the value filled out (so yes they will all have a time zone and have two colors, it cannot be blank) then it is appropriate to add to the users table. 根据我的理解,如果每个用户都将填写值(因此,是的,他们都将有一个时区并有两种颜色,不能为空),那么将其添加到users表是合适的。

Is that correct? 那是对的吗? Or would adding a table called colors be better and having an id, primary, secondary, user_id be better? 还是添加一个名为colors的表会更好,并且拥有一个id,primary,secondary,user_id会更好?

What are the pros and cons? 优缺点都有什么? Thank you! 谢谢!

Edit : So for example if I want to add a place for "slogans" but not every shop will have a slogan, would that be appropriate to create a new table for slogans and if a shop does decide to create their own then it would add it to the table? 编辑 :例如,如果我想为“标语”添加一个地方,但不是每个商店都将有一个标语,那将适合为标语创建一个新表,如果一个商店确实决定创建自己的标语,那么它将添加它到桌子上?

I just want to see if I am understanding this correctly. 我只想看看我是否正确理解了这一点。

As pro - you keep some of the entities separated - users and user_colors 作为专业人士-您将某些实体分开-用户和用户颜色

Cons: 缺点:

  • each time you have to access a user and its colors you have to do a join 每次您必须访问用户及其颜色时,都必须进行加入
  • also, each time a new user is inserted, you have to insert in 2 tables (this should be done in a transaction) 另外,每次插入新用户时,您都必须插入2个表中(这应该在事务中完成)
  • each time a user color is modified, you have to first find the user_id, then update in the colors table, whether otherwise you could simply update the user table with a simple where clause 每次修改用户颜色时,您都必须先找到user_id,然后在颜色表中进行更新,否则是否可以使用简单的where子句简单地更新用户表

I'd go for a single table 我去一张桌子

I would just add this to your user table. 我只是将其添加到您的用户表中。 There does not seem to be any reason for creating a new colours table. 似乎没有任何理由创建新的颜色表。 You would really only do this if each user would have an unlimited number of colours. 实际上,只有每个用户拥有无限数量的颜色时,您才这样做。 Having it in one table means you can get all data from one table in one simple query rather than having to have a join or have seperate queries to get all data for a user. 将其放在一个表中意味着您可以通过一个简单的查询从一个表中获取所有数据,而不必进行联接或使用单独的查询来获取用户的所有数据。

With regards to the Slogans, again just add this to the user table. 关于标语,再次将其添加到用户表中。 If they have a slogan, add this to the field, if they don't leave it blank. 如果他们有口号,请将其添加到字段中(如果他们没有将其留空)。 If a user was to have multiple slogans then perhaps then you would have this in a seperate table. 如果用户要使用多个标语,那么您可能会将其放在单独的表中。

Perhaps familiarise yourself with database normalisation as this really helps when you are unsure of how to design your databases. 也许您熟悉database normalisation因为当您不确定如何设计数据库时,这确实会有所帮助。

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

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