📖 Scenario: You are setting up a database for a small bookstore. You need to create a table to store information about books, including their title, author, price, publication date, and whether they are currently in stock.
🎯 Goal: Create a PostgreSQL table named books with appropriate columns and data types to store the bookstore's book information.
📋 What You'll Learn
Create a table named books
Include a column id as a primary key with type SERIAL
Include a column title with type VARCHAR(100)
Include a column author with type VARCHAR(50)
Include a column price with type NUMERIC(6,2)
Include a column published_date with type DATE
Include a column in_stock with type BOOLEAN
💡 Why This Matters
🌍 Real World
Creating tables with correct data types is essential for storing and organizing data efficiently in real-world applications like bookstores, libraries, or inventory systems.
💼 Career
Database developers and administrators frequently create tables with appropriate data types to ensure data integrity and optimize storage.
Progress0 / 4 steps
1
Create the basic table structure
Write a CREATE TABLE statement to create a table named books with a column id of type SERIAL as the primary key.
PostgreSQL
Hint
Use CREATE TABLE books (id SERIAL PRIMARY KEY); to start.
2
Add title and author columns
Add two columns to the books table: title with type VARCHAR(100) and author with type VARCHAR(50).
PostgreSQL
Hint
Separate columns with commas and specify the length in VARCHAR.
3
Add price and published_date columns
Add two more columns to the books table: price with type NUMERIC(6,2) and published_date with type DATE.
PostgreSQL
Hint
Use NUMERIC(6,2) for prices with two decimal places.
4
Add in_stock column
Add a final column in_stock with type BOOLEAN to the books table to indicate if the book is currently available.
PostgreSQL
Hint
Use BOOLEAN type for true/false values.
Practice
(1/5)
1. Which PostgreSQL data type is best to store a person's full name in a CREATE TABLE statement?
easy
A. INTEGER
B. VARCHAR or TEXT
C. BOOLEAN
D. DATE
Solution
Step 1: Understand the data type purpose
Names are text data, so we need a type that stores characters.
Step 2: Choose appropriate text type in PostgreSQL
VARCHAR or TEXT types store strings of characters, perfect for names.
Final Answer:
VARCHAR or TEXT -> Option B
Quick Check:
Text data = VARCHAR or TEXT [OK]
Hint: Use VARCHAR or TEXT for any text data like names [OK]
Common Mistakes:
Using INTEGER for text data
Using BOOLEAN for names
Using DATE for text fields
2. Which of the following is the correct syntax to create a table named employees with an integer primary key id and a text column name?
easy
A. CREATE TABLE employees (id INTEGER PRIMARY KEY, name TEXT);
B. CREATE employees TABLE (id INT PRIMARY, name STRING);
C. CREATE TABLE employees {id INT PRIMARY KEY, name TEXT};
D. CREATE TABLE employees (id INT, name TEXT PRIMARY KEY);
INTEGER or INT is valid; PRIMARY KEY follows the column type; TEXT is valid for strings.
Final Answer:
CREATE TABLE employees (id INTEGER PRIMARY KEY, name TEXT); -> Option A
Quick Check:
Correct syntax and types = CREATE TABLE employees (id INTEGER PRIMARY KEY, name TEXT); [OK]
Hint: Use parentheses and commas correctly in CREATE TABLE [OK]
Common Mistakes:
Wrong keyword order like PRIMARY before type
Using curly braces instead of parentheses
Wrong data types like STRING instead of TEXT
3. Given the table creation: CREATE TABLE products (product_id SERIAL PRIMARY KEY, name VARCHAR(50) NOT NULL, price NUMERIC(10,2)); What will be the data type of product_id and what does SERIAL do?
medium
A. product_id is an auto-incrementing integer used as a unique ID
B. product_id is a string that stores numbers
C. product_id is a floating-point number
D. product_id is a boolean flag
Solution
Step 1: Understand SERIAL type in PostgreSQL
SERIAL creates an integer column that auto-increments with each new row, perfect for unique IDs.
Step 2: Confirm data type and purpose
It is an integer type, not string or float, used as a primary key to uniquely identify rows.
Final Answer:
product_id is an auto-incrementing integer used as a unique ID -> Option A
Quick Check:
SERIAL = auto-increment integer [OK]
Hint: SERIAL means auto-increment integer primary key [OK]
Common Mistakes:
Thinking SERIAL is a string type
Confusing SERIAL with floating-point
Assuming SERIAL is a boolean
4. Identify the error in this table creation statement: CREATE TABLE orders (order_id INT PRIMARY KEY, order_date DATE NOT NULL, total_amount FLOAT(10,2));
medium
A. Missing comma between columns
B. PRIMARY KEY cannot be used on INT columns
C. FLOAT(10,2) is invalid syntax in PostgreSQL
D. DATE cannot be NOT NULL
Solution
Step 1: Check FLOAT data type usage
PostgreSQL FLOAT accepts at most one precision argument (1-53 digits); FLOAT(10,2) uses two arguments like NUMERIC, which is invalid syntax for FLOAT. Use NUMERIC(10,2) for precise money values.
Step 2: Verify other parts of the statement
INT can be PRIMARY KEY, DATE can be NOT NULL, commas are correctly placed.
Final Answer:
FLOAT(10,2) is invalid syntax in PostgreSQL -> Option C
Quick Check:
FLOAT(p,s) invalid; use NUMERIC(p,s) [OK]
Hint: FLOAT(p) ok, but not FLOAT(p,s); use NUMERIC(p,s) for money [OK]
Common Mistakes:
Using FLOAT(10,2) in PostgreSQL
Thinking PRIMARY KEY can't be INT
Assuming DATE can't be NOT NULL
5. You want to create a table users with columns: user_id as a UUID primary key, email as unique and not null, and created_at as timestamp with default current time. Which is the correct CREATE TABLE statement?
hard
A. CREATE TABLE users (user_id SERIAL PRIMARY KEY, email VARCHAR UNIQUE, created_at TIMESTAMP);
B. CREATE TABLE users (user_id UUID, email TEXT NOT NULL, created_at DATE DEFAULT NOW());
C. CREATE TABLE users (user_id UUID PRIMARY KEY, email VARCHAR(255) NOT NULL, created_at TIMESTAMP DEFAULT NOW());
D. CREATE TABLE users (user_id UUID PRIMARY KEY, email VARCHAR(255) UNIQUE NOT NULL, created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP);
Solution
Step 1: Check UUID primary key syntax
UUID is a valid type for unique IDs; PRIMARY KEY constraint is correctly placed.
Step 2: Validate email constraints and type
VARCHAR(255) with UNIQUE and NOT NULL ensures emails are unique and always present.
Step 3: Confirm timestamp default value
DEFAULT CURRENT_TIMESTAMP sets creation time automatically; this is correct syntax.
Final Answer:
CREATE TABLE users (user_id UUID PRIMARY KEY, email VARCHAR(255) UNIQUE NOT NULL, created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP); -> Option D