开发者

Is there no point to make a 1 length long var char in database?

开发者 https://www.devze.com 2023-02-27 05:53 出处:网络
I would like to store a user status in the database, so, I will use a char to store the status, for example, \"active\", \"inactive\", \"close\". I will use \"a\", \"i\", \"c\" to represent these stat

I would like to store a user status in the database, so, I will use a char to store the status, for example, "active", "inactive", "close". I will use "a", "i", "c" to represent these status. But it is non-sense to store a varchar in database? should I use a char type or 开发者_如何学编程set?

**the database in mysql.


You may want to consider using an int or an enum instead. If you do want a char type, CHAR(1) NOT NULL should be fine. With an enum, it would be:

status ENUM('active', 'inactive', 'close')

With an int:

status tinyint

Using an int is a good option, but document the values well.


One thing to beware of is that adding a new value to an enum requires rewriting the entire table. I generally recommend storing CHAR or VARCHAR. One can add a FK reference to a table of legal values if you want an other aspect of enums, that of requiring the value to be a listed one. Then the 'almost enum' can be extended by adding another value to the FK'ed table.

0

精彩评论

暂无评论...
验证码 换一张
取 消