如何在Oracle中的表上定义默认的where子句

时间:2018-09-22 07:47:30

标签: java oracle

有些查询的where子句与上部分相同。

查询一项:

select [some-selected column] 
  from [TABLE_NAME]
 where [its-own-where-clause]
   and [shared-where-clause]

查询两个:

select [some-selected column] 
  from [TABLE_NAME]
 where [its-own-where-clause]
   and [shared-where-clause]

查询三:

select [some-selected column] 
  from [TABLE_NAME]
 where [its-own-where-clause]
   and [shared-where-clause]





查询n:

select [some-selected column] 
  from [TABLE_NAME]
 where [its-own-where-clause]
   and [shared-where-clause]

如您所见,where子句中有两个部分,第一个部分属于其自己的查询业务,第二个部分在所有这些查询之间共享。
显然,更改[shared-where-clause]时,必须更改所有上述查询。

我想将where-clause的shared-section放在其中,将其更改应用于所有此查询。

在oracle中可能吗?

1 个答案:

答案 0 :(得分:3)

创建一个视图。

示例:

create table PERSON (
    PERSON_ID    number(10, 0)  not null primary key,
    PERSON_NAME  nvarchar(100)  not null,
    AGE          number(3, 0)   not null,
    GENDER       char(1)        not null check(GENDER in ('M', 'F'))
);

create view MALES as
select PERSON_ID, PERSON_NAME, AGE, GENDER
  from PERSON
 where GENDER = 'M';

create view FEMALES as
select PERSON_ID, PERSON_NAME, AGE, GENDER
  from PERSON
 where GENDER = 'F';

现在,我们可以查询各个年龄段的男性,而无需在GENDER上重复相同的条件。

select *
  from MALES
 where AGE between 0 and 19;

select *
  from MALES
 where AGE between 20 and 49;

select *
  from MALES
 where AGE >= 50;