0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

[Oracle] 新入社員&初心者シリーズ#04 Oracle Database サンプル スキーマ データの作成

Last updated at Posted at 2023-05-11

やりたいこと

Oracle Databaseをテスト用に自分の環境にインストールして、
SQLを実行できるようになるまでを解説するシリーズ

第四回は、「データベースにサンプルデータをいれる」です。
書籍やオラクル社の研修ではデータベース内にあるサンプルデータを使って説明されています。
そこでよく使われるのは「EMPLOYEES」表、「DEPARTMENTS」表です。
それらを作成する手順を紹介します。

環境

・Windows 10 Pro ( Windows 2016 , 2019 も同様)
・Oracle Database 19c

・作業するOSユーザ:OracleをインストールしたOSユーザ
 (Administrator等)

作成されるテーブル・DBユーザ

DBユーザ:
HR

テーブル:
EMPLOYEES
DEPARTMENTS
COUNTRIES
LOCATIONS
REGIONS
JOBS
JOB_HISTORY

第四回 オラクルデータベースにサンプルデータをいれる

簡単にいうと、\demo\schema\human_resourcesにあるhr_main.sqlを実行します。
以下に手順を紹介します。

1.エクスプローラで実行するスクリプトがあることを確認。
Oracleをインストールしたフォルダ(ORACLE_HOME)の下のdemo\schema\human_resourcesにあります。

例)C:\app\oracle\product\19.0.0\dbhome_1\demo\schema\human_resources\hr_main.sql

image.png

2.コマンドプロンプトを開いて、以下のコマンドを実行します。
ファイルのパスは自身の環境にあわせてください。

実行するコマンド一覧
cd /d C:\app\oracle\product\19.0.0\dbhome_1\demo\schema\human_resources

sqlplus / as sysdba
@hr_main
  -> hr_password  ※任意。HRユーザのパスワード
  -> users
  -> temp
  -> C:\app\oracle\product\19.0.0\dbhome_1\demo\schema\human_resources  ※任意。ログ出力先
select username from dba_users where username = 'HR';
connect hr/hr_password
select table_name from user_tables;
exit
実行例

★の部分が入力箇所です

--★ カレントディレクトリの変更
cd /d C:\app\oracle\product\19.0.0\dbhome_1\demo\schema\human_resources 

--★ SQL*Plusの起動
C:\app\oracle\product\19.0.0\dbhome_1\demo\schema\human_resources>sqlplus / as sysdba

@hr_main
  -> hr_password  ※任意。HRユーザのパスワード
  -> users
  -> temp
  -> C:\app\oracle\product\19.0.0\dbhome_1\demo\schema\human_resources  ※任意。ログ出力先
select username from dba_users where username = 'HR';
connect hr/hr_password
select table_name from user_tables;
exit
実行例
★の部分が入力箇所です

--★ カレントディレクトリの変更
cd /d C:\app\oracle\product\19.0.0\dbhome_1\demo\schema\human_resources 

--★ SQL*Plusの起動
C:\app\oracle\product\19.0.0\dbhome_1\demo\schema\human_resources>sqlplus / as sysdba

SQL*Plus: Release 19.0.0.0.0 - Production on 日 4月 17 16:08:51 2022
Version 19.3.0.0.0

Copyright (c) 1982, 2019, Oracle.  All rights reserved.



Oracle Database 19c Enterprise Edition Release 19.0.0.0.0 - Production
Version 19.3.0.0.0
に接続されました。

--★ hr_mainスクリプトの実行
SQL> @hr_main

specify password for HR as parameter 1:
--★ (任意)HRユーザのパスワードを指定
1に値を入力してください: hr_password

specify default tablespeace for HR as parameter 2:
--★ (任意)テーブルを作成する表領域を指定。デフォルトのデータベースであれば「USERS」表領域が存在する
2に値を入力してください: users

specify temporary tablespace for HR as parameter 3:
--★ (任意)作業用の一時表領域を指定。デフォルトのデータベースであれば「TEMP」表領域が存在する
3に値を入力してください: temp

specify log path as parameter 4:
--★ (任意)hr_mainスクリプトの実行結果(ログファイル)出力先。ファイルが書き込めればどこでもよい。
4に値を入力してください: C:\app\oracle\product\19.0.0\dbhome_1\demo\schema\human_resources


PL/SQLプロシージャが正常に完了しました。


ユーザーが作成されました。

~ 省略 ~

Commit complete.


PL/SQL procedure successfully completed.

--★ DBユーザ「HR」が作成されたことを確認
SQL> select username from dba_users where username = 'HR';

USERNAME
--------------------------------------------------------------------
HR 

1 row selected.

--★ DBユーザ「hr」で接続
SQL> connect hr/hr_password
接続されました。

--★ 作成されたテーブルの確認。REGIONSなど、7つのテーブル名が表示されること
SQL> select table_name from user_tables;

TABLE_NAME
--------------------------------------------------------------------------------
REGIONS
COUNTRIES
LOCATIONS
DEPARTMENTS
JOBS
EMPLOYEES
JOB_HISTORY

7行が選択されました。

--★ DBから切断して終了
SQL> exit
Oracle Database 19c Enterprise Edition Release 19.0.0.0.0 - Production
Version 19.3.0.0.0との接続が切断されました。

C:\app\oracle\product\19.0.0\dbhome_1\demo\schema\human_resources>
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?