PostgreSQL CREATE TABLE

How to use CREATE TABLE Command in PostgreSQL with Examples

Hey there! 👋 Today, we’re diving into how to create tables in PostgreSQL using the CREATE TABLE command. By the end of this guide, you’ll be well-equipped to make your own tables, set data types, and tackle some common errors. Ready? Let’s get started!

What is CREATE TABLE?

In PostgreSQL, the CREATE TABLE command is your go-to SQL statement for creating a new table. When you create a table, you also define its schema—the blueprint that outlines what kind of data the table will hold, what each column is named, and what type of data each column can store.

Basic Syntax

Here’s the basic syntax for creating a table:

CREATE TABLE table_name (
  column1 datatype,
  column2 datatype,
  column3 datatype,
  ...
);

Example: Creating a TV Series Table

Suppose we’re building an application that tracks TV series. We need a table to store information like the series name, genre, number of seasons, and whether it’s still running. Here’s how you’d create such a table:

CREATE TABLE tv_series (
  id SERIAL PRIMARY KEY,
  name TEXT NOT NULL,
  genre TEXT NOT NULL,
  seasons INTEGER DEFAULT 1,
  is_running BOOLEAN DEFAULT true
);

Here’s what’s happening:

  • id: A unique identifier for each series. We set it as SERIAL so it auto-increments, and as PRIMARY KEY so that each id is unique.
  • name and genre: Text fields that can’t be empty (NOT NULL).
  • seasons: An integer field with a default value of 1.
  • is_running: A boolean field indicating whether the show is still running, defaulting to true.

Common Flags and Usage Patterns

Here are some flags and constraints that you can add when creating tables:

NOT NULL

Ensures that the column cannot have a NULL value.

DEFAULT

Provides a default value for a column.

UNIQUE

Ensures all values in a column are different.

PRIMARY KEY

Identifies a unique record in the table. There can be only one primary key.

FOREIGN KEY

Identifies a unique record in another table, establishing a relation between them.

CHECK

Adds a condition that data in a column must meet.

Common Errors and Solutions

Let’s go over some common errors you might encounter and how to solve them.

Error Message:

ERROR: relation "tv_series" already exists

Cause:

This error occurs when you try to create a table that already exists in the database.

Solution:

To avoid this, you can:

  • Choose a different name for your table.
  • Use the IF NOT EXISTS flag to create the table only if it doesn’t already exist. Like this:

    CREATE TABLE IF NOT EXISTS tv_series (...);

Error Message:

ERROR: syntax error at or near "("

Cause:

This typically means there’s a typo or some sort of syntax error in your SQL statement.

Solution:

  • Double-check your SQL syntax carefully. Make sure all brackets are properly opened and closed, and that you’re using the correct data types and constraints.

Error Message:

ERROR: column "name" specified more than once

Cause:

You’ve accidentally declared the same column name more than once within the table.

Solution:

  • Review your table definition and remove or rename the duplicate column declarations.

That wraps it up for this tutorial on the CREATE TABLE command in PostgreSQL. With this knowledge, you can create tables, specify exactly what they should contain, and troubleshoot issues that come up along the way.

Leave a Reply