Skip to content

Commit 2d47431

Browse files
committed
Add Creating Custom Types as a postgres til
1 parent 05a1185 commit 2d47431

File tree

2 files changed

+33
-0
lines changed

2 files changed

+33
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,7 @@ _393 TILs and counting..._
179179
- [Create A Composite Primary Key](postgres/create-a-composite-primary-key.md)
180180
- [Create hstore From Two Arrays](postgres/create-hstore-from-two-arrays.md)
181181
- [Creating Conditional Constraints](postgres/creating-conditional-constraints.md)
182+
- [Creating Custom Types](postgres/creating-custom-types.md)
182183
- [Day Of Week By Name For A Date](postgres/day-of-week-by-name-for-a-date.md)
183184
- [Day Of Week For A Date](postgres/day-of-week-for-a-date.md)
184185
- [Default Schema](postgres/default-schema.md)

postgres/creating-custom-types.md

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# Creating Custom Types
2+
3+
PostgreSQL has support for creating custom types. When you need something
4+
more expressive than the built-in types and you don't want your data spread
5+
across multiple columns, you can instead create a custom type.
6+
7+
```sql
8+
create type dimensions as (
9+
width integer,
10+
height integer,
11+
depth integer
12+
);
13+
```
14+
15+
This new type can then be used in the definition of a new table
16+
17+
```sql
18+
create table moving_boxes (
19+
id serial primary key,
20+
dims dimensions not null
21+
);
22+
```
23+
24+
and when inserting data
25+
26+
```sql
27+
insert into moving_boxes (dims) values (row(3,4,5)::dimensions);
28+
```
29+
30+
See the [`create type`
31+
docs](http://www.postgresql.org/docs/current/static/sql-createtype.html) for
32+
more details.

0 commit comments

Comments
 (0)