📖 Scenario: You are setting up a simple database for a small online bookstore using Supabase. You will create tables to store information about books and authors.
🎯 Goal: Create two tables named authors and books using the Supabase SQL editor. The authors table will store author details, and the books table will store book details including a reference to the author.
📋 What You'll Learn
Create a table called authors with columns author_id (integer, primary key), name (text, not null), and birth_year (integer).
Create a table called books with columns book_id (integer, primary key), title (text, not null), author_id (integer, foreign key referencing authors.author_id), and published_year (integer).
Use appropriate SQL syntax to define primary keys and foreign key constraints.
Ensure the tables are created successfully in the Supabase SQL editor.
💡 Why This Matters
🌍 Real World
Creating tables is the first step in building a database for any application, such as an online bookstore, to store and organize data efficiently.
💼 Career
Database creation and management skills are essential for cloud engineers, backend developers, and data professionals working with cloud databases like Supabase.
Progress0 / 4 steps
1
Create the authors table
Write a SQL statement to create a table called authors with these columns: author_id as an integer primary key, name as text and not null, and birth_year as an integer.
Supabase
Hint
Use CREATE TABLE authors (...) and define each column with its type and constraints.
2
Create the books table without foreign key
Write a SQL statement to create a table called books with these columns: book_id as an integer primary key, title as text and not null, author_id as an integer, and published_year as an integer. Do not add the foreign key constraint yet.
Supabase
Hint
Define the books table columns with correct types and primary key, but skip foreign key for now.
3
Add foreign key constraint to books table
Modify the books table creation SQL to add a foreign key constraint on author_id referencing authors.author_id.
Supabase
Hint
Add REFERENCES authors(author_id) after the author_id INTEGER column definition.
4
Finalize and verify table creation
Ensure both authors and books tables are created with correct columns, primary keys, and foreign key constraint in one SQL script.
Supabase
Hint
Make sure the full SQL script includes both table creations with all constraints.
Practice
(1/5)
1. What is the main purpose of the CREATE TABLE statement in Supabase SQL editor?
easy
A. To insert data into an existing table
B. To delete an existing table from the database
C. To update data in an existing table
D. To create a new table with specified columns and data types
Solution
Step 1: Understand the purpose of CREATE TABLE
The CREATE TABLE command is used to make a new table structure in the database.
Step 2: Differentiate from other SQL commands
Deleting, inserting, or updating data uses different commands like DROP TABLE, INSERT INTO, or UPDATE.
Final Answer:
To create a new table with specified columns and data types -> Option D
Quick Check:
CREATE TABLE means create table [OK]
Hint: Remember: CREATE TABLE makes new tables, not data changes [OK]
Common Mistakes:
Confusing CREATE TABLE with INSERT or UPDATE commands
Thinking CREATE TABLE deletes tables
Assuming CREATE TABLE modifies existing data
2. Which of the following is the correct syntax to create a table named users with columns id (integer) and email (text) in Supabase SQL editor?
easy
A. CREATE users TABLE (id INT, email STRING);
B. CREATE TABLE users (id INTEGER, email TEXT);
C. TABLE CREATE users (id INT, email TEXT);
D. CREATE TABLE users id INTEGER, email TEXT;
Solution
Step 1: Check the correct SQL syntax for table creation
The correct syntax is CREATE TABLE table_name (column_name data_type, ...); with parentheses around columns.
Step 2: Validate data types and order
Use INTEGER and TEXT as valid data types. The statement must start with CREATE TABLE followed by the table name.
Final Answer:
CREATE TABLE users (id INTEGER, email TEXT); -> Option B
The table creates successfully, but when inserting rows without specifying order_id, Supabase gives an error. What is the most likely cause?
medium
A. No SERIAL type for auto-incrementing primary key
B. Missing semicolon at the end of the statement
C. No column defined as UNIQUE
D. Using INT instead of INTEGER for data type
Solution
Step 1: Check the primary key column definition
The order_id is INT PRIMARY KEY but lacks auto-increment, so inserting rows without specifying order_id causes errors.
Step 2: Understand SERIAL usage for auto-increment
Using SERIAL instead of INT for primary key allows automatic unique number generation, preventing insert errors.
Final Answer:
No SERIAL type for auto-incrementing primary key -> Option A
Quick Check:
Primary key needs SERIAL for auto-increment [OK]
Hint: Use SERIAL for auto-increment primary keys [OK]
Common Mistakes:
Assuming INT auto-increments automatically
Ignoring missing semicolon errors
Confusing UNIQUE with PRIMARY KEY requirements
5. You want to create a table employees with columns id (auto-incrementing primary key), name (text, required), and email (text, unique). Which SQL command correctly achieves this in Supabase?
hard
A. CREATE TABLE employees (id SERIAL PRIMARY KEY, name TEXT NOT NULL, email TEXT UNIQUE);
B. CREATE TABLE employees (id INT PRIMARY KEY, name TEXT, email TEXT UNIQUE NOT NULL);
C. CREATE TABLE employees (id SERIAL, name TEXT NOT NULL, email TEXT);
D. CREATE TABLE employees (id INTEGER PRIMARY KEY, name TEXT NOT NULL, email TEXT UNIQUE);
Solution
Step 1: Define auto-incrementing primary key
Use SERIAL PRIMARY KEY for id to auto-generate unique IDs.
Step 2: Set constraints for name and email
name must be NOT NULL to be required; email must be UNIQUE to avoid duplicates.
Step 3: Verify all constraints and syntax
CREATE TABLE employees (id SERIAL PRIMARY KEY, name TEXT NOT NULL, email TEXT UNIQUE); correctly includes all required constraints and syntax.
Final Answer:
CREATE TABLE employees (id SERIAL PRIMARY KEY, name TEXT NOT NULL, email TEXT UNIQUE); -> Option A
Quick Check:
Auto-increment + NOT NULL + UNIQUE = CREATE TABLE employees (id SERIAL PRIMARY KEY, name TEXT NOT NULL, email TEXT UNIQUE); [OK]
Hint: Combine SERIAL, NOT NULL, and UNIQUE for correct constraints [OK]
Common Mistakes:
Forgetting NOT NULL on required columns
Using INT without SERIAL for auto-increment
Missing UNIQUE constraint on email
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.