Say i have a datatype called "status" as a row in a mysql table. Status can only be a fixed number of strings, say "active", "inactive" and "pending". What datatype is best practice to use?
假设我有一个名为“status”的数据类型作为mysql表中的一行。状态只能是固定数量的字符串,例如“活动”,“非活动”和“待定”。最佳实践使用什么数据类型?
- Make another table "statuses" and have an pointer id in my table?
- 创建另一个表“状态”并在我的表中有一个指针ID?
- Make a php array containing the different statuses and use my status-row as index for the array?
- 创建一个包含不同状态的php数组,并使用我的status-row作为数组的索引?
- Simply letting status be a string containing the current status?
- 只是让status成为包含当前状态的字符串?
4 个解决方案
#1
1
If the set of statuses is fixed at development time you definitely want to use ENUM. Another case is when possible statuses can be added in runtime. In this case you want to use separated table to store them and foreign key to check that all statuses are valid.
如果在开发时固定状态集,您肯定要使用ENUM。另一种情况是可以在运行时添加可能的状态。在这种情况下,您希望使用分隔表来存储它们和外键以检查所有状态是否有效。
Using simple string for this is a kind of bad practice. E.g. just a minor misspelling in status can break everything related to the particular row.
使用简单的字符串是一种不好的做法。例如。只是一个小的拼写错误状态可以打破与特定行相关的所有内容。
#2
3
Specifically for exactly that purpose is the ENUM type: http://dev.mysql.com/doc/refman/5.5/en/enum.html
具体到那个目的是ENUM类型:http://dev.mysql.com/doc/refman/5.5/en/enum.html
CREATE TABLE ... (
status ENUM('active', 'inactive', 'pending')
...
);
#3
1
Indexing is not sufficient for just 3 non-changable values .
只有3个不可变的值,索引是不够的。
For limited and fixed number of strings use SET
type like this :
对于有限和固定数量的字符串,使用SET类型,如下所示:
ALTER TABLE `table` ADD `status` SET( 'active', 'insactive', 'pending' ) NOT NULL
and for check the value of a field you can have access to them by their name
并检查您可以通过其名称访问它们的字段的值
('active', 'insactive', 'pending')
or even numbers ( 1 , 2 , 3 )
.
('主动','主动','待定')或偶数(1,2,3)。
For example if the value of a filed is active :
例如,如果字段的值处于活动状态:
if($value=="active") //returns TRUE
if($value== 1) //returns TRUE too
#4
-1
You may follow any of the three options you proposed plus you may use ENUM.
您可以遵循您建议的三个选项中的任何一个,也可以使用ENUM。
I'd avoid option number 1.
我会避免选项号1。
I'd go (but it's a matter of coding style and preference) with option nr. 3. Store the value as a string, with the fields with fixed size, like char with size of 16...
我会选择nr去(但这是编码风格和偏好的问题)。 3.将值存储为字符串,其中包含固定大小的字段,如大小为16的字符...
#1
1
If the set of statuses is fixed at development time you definitely want to use ENUM. Another case is when possible statuses can be added in runtime. In this case you want to use separated table to store them and foreign key to check that all statuses are valid.
如果在开发时固定状态集,您肯定要使用ENUM。另一种情况是可以在运行时添加可能的状态。在这种情况下,您希望使用分隔表来存储它们和外键以检查所有状态是否有效。
Using simple string for this is a kind of bad practice. E.g. just a minor misspelling in status can break everything related to the particular row.
使用简单的字符串是一种不好的做法。例如。只是一个小的拼写错误状态可以打破与特定行相关的所有内容。
#2
3
Specifically for exactly that purpose is the ENUM type: http://dev.mysql.com/doc/refman/5.5/en/enum.html
具体到那个目的是ENUM类型:http://dev.mysql.com/doc/refman/5.5/en/enum.html
CREATE TABLE ... (
status ENUM('active', 'inactive', 'pending')
...
);
#3
1
Indexing is not sufficient for just 3 non-changable values .
只有3个不可变的值,索引是不够的。
For limited and fixed number of strings use SET
type like this :
对于有限和固定数量的字符串,使用SET类型,如下所示:
ALTER TABLE `table` ADD `status` SET( 'active', 'insactive', 'pending' ) NOT NULL
and for check the value of a field you can have access to them by their name
并检查您可以通过其名称访问它们的字段的值
('active', 'insactive', 'pending')
or even numbers ( 1 , 2 , 3 )
.
('主动','主动','待定')或偶数(1,2,3)。
For example if the value of a filed is active :
例如,如果字段的值处于活动状态:
if($value=="active") //returns TRUE
if($value== 1) //returns TRUE too
#4
-1
You may follow any of the three options you proposed plus you may use ENUM.
您可以遵循您建议的三个选项中的任何一个,也可以使用ENUM。
I'd avoid option number 1.
我会避免选项号1。
I'd go (but it's a matter of coding style and preference) with option nr. 3. Store the value as a string, with the fields with fixed size, like char with size of 16...
我会选择nr去(但这是编码风格和偏好的问题)。 3.将值存储为字符串,其中包含固定大小的字段,如大小为16的字符...