2

是否可以在两个不同的数据库模式之间进行转换,可能使用不同的 DBMS,以便用户可以查询中间模式表示,并将查询转换为底层 sql 查询以返回结果。

一个例子

Underlying database      Intermediate Representation
--------------------    --------------
| _____   _____   |     |  _______  |
|| Men | |Women|  |     | |People | |
||-----| |-----|  |     | |-------| |
||Name:| |Name:|  |---->| |Name:  | |
||Age: | |Age: |  |     | |Age:   | |
||_____| |_____|  |     | |Gender:| |
|                 |     | |_______| | 
|_________________|     |___________| 

我想查询中间表示,例如

SELECT * FROM People WHERE Gender == "Male";

在底层数据库上转换为

SELECT * FROM Men;
4

1 回答 1

0
create table men (
  name varchar(255) primary key,
  age smallint not null
);

create table women (
  name varchar(255) primary key,
  age smallint not null
);

create view people as 
  select
    name,
    age,
    'Male' as gender
  from men
  union
  select
    name,
    age,
    'Female' as gender
  from women;

Querying:

insert into men values ('John', 25);
insert into women values ('Jane', 25);

select * from people where gender = 'Male';

Result:

Name  Age  Gender
John  25   Male

Fiddle with it:

http://sqlfiddle.com/#!15/85939/2

于 2014-01-25T03:46:18.130 回答