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.
- MySQL / PostgreSQL
- SQL Server
-- β
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β
- Lowercase:
school_db(Avoids OS case-sensitivity issues). - Underscores:
my_app_db(Easier to read thanmyappdb). - Descriptive:
ecommerce_prodvsdb1.