在实际开发过程中,特别是在做数据查询的时候,能够根据动态生成的sql语句将查询的结果转化并返回到业务处理逻辑(或页面展示出来)能大大的减轻业务逻辑的处理复杂度。
大部分情况下,这种动态生成的sql查询语句写法如下:代码如下:
select A表.字段1,A表.字段2,B表.字段返回,C表.字段返回 from A表 ,B表,C表 [where A表,B表,C表关联及各自的条件语句]
但是这个方法有一个缺点,那就是在动态的生成这个查询语句的业务逻辑程序仍然很复杂。这里就介绍一个降低业务逻辑复杂度的查询sql生成方式。其语法结构如下:
代码如下:
select A表.字段1,A表.字段2,B表.字段,C表.字段 from A表 [where A表的条件语句]
业务逻辑程序通过这种方式生成的sql语句时只需修改select的字段,而不需像通用方法那样需要同时动态修改select字段,from的表,以及where 语句。这样真个业务逻辑就能将生成sql语句的关注点由3+个减少为1个。下面就该方式实现举例如下:
首先,建立三个表,一个反应学生基本情况的信息表——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 '名称';
然后,执行以下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
注:1.对于二者的性能,这里只是做了个简单测试,1000条数据查询耗时二者相当,而且本文提到方法甚至略优于普通方法。
2.此方法目前只在oracle数据库中实现并测试,其他数据库请自行测试。

oracle asm指的是“自动存储管理”,是一种卷管理器,可自动管理磁盘组并提供有效的数据冗余功能;它是做为单独的Oracle实例实施和部署。asm的优势:1、配置简单、可最大化推动数据库合并的存储资源利用;2、支持BIGFILE文件等。

方法:1、利用“select*from user_indexes where table_name=表名”语句查询表中索引;2、利用“select*from all_indexes where table_name=表名”语句查询所有索引。

在Oracle中,可利用lsnrctl命令查询端口号,该命令是Oracle的监听命令;在启动、关闭或重启oracle监听器之前可使用该命令检查oracle监听器的状态,语法为“lsnrctl status”,结果PORT后的内容就是端口号。

在oracle中,可以利用“TO_SINGLE_BYTE(String)”将全角转换为半角;“TO_SINGLE_BYTE”函数可以将参数中所有多字节字符都替换为等价的单字节字符,只有当数据库字符集同时包含多字节和单字节字符的时候有效。

在oracle中,可以利用“drop sequence sequence名”来删除sequence;sequence是自动增加数字序列的意思,也就是序列号,序列号自动增加不能重置,因此需要利用drop sequence语句来删除序列。

在oracle中,可以利用“select ... From all_tab_columns where table_name=upper('表名') AND owner=upper('数据库登录用户名');”语句查询数据库表的数据类型。

方法:1、利用“LOWER(字段值)”将字段转为小写,或者利用“UPPER(字段值)”将字段转为大写;2、利用“REGEXP_LIKE(字符串,正则表达式,'i')”,当参数设置为“i”时,说明进行匹配不区分大小写。

方法:1、利用“alter system set sessions=修改后的数值 scope=spfile”语句修改session参数;2、修改参数之后利用“shutdown immediate – startup”语句重启服务器即可生效。


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Dreamweaver CS6
Visual web development tools

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment
