🏗️ CREATE Command in SQL
Last Updated: January 2026
The CREATE command is a DDL (Data Definition Language) command used to create new database objects.
- Defines structure
- Allocates space
- Makes the object available for use
- Changes are permanent (auto-commit)
Using CREATE, you can create:
- Tables
- Databases
- Views
- Indexes
Hinglish Tip 🗣: Jab database ya table pehli baar banana ho, tab CREATE command use hota hai.
CREATE DATABASE
Creates a new database.
-- 1ST Way
CREATE DATABASE database_name;
-- 2ND Way (Recommended)
CREATE DATABASE IF NOT EXISTS database_name;
CREATE DATABASE IF NOT EXISTS company_db;
After this, company_db exists, but it has no tables yet.
CREATE TABLE
Creates a new table inside a database.
-- 1st Way
CREATE TABLE table_name (
column_name datatype,
column_name datatype
);
-- 2nd Way (Recommended)
CREATE TABLE IF NOT EXISTS table_name (
column_name datatype,
column_name datatype
);
Example:
CREATE TABLE IF NOT EXISTS employees (
emp_id INT PRIMARY KEY AUTO_INCREMENT,
emp_name VARCHAR(50),
salary INT NOT NULL,
join_date DATE DEFAULT CURRENT_DATE
is_active BOOLEAN DEFAULT TRUE
);
Relationship Table Example
CREATE TABLE IF NOT EXISTS orders (
order_id INT PRIMARY KEY,
customer_id INT NOT NULL,
FOREIGN KEY fk_orders_customers (customer_id) REFERENCES customers(customer_id)
On Update CASCADE On Delete CASCADE
);
- Primary Key: order_id'
- Foreign Key: customer_id
- Foreign Key Constraint: fk_orders_customers Naming Convention is fk_table_name_foreign_key_name
- Foreign Key Constraint: On Update CASCADE On Delete CASCADE Naming Convention is On Update {action} On Delete {action}
- {action} can be NO ACTION, RESTRICT, SET NULL, SET DEFAULT, CASCADE
- NO ACTION is the default value means no action is taken when the referenced key is updated or deleted.
- RESTRICT means the operation is cancelled if the referenced key is updated or deleted.
- SET NULL means the foreign key is set to NULL if the referenced key is updated or deleted.
- SET DEFAULT means the foreign key is set to the default value if the referenced key is updated or deleted.
- CASCADE means the operation is performed on the referenced key if the referenced key is updated or deleted.
🔑 Important Points About CREATE TABLE
- Column order matters
- Data types must be valid
- Constraints can be added
- Table is empty after creation
