Creating Tables
Learn the CREATE TABLE statement by building a real students table, then inspect it with SHOW TABLES and DESCRIBE.
Introduction
With the "school" database created and MySQL's data types understood, you are ready to create your first real table. This lesson walks through the CREATE TABLE statement and uses it to build the students table you will keep working with in upcoming lessons.
You will also learn two essential commands for checking your work: SHOW TABLES, which lists every table in a database, and DESCRIBE, which reveals the exact structure of one specific table.
- The syntax of the CREATE TABLE statement.
- How to define column names and data types.
- How to create the students table (id, name, age, grade) inside the "school" database.
- How to list tables with SHOW TABLES and inspect structure with DESCRIBE.
- That constraints can be added at table creation time, a topic explored fully in upcoming lessons.
The CREATE TABLE Statement
A table is created with CREATE TABLE, followed by the table name, and then a comma-separated list of column definitions inside parentheses. Each column definition has a name and a data type.
CREATE TABLE table_name (
column1_name DATA_TYPE,
column2_name DATA_TYPE,
column3_name DATA_TYPE
);Every column needs both a name (chosen by you) and a data type (from the types covered in the previous lesson). Column definitions are separated by commas, and the entire statement ends with a semicolon.
Make sure you have selected the right database first with USE school; — otherwise the table will be created in whichever database you were last using.
Creating the students Table
Using the "school" database from Lesson 9, here is the full statement to create a students table with four columns: id, name, age, and grade.
USE school;
CREATE TABLE students (
id INT,
name VARCHAR(50),
age INT,
grade INT
);Database changed
Query OK, 0 rows affected (0.03 sec)This creates an empty table — no rows yet, only the structure. id and age and grade are whole numbers, so they use INT; name is variable-length text, so it uses VARCHAR(50), allowing up to 50 characters.
Listing Tables
To see every table that exists in the currently selected database, use SHOW TABLES.
SHOW TABLES;+-------------------+
| Tables_in_school |
+-------------------+
| students |
+-------------------+Since "school" currently has only one table, students is the only row returned. As you add more tables in later lessons — like teachers or courses — they would all appear here.
Inspecting a Table's Structure
To see the exact structure of one table — every column, its data type, and other details — use DESCRIBE followed by the table name.
DESCRIBE students;+-------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| id | int | YES | | NULL | |
| name | varchar(50) | YES | | NULL | |
| age | int | YES | | NULL | |
| grade | int | YES | | NULL | |
+-------+-------------+------+-----+---------+-------+This confirms each column's name, its exact data type, whether NULL (empty) values are currently allowed, and other details you will fill in as you add constraints. Right now, every column allows NULL because no rules have been added yet.
DESC students; is a shorthand for DESCRIBE students; — both produce identical results. Use whichever you find easier to type.
A Note on Constraints
The students table created in this lesson has no rules yet — nothing stops you from inserting a duplicate id, a negative age, or leaving a name blank. That is intentional, so this lesson could focus purely on table structure.
Constraints, like PRIMARY KEY, NOT NULL, UNIQUE, DEFAULT, and CHECK, can be added directly inside a CREATE TABLE statement to enforce exactly these kinds of rules. Each one gets its own dedicated lesson soon, starting with Constraints and then Primary Key.
In upcoming lessons you will recreate and refine this same students table, adding a PRIMARY KEY on id, a NOT NULL rule on name, and other constraints — so keep this basic version in mind as the starting point.
Common Mistakes
- Forgetting to run USE database_name; before CREATE TABLE, creating the table in the wrong database.
- Leaving out a data type for a column — every column definition must include one.
- Adding a trailing comma after the last column definition, which causes a syntax error.
- Assuming a table with no constraints will reject bad data — without constraints, almost anything can be inserted.
Best Practices
- Run SHOW TABLES; after creating a table, to confirm it was actually created.
- Run DESCRIBE table_name; whenever you are unsure of a table's exact structure.
- Choose clear, lowercase, singular-or-plural-but-consistent table and column names, like students and name.
- Plan your columns and their data types on paper before writing the CREATE TABLE statement.
Frequently Asked Questions
What happens if I try to create a table that already exists?
MySQL returns an error, such as "Table 'students' already exists." You can use CREATE TABLE IF NOT EXISTS students (...) to avoid the error when re-running a script.
Can I add columns to a table after creating it?
Yes. The ALTER TABLE statement, covered in the next lesson, lets you add, modify, or remove columns from an existing table without recreating it.
Why does DESCRIBE show "Null: YES" for every column right now?
Because no constraints have been added yet, every column currently allows empty (NULL) values by default. Adding a NOT NULL constraint, covered soon, would change this to "NO" for that column.
Is there a difference between DESCRIBE and SHOW COLUMNS FROM?
They return essentially the same information. DESCRIBE table_name; is simply shorter to type than SHOW COLUMNS FROM table_name;.
Key Takeaways
- CREATE TABLE table_name (...) defines a new table with named columns and data types.
- The students table (id, name, age, grade) was created inside the "school" database using INT and VARCHAR.
- SHOW TABLES; lists every table in the currently selected database.
- DESCRIBE table_name; reveals a table's exact column names, data types, and current rules.
- Constraints like PRIMARY KEY and NOT NULL can be added at creation time, and are covered in full over the next several lessons.
Summary
CREATE TABLE is how every table in MySQL begins its life, and SHOW TABLES and DESCRIBE are the two commands you will reach for constantly to confirm exactly what a database and its tables look like.
In this lesson, you created a real students table inside the "school" database, listed it with SHOW TABLES, and inspected its structure with DESCRIBE. Next, you will learn how to modify an existing table's structure using ALTER TABLE.
- You created the students table with id, name, age, and grade columns.
- You can list a database's tables with SHOW TABLES.
- You can inspect a table's structure with DESCRIBE.
- You are ready to learn how to modify tables with ALTER TABLE.