在oracle 數(shù)據(jù)庫查詢的select 查詢字段中關聯(lián)其他表的方法
更新時間:2009年08月30日 22:36:30 作者:
在實際開發(fā)過程中,特別是在做數(shù)據(jù)查詢的時候,能夠根據(jù)動態(tài)生成的sql語句將查詢的結果轉化并返回到業(yè)務處理邏輯(或頁面展示出來)能大大的減輕業(yè)務邏輯的處理復雜度。
大部分情況下,這種動態(tài)生成的sql查詢語句寫法如下:
select A表.字段1,A表.字段2,B表.字段返回,C表.字段返回 from A表 ,B表,C表 [where A表,B表,C表關聯(lián)及各自的條件語句]
但是這個方法有一個缺點,那就是在動態(tài)的生成這個查詢語句的業(yè)務邏輯程序仍然很復雜。這里就介紹一個降低業(yè)務邏輯復雜度的查詢sql生成方式。其語法結構如下:
select A表.字段1,A表.字段2,B表.字段,C表.字段 from A表 [where A表的條件語句]
業(yè)務邏輯程序通過這種方式生成的sql語句時只需修改select的字段,而不需像通用方法那樣需要同時動態(tài)修改select字段,from的表,以及where 語句。這樣真?zhèn)€業(yè)務邏輯就能將生成sql語句的關注點由3+個減少為1個。下面就該方式實現(xiàn)舉例如下:
首先,建立三個表,一個反應學生基本情況的信息表——student表,兩個存放學生相關信息的代碼表——sexCode表(性別代碼表),gradeCode(年紀代碼表),建表語句如下:
-- 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 '名稱';
然后,執(zhí)行以下insert語句,分別在每個表中填入信息。
--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','三年級');
最后,給出常用sql查詢方式和本文倡導的查詢方式及其查詢結果比較:
通用查詢方式及其查詢結果如下:
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(+)
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
復制代碼 代碼如下:
select A表.字段1,A表.字段2,B表.字段返回,C表.字段返回 from A表 ,B表,C表 [where A表,B表,C表關聯(lián)及各自的條件語句]
但是這個方法有一個缺點,那就是在動態(tài)的生成這個查詢語句的業(yè)務邏輯程序仍然很復雜。這里就介紹一個降低業(yè)務邏輯復雜度的查詢sql生成方式。其語法結構如下:
復制代碼 代碼如下:
select A表.字段1,A表.字段2,B表.字段,C表.字段 from A表 [where A表的條件語句]
業(yè)務邏輯程序通過這種方式生成的sql語句時只需修改select的字段,而不需像通用方法那樣需要同時動態(tài)修改select字段,from的表,以及where 語句。這樣真?zhèn)€業(yè)務邏輯就能將生成sql語句的關注點由3+個減少為1個。下面就該方式實現(xiàn)舉例如下:
首先,建立三個表,一個反應學生基本情況的信息表——student表,兩個存放學生相關信息的代碼表——sexCode表(性別代碼表),gradeCode(年紀代碼表),建表語句如下:
復制代碼 代碼如下:
-- 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 '名稱';
然后,執(zhí)行以下insert語句,分別在每個表中填入信息。
復制代碼 代碼如下:
--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','三年級');
最后,給出常用sql查詢方式和本文倡導的查詢方式及其查詢結果比較:
通用查詢方式及其查詢結果如下:
復制代碼 代碼如下:
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條數(shù)據(jù)查詢耗時二者相當,而且本文提到方法甚至略優(yōu)于普通方法。
2.此方法目前只在oracle數(shù)據(jù)庫中實現(xiàn)并測試,其他數(shù)據(jù)庫請自行測試。
您可能感興趣的文章:
相關文章
Oracle的PDB數(shù)據(jù)庫創(chuàng)建DIRECTORY時遇到ORA-65254問題及解決方法
這篇文章主要介紹了Oracle的PDB數(shù)據(jù)庫創(chuàng)建DIRECTORY時遇到ORA-65254問題,本文給大家分享完美解決方案,需要的朋友可以參考下2023-06-06oracle插入字符串數(shù)據(jù)時字符串中有''單引號問題
這篇文章主要介紹了oracle插入字符串數(shù)據(jù)時字符串中有'單引號問題的相關資料,需要的朋友可以參考下2017-04-04Oracle數(shù)據(jù)庫中的級聯(lián)查詢、級聯(lián)刪除、級聯(lián)更新操作教程
這里整理了Oracle中的三種級聯(lián)操作,其中Oracle定義外健的時候可以定義級聯(lián)刪除,但是沒有級聯(lián)修改的語法,當然可以用觸發(fā)器實現(xiàn),下面我們詳細來看Oracle數(shù)據(jù)庫中的級聯(lián)查詢、級聯(lián)刪除、級聯(lián)更新操作教程2016-05-05