Lesson 2 | Writing the CREATE DATABASE command |
Objective | The correct command to create database in Oracle 23c |
Writing the CREATE DATABASE
command
The process of creating a new database in Oracle Database involves starting an Oracle instance and executing a `CREATE DATABASE` command. Below is a structured breakdown of the key steps and syntax for this operation:
-
Prepare the Environment
- Ensure Oracle software is installed.
- Set up necessary environment variables (
ORACLE_HOME
, ORACLE_SID
).
- Start the Oracle instance in NOMOUNT mode (no control files or datafiles are opened yet).
STARTUP NOMOUNT;
-
Write the
CREATE DATABASE
Command
- Store the command in a script file (e.g.,
create_db.sql
) for execution.
- Basic syntax:
CREATE DATABASE database_name
USER SYS IDENTIFIED BY password
USER SYSTEM IDENTIFIED BY password
LOGFILE GROUP 1 ('/path/to/redo01.log') SIZE 100M,
GROUP 2 ('/path/to/redo02.log') SIZE 100M
MAXLOGFILES 5
MAXLOGMEMBERS 5
MAXLOGHISTORY 100
MAXDATAFILES 100
CHARACTER SET AL32UTF8
NATIONAL CHARACTER SET AL16UTF16
EXTENT MANAGEMENT LOCAL
DATAFILE '/path/to/system01.dbf' SIZE 500M AUTOEXTEND ON
SYSAUX DATAFILE '/path/to/sysaux01.dbf' SIZE 500M AUTOEXTEND ON
DEFAULT TABLESPACE users
DATAFILE '/path/to/users01.dbf' SIZE 100M AUTOEXTEND ON
DEFAULT TEMPORARY TABLESPACE temp
TEMPFILE '/path/to/temp01.dbf' SIZE 100M AUTOEXTEND ON
UNDO TABLESPACE undotbs
DATAFILE '/path/to/undotbs01.dbf' SIZE 200M AUTOEXTEND ON;
- Key Clauses Explained
USER SYS/SYSTEM
: Sets passwords for admin accounts.
LOGFILE
: Defines redo log groups for transaction recovery.
CHARACTER SET
: Specifies database encoding (e.g., AL32UTF8
for Unicode).
DATAFILE
: Creates the system tablespace (required) and optional tablespaces (e.g., SYSAUX
, UNDO
).
DEFAULT TABLESPACE
: Assigns a default storage location for user data.
-
Execute the Script
- Run the script from SQL*Plus or Oracle SQL Developer:
@/path/to/create_db.sql
Post-Creation Steps
Example Minimal Command
For a test database named `ORCL`:
CREATE DATABASE ORCL
USER SYS IDENTIFIED BY sys_password
USER SYSTEM IDENTIFIED BY system_password
LOGFILE GROUP 1 ('/u01/oradata/ORCL/redo01.log') SIZE 50M
CHARACTER SET AL32UTF8
DATAFILE '/u01/oradata/ORCL/system01.dbf' SIZE 300M
EXTENT MANAGEMENT LOCAL;
Important Notes
- Permissions: Requires
SYSDBA
privileges.
- File Paths: Replace
/path/to/
with actual directories (Oracle must have write access).
- Backup: Always back up existing databases before creating new ones.
Oracle 23c Multitenant CREATE DATABASE Script
Here is a complete `CREATE DATABASE` script for setting up an Oracle 23c
multitenant architecture (CDB). This example assumes you're creating a Container Database (CDB) with Oracle Managed Files (OMF) and enabling the use of Pluggable Databases (PDBs):
-- Step 1: Start the instance in NOMOUNT state
STARTUP NOMOUNT;
-- Step 2: Create the Container Database (CDB)
CREATE DATABASE cdb23c
USER SYS IDENTIFIED BY StrongSysPassword1
USER SYSTEM IDENTIFIED BY StrongSysPassword2
ENABLE PLUGGABLE DATABASE
SEED FILE_NAME_CONVERT = ('/u01/app/oracle/oradata/cdb23c',
'/u01/app/oracle/oradata/cdb23c/pdbseed')
LOGFILE GROUP 1 ('/u01/app/oracle/oradata/cdb23c/redo01.log') SIZE 200M,
GROUP 2 ('/u01/app/oracle/oradata/cdb23c/redo02.log') SIZE 200M,
GROUP 3 ('/u01/app/oracle/oradata/cdb23c/redo03.log') SIZE 200M
CHARACTER SET AL32UTF8
NATIONAL CHARACTER SET AL16UTF16
EXTENT MANAGEMENT LOCAL
DATAFILE '/u01/app/oracle/oradata/cdb23c/system01.dbf' SIZE 700M AUTOEXTEND ON NEXT 10M MAXSIZE UNLIMITED
SYSAUX DATAFILE '/u01/app/oracle/oradata/cdb23c/sysaux01.dbf' SIZE 500M AUTOEXTEND ON NEXT 10M MAXSIZE UNLIMITED
DEFAULT TABLESPACE users
DATAFILE '/u01/app/oracle/oradata/cdb23c/users01.dbf' SIZE 100M AUTOEXTEND ON NEXT 10M MAXSIZE UNLIMITED
DEFAULT TEMPORARY TABLESPACE temp
TEMPFILE '/u01/app/oracle/oradata/cdb23c/temp01.dbf' SIZE 100M AUTOEXTEND ON NEXT 10M MAXSIZE UNLIMITED
UNDO TABLESPACE undotbs1
DATAFILE '/u01/app/oracle/oradata/cdb23c/undotbs01.dbf' SIZE 200M AUTOEXTEND ON NEXT 10M MAXSIZE UNLIMITED;
π§ Post-Creation Steps (after CREATE DATABASE)
-- Step 3: Run necessary scripts as SYSDBA
@?/rdbms/admin/catalog.sql
@?/rdbms/admin/catproc.sql
-- Step 4: Connect as SYSTEM to run data dictionary views
CONNECT SYSTEM/StrongSysPassword2
@?/sqlplus/admin/pupbld.sql
π§Ύ Notes
- Replace
/u01/app/oracle/oradata/cdb23c/
with your actual DB_CREATE_FILE_DEST
if using Oracle Managed Files.
SEED FILE_NAME_CONVERT
is required to correctly create the PDB seed in a separate directory.
- Make sure Oracle environment variables are properly configured (
ORACLE_HOME
, ORACLE_SID
, etc.).
- You can create additional PDBs using
CREATE PLUGGABLE DATABASE
once the CDB is operational.
Follow-up script to create a custom Pluggable Database (PDB)
Here is a follow-up script to create a custom
Pluggable Database (PDB)[1] in an existing Container Database (CDB) in Oracle 23c:
β
CREATE PLUGGABLE DATABASE Script
-- Connect as SYSDBA to the root container
CONNECT SYS/StrongSysPassword1@CDB23C AS SYSDBA;
-- Step 1: Create a custom pluggable database named `pdb_custom`
CREATE PLUGGABLE DATABASE pdb_custom
ADMIN USER pdbadmin IDENTIFIED BY StrongPDBAdminPwd1
FILE_NAME_CONVERT = ('/u01/app/oracle/oradata/cdb23c/pdbseed/',
'/u01/app/oracle/oradata/cdb23c/pdb_custom/');
-- Step 2: Open the newly created pluggable database
ALTER PLUGGABLE DATABASE pdb_custom OPEN;
-- Step 3: Save the state of the PDB so it opens automatically on CDB startup
ALTER PLUGGABLE DATABASE pdb_custom SAVE STATE;
π Explanation of Key Parameters
- ADMIN USER pdbadmin: Creates the local admin user for the PDB.
- FILE_NAME_CONVERT: Maps source file locations (from the seed) to the destination directory for the new PDB.
- SAVE STATE: Ensures the PDB opens automatically when the CDB starts.
π Optional: Grant Connection Privileges
To allow the `pdbadmin` user to connect remotely:
-- Switch to the PDB
ALTER SESSION SET CONTAINER = pdb_custom;
-- Grant CREATE SESSION
GRANT CREATE SESSION TO pdbadmin;
π§ If Using Oracle Managed Files (OMF)
If `DB_CREATE_FILE_DEST` is set, you can omit `FILE_NAME_CONVERT`:
CREATE PLUGGABLE DATABASE pdb_custom
ADMIN USER pdbadmin IDENTIFIED BY StrongPDBAdminPwd1;
Parameters for use with the `CREATE DATABASE` statement.
Below is a listing of the parameters which are for use with the `CREATE DATABASE` statement in Oracle 23c.
However, several are legacy constructs that are less commonly used today, especially in environments using
Oracle Managed Files (OMF)and
multitenant architecture (CDB/PDBs). Below is a breakdown of the current status and recommendations for each parameter:
β
Still Supported and Commonly Used
Parameter |
Status |
Notes |
LOGFILE |
β
Active |
Still used when explicitly managing redo logs. Required if OMF is not used. |
GROUP group_no |
β
Active |
Required within LOGFILE for defining groups. |
log_file_name |
β
Active |
Required unless OMF is configured. |
SIZE integer[K M] |
β
Active |
Recommended to explicitly specify sizes for control, redo, and datafiles. |
MAXDATAFILES |
β
Active |
Still supported, but can be dynamically extended via DB_FILES parameter. |
ARCHIVELOG |
β
Active |
Still fully valid. |
NOARCHIVELOG |
β
Active |
Default, unless overridden. |
CHARACTER SET |
β
Active |
Use AL32UTF8 for Unicode support. |
NATIONAL CHARACTER SET |
β
Active |
Use AL16UTF16 typically. |
DATAFILE |
β
Active |
Needed unless using OMF. |
AUTOEXTEND |
β
Active |
Still supported and widely used. |
OFF , ON (for AUTOEXTEND) |
β
Active |
Still required to control behavior. |
NEXT , MAXSIZE |
β
Active |
Used with AUTOEXTEND . |
Supported but Legacy or Use is Discouraged in Modern Configurations
Parameter |
Status |
Notes |
CONTROLFILE_REUSE |
β
Active, but legacy |
Supported, but rarely used in practice today. Instead, recreate control files or let Oracle manage them. |
MAXLOGFILES |
β
Active, but less relevant |
Oracle can handle more redo log groups dynamically. Still valid but typically unnecessary to specify. |
MAXLOGMEMBERS |
β
Active, but rarely changed |
Default is usually sufficient. Multiplexing is managed separately. |
MAXLOGHISTORY |
β
Active, but specific use case |
Only relevant for RAC/Data Guard with archivelog mode. Rarely adjusted in modern setups. |
MAXINSTANCES |
β
Active, RAC-specific |
Only applicable to Real Application Clusters (RAC). Not used in single-instance deployments. |
β
Summary
- None of the listed parameters are deprecated in Oracle 23c.
- Several parameters (
CONTROLFILE_REUSE
, MAXLOGHISTORY
, MAXINSTANCES
) are legacy or niche-use in RAC or manual control file configurations.
- In modern Oracle 23c deployments, especially those using CDB/PDB with Oracle Managed Files, many of these parameters become optional or are better managed dynamically.
Specific clauses in command: Best practices in real-world Oracle DBA work
Hereβs a concise breakdown that reinforces your observations for learners in a course setting:
β
Clause Ordering and Optionality
- The clauses such as
MAXLOGFILES
, MAXDATAFILES
, MAXLOGMEMBERS
, etc., do not have to be written in a specific order in the CREATE DATABASE
command.
- Most are optional because Oracle provides sensible defaults based on the initialization parameters and system configuration.
π§ Why Explicitly Include LOGFILE and DATAFILE
LOGFILE
Clause:
- Lets you name redo log groups and their members.
- Allows you to control file placement and and size, which is crucial for performance and diagnostics.
- Useful when not using OMF or when deploying databases on specific I/O subsystems.
DATAFILE
Clause:
- Required to define the SYSTEM tablespace location.
- You should always control the initial size of the SYSTEM tablespace and related datafiles like SYSAUX and USERS.
- When omitted, Oracle may place the files in a default location, which can lead to unpredictable file organization.
πBest Practice for Students in a Course Setting
> βIn this course, unless there's a specific reason to override a default, you can omit most optional clauses. However, you should always include the `LOGFILE` and `DATAFILE` clauses to maintain control over file placement and sizing.β
This guidance helps students avoid future headaches with storage management and fosters the habit of thinking like a production DBA.
Write Create Database - Exercise
[1]pluggable database (PDB) : In Oracle Database, a pluggable database (PDB) is a self-contained unit of data that can be plugged into a container database (CDB). It's like having multiple, independent databases within a single Oracle instance. PDBs are portable, meaning they can be moved between different CDBs, even with different Oracle Database versions. From an application's perspective, a PDB appears as a regular, non-CDB database.
[2]Oracle Managed Files (OMF): Oracle Managed Files (OMF) in Oracle 23c simplifies database administration by automating the creation, naming, and deletion of various database files (like data files, redo logs, and control files). By setting specific initialization parameters, DBAs can let Oracle handle the physical file management, reducing the risk of errors and streamlining operations, especially in environments utilizing Oracle's multitenant architecture and Automatic Storage Management (ASM).
[3] multitenant architecture: In Oracle 23c, the multitenant architecture is a fundamental design where a single Oracle database instance, called a Container Database (CDB), can host multiple isolated and portable databases known as Pluggable Databases (PDBs). This allows for efficient consolidation of many databases, sharing a common set of background processes and memory structures while maintaining the appearance of separate databases for applications.