How to CREATE TABLE in Amazon Redshift

Galaxy Glossary

How do I use CREATE TABLE in Amazon Redshift?

CREATE TABLE defines a new, permanently stored table with chosen distribution and sort keys in Amazon Redshift.

Sign up for the latest in SQL knowledge from the Galaxy Team!
Welcome to the Galaxy, Guardian!
Oops! Something went wrong while submitting the form.

Description

Why is CREATE TABLE essential in Redshift?

CREATE TABLE materializes a dataset inside your Redshift cluster, letting you control column types, compression, distribution, and sort order for fast analytics.

What is the basic CREATE TABLE syntax?

The command starts with CREATE TABLE, an optional IF NOT EXISTS clause, column definitions, and table-level options such as DISTKEY and SORTKEY.

How do I define columns correctly?

Specify column_name followed by Redshift data types (INTEGER, VARCHAR(n), BOOLEAN, etc.). Add DEFAULT, IDENTITY, or ENCODE options to optimize storage and loading.

When should I set DISTKEY and SORTKEY?

Use DISTKEY on columns heavily joined across large tables (e.g., customer_id). Choose SORTKEY columns frequently filtered or ordered (e.g., order_date) to speed up scans.

Can I copy PostgreSQL DDL directly?

Mostly yes, but adjust Redshift-specific features (DISTKEY, SORTKEY, ENCODE). Also replace serial with IDENTITY.

Example: Creating an Orders table

CREATE TABLE IF NOT EXISTS public.orders (
id INT IDENTITY(1,1),
customer_id INT NOT NULL,
order_date DATE DEFAULT CURRENT_DATE,
total_amount NUMERIC(10,2) ENCODE az64,
DISTKEY(customer_id),
SORTKEY(order_date)
);

Best practices for CREATE TABLE

Choose compression via ANALYZE COMPRESSION on sample data. Keep SORTKEYs under four columns. Use VARCHAR lengths just large enough for data.

What are common mistakes?

Omitting distribution style, creating wide VARCHARs, forgetting BACKUP NO for transient tables, and mixing DISTKEY with EVEN distribution are typical issues.

Why How to CREATE TABLE in Amazon Redshift is important

How to CREATE TABLE in Amazon Redshift Example Usage


CREATE TABLE IF NOT EXISTS public.orderitems (
    id INT IDENTITY(1,1),
    order_id INT NOT NULL,
    product_id INT NOT NULL,
    quantity SMALLINT DEFAULT 1,
    ENCODE zstd,
    DISTKEY(order_id),
    SORTKEY(order_id, product_id)
);

How to CREATE TABLE in Amazon Redshift Syntax


CREATE TABLE [IF NOT EXISTS] schema.table_name (
    column_name data_type [IDENTITY(seed, step)] [DEFAULT expression]
                [ENCODE encoding],
    ...
)
[BACKUP { YES | NO }]
[DISTSTYLE { AUTO | EVEN | KEY | ALL }]
[DISTKEY (dist_key_column)]
[COMPOUND | INTERLEAVED] SORTKEY (sort_key_column [, ...]);

Common Mistakes

Frequently Asked Questions (FAQs)

Can I alter DISTKEY or SORTKEY after creation?

No. You must recreate the table with the new keys and copy data back (CTAS or ALTER TABLE … ALTER DISTKEY is unsupported).

Is CREATE TABLE transactional?

Yes. If the session rolls back before commit, the new table disappears.

What’s the difference between DISTSTYLE KEY and ALL?

KEY co-locates rows by a chosen column; ALL replicates the entire small table to every node, ideal for static dimension tables.

Want to learn about other SQL terms?

Trusted by top engineers on high-velocity teams
Aryeo Logo
Assort Health
Curri
Rubie
BauHealth Logo
Truvideo Logo
Welcome to the Galaxy, Guardian!
Oops! Something went wrong while submitting the form.