Skip to content

1. Create Databases and Tables

Updated
17 Sept 2026
1 min read
7 sections
Page 3 of 22
Data Engineering

Create Databases and Tables

A database is an organized collection of data. In MySQL, the usual beginner workflow is:

  1. Create a database.
  2. Select the database with USE.
  3. Create one or more tables.
  4. Insert rows into those tables.
  5. Query the data with SELECT.
  6. Drop temporary practice objects when you are done.

Create databases

Use CREATE DATABASE to create a new database. For practice, the script creates one database for a library and one for ecommerce.

sql
CREATE DATABASE librarydb;
CREATE DATABASE ecommercedb;

Good naming habits matter early:

HabitWhy it helps
Use meaningful nameslibrarydb is clearer than db1.
Avoid reserved wordsNames like order or group can conflict with SQL keywords.
Stay consistent with caseMySQL table name behavior can differ across operating systems.

Select the database

Before creating a table, tell MySQL which database should receive it.

sql
USE librarydb;

Create a table without constraints

This first table does not enforce primary keys, required fields, or relationships. That is useful for learning the table shape before adding rules.

sql
CREATE TABLE Books (
  BookID INT,
  Title VARCHAR(25),
  Author VARCHAR(25),
  Genre VARCHAR(25),
  PublicationYear INT
);

The table columns describe one book record:

ColumnMeaning
BookIDNumeric identifier for the book.
TitleBook title.
AuthorAuthor name or short code.
GenreCategory such as romance or sci-fi.
PublicationYearYear the book was published.

Inspect and query the table

SHOW DATABASES lists available databases. SELECT * retrieves all columns from a table.

sql
SHOW DATABASES;

SELECT *
FROM Books;

At this point, the table exists but has no rows.

Insert rows

Use INSERT INTO with a column list so the values are mapped clearly.

sql
INSERT INTO Books (BookID, Title, Author, Genre, PublicationYear)
VALUES
  (1, "Twilight", "KN", "Romantic", 2020),
  (2, "Harry Potter", "ALAS", "Sci-Fi", 2018);

Then read the table again.

sql
SELECT *
FROM Books;

Drop practice objects

DROP TABLE removes a table. DROP DATABASE removes the entire database and every table inside it.

sql
DROP TABLE Books;

DROP DATABASE ecommercedb;

Use these commands carefully. In real projects, prefer backups, migrations, and review before dropping anything permanent.