Academic Block

SQL CREATE DATABASE Get Certified

Learn how databases are created in different SQL systems and how SQLite handles database creation differently.

What is CREATE DATABASE?

The statement

CREATE DATABASE database_name;

creates a new database in systems such as MySQL, PostgreSQL and SQL Server. A database is the top-level container that holds tables, views, indexes and other objects.

Important for this tutorial: SQLite does not support the CREATE DATABASE statement. In SQLite a database is simply a file on disk. Opening a new file automatically creates the database. All interactive examples below therefore use SQLite-compatible SQL that can be run directly in the browser editor.

Traditional CREATE DATABASE Syntax

In database systems that support the statement the basic form is:

CREATE DATABASE school_db;

Some systems also allow:

CREATE DATABASE IF NOT EXISTS school_db;

These statements produce an error if executed in SQLite.

How SQLite Creates a Database

SQLite creates a database when a connection is opened to a file that does not yet exist. Once the database is open you create tables with the normal CREATE TABLE statement. The browser editor already provides an in-memory SQLite database, so the practical next step is to create tables.

Creating a Simple Table (SQLite Equivalent)

What this code does: Creates a students table, inserts three rows and returns them. This is the everyday equivalent of “starting a new database” in SQLite.

DROP TABLE IF EXISTS students;
CREATE TABLE students (
  student_id INTEGER,
  student_name TEXT,
  course TEXT
);
INSERT INTO students VALUES
  (1, 'Aarav', 'Computer Science'),
  (2, 'Meera', 'Data Science'),
  (3, 'Kabir', 'Web Development');
SELECT *
FROM students
ORDER BY student_id;

Creating a Table with Constraints

What this code does: Creates a products table that uses a primary key and a NOT NULL constraint, then inserts three products.

DROP TABLE IF EXISTS products;
CREATE TABLE products (
  product_id INTEGER PRIMARY KEY,
  product_name TEXT NOT NULL,
  price REAL
);
INSERT INTO products (product_name, price) VALUES
  ('Wireless Mouse', 899.00),
  ('USB Keyboard', 1299.00),
  ('Laptop Stand', 2199.00);
SELECT *
FROM products
ORDER BY product_id;

Creating Multiple Related Tables

A real database usually contains several tables linked by foreign keys.

What this code does: Creates an authors table and a books table with a foreign-key relationship, inserts sample data and joins the two tables.

DROP TABLE IF EXISTS books;
DROP TABLE IF EXISTS authors;
CREATE TABLE authors (
  author_id INTEGER PRIMARY KEY,
  author_name TEXT NOT NULL
);
CREATE TABLE books (
  book_id INTEGER PRIMARY KEY,
  title TEXT NOT NULL,
  author_id INTEGER,
  FOREIGN KEY (author_id) REFERENCES authors(author_id)
);
INSERT INTO authors VALUES
  (1, 'Ravi Kumar'),
  (2, 'Anita Sharma');
INSERT INTO books VALUES
  (101, 'Learning SQL', 1),
  (102, 'Database Fundamentals', 2),
  (103, 'SQL in Practice', 1);
SELECT
  books.title,
  authors.author_name
FROM books
JOIN authors
  ON books.author_id = authors.author_id
ORDER BY books.book_id;

Inspecting Objects with sqlite_master

SQLite stores metadata about tables, indexes and views in the system table sqlite_master.

What this code does: Creates a departments table and then lists every table currently present in the database.

DROP TABLE IF EXISTS departments;
CREATE TABLE departments (
  department_id INTEGER PRIMARY KEY,
  department_name TEXT
);
SELECT
  name,
  type
FROM sqlite_master
WHERE type = 'table'
ORDER BY name;

CREATE DATABASE Support Across Systems

Database System
CREATE DATABASE
How a Database is Created
MySQL
Supported
CREATE DATABASE db_name;
PostgreSQL
Supported
CREATE DATABASE db_name;
SQL Server
Supported
CREATE DATABASE db_name;
SQLite
Not supported
Open / create a database file

CREATE DATABASE vs CREATE TABLE

Statement
Purpose
SQLite Support
CREATE DATABASE
Creates a new database
No
CREATE TABLE
Creates a table inside a database
Yes

Important Points

  • CREATE DATABASE is the standard way to create a database in MySQL, PostgreSQL and SQL Server.
  • SQLite does not implement the statement; a database is a file that is created when it is first opened.
  • After a SQLite database is open you create objects with ordinary CREATE TABLE, CREATE INDEX, etc.
  • You can inspect existing tables by querying the system table sqlite_master.
  • Always consult the documentation of the particular database system you are using.

Best Practices

  • Choose clear, meaningful names for databases and tables.
  • Use consistent naming conventions across the whole project.
  • When the target system supports it, prefer CREATE DATABASE IF NOT EXISTS to avoid errors on re-runs.
  • Plan the tables and their relationships before loading application data.
  • Remember that SQLite users normally never need a CREATE DATABASE statement – they simply open a file and start creating tables.

Although the classic CREATE DATABASE statement is not available in SQLite, the practical equivalent is simply to open (or create) a database file and then use CREATE TABLE to define the structure you need. Click Test My Code on any example to run it live, or Code Explanation to walk through the query block by block.

Ctrl + Enter to run code in the editor Esc to close the editor

🧪 Test Your SQL Code

Edit the SQL code on the left and click “Run Code” to see the result on the right.

📝 SQL Code
👁️ Preview (query result)

Click “Run Code” to see the result here.