MySQL
MySQL

CREATE TABLE

CREATE TABLE builds a new table inside the database you're currently using. You define its name and every column, along with each column's data type, in one statement. CREATE TABLE students ( id INT PRIMARY KEY, name VARCHAR(100), age INT ); Reading this line by line: students is the table's name. Inside the parentheses, each line defines one column: id is a whole number and is marked PRIMARY KEY (its unique identifier); name is text up to 100 characters; age is a whole number. Columns are separated by commas, and the whole statement ends with a semicolon. Once created, the table exists but is empty CREATE TABLE defines the shape, it doesn't add any actual rows. You'll refine this basic pattern soon with constraints like NOT NULL, DEFAULT, and AUTO_INCREMENT, covered in their own topics shortly for now, focus on the core shape: a table name, and a list of columns with their types.

Why this matters

  • ·CREATE TABLE defines a clear, enforced shape for every row that will ever go into it.
  • ·Defining columns and types up front stops obviously wrong data (like text in an age column) from being accepted later.
  • ·A table's structure is easy to inspect afterward with DESCRIBE, so the definition is never a mystery.

Common mistakes

  • ·Running CREATE TABLE for a table that already exists causes an error unless you use IF NOT EXISTS.
  • ·Choosing the wrong data type early can force a disruptive structural change once real data already exists.
  • ·Forgetting a primary key makes it harder to reliably update or delete one specific row later.
Your codePHP
Expected output
Query OK, 0 rows affected

+-------+--------------+------+-----+---------+
| Field | Type         | Null | Key | Default |
+-------+--------------+------+-----+---------+
| id    | int          | NO   | PRI | NULL    |
| name  | varchar(100) | YES  |     | NULL    |
| age   | int          | YES  |     | NULL    |
+-------+--------------+------+-----+---------+
3 rows in set

PHP runs on a server, not in the browser this shows the expected output instead of a live one.

Frequently asked questions

No. It only defines the table's structure its columns and their types. Rows are added separately with INSERT.

Practice questions

Question 1

Create a table called books with columns id (INT, primary key), title (VARCHAR(150)), and pages (INT).

Your codePHP
Expected output

PHP runs on a server, not in the browser this shows the expected output instead of a live one.

Question 2

Create a table called employees with columns id (INT, primary key), full_name (VARCHAR(120)), and salary (DECIMAL(10,2)).

Your codePHP
Expected output

PHP runs on a server, not in the browser this shows the expected output instead of a live one.

Quick check

Just for this topic answer honestly, then check yourself.

1 / 3

What does CREATE TABLE define?