如果萬一你正在使用Postgres的,你可以做繼承,就像在面向對象:
CREATE TABLE person
(
person_id serial primary key,
person_name VARCHAR(20),
birth_year CHAR(4),
gender CHAR(1)
);
create table actor
(
role text
) inherits(person);
測試:
insert into person(person_name,birth_year,gender)
values('john lennon','1940','M');
insert into actor(person_name,birth_year,gender,role)
values('johnny depp','19xx','M','hacker');
insert into actor(person_name,birth_year,gender,role)
values('johnny walker','19xx','M','walker');
select * from person order by person_name;
select * from actor order by person_name;
Output:
PERSON_ID PERSON_NAME BIRTH_YEAR GENDER
1 john lennon 1940 M
2 johnny depp 19xx M
3 johnny walker 19xx M
PERSON_ID PERSON_NAME BIRTH_YEAR GENDER ROLE
2 johnny depp 19xx M hacker
3 johnny walker 19xx M walker
----------------------------------------------------------------
update actor set
role = 'pirates', birth_year = 1963
where person_name = 'johnny depp';
select * from person;
select * from actor;
Output:
PERSON_ID PERSON_NAME BIRTH_YEAR GENDER
1 john lennon 1940 M
2 johnny depp 1963 M
3 johnny walker 19xx M
PERSON_ID PERSON_NAME BIRTH_YEAR GENDER ROLE
2 johnny depp 1963 M pirates
3 johnny walker 19xx M walker
----------------------------------------------------------------
delete from actor where person_name = 'johnny depp';
select * from person;
select * from actor;
Output:
PERSON_ID PERSON_NAME BIRTH_YEAR GENDER
1 john lennon 1940 M
3 johnny walker 19xx M
PERSON_ID PERSON_NAME BIRTH_YEAR GENDER ROLE
3 johnny walker 19xx M walker
現場測試:http://www.sqlfiddle.com/#!1/463f4/1
使用數據庫支持該範式(OOP,繼承)。 http://www.postgresql.org/docs/9.1/static/ddl-inherit.html –