发布于 2016-03-18 08:26:00 | 173 次阅读 | 评论: 0 | 来源: 网友投递
Oracle关系数据库管理系统
Oracle Database,又名Oracle RDBMS,或简称Oracle。是甲骨文公司的一款关系数据库管理系统。到目前仍在数据库市场上占有主要份额。
select A表.字段1,A表.字段2,B表.字段返回,C表.字段返回 from A表 ,B表,C表 [where A表,B表,C表关联及各自的条件语句]
select A表.字段1,A表.字段2,B表.字段,C表.字段 from A表 [where A表的条件语句]
-- Create table STUDENT
create table STUDENT
(
ID number,
name nvarchar2(10),
sex char(1),
grade char(1),
age number(2)
)
tablespace SDMP
storage
(
initial 64K
minextents 1
maxextents unlimited
);
-- Add comments to the columns
comment on column STUDENT.name
is '学生姓名';
comment on column STUDENT.sex
is '学生性别';
comment on column STUDENT.grade
is '年级';
comment on column STUDENT.age
is '年龄';
-- Create table SEXCODE
create table SEXCODE
(
DM char(1),
MC nvarchar2(5)
)
tablespace SDMP
storage
(
initial 64K
minextents 1
maxextents unlimited
);
-- Add comments to the columns
comment on column SEXCODE.DM
is '代码';
comment on column SEXCODE.MC
is '名称';
-- Create table GRADECODE
create table GRADECODE
(
DM CHAR(1),
MC NVARCHAR2(5)
)
tablespace SDMP
pctfree 10
initrans 1
maxtrans 255
storage
(
initial 64K
minextents 1
maxextents unlimited
);
-- Add comments to the columns
comment on column GRADECODE.DM
is '代码';
comment on column GRADECODE.MC
is '名称';
--insert into student
insert into student(id,name,sex,grade,age) values(1,'张三','1','2',8);
insert into student(id,name,sex,grade,age) values(2,'李四','0','1',11);
insert into student(id,name,sex,grade,age) values(3,'王五','1','2',9);
insert into student(id,name,sex,grade,age) values(4,'刘二','0','4',8);
insert into student(id,name,sex,grade,age) values(5,'韩六','0','3',6);
--insert into sexcode
insert into sexcode(dm,mc) values('1','男');
insert into sexcode(dm,mc) values('0','女');
--insert into gradecode
insert into gradecode(dm,mc) values('1','一年级');
insert into gradecode(dm,mc) values('2','二年级');
insert into gradecode(dm,mc) values('3','三年级');
select s.id,s.name,sc.mc sex,gc.mc grade,s.age
from student s,sexcode sc,gradecode gc
where sc.dm=s.sex(+) and s.grade=gc.dm(+)
ID | NAME | SEX | GRADE | AGE | |
1 | 2 | 李四 | 女 | 一年级 | 11 |
2 | 3 | 王五 | 男 | 二年级 | 9 |
3 | 1 | 张三 | 男 | 二年级 | 8 |
4 | 5 | 韩六 | 女 | 三年级 | 6 |
5 | 4 | 刘二 | 女 | 8 |
本问题出查询方法及其查询结果如下
select s.id,s.name,s.age,
(select mc from sexcode where dm=s.sex) sex,
(select mc from gradecode where dm=s.grade) grade
from student s
ID | NAME | AGE | SEX | GRADE | |
1 | 1 | 张三 | 8 | 男 | 二年级 |
2 | 2 | 李四 | 11 | 女 | 一年级 |
3 | 3 | 王五 | 9 | 男 | 二年级 |
4 | 4 | 刘二 | 8 | 女 | |
5 | 5 | 韩六 | 6 | 女 | 三年级 |
注:1.对于二者的性能,这里只是做了个简单测试,1000条数据查询耗时二者相当,而且本文提到方法甚至略优于普通方法。
2.此方法目前只在oracle数据库中实现并测试,其他数据库请自行测试。