Skip to main content

Command Palette

Search for a command to run...

Analyzing CREATE TABLE (PostgreSQL)

Updated
3 min read
M

As a former 3D Animator with more than 12 years of experience, I have always been fascinated by the intersection of technology and creativity. That's why I recently shifted my career towards MERN stack development and software engineering, where I have been serving since 2021.

With my background in 3D animation, I bring a unique perspective to software development, combining creativity and technical expertise to build innovative and visually engaging applications. I have a passion for learning and staying up-to-date with the latest technologies and best practices, and I enjoy collaborating with cross-functional teams to solve complex problems and create seamless user experiences.

In my current role as a MERN stack developer, I have been responsible for developing and implementing web applications using MongoDB, Express, React, and Node.js. I have also gained experience in Agile development methodologies, version control with Git, and cloud-based deployment using platforms like Heroku and AWS.

I am committed to delivering high-quality work that meets the needs of both clients and end-users, and I am always seeking new challenges and opportunities to grow both personally and professionally.

In the previous section, we wrote our first SQL query to create a table in PostgreSQL.

Now, let’s slow down a bit and understand what that SQL actually means.

We’ll break the query into parts and understand the syntax step by step.


The SQL We Wrote

create table cities (
  name varchar(50),
  country varchar(50),
  population integer,
  area integer
);

This single SQL statement created a table in our database.
Let’s understand how.


Keywords vs Identifiers

Every SQL statement is made up of different words.

These words fall into two main categories:

🔹 Keywords

  • Special words defined by SQL

  • They tell the database what action to perform

Examples:

  • create

  • table

  • integer

  • varchar

Keywords are usually written in uppercase (this is a convention, not a rule).


🔹 Identifiers

  • Names that we choose

  • Used to tell the database what to operate on

Examples:

  • cities (table name)

  • name, country, population, area (column names)

Identifiers are usually written in lowercase.


Breaking Down the Statement

1️⃣ create table cities

create table cities

This line tells PostgreSQL:

  • create a new table

  • name the table cities

At this point, PostgreSQL knows what we want to create.


2️⃣ Parentheses ( ... )

(
  name varchar(50),
  country varchar(50),
  population integer,
  area integer
)

Everything inside these parentheses defines the structure of the table.

Each line represents one column.


Understanding Columns and Data Types

Each column has two parts:

column_name data_type

Let’s go through them.


varchar(50)

varchar stands for variable character.

You can think of it as a string.

name varchar(50)
country varchar(50)
  • Stores text

  • 50 means the maximum allowed length is 50 characters

If we try to insert a string longer than 50 characters, PostgreSQL will throw an error.

This helps keep data clean and controlled.


integer

population integer
area integer
  • Stores whole numbers

  • No decimal values allowed

In PostgreSQL, an integer can store values roughly between:

  • -2 billion

  • +2 billion


Is integer Enough for Population?

In our case, we are storing city data.

The largest city population today is around 38 million.

So realistically:

  • we do not expect a city population to cross 2 billion

  • using integer is perfectly fine for this example

Choosing the right data type is an important part of database design.


Why Data Types Matter

Data types help PostgreSQL:

  • validate data

  • prevent invalid values

  • store data efficiently

  • improve performance

That’s why we must always think carefully before choosing a data type.


What We Have So Far

After running this SQL:

  • a table named cities exists

  • it has four columns

  • each column has a defined data type

At this point, the table structure is ready — but it is still empty.