Skip to content

Commit 19e7959

Browse files
committed
enum documentation
features documented: - create type - basic usage - comparison - drop type - alter type add value - alter type owner to adhered to Elena's comments
1 parent 6213d85 commit 19e7959

4 files changed

Lines changed: 107 additions & 1 deletion

File tree

content/references/datatypes/_index.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ The complete list of supported data types in CedarDB is as follows:
2222
* [`date`](date)
2323
* [`decimal`](numeric)
2424
* [`double precision`](float)
25+
* [`enum`](enums)
2526
* [`float`](float)
2627
* [`integer`](integer)
2728
* [`json`](json)
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
# Enum Types
2+
3+
Just like in many programming languages, an enum types consist of a static, ordered set of labels defined by the user.
4+
They combine the clarity of text with the compactness of numerics, when the inherent meaning of a value is more than should be encoded by a single number, but the number of possible values is relatively small.
5+
6+
## Creation of Enum types
7+
8+
Enum types can be created via the Create Type command.
9+
10+
```sql
11+
create type enum_name as enum (['label' [,...]]);
12+
```
13+
14+
An example usage could look like this:
15+
16+
```sql
17+
create type importance as enum ('minor', 'major', 'critical');
18+
```
19+
20+
## Usage of Enum types
21+
22+
Enums can be used just like any other type inside of tables, views, queries, etc. .
23+
24+
```sql
25+
create table tasks (id int, priority importance);
26+
insert into tasks values (1, 'major'), (2, 'minor'), (3, 'critical'), (4, 'major');
27+
```
28+
29+
The enum labels are case sensitive, whereas the enum names are not.
30+
31+
This does not work:
32+
```sql
33+
select 'mInOr'::importance;
34+
```
35+
```
36+
ERROR: invalid input value for enum importance: "mInOr"
37+
```
38+
39+
This works:
40+
```sql
41+
select 'minor'::iMpOrTaNcE;
42+
```
43+
```
44+
enum importance
45+
---------------
46+
minor
47+
```
48+
49+
## Comparison of Enum types
50+
51+
Values of the same enum type are comparable. Their ordering corresponds the order in which they were listed at creation time. Values of different enum types are incomparable. Similarly, an enum cannot be compared with a builtin type.
52+
53+
54+
In this example ID2 gets filtered out as its corresponding priority is too low in the enum ordering.
55+
```sql
56+
select id, priority from tasks where priority >= 'major';
57+
```
58+
59+
```
60+
id | priority
61+
-------------
62+
1 | major
63+
3 | critical
64+
4 | major
65+
```
66+
67+
```sql
68+
select id from tasks where priority > 1;
69+
```
70+
```
71+
ERROR: cannot compare enum importance and integer
72+
```
73+
## Deletion of Enum types
74+
75+
Enum types can be removed via the Drop Type command.
76+
77+
```sql
78+
drop type [if exists] name;
79+
```
80+
The deletion of an enum type is not possible, when any other object still depends on it. Trying to do so regardless results in an error.
81+
82+
## Alter Enum types
83+
84+
### Add new label
85+
A new label can be added to an existing enum via
86+
```sql
87+
alter type enum_name add value [if not exists] added_enum_label;
88+
```
89+
The newly inserted label is the new maximum in this enum type. Inserting a new label at another location is currently not supported.
90+
If the label is already present, the insertion fails with an error. Specifying "if not exists" suppresses this error.
91+
92+
### Change ownership
93+
The owner of an enum can be changed via
94+
```sql
95+
alter type enum_name owner to new_owner;
96+
```

content/references/sqlreference/expressions/_index.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ CedarDB supports many expressions that manipulate data:
1212
* At time zone
1313
* Between, between symmetric
1414
* Bit and (`&`) on [bitsrings](/docs/references/sqlreference/functions/bitstring#bit--bit)
15-
* Bit or (`|`) on [bitstrings]](/docs/references/sqlreference/functions/bitstring#bit--bit-1)
15+
* Bit or (`|`) on [bitstrings](/docs/references/sqlreference/functions/bitstring#bit--bit-1)
1616
* Bit xor (`#`) on [bitstrings](/docs/references/sqlreference/functions/bitstring#bit--bit-2)
1717
* Case-insensitive like (`ilike`, `~~*`), negated (`not ilike`, `!~~*`)
1818
* Case-insensitive regular expression (`~*`), negated (`!~*`)

content/references/sqlreference/statements/_index.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@ The complete list of supported SQL statements in CedarDB is as follows:
1515
Alter table
1616
: modify a table definition
1717

18+
[Alter type](/docs/references/datatypes/enums/#alter-enum-types)
19+
: modify a user-defined type
20+
1821
[Alter table rename column](altertable)
1922
: rename a column of a given table
2023

@@ -66,6 +69,9 @@ Create sequence
6669
[Create table as](createtableas)
6770
: create and populate a new table with contents from a query
6871

72+
[Create type](/docs/references/datatypes/enums/#creation-of-enum-types)
73+
: define a new datatype
74+
6975
[Create user](createrole)
7076
: create a new database role
7177

@@ -84,6 +90,9 @@ Create sequence
8490
Drop
8591
: remove schema definitions
8692

93+
[Drop Type](/docs/references/datatypes/enums/#deletion-of-enum-types)
94+
: remove a user-defined type
95+
8796
[End](/docs/references/sqlreference/transaction)
8897
: commit the transaction
8998

0 commit comments

Comments
 (0)