From 8a42b131dc041d4c4c31df38c1afe10d5b50bdf3 Mon Sep 17 00:00:00 2001 From: jbranchaud Date: Wed, 30 Sep 2015 20:37:32 -0500 Subject: [PATCH] Add Defining Arrays as a postgres til. --- README.md | 1 + postgres/defining-arrays.md | 33 +++++++++++++++++++++++++++++++++ 2 files changed, 34 insertions(+) create mode 100644 postgres/defining-arrays.md diff --git a/README.md b/README.md index 1495cc5..ed99e0b 100644 --- a/README.md +++ b/README.md @@ -77,6 +77,7 @@ smart people at [Hashrocket](http://hashrocket.com/). - [Configure The Timezone](postgres/configure-the-timezone.md) - [Count Records By Type](postgres/count-records-by-type.md) - [Default Schema](postgres/default-schema.md) +- [Defining Arrays](postgres/defining-arrays.md) - [Edit Existing Functions](postgres/edit-existing-functions.md) - [Extracting Nested JSON Data](postgres/extracting-nested-json-data.md) - [Fizzbuzz With Common Table Expressions](postgres/fizzbuzz-with-common-table-expressions.md) diff --git a/postgres/defining-arrays.md b/postgres/defining-arrays.md new file mode 100644 index 0000000..acc7467 --- /dev/null +++ b/postgres/defining-arrays.md @@ -0,0 +1,33 @@ +# Defining Arrays + +In postgres, an array can be defined using the `array` syntax like so: + +```sql +> select array['a','b','c']; + array +--------- + {a,b,c} +``` + +If you are inserting into an existing array column, you can use the array +literal syntax. + +```sql +> create temp table favorite_numbers(numbers integer[]); +CREATE TABLE +> insert into favorite_numbers values( '{7,3,9}' ); +INSERT 0 1 +> select numbers[2] from favorite_numbers; + numbers +--------- + 3 +``` + +Postgres also supports two-dimensional arrays. + +```sql +select array[[1,2,3],[4,5,6],[7,8,9]] telephone; + telephone +--------------------------- + {{1,2,3},{4,5,6},{7,8,9}} +```