Skip to content

Instantly share code, notes, and snippets.

@thalesbruno
Created December 18, 2022 09:24
Show Gist options
  • Select an option

  • Save thalesbruno/9c94ce752b29f0eed954ceca41aadefd to your computer and use it in GitHub Desktop.

Select an option

Save thalesbruno/9c94ce752b29f0eed954ceca41aadefd to your computer and use it in GitHub Desktop.

Revisions

  1. thalesbruno created this gist Dec 18, 2022.
    32 changes: 32 additions & 0 deletions postgres.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,32 @@
    # PostgreSQL Basic Guide

    ## Primary Keys

    Autoincrement numeric id
    ```sql
    CREATE TABLE users (
    id SERIAL PRIMARY KEY
    );
    ```

    Autogenerated UUID id
    ```sql
    CREATE TABLE products (
    id UUID DEFAULT gen_random_uuid() PRIMARY KEY
    );
    ```

    ## ENUM type

    Create an ENUM type
    ```sql
    CREATE TYPE order_status AS ENUM ('open', 'pending', 'closed', 'canceled');
    ```

    Use created type
    ```sql
    CREATE TABLE order (
    id SERIAL PRIMARY KEY,
    status order_status
    );
    ```