🏗️ 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_dbexists, 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_customersNaming Convention isfk_table_name_foreign_key_name - Foreign Key Constraint:
On Update CASCADE On Delete CASCADENaming Convention isOn Update {action} On Delete {action} {action}can beNO ACTION,RESTRICT,SET NULL,SET DEFAULT,CASCADENO ACTIONis the default value means no action is taken when the referenced key is updated or deleted.RESTRICTmeans the operation is cancelled if the referenced key is updated or deleted.SET NULLmeans the foreign key is set to NULL if the referenced key is updated or deleted.SET DEFAULTmeans the foreign key is set to the default value if the referenced key is updated or deleted.CASCADEmeans 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