Skip to content

CREATE DATABASE Statement πŸ—οΈ

Mentor's Note: Before you can build a table, you need a house for it. The Database is that house. Creating it is the very first step in any project. πŸ’‘


🌟 The Scenario: The Empty Plot πŸ—ΊοΈ

Imagine you just bought a piece of land. - Currently, it's just "System Space". - You need to officially mark it as "My Office" or "My Home". - Once marked, you can start building rooms (tables) inside it.


πŸ’» 1. The Basic Syntax

CREATE DATABASE database_name;

Example: Creating a School Database

CREATE DATABASE school_db;

πŸ’» 2. Advanced Creation (With Checks)

It's good practice to check if a database already exists before trying to create it, to avoid errors.

-- βœ… Safe Creation
CREATE DATABASE IF NOT EXISTS school_db;
-- ⚠️ T-SQL Logic
IF NOT EXISTS(SELECT * FROM sys.databases WHERE name = 'school_db')
BEGIN
    CREATE DATABASE school_db;
END

πŸ’» 3. Selecting the Database (USE)

Once created, you must "enter" the house to start working.

USE school_db;

(Note: PostgreSQL uses \c school_db in the command line tool, but USE is standard for most GUI clients).


πŸ’‘ Best Practices for Naming

  1. Lowercase: school_db (Avoids OS case-sensitivity issues).
  2. Underscores: my_app_db (Easier to read than myappdb).
  3. Descriptive: ecommerce_prod vs db1.

πŸ“ˆ Learning Path