Welcome!

Functions for Manipulating Data in PostgreSQL

Brian Piccolo

Sr. Director, Digital Strategy

The Sakila Database

  • Highly normalized
  • Representative data types
  • Custom functions
Functions for Manipulating Data in PostgreSQL

Topics

  • Common data types in PostgreSQL
  • Date and time functions and operators
  • Parsing and manipulating text
  • Full-text search and PostgreSQL Extensions
Functions for Manipulating Data in PostgreSQL

Common data types

  • Text data types
    • CHAR, VARCHAR and TEXT
  • Numeric data types
    • INT and DECIMAL
  • Date / time data types
    • DATE, TIME, TIMESTAMP, INTERVAL
  • Arrays
Functions for Manipulating Data in PostgreSQL

Text data types

SELECT title 
FROM film
LIMIT 5
+-------------------+
| title             |
|-------------------|
| ACADEMY DINOSAUR  |
| ACE GOLDFINGER    |
| ADAPTATION HOLES  |
| AFFAIR PREJUDICE  |
| AFRICAN EGG       |
+-------------------+
SELECT description 
FROM film
LIMIT 2
+-----------------------------------------+
| description                             |
|-----------------------------------------|
| A Epic Drama of a Feminist And a Mad    |     
|  Scientist who must Battle a Teacher in |
|  The Canadian Rockies.                  |
| A Astounding Epistle of a Database      |
|  Administrator And a Explorer who       |
|  must Find a Car in Ancient China       |
+-----------------------------------------+
Functions for Manipulating Data in PostgreSQL

Numeric data types

SELECT 
    payment_id 
FROM payment 
LIMIT 5
+-------------+
| payment_id  |
|-------------|
| 1           |
| 2           |
| 3           |
| 4           |
| 5           |
+-------------+
SELECT 
    amount
FROM payment 
LIMIT 5
+--------+
| amount |
|--------|
| 2.99   |
| 0.99   |
| 5.99   |
| 0.99   |
| 9.99   |
+--------+
Functions for Manipulating Data in PostgreSQL

Determining data types from existing tables

SELECT 
    title, 
    description, 
    special_features 
FROM FILM 
LIMIT 5
+---------------+------------------+------------------------------+
| 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...} |
+---------------+------------------+------------------------------+
Functions for Manipulating Data in PostgreSQL

Determining data types from existing tables

SELECT
    column_name, 
    data_type
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             |
+------------------+-------------------+
Functions for Manipulating Data in PostgreSQL

Let's practice!

Functions for Manipulating Data in PostgreSQL

Preparing Video For Download...