General Question About Unsigned Table Fields

General Question about unsigned table fields

Hello together,

I recognized that yii migrations create primary keys without unsigned attribute…

And now I’m confused.

So I googled and found as answer for example:

  • not all databses are using unsigned so the compatibility for the migration is higher

  • unsigned int => you face the problem that it may not be accurately represented as a PHP integer.

Okay so far so good… I understood that.

But now I have questions in my mind like:

  1. What is the best practise?

  2. How do you guys handle that in general?

  3. Are there cases where it makes sense to use unsinged?

  4. Should I don’t care about it anymore and avoid using it in general?

  5. Except the possible higher positive numbers, what would be the benefit of unsinged?

Thank you for your feedback.

Regards

Hi taggert,

I assume you work with MySQL db. There is no "real" benefit of using unsigned int, unless you worry about having your db schema changed one day for having too many records. However, using unsigned int for primary key provides readability to your design, having one enforces that the PK cannot go negative. This could be of use for tables like persons or products. You could, totally ignore it; but keep in mind that if you decide to use it, make it persistent throughout your db schema: all unsigned int or none at all.