Tables in SQL

A single database can have multiple tables inside. Think of them as collections in a MongoDB database.

notion image

notion image

Until now, we have a database that we can interact with. The next step in case of postgres is to define the schema of your tables.

SQL stands for Structured query language. It is a language in which you can describe what/how you want to put data in the database.

To create a table, the command to run is -

CREATE TABLE users (
    id SERIAL PRIMARY KEY,
    username VARCHAR(50) UNIQUE NOT NULL,
    email VARCHAR(255) UNIQUE NOT NULL,
    password VARCHAR(255) NOT NULL,
    created_at TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP
);

There are a few parts of this SQL statement, let’s decode them one by one

1. CREATE TABLE users

CREATE TABLE users: This command initiates the creation of a new table in the database named users.

2. id SERIAL PRIMARY KEY

3. email VARCHAR(255) UNIQUE NOT NULL,

4. password VARCHAR(255) NOT NULL