Lesson 13 of 60 – CREATE TABLE
22%

CREATE TABLE in SQL

The CREATE TABLE statement is used to create a new table inside a database. A table stores data in the form of rows and columns.

Note: Before creating a table, you should select the database in which you want to create the table.

1. What is a Table?

A table is a structure used to store related information in a database. It contains columns and rows.

Students

ID    Name       Age
1     Rahul      20
2     Priya      21
3     Amit       19

Here, ID, Name, and Age are columns and each student record is a row.

2. CREATE TABLE Statement

The basic syntax for creating a table is:

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

You must provide a table name, column names, and appropriate data types.

3. Create a Students Table

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

This creates a table named Students with three columns: id, name, and age.

4. Selecting a Database

Use the USE statement to select a database before creating a table.

USE school;

After this statement, tables will be created inside the school database.

5. Creating a Database and Table

You can first create a database and then create a table inside it.

CREATE DATABASE school;

USE school;

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

6. Column Names

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

CREATE TABLE Students (
    student_id INT,
    student_name VARCHAR(100),
    mobile VARCHAR(15),
    age INT
);

Here, student_id, student_name, mobile, and age are column names.

7. INT Data Type

The INT data type is commonly used for whole numbers.

CREATE TABLE Students (
    id INT,
    age INT
);

Examples of integer values are:

10
25
100
500

8. VARCHAR Data Type

VARCHAR is used to store variable-length text.

CREATE TABLE Students (
    name VARCHAR(100)
);

The number inside the brackets specifies the maximum length.

9. DATE Data Type

The DATE data type is used to store dates.

CREATE TABLE Students (
    admission_date DATE
);

A date can be stored in the format:

2026-09-20

10. DECIMAL Data Type

The DECIMAL data type is useful for values that require decimal precision, such as fees and prices.

CREATE TABLE Courses (
    course_name VARCHAR(100),
    fee DECIMAL(10,2)
);

For example:

5000.00
12500.50

11. Multiple Columns

A table can contain many columns.

CREATE TABLE Employees (
    id INT,
    name VARCHAR(100),
    department VARCHAR(50),
    salary DECIMAL(10,2),
    joining_date DATE
);

12. PRIMARY KEY

A primary key uniquely identifies each record in a table.

CREATE TABLE Students (
    id INT PRIMARY KEY,
    name VARCHAR(100),
    age INT
);

The value of id should be unique for every student.

13. AUTO_INCREMENT

In MySQL, AUTO_INCREMENT can automatically generate a new numeric ID.

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

When a new record is inserted, the ID can be generated automatically.

14. NOT NULL

NOT NULL means that a column cannot contain a NULL value.

CREATE TABLE Students (
    id INT PRIMARY KEY AUTO_INCREMENT,
    name VARCHAR(100) NOT NULL,
    age INT
);

The student name must be provided when inserting a record.

15. DEFAULT Value

The DEFAULT keyword provides a value automatically when no value is supplied.

CREATE TABLE Students (
    id INT PRIMARY KEY AUTO_INCREMENT,
    name VARCHAR(100) NOT NULL,
    city VARCHAR(50) DEFAULT 'Aurangabad'
);

16. UNIQUE Constraint

The UNIQUE constraint ensures that values in a column are not duplicated.

CREATE TABLE Students (
    id INT PRIMARY KEY AUTO_INCREMENT,
    name VARCHAR(100),
    email VARCHAR(150) UNIQUE
);

17. Complete Student Table

Here is a practical example of a student table:

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

18. Employee Table Example

CREATE TABLE Employees (
    employee_id INT PRIMARY KEY AUTO_INCREMENT,
    employee_name VARCHAR(100) NOT NULL,
    department VARCHAR(50),
    salary DECIMAL(10,2),
    joining_date DATE
);

19. Course Table Example

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

20. Checking Tables

After creating a table, you can use SHOW TABLES to see the tables inside the selected database.

SHOW TABLES;

21. Viewing Table Structure

The DESCRIBE statement can be used to view the structure of a table.

DESCRIBE Students;

You can also use:

DESC Students;

22. IF NOT EXISTS

The IF NOT EXISTS option prevents an error if the table already exists.

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

23. Table Naming Rules

  • Use meaningful table names.
  • Avoid unnecessary spaces in names.
  • Use consistent naming conventions.
  • Do not use SQL reserved words as names.
  • Keep names easy to understand.

Example:

Students
Student_Courses
Employee_Details

24. Common Mistake

Every column definition should normally contain a column name and a data type.

Incorrect:

CREATE TABLE Students (
    id,
    name
);

Correct:

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

25. CREATE TABLE with Constraints

CREATE TABLE Students (
    id INT PRIMARY KEY AUTO_INCREMENT,
    name VARCHAR(100) NOT NULL,
    email VARCHAR(150) UNIQUE,
    age INT,
    city VARCHAR(50) DEFAULT 'Aurangabad'
);

This example combines several concepts such as PRIMARY KEY, AUTO_INCREMENT, NOT NULL, UNIQUE, and DEFAULT.

26. CREATE TABLE in Real Projects

In real-world applications, tables are created for different types of data.

  • Students
  • Employees
  • Customers
  • Products
  • Orders
  • Courses
  • Payments
  • Attendance

Good table design makes database applications easier to manage.

📌 Key Points

  • CREATE TABLE is used to create a new table.
  • A table contains rows and columns.
  • Every column should have a suitable data type.
  • INT is commonly used for whole numbers.
  • VARCHAR is commonly used for text.
  • DATE is used to store dates.
  • DECIMAL is useful for precise numeric values.
  • PRIMARY KEY uniquely identifies records.
  • AUTO_INCREMENT can automatically generate IDs in MySQL.
  • NOT NULL prevents NULL values.
  • UNIQUE prevents duplicate values.
  • DEFAULT provides a default value.

🧠 Quick Quiz

Question: Which SQL statement is used to create a new table?