Creating Tables and Defining Structure
Tables are where all your data lives. Getting their structure right — choosing correct data types, defining primary keys, and adding the right constraints — prevents bad data from ever entering your database.
CREATE TABLE
Once you have a database selected, you define the tables that will hold your data. CREATE TABLE specifies the table name, every column, and the rules that apply to each column — its data type, whether it can be empty, and whether it uniquely identifies a row.
CREATE TABLE table_name (
column_name data_type constraints,
column_name data_type constraints,
...
);
CREATE TABLE students (
student_id INT NOT NULL AUTO_INCREMENT,
full_name VARCHAR(100) NOT NULL,
email VARCHAR(150) NOT NULL UNIQUE,
date_of_birth DATE NOT NULL,
enrolled_on DATE DEFAULT (CURRENT_DATE),
is_active BOOLEAN DEFAULT TRUE,
PRIMARY KEY (student_id)
);
-- Safe version — only creates if table doesn't exist
CREATE TABLE IF NOT EXISTS students ( ... );
Core Data Types
Every column must have a data type. The type tells the database what kind of values to expect and how much storage to allocate. Choosing the right type prevents invalid data and saves storage space.
INTTINYINT (0–255), BIGINT (very large numbers).VARCHAR(n)VARCHAR(100) stores up to 100 characters.TEXTDATE2025-03-15. No time component. Use for birthdays, enrollment dates, deadlines.DATETIMEDECIMAL(p, s)DECIMAL(10, 2) stores up to 10 digits, 2 after the decimal point. Always use for money — never FLOAT.BOOLEANPrimary Keys
A primary key is the column (or combination of columns) that uniquely identifies every row in the table. Rules: it must be unique, it cannot be NULL, and each table should have exactly one.
AUTO_INCREMENT tells MySQL to automatically assign the next available integer when a new row is inserted — you never have to specify the ID manually.
-- Method 1: inline on the column (most common)
CREATE TABLE courses (
course_id INT PRIMARY KEY AUTO_INCREMENT,
title VARCHAR(150) NOT NULL
);
-- Method 2: as a table constraint (same result)
CREATE TABLE courses (
course_id INT NOT NULL AUTO_INCREMENT,
title VARCHAR(150) NOT NULL,
PRIMARY KEY (course_id)
);
-- Composite primary key (two columns together)
CREATE TABLE enrolments (
student_id INT NOT NULL,
course_id INT NOT NULL,
enrolled_on DATE,
PRIMARY KEY (student_id, course_id)
);
DESCRIBE — Inspecting a Table
DESCRIBE (or DESC) shows you the structure of an existing table — its columns, data types, whether NULL is allowed, keys, and default values. Essential for understanding a database you didn't build yourself.
DESCRIBE students;
-- or shorthand:
DESC students;
-- Output:
-- +--------------+--------------+------+-----+---------+----------------+
-- | Field | Type | Null | Key | Default | Extra |
-- +--------------+--------------+------+-----+---------+----------------+
-- | student_id | int | NO | PRI | NULL | auto_increment |
-- | full_name | varchar(100) | NO | | NULL | |
-- | email | varchar(150) | NO | UNI | NULL | |
-- | date_of_birth| date | NO | | NULL | |
-- | enrolled_on | date | YES | | today | |
-- | is_active | tinyint(1) | YES | | 1 | |
-- +--------------+--------------+------+-----+---------+----------------+
-- Also useful:
SHOW TABLES; -- list all tables in current database
SHOW CREATE TABLE students; -- shows full CREATE TABLE statement