使用类似属性在不同数据库模式之间进行转换

时间:2013-04-02 04:38:37

标签: sql database database-schema

是否可以在两个不同的数据库模式之间进行转换,可能使用不同的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;

1 个答案:

答案 0 :(得分: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;

查询:

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

select * from people where gender = 'Male';

结果:

Name  Age  Gender
John  25   Male

摆弄它:

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