db2视图入门二

以下简单的示例所使用的数据库都是建立在db2 9.5版本下,示例代码运行是在quest4.8版本运行

建立三张表: employees员工表,department部门表,files文档表

 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 table administrator.files (
  id integer ,                     --文件编号
  name varchar(50),      --文件名称
  lengths integer,           --文件长度
  author varchar(50)) ;  --作者名称
  
  insert into administrator.files(id,name,lengths,author)values(1,'java文件',1,'马士兵');
  insert into administrator.files(id,name,lengths,author)values(21,'APT帮助文档',21,'俞敏洪');
  insert into administrator.files(id,name,lengths,author)values(31,'技术部',31,'李彦宏');
  
  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,
         f.id as fid,f.name as fname,f.lengths as flengths,f.author as fauthor
  from administrator.department as d,
  inner join administrator.employees as e on d.id = e.departmentid
  inner join administrator.files as f on d.fileid = f.id

执行完上面语句,后执行查询:

select * from readfile

结果集是:

1 'zhangsan' 23 1 '技术部' 1 1 'java文件' 1 '马士兵'
2 'lisi' 24 2 '文化部' 21 21 'APT帮助文档' 21 '俞敏洪'
3 'wangwu' 22 2 '文化部' 21 21 'APT帮助文档' 21 '俞敏洪'
4 'liuliu' 26 3 '组织部' 31 31 '技术部' 31 '李彦宏'

你可能感兴趣的:(db2视图入门二)