SQL Topics
Create Table
title: Create Table
After creating a database, the next step is creating tables. A database by itself is simply a container. The actual information is stored inside tables.
Think of a database as a filing cabinet and tables as folders inside that cabinet. Each folder stores a specific type of information. For example, a school database may contain separate tables for students, teachers, courses, attendance records, and examination results.
Tables are the foundation of every relational database. Without tables, there is no place to store, organize, or retrieve data. This is why learning how to create tables correctly is one of the most important skills for database developers and administrators.
In this lesson, you will learn what tables are, why they are important, how the CREATE TABLE statement works, how to define columns and data types, and best practices used in professional database design.
What is a Table?
A table is a structured collection of related data organized into rows and columns.
For example:
| StudentID | Name | Age |
|---|---|---|
| 1 | Rahul | 20 |
| 2 | Priya | 21 |
| 3 | Amit | 19 |
In this table:
- Each row represents a record.
- Each column represents a field or attribute.
- The entire structure represents a table.
Tables help organize information in a logical and manageable format.
Why Are Tables Important?
Tables provide structure to data.
Without tables:
Data
Data
Data
DataInformation becomes difficult to manage.
With tables:
Students Table
Teachers Table
Courses Table
Attendance TableData becomes organized and easy to access.
Benefits include:
- Better organization
- Faster querying
- Easier maintenance
- Reduced duplication
- Improved scalability
Almost every database application relies on well-designed tables.
SQL CREATE TABLE Statement
The SQL statement used to create a table is:
CREATE TABLE TableName (
Column1 DataType,
Column2 DataType,
Column3 DataType
);Example:
CREATE TABLE Students (
StudentID INT,
Name VARCHAR(100),
Age INT
);This creates a table named Students with three columns.
Understanding the Syntax
Let's analyze the statement:
CREATE TABLE Students (
StudentID INT,
Name VARCHAR(100),
Age INT
);CREATE TABLE
Tells SQL to create a new table.
Students
Represents the table name.
StudentID
Column name.
INT
Data type for storing integers.
Name
Column name for student names.
VARCHAR(100)
Stores text up to 100 characters.
Age
Stores student age.
Parentheses ()
Contain column definitions.
Commas
Separate column definitions.
Creating Your First Table
Suppose you are building a school management system.
Create a Students table:
CREATE TABLE Students (
StudentID INT,
Name VARCHAR(100),
Age INT
);After execution:
The table now exists but contains no data.
Viewing Created Tables
To verify table creation:
SHOW TABLES;Output:
StudentsThis confirms successful creation.
Understanding Columns
Columns define the type of information stored in a table.
Example:
CREATE TABLE Students (
StudentID INT,
Name VARCHAR(100),
Age INT
);Columns:
| Column | Purpose |
|---|---|
| StudentID | Stores student IDs |
| Name | Stores student names |
| Age | Stores student ages |
Each column has a specific purpose.
Understanding Rows
Rows store individual records.
Example:
| StudentID | Name | Age |
|---|---|---|
| 1 | Rahul | 20 |
| 2 | Priya | 21 |
Each row represents one student.
Rows are added using INSERT statements.
Choosing Data Types
Every column requires a data type.
Common data types:
INT
Stores whole numbers.
Example:
StudentID INTVARCHAR
Stores text.
Example:
Name VARCHAR(100)DATE
Stores dates.
Example:
BirthDate DATEDECIMAL
Stores precise numeric values.
Example:
Salary DECIMAL(10,2)Choosing appropriate data types improves performance and storage efficiency.
Creating a Table with Constraints
Professional databases use constraints to enforce rules.
Example:
CREATE TABLE Students (
StudentID INT PRIMARY KEY,
Name VARCHAR(100) NOT NULL,
Age INT CHECK (Age > 0)
);Here:
- StudentID must be unique.
- Name cannot be empty.
- Age must be greater than zero.
Constraints improve data quality.
Using PRIMARY KEY
Every table should have a way to uniquely identify records.
Example:
CREATE TABLE Students (
StudentID INT PRIMARY KEY,
Name VARCHAR(100)
);Benefits:
- Prevents duplicate IDs
- Improves performance
- Supports relationships
Using NOT NULL
Some fields should never be empty.
Example:
CREATE TABLE Students (
StudentID INT,
Name VARCHAR(100) NOT NULL
);This ensures every student has a name.
Using DEFAULT Values
Default values are inserted automatically when no value is provided.
Example:
CREATE TABLE Users (
UserID INT,
Status VARCHAR(20) DEFAULT 'Active'
);If Status is omitted:
Activeis automatically stored.
Creating Multiple Tables
Real applications contain many tables.
Example:
CREATE TABLE Teachers (
TeacherID INT PRIMARY KEY,
TeacherName VARCHAR(100)
);CREATE TABLE Courses (
CourseID INT PRIMARY KEY,
CourseName VARCHAR(100)
);Database structure:
Table Naming Conventions
Good table names improve readability.
Good Examples
Students
Teachers
Employees
Products
OrdersPoor Examples
tbl1
data
abc
xChoose names that clearly describe the table contents.
Common Mistakes Beginners Make
Missing Data Types
Wrong:
CREATE TABLE Students (
StudentID,
Name
);Correct:
CREATE TABLE Students (
StudentID INT,
Name VARCHAR(100)
);Missing Commas
Wrong:
CREATE TABLE Students (
StudentID INT
Name VARCHAR(100)
);Correct:
CREATE TABLE Students (
StudentID INT,
Name VARCHAR(100)
);Duplicate Column Names
Wrong:
CREATE TABLE Students (
Name VARCHAR(100),
Name VARCHAR(50)
);Column names must be unique.
Poor Naming Practices
Avoid unclear names such as:
x
abc
data1Use meaningful names.
Real-World Example
Imagine building an e-commerce application.
You may create:
Customers Table
Stores customer information.
Products Table
Stores product details.
Orders Table
Stores customer orders.
Payments Table
Stores transaction records.
Example:
CREATE TABLE Products (
ProductID INT PRIMARY KEY,
ProductName VARCHAR(200),
Price DECIMAL(10,2)
);This structure provides a solid foundation for managing product information.
Best Practices
Use Meaningful Table Names
Names should describe the data.
Always Define Primary Keys
Every important table should have a unique identifier.
Choose Appropriate Data Types
Avoid using overly large data types.
Use Constraints
Protect data quality with validation rules.
Plan Before Creating
Understand future requirements before designing tables.
Follow Naming Standards
Consistency improves maintainability.
Interview Questions
What is a table in SQL?
A table is a collection of related data organized into rows and columns.
Which SQL statement creates a table?
CREATE TABLEWhy are data types required?
They define the type of information each column can store.
What is a PRIMARY KEY?
A PRIMARY KEY uniquely identifies each record in a table.
Summary
Tables are the foundation of relational databases. They provide a structured way to store and organize information using rows and columns.
In this lesson, you learned:
- What tables are
- Why tables are important
- CREATE TABLE syntax
- Columns and rows
- Data types
- Constraints
- Naming conventions
- Best practices
- Common mistakes
Understanding table creation is essential because every database application relies on properly designed tables.
Next Step
Continue to the next lesson:
ALTER TABLE →
Exam Focus
Revise definitions, diagrams, examples, and short-answer points for Create Table.
Interview Use
Prepare one clear explanation, one practical example, and one common mistake for this SQL topic.
Search Terms
sql, sql complete guide, sql tutorial, sql notes, complete, guide, table, operations
Related SQL Topics