-
Notifications
You must be signed in to change notification settings - Fork 0
Enumerations
Enumeration is a special data type describing some limited collection of items that is static or pseudo-static (changing very rare). Enumerations are used as a one of data types of entity attributes.
Enumerations differ from entities by following reasons
- They have static (or pseudo-static) list of values
- They don't have many different values (usually from 2-3 to 10-15 different values)
- Their values can be hardcoded and used in algorithms (i.e. write code like
client.gender = Gender.Male), while using entity record references (i.e. code likeclient.product_id = 2) is a bad idea
Good examples of enumerations are
- Gender (male \ female)
- Monthes of the year (Jab,Feb,March etc.)
- Programming languages (Java, C, PHP etc.)
Bad examples of enumerations are
- Products (because list of products can be changed very often)
- Car models (same reason, we see that every year new car models appear and it would be hard to support code directly referencing certain car model)
There are 2 types of enumerations from the storage point of view
Inline enumerations are not stored in a separate table. Enumeration value is stored directly in a table column. In that case table column has a type of enumeration value (for example, String).
Pseudo code example
create table SBT_CLIENT
(
ID int,
NAME varchar(255),
GENDER char(1)
)In this example column GENDER has a enumeration type Gender which values (M or F) are stored directly in a table
| ID | NAME | GENDER |
|---|---|---|
| 1 | John Smith | M |
| 2 | Ted Frank | M |
| 3 | Mary Thompson | F |
Stored enumeration values are stored in a separate tables, and are referenced by other table records by their primary key
create table SBT_GENDER
(
ID int,
CODE char(1),
NAME varchar(10)
)
create table SBT_CLIENT
(
ID int,
NAME varchar(255),
GENDER_ID int
)In this example column GENDER_ID has a enumeration type Gender which values (M or F) are stored in a table SBT_GENDER
| ID | CODE | NAME |
|---|---|---|
| 1 | M | Male |
| 2 | F | Female |
And SBT_CLIENT values in that case are
| ID | NAME | GENDER_ID |
|---|---|---|
| 1 | John Smith | 1 |
| 2 | Ted Frank | 1 |
| 3 | Mary Thompson | 2 |