Srikanth Technologies

Installing HR Schema in Oracle Database 21c Express Edition (XE)

When you install Oracle Database 21c XE (Express Edition), you don't get any sample schemas by default.

Here are the steps to install HR Sample Schema. More or less the same procedure can be followed for other schemas also.
  1. Go to Oracle Database 21c Sample Schemas in GitHub.
  2. Download Source code (ZIP) from that repository. These are the scripts that we need to run to create users and sample tables.
  3. Once you downloaded .zip file to your system, unzip .zip file and it creates a folder - db-sample-schemas-21.1
  4. This folder contains human_resources folder, where all scripts related to HR schema are present.
  5. We need to modify hr_main.sql file to replace all occurrences of __SUB__CWD__/human_resources/ to empty string. Otherwise, it throws errors saying __SUB__CMD__ is not found.

    I removed all REM statements from the script to keep it short.

    Script hr_main.sql drops HR user if it is present and then creates the user, creates tables and loads data into those tables. For this purpose, it uses some other scripts in the same folder.

    As we are referring to other scripts directly without any reference to folder, you must be in the folder human_resources while you run hr_main.sql script.

    Here is the modified version of the file - hr_main.sql.

    We are referring to other scripts directly as you can see in @hr_cre, @hr_popul etc.

    SET ECHO OFF
    SET VERIFY OFF
    
    PROMPT 
    PROMPT specify password for HR as parameter 1:
    DEFINE pass     = &1
    PROMPT 
    PROMPT specify default tablespace for HR as parameter 2:
    DEFINE tbs      = &2
    PROMPT 
    PROMPT specify temporary tablespace for HR as parameter 3:
    DEFINE ttbs     = &3
    PROMPT 
    PROMPT specify password for SYS as parameter 4:
    DEFINE pass_sys = &4
    PROMPT 
    PROMPT specify log path as parameter 5:
    DEFINE log_path = &5
    PROMPT
    PROMPT specify connect string as parameter 6:
    DEFINE connect_string     = &6
    PROMPT
    
    -- The first dot in the spool command below is 
    -- the SQL*Plus concatenation character
    
    DEFINE spool_file = &log_path.hr_main.log
    SPOOL &spool_file
     
    
    DROP USER hr CASCADE;
     
    
    CREATE USER hr IDENTIFIED BY &pass;
    
    ALTER USER hr DEFAULT TABLESPACE &tbs
                  QUOTA UNLIMITED ON &tbs;
    
    ALTER USER hr TEMPORARY TABLESPACE &ttbs;
    
    GRANT CREATE SESSION, CREATE VIEW, ALTER SESSION, CREATE SEQUENCE TO hr;
    GRANT CREATE SYNONYM, CREATE DATABASE LINK, RESOURCE , UNLIMITED TABLESPACE TO hr;
     
    
    CONNECT sys/&pass_sys@&connect_string AS SYSDBA;
    GRANT execute ON sys.dbms_stats TO hr;
     
    
    CONNECT hr/&pass@&connect_string
    ALTER SESSION SET NLS_LANGUAGE=American;
    ALTER SESSION SET NLS_TERRITORY=America;
    
    --
    -- create tables, sequences and constraint
    --
    
    @hr_cre
    
    -- 
    -- populate tables
    --
    
    @hr_popul
    
    --
    -- create indexes
    --
    
    @hr_idx
    
    --
    -- create procedural objects
    --
    
    @hr_code
    
    --
    -- add comments to tables and columns
    --
    
    @hr_comnt
    
    --
    -- gather schema statistics
    --
    
    @hr_analz
    
    spool off
    
                  
  6. Now go to human_resources directory and run the SQLPLUS as follows.

    Password for system account is oracle (given by me at the time of installation, change it according to your password).

    Inputs given by me are shown in bold. Give users for tablespace and temp for temporary tablespace. The rest can be changed as you like.

    >sqlplus system/oracle@localhost:1521/XEPDB1 
    
    SQL*Plus: Release 21.0.0.0.0 - Production on Tue Jun 21 11:19:41 2022
    Version 21.3.0.0.0
    
    Copyright (c) 1982, 2021, Oracle.  All rights reserved.
    
    Last Successful login time: Mon Jun 20 2022 12:37:39 +05:30
    
    Connected to:
    Oracle Database 21c Express Edition Release 21.0.0.0.0 - Production
    Version 21.3.0.0.0
    
    SQL>@hr_main
    
    specify password for HR as parameter 1:
    Enter value for 1: hr
    
    specify default tablespace for HR as parameter 2:
    Enter value for 2: users
    
    specify temporary tablespace for HR as parameter 3:
    Enter value for 3: temp
    
    specify password for SYS as parameter 4:
    Enter value for 4: oracle
    
    specify log path as parameter 5:
    Enter value for 5: hr
    
    specify connect string as parameter 6:
    Enter value for 6: localhost:1521/XEPDB1
    
    User dropped.
    
    
    User created.
    
    
    User altered.
    
    ....
    
    PL/SQL procedure successfully completed.
                
  7. Exit SQLPLUS using exit command as follows:

    SQL>exit
    Disconnected from Oracle Database 21c Express Edition Release 21.0.0.0.0 - Production
    Version 21.3.0.0.0
                   
  8. Now you can connect to hr account as follows:

    SQL> connect hr/hr@localhost:1521/XEPDB1
    Connected.
    SQL> select tname from tab;
    
    TNAME
    --------------------------------------------------------------------------------
    REGIONS
    COUNTRIES
    LOCATIONS
    DEPARTMENTS
    JOBS
    EMPLOYEES
    JOB_HISTORY
    EMP_DETAILS_VIEW
    
    8 rows selected.
    
    SQL>
    

That's all you have to do to install HR Schema with sample tables and data.

For more details, refer to Installing the HR Schema in Oracle Documentation.