Tables are the most important objects in a relational database. A database stores data in the form of rows and columns, and these rows and columns are organized inside tables. Before inserting any data into a database, we must first create a table structure that defines what type of information will be stored.
The SQL CREATE TABLE statement is used to create new tables inside a database. During table creation, we define column names, data types, constraints, keys, and other rules that control how data will be stored and managed.
Whether you are developing a school management system, hospital database, banking application, e-commerce platform, or inventory management software, understanding table creation is essential because every database begins with properly designed tables.
A table is a collection of related data organized into rows and columns.
For example, a Student table may contain the following columns:
| StudentID | Name | Course | Age |
|---|---|---|---|
| 101 | Rahul | B.Tech | 20 |
| 102 | Priya | BCA | 19 |
In this example, StudentID, Name, Course, and Age are columns, while each student entry represents a row.
Tables provide a structured way to store and organize data.
The CREATE TABLE statement is used to create a new table inside an existing database.
CREATE TABLE TableName ( Column1 DataType, Column2 DataType, Column3 DataType );
CREATE TABLE Student ( StudentID INT, StudentName VARCHAR(100), Course VARCHAR(50) );
This statement creates a Student table containing three columns.
| Component | Description |
|---|---|
| Table Name | Name assigned to the table |
| Column Name | Name of a field in the table |
| Data Type | Type of data stored in a column |
| Constraints | Rules applied to data |
Each column must be assigned a suitable data type.
| Data Type | Purpose |
|---|---|
| INT | Whole numbers |
| VARCHAR | Variable length text |
| CHAR | Fixed length text |
| DATE | Date values |
| DECIMAL | Financial values |
| BOOLEAN | True or False values |
CREATE TABLE Student ( StudentID INT, StudentName VARCHAR(100), Age INT, Course VARCHAR(50) );
This table stores basic student information.
Constraints are rules that ensure data accuracy and integrity.
CREATE TABLE Student ( StudentID INT, StudentName VARCHAR(100) NOT NULL );
StudentName cannot be left empty.
CREATE TABLE Student ( Email VARCHAR(100) UNIQUE );
Duplicate email addresses are not allowed.
A Primary Key uniquely identifies each record in a table.
CREATE TABLE Student ( StudentID INT PRIMARY KEY, StudentName VARCHAR(100) );
Foreign Keys establish relationships between tables.
CREATE TABLE Department ( DepartmentID INT PRIMARY KEY, DepartmentName VARCHAR(100) ); CREATE TABLE Employee ( EmployeeID INT PRIMARY KEY, EmployeeName VARCHAR(100), DepartmentID INT, FOREIGN KEY (DepartmentID) REFERENCES Department(DepartmentID) );
The DepartmentID in Employee references the Department table.
CHECK validates data before insertion.
CREATE TABLE Student ( StudentID INT PRIMARY KEY, Age INT CHECK (Age >= 18) );
Only students aged 18 or older can be inserted.
CREATE TABLE Employee ( EmployeeID INT PRIMARY KEY, Status VARCHAR(20) DEFAULT 'Active' );
If no value is supplied, Active becomes the default status.
CREATE TABLE Student ( StudentID INT PRIMARY KEY, StudentName VARCHAR(100) NOT NULL, Email VARCHAR(100) UNIQUE, Age INT CHECK (Age >= 18), Course VARCHAR(50) DEFAULT 'B.Tech' );
This example combines several constraints in a single table.
A composite key consists of multiple columns.
CREATE TABLE Enrollment ( StudentID INT, CourseID INT, PRIMARY KEY (StudentID, CourseID) );
The combination uniquely identifies each enrollment record.
CREATE TABLE Books ( BookID INT PRIMARY KEY, BookTitle VARCHAR(150), Author VARCHAR(100), Price DECIMAL(8,2) );
CREATE TABLE Patient ( PatientID INT PRIMARY KEY, PatientName VARCHAR(100), Gender CHAR(1), AdmissionDate DATE );
CREATE TABLE Product ( ProductID INT PRIMARY KEY, ProductName VARCHAR(100), Price DECIMAL(10,2), Stock INT );
| Good Name | Poor Name |
|---|---|
| Student | Tbl1 |
| Employee | Data123 |
| Department | TestTable |
CREATE TABLE is used to create a new table inside a database where data can be stored in rows and columns.
CREATE TABLE TableName ( Column1 DataType, Column2 DataType );
It uniquely identifies each record and prevents duplicate entries.
No. A table can have only one Primary Key, although it may consist of multiple columns.
A Composite Key is a Primary Key made up of two or more columns.
VARCHAR stores variable-length strings, while CHAR stores fixed-length strings.
A Foreign Key creates a relationship between two tables and references a Primary Key.
Constraints enforce rules on data and maintain integrity.
The database generates an error because the column requires a value.
Meaningful names improve readability, maintenance, and collaboration among developers.
CREATE TABLE Student ( StudentID INT PRIMARY KEY, StudentName VARCHAR(100) NOT NULL, Email VARCHAR(100) UNIQUE, Age INT CHECK (Age >= 18), Course VARCHAR(50), AdmissionDate DATE, Status VARCHAR(20) DEFAULT 'Active' );
This example demonstrates a professionally designed table using data types, keys, and constraints together.
The CREATE TABLE statement is one of the most frequently used SQL commands because every database application relies on properly designed tables. Tables define how information is stored, validated, and organized inside a database.
By understanding table creation, data types, constraints, keys, naming conventions, and best practices, developers can build reliable, efficient, and scalable database systems. A strong understanding of CREATE TABLE forms the foundation for advanced SQL concepts such as relationships, indexing, normalization, stored procedures, and database optimization.