Skip to main content
SQL Fundamentals
CHAPTER 04 Beginner

Creating Databases and Tables

Updated: May 16, 2026
15 min read

# CHAPTER 4

Creating Databases and Tables

1. Introduction

You have your local server running, and you understand how tables work conceptually. Now, we must translate those concepts into reality. In SQL, commands that define or modify the structural blueprint of the database are called DDL (Data Definition Language). In this chapter, we will write our first raw SQL commands: CREATE DATABASE and CREATE TABLE, laying the concrete foundation for our applications.

2. Learning Objectives

By the end of this chapter, you will be able to:
  • Differentiate between DDL and DML in SQL.
  • Write a CREATE DATABASE statement.
  • Select an active database using the USE command.
  • Write a CREATE TABLE statement with columns and basic data types.
  • Delete databases and tables using the DROP command.

3. DDL vs DML

SQL commands are categorized based on what they do:
  • DDL (Data Definition Language): Commands that build the house. (CREATE, ALTER, DROP). They define the structure (Tables, Columns).
  • DML (Data Manipulation Language): Commands that put furniture in the house. (INSERT, SELECT, UPDATE, DELETE). They handle the actual data rows.

*In this chapter, we are exclusively focusing on DDL.*

4. Creating a Database

Open your SQL GUI (phpMyAdmin, pgAdmin, or terminal). To create a new database to hold our tables, we use the CREATE DATABASE command.
sql
12
-- Create a brand new database
CREATE DATABASE school_system;

*(Note: In SQL, two dashes -- are used to write comments that the engine ignores).* *(Note 2: Every SQL command MUST end with a semicolon ;)*

5. Selecting the Database (USE)

If you have 10 databases on your server, and you tell the server to "Create a Table", it will throw an error because it doesn't know *which* database to put it in. You must select the database first.
sql
12
-- Tell the server we want to work inside the school_system database
USE school_system;

*(Note: If you are using PostgreSQL/pgAdmin, you select the database by clicking it in the visual sidebar rather than using the USE command).*

6. Creating a Table

Now we build our first table. The CREATE TABLE syntax requires the table name, followed by parentheses () containing a comma-separated list of columns. For each column, you must specify its name and its Data Type (e.g., INT for numbers, VARCHAR for text).
sql
1234567
-- Create a table to store Student information
CREATE TABLE students (
    id INT,
    first_name VARCHAR(50),
    last_name VARCHAR(50),
    enrollment_date DATE
);

Breaking it down:

  • id INT: A column for the student ID number.
  • firstname VARCHAR(50): A column for text, with a maximum limit of 50 characters.
  • enrollmentdate DATE: A column strictly formatted for calendar dates.

7. Destroying Architecture (DROP)

If you make a mistake and want to completely erase a table (or a whole database) and start over, you use the DROP command.
sql
12345
-- DANGER: This permanently deletes the table AND all data inside it!
DROP TABLE students;

-- DANGER: This permanently deletes the entire database!
DROP DATABASE school_system;

*Warning: There is no "Undo" or "Recycle Bin" in SQL. Once you execute a DROP command, the data is gone forever.*

8. Mini Project: Student Management Database

Let's build a complete, multi-table schema for a school.
sql
12345678910111213141516
CREATE DATABASE university_db;
USE university_db;

-- Table 1: Teachers
CREATE TABLE teachers (
    teacher_id INT,
    full_name VARCHAR(100),
    department VARCHAR(50)
);

-- Table 2: Courses
CREATE TABLE courses (
    course_id INT,
    course_title VARCHAR(100),
    credits INT
);

*(We have successfully created the structural schema! The tables currently have 0 rows of data in them).*

9. Common Mistakes

  • Forgetting the Semicolon: If you type CREATE DATABASE test CREATE TABLE users(id INT), SQL will crash. It thinks it is one giant sentence. You must separate commands with a semicolon ;.
  • Missing Commas in Table Creation: Inside the CREATE TABLE () parentheses, every column definition MUST be separated by a comma, EXCEPT the very last one!

10. Best Practices

  • Snake Case Naming: Avoid spaces and capital letters in table and column names. Use snakecase.
  • *Bad:* CREATE TABLE Student Data (Will crash)
  • *Bad:* CREATE TABLE StudentData (Hard to read)
  • *Good:* CREATE TABLE studentdata

11. Exercises

  1. 1. What SQL command is used to select an active database before creating tables?
  1. 2. What is the highly dangerous SQL command used to permanently delete an entire table and its structure?

12. SQL Challenges

Write the SQL code to create a database named ecommercedb. Switch to that database, and create a table named products with three columns: productid (Integer), product_name (Text up to 100 characters), and price (Integer).
sql
1234567
CREATE DATABASE ecommerce_db;
USE ecommerce_db;
CREATE TABLE products (
    product_id INT,
    product_name VARCHAR(100),
    price INT
);

13. MCQ Quiz with Answers

Question 1

Which of the following SQL commands belongs to the Data Definition Language (DDL) category?

Question 2

When writing a CREATE TABLE statement, what information must be provided for every single column defined inside the parentheses?

14. Interview Questions

  • Q: Explain the difference between DDL (Data Definition Language) and DML (Data Manipulation Language). Provide two examples of commands for each.
  • Q: Why does SQL enforce strict Data Types during the CREATE TABLE process? What architectural advantage does this provide over a standard Excel spreadsheet?

15. FAQs

Q: Can I change a table's structure after I create it? A: Yes! If you forget to add an email column to the students table, you don't have to DROP it. You can use the DDL command ALTER TABLE students ADD COLUMN email VARCHAR(100); to dynamically modify the structure.

16. Summary

You are officially a database architect. By executing DDL commands like CREATE DATABASE and CREATE TABLE, you have built the physical containers that will hold your application's data. You also learned the grave responsibility of using the DROP command, which destroys architecture permanently.

17. Next Chapter Recommendation

In our table creation, we used terms like INT and VARCHAR(50). But what do those actually mean? If we want to store money, decimals, or massive paragraphs of text, what do we use? In Chapter 5: SQL Data Types Explained, we will dive deep into how SQL precisely categorizes data.

Finish this Chapter

Save your progress on your learning path and prepare for coding interview challenges.

Discussion

Join the discussion

Log in or create a free account to participate.

Sort: ·