++++Learn the first MySQL workflow: create a database, select it, create a table, insert rows, query the rows, and clean up tables or databases.
1. Create Databases and Tables
Create Databases and Tables
A database is an organized collection of data. In MySQL, the usual beginner workflow is:
- Create a database.
- Select the database with
USE. - Create one or more tables.
- Insert rows into those tables.
- Query the data with
SELECT. - 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.
CREATE DATABASE librarydb;
CREATE DATABASE ecommercedb;Good naming habits matter early:
| Habit | Why it helps |
|---|---|
| Use meaningful names | librarydb is clearer than db1. |
| Avoid reserved words | Names like order or group can conflict with SQL keywords. |
| Stay consistent with case | MySQL table name behavior can differ across operating systems. |
Select the database
Before creating a table, tell MySQL which database should receive it.
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.
CREATE TABLE Books (
BookID INT,
Title VARCHAR(25),
Author VARCHAR(25),
Genre VARCHAR(25),
PublicationYear INT
);The table columns describe one book record:
| Column | Meaning |
|---|---|
BookID | Numeric identifier for the book. |
Title | Book title. |
Author | Author name or short code. |
Genre | Category such as romance or sci-fi. |
PublicationYear | Year the book was published. |
Inspect and query the table
SHOW DATABASES lists available databases. SELECT * retrieves all columns from a table.
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.
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.
SELECT *
FROM Books;Drop practice objects
DROP TABLE removes a table. DROP DATABASE removes the entire database and every table inside it.
DROP TABLE Books;
DROP DATABASE ecommercedb;Use these commands carefully. In real projects, prefer backups, migrations, and review before dropping anything permanent.