Lesson 15 of 60 – CREATE TABLE
25%

CREATE TABLE in MySQL

The CREATE TABLE statement is used to create a new table inside a MySQL database. A table contains columns that define the structure of the data and rows that store actual records.

Note: Before creating a table, select the required database using the USE statement, or specify the database name directly in the table name.

1. What is CREATE TABLE?

CREATE TABLE is a SQL statement used to create a new table.

CREATE TABLE students (
    id INT,
    name VARCHAR(100)
);

This creates a table named students with two columns.

2. Basic Syntax

The basic syntax is:

CREATE TABLE table_name (
    column1 datatype,
    column2 datatype,
    column3 datatype
);

Each column definition contains a column name and a data type.

3. Selecting a Database First

Normally, select the database before creating the table.

USE school;

CREATE TABLE students (
    id INT,
    name VARCHAR(100)
);

The students table is created inside the selected school database.

4. Creating a Simple Table

A simple table can contain only a few columns.

CREATE TABLE students (
    id INT,
    name VARCHAR(100),
    age INT
);

The table contains id, name, and age.

5. Table Name

The name after CREATE TABLE is the table name.

CREATE TABLE employees (
    id INT,
    name VARCHAR(100)
);

Here, employees is the table name.

6. Column Names

Column names identify the type of information stored in each column.

CREATE TABLE students (
    id INT,
    name VARCHAR(100),
    mobile VARCHAR(15),
    course VARCHAR(100)
);

7. Data Types

Each column should have a suitable data type.

id       INT
name     VARCHAR(100)
fee      DECIMAL(10,2)
dob      DATE

The data type determines the kind of value that can be stored in a column.

8. INT Column

INT is commonly used for whole numbers.

CREATE TABLE students (
    id INT,
    age INT
);

Values such as 1, 25, and 100 can be stored in INT columns.

9. VARCHAR Column

VARCHAR is commonly used for variable-length text.

CREATE TABLE students (
    name VARCHAR(100),
    course VARCHAR(100)
);

The number inside VARCHAR represents the maximum length for the column definition.

10. DECIMAL Column

DECIMAL is useful for exact decimal values such as fees or prices.

CREATE TABLE fees (
    id INT,
    amount DECIMAL(10,2)
);

11. DATE Column

The DATE data type can be used to store calendar dates.

CREATE TABLE students (
    id INT,
    name VARCHAR(100),
    admission_date DATE
);

12. Primary Key

A primary key uniquely identifies each row in a table.

CREATE TABLE students (
    id INT PRIMARY KEY,
    name VARCHAR(100),
    course VARCHAR(100)
);

Here, id is the primary key.

13. NOT NULL

The NOT NULL constraint prevents a column from storing NULL values.

CREATE TABLE students (
    id INT PRIMARY KEY,
    name VARCHAR(100) NOT NULL
);

Every student record must have a value for the name column.

14. DEFAULT Value

A DEFAULT value is automatically used when a value is not supplied for the column.

CREATE TABLE students (
    id INT PRIMARY KEY,
    name VARCHAR(100),
    status VARCHAR(20) DEFAULT 'Active'
);

15. Multiple Constraints

A column can have more than one constraint.

CREATE TABLE students (
    id INT PRIMARY KEY AUTO_INCREMENT,
    name VARCHAR(100) NOT NULL,
    status VARCHAR(20) DEFAULT 'Active'
);

Here, the id column has both PRIMARY KEY and AUTO_INCREMENT.

16. AUTO_INCREMENT

AUTO_INCREMENT automatically generates a new numeric value for a column when a new row is inserted.

CREATE TABLE students (
    id INT PRIMARY KEY AUTO_INCREMENT,
    name VARCHAR(100)
);

17. CREATE TABLE IF NOT EXISTS

You can use IF NOT EXISTS to avoid an error when a table with the same name already exists.

CREATE TABLE IF NOT EXISTS students (
    id INT PRIMARY KEY,
    name VARCHAR(100)
);

18. Checking Tables

After creating a table, use SHOW TABLES to see the tables in the selected database.

SHOW TABLES;

The newly created table should appear in the result.

19. Checking Table Structure

Use DESCRIBE to inspect the table structure.

DESCRIBE students;

You can also use the shorter form:

DESC students;

20. Creating an Employees Table

