Column Types and Constraints

Exploratory Data Analysis in SQL

Christina Maimone

Data Scientist

Column constraints

  • Foreign key: value that exists in the referenced column, or NULL
  • Primary key: unique, not NULL

  • Unique: values must all be different except for NULL

  • Not null: NULL not allowed: must have a value

  • Check constraints: conditions on the values

    • column1 > 0
    • columnA > columnB
Exploratory Data Analysis in SQL

Data types

Common

  • Numeric
  • Character
  • Date/Time
  • Boolean

Special

  • Arrays
  • Monetary
  • Binary
  • Geometric
  • Network Address
  • XML
  • JSON
  • and more!
Exploratory Data Analysis in SQL

Numeric types: PostgreSQL documentation

Table of numeric data type names and descriptions

Exploratory Data Analysis in SQL

Types in entity relationship diagrams

Fortune 500 databse table

Exploratory Data Analysis in SQL

Casting with CAST()

Format

-- With the CAST function
SELECT CAST (value AS new_type);

Examples

-- Cast 3.7 as an integer
SELECT CAST (3.7 AS integer);
4
-- Cast a column called total as an integer
SELECT CAST (total AS integer)
  FROM prices;
Exploratory Data Analysis in SQL

Casting with ::

Format

-- With :: notation
SELECT value::new_type;

Examples

-- Cast 3.7 as an integer
SELECT 3.7::integer;
-- Cast a column called total as an integer
SELECT total::integer
  FROM prices;
Exploratory Data Analysis in SQL

Time to practice!

Exploratory Data Analysis in SQL

Preparing Video For Download...