Physical Design   «Prev  Next»

Lesson 12 Using SQL to Create a Database
Objective Explain how to create a database with SQL.

Using SQL to Create a Database

Creating a database with SQL is a core part of the physical implementation stage of the database life cycle (DBLC). After designing the schema during conceptual and logical modeling, the next step is to use SQL statements to create the physical structures—schemas, tables, indexes, constraints, and supporting objects—that will store and manage your data.

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.

1. Finalize the Database Schema

Before writing SQL, the database schema must be finalized. This includes identifying:

  • Tables and their attributes
  • Primary and foreign keys
  • Data types that match business and performance requirements
  • Indexes required for efficient querying
  • Views, constraints, triggers, and stored procedures

A complete logical design ensures that SQL statements accurately reflect the intended structure.

2. Connect to the Database Server

Once your DBMS is installed (Oracle, SQL Server, PostgreSQL, etc.), connect to it using:

  • A command-line client (e.g., psql, sqlcmd, mysql)
  • A graphical tool (e.g., SQL Server Management Studio, pgAdmin, Oracle SQL Developer)
  • Programmatic connectors (JDBC, Python drivers, etc.)

Authentication typically involves specifying a username, password, host, and database instance or service.


Database Design for Mere Mortals

3. Create a Database or Schema

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.

4. Select the Working Schema / Database

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.

5. Create Tables with SQL

The CREATE TABLE statement defines the structure of each table. Modern best practices include:

  • Using meaningful data types (e.g., VARCHAR2 vs VARCHAR)
  • Defining primary keys explicitly
  • Using GENERATED identity columns instead of manual sequences when supported
  • Adding foreign keys to preserve referential integrity
  • Including NOT NULL constraints where applicable

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)
);
  

6. Create Indexes to Improve Performance

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.

7. Create Additional Database Objects

Depending on business needs, you may also create:

  • Views for reusable query logic
  • Stored procedures to encapsulate operations
  • Triggers to enforce rules automatically
  • Materialized views for performance

Modern systems such as Oracle 23ai also support advanced features like auto-partitioning, hybrid tables, JSON-based relational integration, and AI-assisted SQL optimizations.

8. Verify and Document the Database

A good implementation concludes with:

  • Schema validation
  • Permission assignments
  • Backup/testing routines
  • Documentation of objects and dependencies

These steps ensure the database is stable and ready for production.

Fourth Stage of the DBLC: Implementation

At this point in the DBLC, you move from design to construction. Using SQL, you create the schema, define tables, enforce integrity, and prepare the structure that will store real data. The next lesson explains how to build the tables that make up your physical data model.

[1] implementation: Using SQL to create a database; the fourth step in the database life cycle.
[2] SQL: Structured Query Language, the universal language for creating, updating, and querying relational data.

SEMrush Software 12 SEMrush Banner 12