CREATE TABLE employees (
    id INT PRIMARY KEY AUTO_INCREMENT,
    name VARCHAR(100) NOT NULL,
    email VARCHAR(150),
    salary DECIMAL(10,2)
);

This table stores basic employee information.

21. Creating a Products Table

CREATE TABLE products (
    id INT PRIMARY KEY AUTO_INCREMENT,
    product_name VARCHAR(150) NOT NULL,
    price DECIMAL(10,2),
    quantity INT
);

This structure can be used for a basic inventory or shopping application.

22. Creating a Courses Table

CREATE TABLE courses (
    id INT PRIMARY KEY AUTO_INCREMENT,
    course_name VARCHAR(100) NOT NULL,
    duration VARCHAR(50),
    fee DECIMAL(10,2)
);

This table can store course information.

23. Creating a Table with Date

CREATE TABLE admissions (
    id INT PRIMARY KEY AUTO_INCREMENT,
    student_name VARCHAR(100) NOT NULL,
    admission_date DATE,
    course VARCHAR(100)
);

The admission_date column can store the admission date.

24. Creating a Table with Multiple Data Types

CREATE TABLE students (
    id INT PRIMARY KEY AUTO_INCREMENT,
    name VARCHAR(100) NOT NULL,
    age INT,
    fee DECIMAL(10,2),
    admission_date DATE,
    active BOOLEAN
);

A table can contain columns using different data types.

25. Table Names with Database Name

You can specify the database name directly when creating a table.

CREATE TABLE school.students (
    id INT PRIMARY KEY,
    name VARCHAR(100)
);

This creates the students table inside the school database.

26. Common CREATE TABLE Errors

Common problems include:

  • No database selected
  • Table already exists
  • Invalid column definition
  • Incorrect data type
  • Missing comma between columns
  • Missing closing parenthesis
  • Insufficient privileges

27. Correct Comma Placement

Column definitions inside CREATE TABLE are separated by commas.

CREATE TABLE students (
    id INT,
    name VARCHAR(100),
    age INT
);

Do not forget the commas between column definitions.

28. Practical Table Creation Workflow

1. Create database
2. Select database
3. Plan table columns
4. Select suitable data types
5. Add primary key if required
6. Add constraints
7. Execute CREATE TABLE
8. Run SHOW TABLES
9. Run DESCRIBE table_name

29. Complete CREATE TABLE Example

CREATE DATABASE IF NOT EXISTS school;

USE school;

CREATE TABLE students (
    id INT PRIMARY KEY AUTO_INCREMENT,
    name VARCHAR(100) NOT NULL,
    mobile VARCHAR(15),
    course VARCHAR(100),
    fee DECIMAL(10,2),
    admission_date DATE
);

SHOW TABLES;

DESCRIBE students;

This example creates a database, selects it, creates a complete students table, and checks the table structure.

30. Practical Student Management Table

Let's create a practical student table for a training institute.

CREATE DATABASE IF NOT EXISTS training_db;

USE training_db;

CREATE TABLE students (
    id INT PRIMARY KEY AUTO_INCREMENT,
    student_name VARCHAR(100) NOT NULL,
    mobile VARCHAR(15) NOT NULL,
    email VARCHAR(150),
    course VARCHAR(100) NOT NULL,
    fee DECIMAL(10,2) DEFAULT 0.00,
    admission_date DATE,
    status VARCHAR(20) DEFAULT 'Active'
);

INSERT INTO students
(student_name, mobile, email, course, fee, admission_date)
VALUES
('Rahul Kumar', '9876543210', 'rahul@example.com',
 'Python Full Stack', 15000.00, '2026-09-21');

SELECT * FROM students;

This example demonstrates how CREATE TABLE can be used to build a practical table for a real-world application.

📌 Key Points

  • CREATE TABLE creates a new table.
  • The table name is specified after CREATE TABLE.
  • Each column normally has a name and data type.
  • PRIMARY KEY can uniquely identify records.
  • NOT NULL prevents NULL values in a column.
  • DEFAULT provides a default value.
  • AUTO_INCREMENT can generate numeric IDs automatically.
  • IF NOT EXISTS can prevent an error if the table already exists.
  • SHOW TABLES lists tables in the selected database.
  • DESCRIBE displays the structure of a table.

🧠 Quick Quiz

Question: Which statement is used to create a table in MySQL?