Professional Documents
Culture Documents
F U N C T I O N S F O R M A N I P U L AT I N G D ATA I N P O S TG R E S Q L
Brian Piccolo
Sr. Director, Digital Strategy
The Sakila Database
Highly normalized
Custom functions
Arrays
+-------------------+ +-----------------------------------------+
| title | | description |
|-------------------| |-----------------------------------------|
| ACADEMY DINOSAUR | | A Epic Drama of a Feminist And a Mad |
| ACE GOLDFINGER | | Scientist who must Battle a Teacher in |
| ADAPTATION HOLES | | The Canadian Rockies. |
| AFFAIR PREJUDICE | | A Astounding Epistle of a Database |
| AFRICAN EGG | | Administrator And a Explorer who |
+-------------------+ | must Find a Car in Ancient China |
+-----------------------------------------+
+-------------+ +--------+
| payment_id | | amount |
|-------------| |--------|
| 1 | | 2.99 |
| 2 | | 0.99 |
| 3 | | 5.99 |
| 4 | | 0.99 |
| 5 | | 9.99 |
+-------------+ +--------+
+---------------+------------------+------------------------------+
| title | description | special_features |
|---------------|------------------|------------------------------|
| ACADEMY D... | A Epic... | {Deleted Scenes,Behi...} |
| ACE GOLD... | A Astound.. | {Trailers,Deleted Scenes} |
| AFFAIR PR... | A Fanciful,.. | {Commentaries,Behind the...} |
+---------------+------------------+------------------------------+
data_type By querying certain tables in this databse you can determine info about the
databse including data types of cols
FROM INFORMATION_SCHEMA.COLUMNS
WHERE column_name in ('title','description','special_features')
AND table_name ='film';
+------------------+-------------------+
| column_name | data_type |
|------------------|-------------------|
| title | character varying |
| description | text |
| special_features | ARRAY |
+------------------+-------------------+
Brian Piccolo
Sr. Director, Digital Strategy
TIMESTAMP data types
ISO 8601 format: yyyy-mm-dd
+------------------------------+
| timestamp |
|------------------------------|
| 2019-03-26 01:05:17.93027+00 |
+------------------------------+
SELECT payment_date
FROM payment;
+---------------------+
| payment_date |
|---------------------|
| 2005-05-25 11:30:37 |
+---------------------+
SELECT create_date
FROM customer
+-------------+
| create_date |
|-------------|
| 2006-02-14 |
+-------------+
+---------------------+
| expected_return |
|---------------------|
| 2005-05-27 22:53:30 |
+---------------------+
+-------------+-----------------------------+
| column_name | data_type |
|-------------|-----------------------------|
| rental_date | timestamp without time zone |
+-------------+-----------------------------+
Brian Piccolo
Sr. Director, Digital Strategy
Before we get started
CREATE TABLE example
INSERT example
+--------+--------------------+-------------+
| type | address | test_scores |
|--------|--------------------|-------------|
| work | work1@datacamp.com | 92 |
| work | work2@datacamp.com | 76 |
+--------+--------------------+-------------+
Note that PostgreSQL array indexes start with one and not zero.
+--------+--------------------+-------------+
| type | address | test_scores |
|--------|--------------------|-------------|
| work | work1@datacamp.com | 92 |
| work | work2@datacamp.com | 76 |
+--------+--------------------+-------------+
+---------+---------------------+-------------+
| type | address | test_scores |
|---------|-----------------------------------|
| other | other1@datacamp.com | 92 |
| null | null | 76 |
+---------+---------------------+-------------+
+---------+---------------------+-------------+
| type | address | test_scores |
|---------|-----------------------------------|
| other | other1@datacamp.com | 92 |
| null | null | 76 |
+---------+---------------------+-------------+
Let's practice!
F U N C T I O N S F O R M A N I P U L AT I N G D ATA I N P O S TG R E S Q L