| Lesson 12 | Using SQL to Create a Database |
| Objective | Explain how to create a database with SQL. |
Although each relational database management system (RDBMS) has its own extensions and administrative tools, the essential SQL workflow is consistent across modern platforms such as Oracle Database 23ai, PostgreSQL, SQL Server 2022, and MySQL. This lesson walks through the standard process of creating a database with SQL and highlights best practices used in current systems.
Before writing SQL, the database schema must be finalized. This includes identifying:
A complete logical design ensures that SQL statements accurately reflect the intended structure.
Once your DBMS is installed (Oracle, SQL Server, PostgreSQL, etc.), connect to it using:
psql, sqlcmd, mysql)Authentication typically involves specifying a username, password, host, and database instance or service.
In most SQL implementations, the schema is the logical container that holds tables and other objects. The exact command varies by RDBMS:
Generic SQL:
CREATE SCHEMA StoriesOnCDOrders;
MySQL / MariaDB:
CREATE DATABASE StoriesOnCDOrders;
SQL Server:
CREATE DATABASE StoriesOnCDOrders;
GO
CREATE SCHEMA Orders AUTHORIZATION dbo;
Oracle (multitenant-aware):
CREATE USER stories IDENTIFIED BY password;
GRANT CONNECT, RESOURCE TO stories;
ALTER SESSION SET CURRENT_SCHEMA = stories;
The terminology changes slightly between systems, but the principle is the same: create a logical container for tables and objects.
To create objects inside the correct container, set your active schema:
SET SCHEMA StoriesOnCDOrders;
Or when using systems like MySQL:
USE StoriesOnCDOrders;
From this point forward, all tables, constraints, and indexes will be created inside the selected schema.
The CREATE TABLE statement defines the structure of each table. Modern best practices include:
VARCHAR2 vs VARCHAR)GENERATED identity columns instead of manual sequences when supported
CREATE TABLE Orders (
OrderID INT GENERATED ALWAYS AS IDENTITY,
CustomerID INT NOT NULL,
OrderDate DATE NOT NULL,
Amount DECIMAL(10,2),
PRIMARY KEY (OrderID),
FOREIGN KEY (CustomerID) REFERENCES Customers(CustomerID)
);
Indexes help the DBMS locate rows quickly. You create indexes using:
CREATE INDEX idx_orders_customer
ON Orders(CustomerID);
Indexing strategies vary by workload, so this step should be guided by real usage patterns.
Depending on business needs, you may also create:
Modern systems such as Oracle 23ai also support advanced features like auto-partitioning, hybrid tables, JSON-based relational integration, and AI-assisted SQL optimizations.
A good implementation concludes with:
These steps ensure the database is stable and ready for production.