MySQL data types for common columns

datatypesMySQL

I have been a frontend programmer for a while and moving to making some applications on my own and becoming a full stack engineer. I have worked with plenty of databases for applications and sites but as I am building my own I want to make sure I am thinking in best practice. Now many applications have some shared concenpts so was wondering if there is some cheat sheet for common attributes paired with data types and why. Here are some expamles of what I was curious about:

  • table entry created and updated: date or timestamp? Why? With date isnt it easier to display and sort on the frontend?
  • Entry title – VARCHAR or text?
  • Entry body – I assume text.
  • 5 Star rating – INT or Decimal? Why since I have read about both?
  • Phone & Zip – INT or VARCHAR?
  • Event Date – Again, Date or Timestamp

Thanks. I know this seems pretty basic, but just trying to an idea of best practice and many of these items are exchangable between applications.

Best Answer

This is all IMHO:

•table entry created and updated: date or timestamp? Why? With date isnt it easier to display and sort on the frontend?

Use a timestamp - better for multiple mods in a given day.

•Entry title - VARCHAR or text?

You're not going to lose much with a text over VARCHAR, but I think that VARCHAR is the reasonable choice.

•Entry body - I assume text.

BLOB.

•5 Star rating - INT or Decimal? Why since I have read about both?

How flexible do you want to be - I'd tend to Decimal.

•Phone & Zip - INT or VARCHAR?

VARCHAR - intl codes, extensions... more flexible.

•Event Date - Again, Date or Timestamp

Again you're not losing much adding the time - never throw away information that you may use later.