简体   繁体   中英

Should I split a table into 2 separate tables?

I'm very new to SQL and I'm trying to learn it by working on a small personal project. I have a table of users like this:

  Table users{
  id int [pk, increment] 
  username varchar(14) [not null, unique]
  username_lower_case varchar (14) [not null, unique]
  email varchar(40) [not null, unique]
  password varchar(72) [not null]
  admin boolean [not null]
  bio text [default: 'No info ']
  banned boolean [not null]
  banned_by id 
  banned_reason varchar
  active_account boolean [not null]
  created_at timestamp [not null]
}

I'm debating if banned, banned_by, banned_reason should be extracted into a different table or it is ok to have current structure. I fill like it's easier to leave as is because later on I don't need to join tables to get data, however, my understanding might be completely wrong. I'd appreciate any advice.

I believe what you are referring to is normalization in databases. The goal of normalisation at a very basic level is to take what is redundant and repeated data and have a reference to it to save on writes and storage. You can read more on that here database-normalization .

Typically this is a rule that can be broken for business reasons or ease of use especially in reporting however it's best to understand the rules before breaking them. I have worked on large datasets where it was not normalised (denormalised) to make life easier when querying however this approach is not as efficient in writes as there is now redundant data.

I personally would have a separate table for bans as it's enough to be a distinct entity in your model and if it remains one complete table you would have a lot of superfluous data thus making scalability a potential issue. For example you may implement a new feature on bans which would be easier if it was it's own table.

Ultimately it depends on the logic and the needs of your application and what you'll be doing with the data. The best bet for learning or an application where the future use of data is unknown in my opinion is to implement a 3rd Normal Form database or beyond.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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