|
| 1 | +-- testing sql found in https://supabase.com/docs/guides/database/postgresenums |
| 2 | +create type mood as enum ( |
| 3 | + 'happy', |
| 4 | + 'sad', |
| 5 | + 'excited', |
| 6 | + 'calm' |
| 7 | +); |
| 8 | +create table person ( |
| 9 | + id serial primary key, |
| 10 | + name text, |
| 11 | + current_mood mood |
| 12 | +); |
| 13 | +insert into person |
| 14 | + (name, current_mood) |
| 15 | +values |
| 16 | + ('Alice', 'happy'); |
| 17 | +insert into person |
| 18 | + (name, current_mood) |
| 19 | +values |
| 20 | + ('Bob', 'sad'); |
| 21 | +insert into person |
| 22 | + (name, current_mood) |
| 23 | +values |
| 24 | + ('Charlie', 'excited'); |
| 25 | +select * |
| 26 | +from person |
| 27 | +where current_mood = 'sad'; |
| 28 | + id | name | current_mood |
| 29 | +----+------+-------------- |
| 30 | + 2 | Bob | sad |
| 31 | +(1 row) |
| 32 | + |
| 33 | +select * |
| 34 | +from person |
| 35 | +where current_mood = 'happy'; |
| 36 | + id | name | current_mood |
| 37 | +----+-------+-------------- |
| 38 | + 1 | Alice | happy |
| 39 | +(1 row) |
| 40 | + |
| 41 | +update person |
| 42 | +set current_mood = 'excited' |
| 43 | +where name = 'Alice'; |
| 44 | +select * |
| 45 | +from person |
| 46 | +where name = 'Alice'; |
| 47 | + id | name | current_mood |
| 48 | +----+-------+-------------- |
| 49 | + 1 | Alice | excited |
| 50 | +(1 row) |
| 51 | + |
| 52 | +alter type mood add value 'content'; |
| 53 | +insert into person |
| 54 | + (name, current_mood) |
| 55 | +values |
| 56 | + ('David', 'content'); |
| 57 | +select enum_range(null::mood); |
| 58 | + enum_range |
| 59 | +---------------------------------- |
| 60 | + {happy,sad,excited,calm,content} |
| 61 | +(1 row) |
| 62 | + |
| 63 | +select * |
| 64 | +from person |
| 65 | +where current_mood = 'content'; |
| 66 | + id | name | current_mood |
| 67 | +----+-------+-------------- |
| 68 | + 4 | David | content |
| 69 | +(1 row) |
| 70 | + |
| 71 | +create type status as enum ( |
| 72 | + 'active', |
| 73 | + 'inactive', |
| 74 | + 'pending' |
| 75 | +); |
| 76 | +create table orders ( |
| 77 | + id serial primary key, |
| 78 | + order_number text, |
| 79 | + status status |
| 80 | +); |
| 81 | +insert into orders |
| 82 | + (order_number, status) |
| 83 | +values |
| 84 | + ('ORD-001', 'active'), |
| 85 | + ('ORD-002', 'pending'), |
| 86 | + ('ORD-003', 'inactive'); |
| 87 | +select * |
| 88 | +from orders |
| 89 | +where status = 'active'; |
| 90 | + id | order_number | status |
| 91 | +----+--------------+-------- |
| 92 | + 1 | ORD-001 | active |
| 93 | +(1 row) |
| 94 | + |
| 95 | +update orders |
| 96 | +set status = 'inactive' |
| 97 | +where order_number = 'ORD-002'; |
| 98 | +select * |
| 99 | +from orders |
| 100 | +where order_number = 'ORD-002'; |
| 101 | + id | order_number | status |
| 102 | +----+--------------+---------- |
| 103 | + 2 | ORD-002 | inactive |
| 104 | +(1 row) |
| 105 | + |
| 106 | +alter type status add value 'cancelled'; |
| 107 | +insert into orders |
| 108 | + (order_number, status) |
| 109 | +values |
| 110 | + ('ORD-004', 'cancelled'); |
| 111 | +select enum_range(null::status); |
| 112 | + enum_range |
| 113 | +------------------------------------- |
| 114 | + {active,inactive,pending,cancelled} |
| 115 | +(1 row) |
| 116 | + |
| 117 | +select * |
| 118 | +from orders |
| 119 | +where status = 'cancelled'; |
| 120 | + id | order_number | status |
| 121 | +----+--------------+----------- |
| 122 | + 4 | ORD-004 | cancelled |
| 123 | +(1 row) |
| 124 | + |
| 125 | +create type priority as enum ( |
| 126 | + 'low', |
| 127 | + 'medium', |
| 128 | + 'high', |
| 129 | + 'critical' |
| 130 | +); |
| 131 | +create table tasks ( |
| 132 | + id serial primary key, |
| 133 | + title text, |
| 134 | + priority priority |
| 135 | +); |
| 136 | +insert into tasks |
| 137 | + (title, priority) |
| 138 | +values |
| 139 | + ('Fix bug', 'high'), |
| 140 | + ('Update docs', 'low'), |
| 141 | + ('Security audit', 'critical'); |
| 142 | +select * |
| 143 | +from tasks |
| 144 | +where priority = 'critical'; |
| 145 | + id | title | priority |
| 146 | +----+----------------+---------- |
| 147 | + 3 | Security audit | critical |
| 148 | +(1 row) |
| 149 | + |
| 150 | +update tasks |
| 151 | +set priority = 'medium' |
| 152 | +where title = 'Update docs'; |
| 153 | +select * |
| 154 | +from tasks |
| 155 | +where title = 'Update docs'; |
| 156 | + id | title | priority |
| 157 | +----+-------------+---------- |
| 158 | + 2 | Update docs | medium |
| 159 | +(1 row) |
| 160 | + |
| 161 | +alter type priority add value 'urgent'; |
| 162 | +insert into tasks |
| 163 | + (title, priority) |
| 164 | +values |
| 165 | + ('Server maintenance', 'urgent'); |
| 166 | +select enum_range(null::priority); |
| 167 | + enum_range |
| 168 | +----------------------------------- |
| 169 | + {low,medium,high,critical,urgent} |
| 170 | +(1 row) |
| 171 | + |
| 172 | +select * |
| 173 | +from tasks |
| 174 | +where priority = 'urgent'; |
| 175 | + id | title | priority |
| 176 | +----+--------------------+---------- |
| 177 | + 4 | Server maintenance | urgent |
| 178 | +(1 row) |
| 179 | + |
| 180 | +drop table tasks; |
| 181 | +drop table orders; |
| 182 | +drop table person; |
| 183 | +drop type priority; |
| 184 | +drop type status; |
| 185 | +drop type mood; |
0 commit comments