In Module 1, you learned what SQL and databases mean. You also discovered how schools, banks, hospitals and supermarkets use databases to organise information.
In this module, you will learn how to start working with an SQL database. You will discover how to create a database, create tables, choose data types and insert information into a table.
We will use MySQL for our examples because it is a popular database system used by many websites and businesses.
By the end of this module, you should be able to:
An SQL environment is the software or workspace where you write and execute SQL commands to communicate with a database.
Think of it like a classroom where you carry out practical exercises. Instead of writing notes in a notebook, you write SQL commands and the database responds to your instructions.
Examples of tools used for working with SQL include:
MySQL is a relational database management system. It helps users store, organise and retrieve information.
Imagine a supermarket in Ibadan that sells rice, beans, cooking oil and other products.
The supermarket can use MySQL to store information about:
Instead of keeping all this information in paper files, the supermarket can organise it in a database.
Before working with a database, you need access to a database system. This usually involves opening a database tool and connecting to a MySQL server.
A MySQL connection may require the following details:
For example, a local computer may use localhost as its hostname. Your actual connection details depend on how MySQL is installed and configured.
Important: Never share your database password with strangers. It helps protect your information.
A database is like a large digital storage room. It can contain several tables that store related information.
Imagine you want to create a database for a school. You can name it SchoolDB.
Use this SQL command:
CREATE DATABASE SchoolDB;
Let's understand the command:
When the command runs successfully, MySQL creates a database called SchoolDB.
Sometimes, you may want to see all the databases available on your MySQL server.
Use this command:
SHOW DATABASES;
MySQL will display a list of databases you are allowed to see.
Creating a database does not automatically mean you are working inside it. You must first select the database.
Use the USE command:
USE SchoolDB;
This tells MySQL that you want to work with the SchoolDB database.
Remember: The USE command selects an existing database. It does not create a new one.
When creating a database table, you must tell the database what kind of information each column will contain.
This is where data types become important.
A data type tells the database what kind of value can be stored in a particular column.
| Data Type | Meaning | Example |
|---|---|---|
| INT | Stores whole numbers. | 13, 25, 100 |
| VARCHAR | Stores text with a maximum length. | Samuel, Lagos |
| TEXT | Stores longer pieces of text. | A student's address |
| DECIMAL | Stores numbers with decimal places. | 2500.50 |
| DATE | Stores dates. | 2026-12-01 |
| DATETIME | Stores a date and time. | 2026-12-01 10:30:00 |
| BOOLEAN | Represents true or false values. | TRUE or FALSE |
For example, a student's age can be stored using INT because it is a whole number. The student's name can be stored using VARCHAR because it contains text.
A database can contain many tables. Each table stores a particular type of information.
Inside our SchoolDB database, let's create a table called Students.
CREATE TABLE Students (
student_id INT,
student_name VARCHAR(100),
age INT,
class VARCHAR(20)
);
Let's understand what we have created:
Notice that every column has a name and a data type. This helps MySQL understand the information it should store.
Imagine your school has two students named Samuel. How would you identify the correct Samuel when searching for their records?
A primary key solves this problem.
A primary key is a column, or combination of columns, that uniquely identifies each record in a database table.
For our Students table, student_id can serve as the primary key. Every student should have a different ID.
CREATE TABLE Students (
student_id INT PRIMARY KEY,
student_name VARCHAR(100),
age INT,
class VARCHAR(20)
);
The PRIMARY KEY rule prevents two students from having the same student_id and does not allow the ID to be NULL.
What if you do not want to type a new student ID every time you add a student?
MySQL provides a feature called AUTO_INCREMENT. It automatically generates the next number for a column when a new record is inserted.
CREATE TABLE Students (
student_id INT AUTO_INCREMENT PRIMARY KEY,
student_name VARCHAR(100),
age INT,
class VARCHAR(20)
);
If the first student receives ID 1, the next student may automatically receive ID 2, followed by ID 3.
Creating a table gives you an empty structure. You still need to add information to it.
The INSERT INTO command is used to add new records.
Example:
INSERT INTO Students
(student_name, age, class)
VALUES
('Samuel', 13, 'JSS 2');
This command adds a student named Samuel, who is 13 years old and belongs to JSS 2.
Because student_id uses AUTO_INCREMENT, MySQL generates the ID automatically.
INSERT INTO Students
(student_name, age, class)
VALUES
('Samuel', 13, 'JSS 2'),
('Mary', 12, 'JSS 1'),
('Peter', 14, 'JSS 3'),
('Esther', 13, 'JSS 2');
This single SQL command inserts four student records into the Students table.
After creating your Students table, you may want to see all the tables inside your selected database.
Use this command:
SHOW TABLES;
MySQL will display the names of the tables in your current database.
You can use the SELECT command to view the records you inserted.
SELECT * FROM Students;
The database will display all the columns and records in the Students table.
Now let's put everything together. Imagine you are creating a database for a secondary school in Ibadan.
Follow these commands in order:
-- Step 1: Create a database
CREATE DATABASE SchoolDB;
-- Step 2: Select the database
USE SchoolDB;
-- Step 3: Create a Students table
CREATE TABLE Students (
student_id INT AUTO_INCREMENT PRIMARY KEY,
student_name VARCHAR(100),
age INT,
class VARCHAR(20)
);
-- Step 4: Insert student records
INSERT INTO Students
(student_name, age, class)
VALUES
('Samuel', 13, 'JSS 2'),
('Mary', 12, 'JSS 1'),
('Peter', 14, 'JSS 3'),
('Esther', 13, 'JSS 2');
-- Step 5: View the student records
SELECT * FROM Students;
Congratulations! You have created a database, created a table, inserted student records and retrieved the information.
In this module, you have learned:
Answer the following questions to test your understanding.
Your assignment is to create a database for a small supermarket. Name the database SupermarketDB.
Create a table called Products with these columns:
Use suitable data types for each column.
Then complete the following tasks:
Challenge: Can you add a primary key and AUTO_INCREMENT to the product_id column?
You have completed Module 2. You now know how to create a database, build tables and insert information using SQL.
In Module 3, you will learn how to retrieve specific information from databases using the SELECT statement. This is one of the most important skills used by data analysts.
CRMNuggets โ Learn. Practice. Get Certified.