Designing Databases Lesson 3 of 13
MySQL Databases and Tables — CREATE, USE, SHOW and DESCRIBE
Create and select databases with the right character set, create tables, inspect them with SHOW TABLES and DESCRIBE, and change or remove them with ALTER, DROP and TRUNCATE.
A MySQL server holds many databases, one per project, and each database holds tables. This lesson covers the commands for creating and inspecting them.
Creating a database
CREATE DATABASE school
CHARACTER SET utf8mb4
COLLATE utf8mb4_unicode_ci;
USE school;
CREATE DATABASEmakes a new, empty database.utf8mb4stores every character correctly, including ñ, accents and emojis. Always use it.utf8mb4_unicode_cidecides how text is sorted and compared;cimeans case-insensitive, so "ana" equals "Ana".USE school;selects it, so the following commands apply to it.
Listing databases and tables
SHOW DATABASES;
SHOW TABLES;
SELECT DATABASE(); -- which database am I using?
Creating a table
CREATE TABLE students (
id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
lrn CHAR(12) NOT NULL UNIQUE,
first_name VARCHAR(50) NOT NULL,
last_name VARCHAR(50) NOT NULL,
birthday DATE,
grade_level TINYINT UNSIGNED NOT NULL,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
) ENGINE=InnoDB;
Each line is a column: a name, a data type and options. AUTO_INCREMENT makes MySQL number new rows automatically (1, 2, 3…). ENGINE=InnoDB is the default storage engine; it supports transactions and foreign keys.
DESCRIBE: see a table's structure
DESCRIBE students;
Output
+-------------+---------------------+------+-----+---------------------+----------------+ | Field | Type | Null | Key | Default | Extra | +-------------+---------------------+------+-----+---------------------+----------------+ | id | int(10) unsigned | NO | PRI | NULL | auto_increment | | lrn | char(12) | NO | UNI | NULL | | | first_name | varchar(50) | NO | | NULL | | | last_name | varchar(50) | NO | | NULL | | | birthday | date | YES | | NULL | | | grade_level | tinyint(3) unsigned | NO | | NULL | | | created_at | timestamp | NO | | current_timestamp() | | +-------------+---------------------+------+-----+---------------------+----------------+
SHOW CREATE TABLE students; shows the full CREATE TABLE statement, handy for copying a table's design.
Changing a table: ALTER TABLE
ALTER TABLE students ADD COLUMN section VARCHAR(30) AFTER grade_level;
ALTER TABLE students MODIFY first_name VARCHAR(80) NOT NULL;
ALTER TABLE students RENAME COLUMN birthday TO birth_date;
ALTER TABLE students DROP COLUMN created_at;
RENAME TABLE students TO learners;
MySQL's AFTER column and FIRST let you choose where a new column appears. MODIFY changes a column's type or options.
Removing data and tables
| Command | Effect |
|---|---|
DELETE FROM t; | Removes all rows, one by one; AUTO_INCREMENT continues where it was |
TRUNCATE TABLE t; | Removes all rows instantly and resets AUTO_INCREMENT to 1 |
DROP TABLE t; | Deletes the table completely |
DROP DATABASE d; | Deletes the whole database and all its tables |
IF EXISTS / IF NOT EXISTS
CREATE DATABASE IF NOT EXISTS school;
DROP TABLE IF EXISTS temp_list;
These avoid errors when a script is run more than once.
Naming conventions
- Lowercase names with underscores:
order_items,first_name. - Plural table names (
students) or singular (student): pick one style and keep it. - Avoid spaces and reserved words like
orderorgroupas names; if you must, wrap them in backticks.
Quick Quiz
Check what you learned. Walang grade ito, practice lang. 🙂
By Sir Reyel · Last updated 2026-12-04