db2視圖入門一

 

以下簡單的示例所使用的數據庫都是建立在db2 9.5版本下,示例代碼運行是在quest4.8版本運行

 

建立二張表: employees員工表,department部門表

  create table administrator.employees ( --員工表
  id integer ,                        --員工編號
  name varchar(50),         --員工姓名
  age integer,                    --員工年齡
  departmentid integer) ;--員工所在部門的編號
  
  insert into administrator.employees(id,name,age,department)values(1,"zhangsan",23,1);
  insert into administrator.employees(id,name,age,department)values(2,"lisi",24,2);
  insert into administrator.employees(id,name,age,department)values(3,"wangwu",22,2);
  insert into administrator.employees(id,name,age,department)values(4,"wangwu",26,3);
  
  create table administrator.department (
  id integer ,                     --部門編號
  name varchar(50),      --部門名稱
  fileid integer) ;             --文件名稱
  
  insert into administrator.department(id,name,fileid)values(1,'技術部',1);
  insert into administrator.department(id,name,fileid)values(2,'文化部',21);
  insert into administrator.department(id,name,fileid)values(3,'組織部',31);

//建立視圖,語法是create view 模式.表名 as select....(sql語句)

create view readfile as
  select e.id as eid,e.name as ename,e.age as eage,
         d.id as did,d.name as dname,d.fileid as dfileid,
  from  administrator.department as d,administrator.employees as e
  where d.id = e.departmentid

執行查詢:

select * from readfile01

查詢結果:

1 'zhangsan' 23 1 '技術部' 1
2 'lisi' 24 2 '文化部' 21
3 'wangwu' 22 2 '文化部' 21
4 'liuliu' 26 3 '組織部' 31

發佈了43 篇原創文章 · 獲贊 6 · 訪問量 13萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章