Skip to main content
MySQL Basics
CHAPTER 04 Beginner

Creating Databases and Tables

Updated: May 16, 2026
6 min read

# CHAPTER 4

Creating Databases and Tables

1. Introduction

We have established our local server and learned the theory of relational tables. Now, the real work begins. To instruct the MySQL engine to build our architecture, we use Data Definition Language (DDL)—a subset of SQL used specifically to define database structures. In this chapter, we will write raw SQL code to create our first database, navigate into it, and construct a professional users table from scratch.

2. Learning Objectives

By the end of this chapter, you will be able to:
  • Use the CREATE DATABASE command.
  • Select an active database using the USE command.
  • Understand the syntax of the CREATE TABLE command.
  • Define basic columns and their constraints.
  • Follow professional SQL naming conventions.

3. Creating and Selecting a Database

Before creating tables, you need a "container" to hold them.

Open your MySQL Command Line (mysql -u root -p) or use the SQL tab in phpMyAdmin and execute the following:

sql
12345678
-- 1. Create the database
CREATE DATABASE school_system;

-- 2. Verify it was created
SHOW DATABASES;

-- 3. Tell MySQL you want to work inside this specific database
USE school_system;

*Note: If you forget the USE command, MySQL will throw an error when you try to create a table, because it won't know which database to put it in!*

4. Creating a Table

Creating a table requires defining its exact structure. You must specify the table name, the column names, and exactly what Data Type each column is allowed to hold (e.g., Numbers, Text, Dates).

Let's create a table to hold our students.

sql
123456
CREATE TABLE students (
    student_id INT,
    first_name VARCHAR(50),
    last_name VARCHAR(50),
    enrollment_date DATE
);

Breaking down the syntax:

  • CREATE TABLE students: The command telling MySQL to build a table named 'students'.
  • (...): The parentheses encapsulate all the column definitions.
  • studentid INT: A column named 'studentid' that only accepts Integers (whole numbers).
  • firstname VARCHAR(50): A column named 'firstname' that accepts Variable Characters (text/strings) up to a maximum of 50 characters long.
  • enrollment_date DATE: A column that strictly accepts valid calendar dates (YYYY-MM-DD).

5. Verifying the Table Structure

Once the table is created, how do we know it worked? We can ask MySQL to describe the structure back to us.
sql
12345
-- Show all tables in the current database
SHOW TABLES;

-- Describe the exact column structure of the 'students' table
DESCRIBE students;

*(The DESCRIBE command will print a neat textual representation of your columns and data types!)*

6. Dropping (Deleting) Databases and Tables

If you make a massive mistake and want to start completely over, you can use the DROP command. WARNING: DROP is permanent. There is no undo button. It instantly deletes the table and all data inside it.
sql
12345
-- Delete a table
DROP TABLE students;

-- Delete an entire database
DROP DATABASE school_system;

7. Mini Project: Build an Inventory Table

Let's build a slightly more advanced table for a store inventory system. We will add a few basic rules.
sql
1234567891011
CREATE DATABASE store;
USE store;

CREATE TABLE products (
    product_id INT PRIMARY KEY,       -- Uniquely identifies each row
    product_name VARCHAR(100),
    price DECIMAL(10, 2),             -- A decimal number (e.g., 99.99)
    in_stock BOOLEAN                  -- True or False
);

DESCRIBE products;

8. Common Mistakes

  • Forgetting Commas: Inside the CREATE TABLE parentheses, every column definition MUST be separated by a comma. But, critically, the *very last* column definition must NOT have a comma after it.
  • Spaces in Names: Never put spaces in database names, table names, or column names. first name is invalid and will crash. Use underscores: firstname.
  • Reserved Keywords: Do not name your tables or columns after SQL commands. For example, creating a column named select or table will deeply confuse the MySQL engine.

9. Best Practices

  • Plural Table Names: It is an industry standard to name databases in the singular, but tables in the plural. A database holds many tables, but a users table holds many users. (e.g., CREATE TABLE users;, CREATE TABLE orders;).
  • Lowercase Everything: While SQL commands (like CREATE TABLE) are often written in UPPERCASE for readability, you should always name your actual databases, tables, and columns in lowercase (firstname, products).

10. Exercises

  1. 1. Write the SQL command to completely delete a database named oldtestdb.
  1. 2. Write the SQL command to create a table named books with three columns: bookid (Integer), title (VARCHAR 150), and publishyear (Integer).

11. MCQ Quiz with Answers

Question 1

What is the purpose of the USE command in MySQL?

Question 2

When defining a table structure using CREATE TABLE, what information MUST you provide for every single column?

12. Interview Questions

  • Q: Explain the difference between Data Definition Language (DDL) and Data Manipulation Language (DML) in SQL. Give an example of a DDL command.
  • Q: Why is it considered a terrible practice to use spaces or SQL reserved keywords when naming database columns?

13. FAQs

Q: I made a typo in a column name when creating the table. Do I have to DROP the whole table and start over? A: No! You can modify an existing table structure without deleting it by using the ALTER TABLE command (e.g., ALTER TABLE students RENAME COLUMN fname TO firstname;).

14. Summary

You are now an architect. By mastering the CREATE DATABASE, USE, and CREATE TABLE commands, you possess the ability to construct the foundational blueprint of any software application. However, a table is only as strong as the data types that define its columns.

15. Next Chapter Recommendation

In our table examples, we used INT, VARCHAR(50), and DATE. What do these mean? How much text can a VARCHAR hold? In Chapter 5: MySQL Data Types Explained, we will dive deep into the specific constraints we place on our columns to guarantee data integrity.

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: ·