Tables are the foundational relational structures where all enterprise data resides. A table consists of vertical columns (each with a defined name, data type, and constraints) and horizontal rows of record data. In this class, we learn the complete syntax of CREATE TABLE, auto-incrementing ID generators, temporary tables, and DROP TABLE.
To create a table, specify the table name followed by a comma-separated list of column definitions enclosed in parentheses:
CREATE TABLE [IF NOT EXISTS] table_name (
column1_name data_type [constraints],
column2_name data_type [constraints],
...,
[table_level_constraints]
);
Let us construct a production-ready students table incorporating proper data types, primary keys, and default values:
CREATE TABLE IF NOT EXISTS students (
student_id INT AUTO_INCREMENT PRIMARY KEY,
first_name VARCHAR(50) NOT NULL,
last_name VARCHAR(50) NOT NULL,
email VARCHAR(100) NOT NULL UNIQUE,
course VARCHAR(30) DEFAULT 'BCA',
enrollment_date DATE NOT NULL,
gpa DECIMAL(3, 2) DEFAULT 0.00,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
• INT AUTO_INCREMENT PRIMARY KEY: Unique integer identifier that automatically generates consecutive IDs (1, 2, 3...) on every new row insert.
• NOT NULL: Enforces that a value must be supplied; empty NULL values are rejected.
• UNIQUE: Guarantees that no two students can share the same email address.
• DEFAULT 'BCA': If the user does not specify a course during insert, the database automatically supplies 'BCA'.
• TIMESTAMP DEFAULT CURRENT_TIMESTAMP: Automatically records the exact system date and time when the record is created.
In MySQL and Oracle, you can inspect the structural definition of any table with DESCRIBE (or DESC):
DESCRIBE students;
| Field | Type | Null | Key | Default | Extra |
|---|---|---|---|---|---|
| student_id | int | NO | PRI | NULL | auto_increment |
| first_name | varchar(50) | NO | NULL | ||
| varchar(100) | NO | UNI | NULL | ||
| course | varchar(30) | YES | BCA | ||
| gpa | decimal(3,2) | YES | 0.00 |
You can create a new table and populate it with data from an existing table in a single atomic step using CREATE TABLE AS SELECT (CTAS):
-- Create a dedicated table containing only BCA honors students
CREATE TABLE bca_honors AS
SELECT student_id, first_name, last_name, gpa
FROM students
WHERE course = 'BCA' AND gpa >= 3.80;
To permanently delete a table and all its stored rows from disk:
-- Safely remove obsolete table
DROP TABLE IF EXISTS old_backup_students;
Next, let us learn how to modify table structures without deleting existing data using ALTER TABLE!
Without IF NOT EXISTS, the database compiler throws an error (e.g., 'Table already exists'). Using CREATE TABLE IF NOT EXISTS instructs the database to gracefully bypass creation without throwing an exception.
In MySQL, use INT AUTO_INCREMENT PRIMARY KEY. In PostgreSQL, use SERIAL PRIMARY KEY or GENERATED ALWAYS AS IDENTITY. In SQLite, use INTEGER PRIMARY KEY AUTOINCREMENT. In SQL Server, use INT IDENTITY(1,1) PRIMARY KEY.
A temporary table (CREATE TEMPORARY TABLE) exists only for the duration of the current client connection or session. When the connection terminates or closes, the database automatically drops the temporary table and frees its memory.
Proceed to the next class in sequence to build production mastery of relational database architecture.
Continue to Class 10: ALTER TABLE Statement → Previous: ← CREATE DATABASE | Full Syllabus
Adding, renaming, and modifying columns on live tables.
Enforcing NOT NULL, UNIQUE, CHECK, and DEFAULT constraints.
Defining unique record identifiers and composite keys.
Establishing referential integrity with parent tables.