0
0
Supabasecloud~5 mins

Creating tables via SQL editor in Supabase

Choose your learning style9 modes available
Introduction

We create tables to store and organize data in a database. Tables help keep information neat and easy to find.

When starting a new project and you need a place to save user information.
When you want to organize product details for an online store.
When you need to keep track of orders or transactions.
When you want to store logs or records for your application.
When you want to create relationships between different types of data.
Syntax
Supabase
CREATE TABLE table_name (
  column1 datatype constraints,
  column2 datatype constraints,
  ...
);

Use CREATE TABLE followed by the table name you want.

Inside parentheses, list columns with their data types and any rules (constraints).

Examples
This creates a users table with an auto-incrementing id, a name, and a unique email.
Supabase
CREATE TABLE users (
  id SERIAL PRIMARY KEY,
  name VARCHAR(100) NOT NULL,
  email VARCHAR(100) UNIQUE NOT NULL
);
This creates a products table with an integer product_id, a product name, and a price with two decimals.
Supabase
CREATE TABLE products (
  product_id INT PRIMARY KEY,
  product_name TEXT NOT NULL,
  price NUMERIC(10,2) NOT NULL
);
Sample Program

This example creates an employees table with an auto-incrementing ID, names, unique email, and hire date.

Supabase
CREATE TABLE employees (
  employee_id SERIAL PRIMARY KEY,
  first_name VARCHAR(50) NOT NULL,
  last_name VARCHAR(50) NOT NULL,
  email VARCHAR(100) UNIQUE NOT NULL,
  hire_date DATE NOT NULL
);
OutputSuccess
Important Notes

Always choose clear and simple column names.

Use constraints like PRIMARY KEY and UNIQUE to keep data correct.

Data types like VARCHAR, INT, and DATE tell the database what kind of data to expect.

Summary

Tables organize data into rows and columns.

Use CREATE TABLE with column names and data types.

Constraints help keep data accurate and unique.