Home

Topic: Data Types

Learning objective


Warm up

To see the metadata information on the employees database table (or any other table).

  1. Right click on the table employees.
  2. Select Properties.
  3. Navigate to the Columns tab.

You should see something like this:

Column     |         Type          | Nullable | 
------------+----------------------+----------+
emp_no     | integer               | not null |
birth_date | date                  | not null |
first_name | character varying(14) | not null |
last_name  | character varying(16) | not null |
gender     | character varying     | not null |
hire_date  | date                  | not null |

Data types in PostgreSQL

Up until now, we have been casual in our definition of data types (e.g. “text” or “number”). Here, we will take a more formal approach.

The data type defines what kind of value(s) a column can hold. Each column in a database table is required to have a column name and a data type. The SQL developer must decide on the data type when creating the table.

The data type is a guideline for SQL to understand what type of data is expected inside each column and also identifies how SQL will interact with the stored data. For example, we can only apply SQL string/text functions to pre-defined text columns.

A data type error

ROUND() is a mathematical function. Let’s see what happens when it is applied to a text column.

SELECT ROUND(gender)
FROM employees
;

The error generated is a data type error:

HINT:  No function matches the given name and argument types.
You might need to add explicit type casts.

Data types

PostgreSQL supports many data types. The four major ones are:

For the full list of data types, check out PostgreSQL’s official documentation at https://www.postgresql.org/docs/current/datatype.html.

Note that some data types have many different names (see the aliases column in the documentation) and not all PostgreSQL data types have equivalents outside of PostgreSQL.

Also note, quotation marks play a large role in data types as well. The differentiation between single quotes and double quotes is that something unique to PostgreSQL:


Data type: Character

PostgreSQL provides three character data types: CHAR(n), VARCHAR(n), and TEXT


Exercise:

How many maximum characters can the column dept_name in the table departments store?


Data type: Numeric

PostgreSQL provides two distinct types for number data:

Integer

There are two main types of integers (think: whole numbers):

Floating-point numbers

There are three main types of floating-point numbers (think: decimals):


Exercise:

Which column(s) in the salaries table are numeric? What kind of numeric data type, specifically?


Data type: Time

The time (temporal) data types allow you to store date and/or time (e.g. datetime) data.

PostgreSQL has five main data types in this category:


Exercise:

Find the data type(s) for columns to_date and from_date in the salaries table.


Data type: Boolean

A PostgreSQL boolean data type has three possible values:

When loading data into a PostgreSQL database and the column is declared to be a Boolean data type, the following values are accepted:

There isn’t a boolean type column in the employees database, but we will be working with booleans quite a bit when we encounter data filtering with the WHERE clause.


Resources


Questions?