DB研修2

今日の作業

DB研修

今日の作業でわかったこと&わからなかったこと

今日はDB研修の二日目でした。
具体的にはワイルドカードと一緒に使うLIKE演算子から始まり表の作成・削除をやり、
この辺までは外部研修で一回やっていたのでわかっていたのですが、
表の結合や副問い合わせはなじみがなくまだ理解しきれていないと思うので、
しっかり復習したい。

明日やること

DB研修3

まとめ

準備以下のSQLを別ファイルに書いて、sqlplusからは@コマンドで実行してみる事

適当にitem_list.splというファイルを作り下記を貼り付ける

drop table ITEM_LIST;
create table ITEM_LIST (
  CODE     VARCHAR(8),
  NAME     VARCHAR(25),
  CATEGORY VARCHAR(25),
  PRICE    NUMBER(8),
  SUPPLIER VARCHAR(25)
);
insert into ITEM_LIST values('USA-0012', '眠いうさぎ',       'ぬいぐるみ', 9800, 'ウサギ社');
insert into ITEM_LIST values('USA-1011', 'だれうさぎ',       'きぐるみ',   5000, 'ウサギ社');
insert into ITEM_LIST values('EDU-0012', 'うさぎ算',         '知能玩具',   1800, '実験堂');
insert into ITEM_LIST values('GAM-0004', '子うさぎクエスト', 'ゲーム',     6800, 'USAGE.Corp');
insert into ITEM_LIST values('OOK-1001', '舌の出た狼',       'きぐるみ',   5200, 'がるる社');
commit;

適当にC:の直下に置く
sqlplusで

SQL>@C:\item_list.sql;

を実行


item_list.sqlをデスクトップおいた場合

SQL>@"C:\Documents and Settings\m-matsumoto\デスクトップ\item_list.sql"

にする

Documents and Settingsにスペースがあるので""で囲む

これから使うテーブル
CUSTOMER_MASTER

CUSTOMER_CODE NAME                 TEL
------------- -------------------- -----------
          110 山田花子             0312341234
          112 斎藤隆               0451231234
          115 佐藤信也             0354325432
          120 森洋子               0434444444
          122 小泉加奈子           0311221122
SALES_LIST

ITEM_CODE ITEM_NAME                 PRICE SOLD_DAT CUSTOMER_CODE
--------- -------------------- ---------- -------- -------------
     1100 レンズなしカメラ           1100 04-06-01           112
     1300 透明なインク                180 04-06-01           112
     1100 レンズなしカメラ           1200 04-06-02           115
     1201 角の固い豆腐                400 04-06-02           120
     1301 開かない蓋                  480 04-06-03           122
ITEM_MASTER

     CODE NAME                           PRICE SUPPLIER
--------- ------------------------- ---------- -------------------------
     1100 レンズなしカメラ                1200 びっくり堂
     1201 角の固い豆腐                     400 うつけ屋
     1300 透明なインク                     180 裸の王様堂
     1301 開かない蓋                       500 非実用.Inc
左外部結合
select customer_master.name, sales_list.item_name,sales_list.price
from customer_master left outer join sales_list
on customer_master.customer_code = sales_list.customer_code;
NAME                 ITEM_NAME                 PRICE
-------------------- -------------------- ----------
斎藤隆               レンズなしカメラ           1100
斎藤隆               透明なインク                180
佐藤信也             レンズなしカメラ           1200
森洋子               角の固い豆腐                400
小泉加奈子           開かない蓋                  480
山田花子
右外部結合
select customer_master.name, sales_list.item_name,sales_list.price
from customer_master right outer join sales_list
on customer_master.customer_code = sales_list.customer_code;
NAME                 ITEM_NAME                 PRICE
-------------------- -------------------- ----------
斎藤隆               透明なインク                180
斎藤隆               レンズなしカメラ           1100
佐藤信也             レンズなしカメラ           1200
森洋子               角の固い豆腐                400
小泉加奈子           開かない蓋                  480
全外部結合
select customer_master.name, sales_list.item_name,sales_list.price
from customer_master full outer join sales_list
on customer_master.customer_code = sales_list.customer_code;
NAME                 ITEM_NAME                 PRICE
-------------------- -------------------- ----------
斎藤隆               レンズなしカメラ           1100
斎藤隆               透明なインク                180
佐藤信也             レンズなしカメラ           1200
森洋子               角の固い豆腐                400
小泉加奈子           開かない蓋                  480
山田花子
副問い合わせ
select * from sales_list where item_code = (
select code from item_master where name = 'レンズなしカメラ');
ITEM_CODE ITEM_NAME                 PRICE SOLD_DAT CUSTOMER_CODE
--------- -------------------- ---------- -------- -------------
     1100 レンズなしカメラ           1100 04-06-01           112
     1100 レンズなしカメラ           1200 04-06-02           115
select customer_master.name, sales_list.item_name,sales_list.price,sales_list.sold_date
from customer_master inner join sales_list
on customer_master.customer_code = sales_list.customer_code
where sales_list.item_code = (
select code from item_master where name = 'レンズなしカメラ');
NAME                 ITEM_NAME                 PRICE SOLD_DAT
-------------------- -------------------- ---------- --------
斎藤隆               レンズなしカメラ           1100 04-06-01
佐藤信也             レンズなしカメラ           1200 04-06-02