diff --git a/README.md b/README.md index 789837b..fa9a0ca 100644 --- a/README.md +++ b/README.md @@ -179,6 +179,7 @@ _393 TILs and counting..._ - [Create A Composite Primary Key](postgres/create-a-composite-primary-key.md) - [Create hstore From Two Arrays](postgres/create-hstore-from-two-arrays.md) - [Creating Conditional Constraints](postgres/creating-conditional-constraints.md) +- [Creating Custom Types](postgres/creating-custom-types.md) - [Day Of Week By Name For A Date](postgres/day-of-week-by-name-for-a-date.md) - [Day Of Week For A Date](postgres/day-of-week-for-a-date.md) - [Default Schema](postgres/default-schema.md) diff --git a/postgres/creating-custom-types.md b/postgres/creating-custom-types.md new file mode 100644 index 0000000..ff935c1 --- /dev/null +++ b/postgres/creating-custom-types.md @@ -0,0 +1,32 @@ +# Creating Custom Types + +PostgreSQL has support for creating custom types. When you need something +more expressive than the built-in types and you don't want your data spread +across multiple columns, you can instead create a custom type. + +```sql +create type dimensions as ( + width integer, + height integer, + depth integer +); +``` + +This new type can then be used in the definition of a new table + +```sql +create table moving_boxes ( + id serial primary key, + dims dimensions not null +); +``` + +and when inserting data + +```sql +insert into moving_boxes (dims) values (row(3,4,5)::dimensions); +``` + +See the [`create type` +docs](http://www.postgresql.org/docs/current/static/sql-createtype.html) for +more details.