mirror of
https://github.com/jbranchaud/til
synced 2026-01-03 07:08:01 +00:00
Add Defining Arrays as a postgres til.
This commit is contained in:
@@ -77,6 +77,7 @@ smart people at [Hashrocket](http://hashrocket.com/).
|
|||||||
- [Configure The Timezone](postgres/configure-the-timezone.md)
|
- [Configure The Timezone](postgres/configure-the-timezone.md)
|
||||||
- [Count Records By Type](postgres/count-records-by-type.md)
|
- [Count Records By Type](postgres/count-records-by-type.md)
|
||||||
- [Default Schema](postgres/default-schema.md)
|
- [Default Schema](postgres/default-schema.md)
|
||||||
|
- [Defining Arrays](postgres/defining-arrays.md)
|
||||||
- [Edit Existing Functions](postgres/edit-existing-functions.md)
|
- [Edit Existing Functions](postgres/edit-existing-functions.md)
|
||||||
- [Extracting Nested JSON Data](postgres/extracting-nested-json-data.md)
|
- [Extracting Nested JSON Data](postgres/extracting-nested-json-data.md)
|
||||||
- [Fizzbuzz With Common Table Expressions](postgres/fizzbuzz-with-common-table-expressions.md)
|
- [Fizzbuzz With Common Table Expressions](postgres/fizzbuzz-with-common-table-expressions.md)
|
||||||
|
|||||||
33
postgres/defining-arrays.md
Normal file
33
postgres/defining-arrays.md
Normal file
@@ -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}}
|
||||||
|
```
|
||||||
Reference in New Issue
Block a user