đī¸ The CREATE TABLE statement is a Data Definition Language (DDL) command used to create a new table in a database. A table stores data in the form of rows (records) and columns (fields), where each column has a specific data type and optional constraints.
đ What is CREATE TABLE?
Before storing any data, you must create a table that defines how the data will be organized. The CREATE TABLE statement specifies the table name, column names, data types, and constraints such as PRIMARY KEY, NOT NULL, and UNIQUE.
Information
đ¯ Why Use CREATE TABLE?
Tables provide a structured way to organize and store related information.
- đ Store application data in a structured format.
- đ Define column names and data types.
- đ Enforce data integrity using constraints.
- đ Build relationships between tables.
- đ Create the foundation for SQL queries and reports.
đ Basic Syntax
CREATE TABLE Syntax
CREATE TABLE table_name
(
column1 data_type,
column2 data_type,
column3 data_type
);đĄ Create a Simple Table
Create a table named Students with four columns.
Create Students Table
CREATE TABLE Students
(
StudentID INT,
Name VARCHAR(100),
Department VARCHAR(100),
Age INT
);The table is now ready to store student records.
đ Create a Table with Constraints
Constraints help enforce data integrity by restricting the type of data that can be stored.
Table with Constraints
CREATE TABLE Students
(
StudentID INT PRIMARY KEY,
Name VARCHAR(100) NOT NULL,
Department VARCHAR(100),
Age INT,
Email VARCHAR(150) UNIQUE
);Important
đ Common SQL Data Types
| Data Type | Description | Example |
|---|---|---|
| INT | Whole numbers. | 101 |
| VARCHAR(n) | Variable-length text. | Alice |
| CHAR(n) | Fixed-length text. | IN |
| DATE | Date values. | 2026-07-02 |
| DECIMAL(p,s) | Exact numeric values. | 1250.50 |
| BOOLEAN | True/False values (where supported). | TRUE |
đ Create a Table with Default Values
Default values are automatically assigned when no value is provided during an INSERT operation.
Default Values
CREATE TABLE Employees
(
EmployeeID INT PRIMARY KEY,
Name VARCHAR(100),
Department VARCHAR(50),
Salary DECIMAL(10,2),
Status VARCHAR(20) DEFAULT 'Active'
);đ Create a Table with a Foreign Key
Foreign keys establish relationships between tables and help maintain referential integrity.
Foreign Key Example
CREATE TABLE Enrollments
(
EnrollmentID INT PRIMARY KEY,
StudentID INT,
CourseID INT,
FOREIGN KEY (StudentID)
REFERENCES Students(StudentID)
);đĻ Create a Table from Another Table
Some database systems allow creating a new table directly from the result of a SELECT query.
CREATE TABLE AS SELECT
CREATE TABLE TopStudents AS
SELECT StudentID,
Name,
Department
FROM Students
WHERE Marks >= 90;Tip
âī¸ CREATE TABLE vs ALTER TABLE
| Feature | CREATE TABLE | ALTER TABLE |
|---|---|---|
| Purpose | Create a new table. | Modify an existing table. |
| Add Columns | During creation. | After creation. |
| Define Constraints | Yes. | Yes. |
| Requires Existing Table | â No | â Yes |
đŧ Real-World Example
A university is developing a Student Management System. The first step is to create a table that stores student information.
Student Management Table
CREATE TABLE Students
(
StudentID INT PRIMARY KEY,
Name VARCHAR(100) NOT NULL,
Department VARCHAR(100),
Email VARCHAR(150) UNIQUE,
AdmissionDate DATE
);Once the table is created, student records can be inserted, updated, queried, and deleted as needed.
â ī¸ Common Mistakes
- â Choosing inappropriate data types.
- â Forgetting to define a primary key.
- â Using duplicate column names.
- â Selecting column sizes that are too small or unnecessarily large.
- â Omitting important constraints such as NOT NULL or UNIQUE.
Warning
â ī¸ Best Practices
Best Practice
đ Key Points to Remember
- đ CREATE TABLE creates a new database table.
- đ Every column must have a data type.
- đ Constraints help ensure data accuracy and consistency.
- đ Foreign keys establish relationships between tables.
- đ Table design has a significant impact on database performance and maintainability.
- đ Different SQL databases may support additional table creation options.