Chinaunix首页 | 论坛 | 博客
  • 博客访问: 129252
  • 博文数量: 69
  • 博客积分: 595
  • 博客等级: 中士
  • 技术积分: 670
  • 用 户 组: 普通用户
  • 注册时间: 2008-04-16 17:37
文章分类

全部博文(69)

文章存档

2017年(2)

2016年(9)

2015年(13)

2014年(30)

2012年(4)

2011年(2)

2010年(2)

2009年(5)

2008年(2)

我的朋友

分类: 数据库开发技术

2016-08-13 05:11:42

Suppose the database stores information about actors, together with their names, birthdays, and ratings, where ratings can change over time:

actor-attrs

According to anchor modeling, each attribute should go into its own table:

  • the 'anchor' table which only has a synthetic primary key:
create table ac_anchor(AC_ID int primary key); 
  • a table for the 'name' attribute:
create table ac_name(AC_ID int, ACNAM_name char(N), primary key(AC_ID)); 
  • a table for the 'birthdate' attribute:
create table ac_dob(AC_ID int, ACDOB_birthdate date, primary key(AC_ID)); 
  • a table for the ‘rating’ attribute, which is historized:
create table ac_rating(AC_ID int, ACRAT_rating int, ACRAT_fromdate date, primary key(AC_ID, ACRAT_fromdate)); 

With this approach it becomes easy to add/change/remove attributes, but this comes at a cost of added complexity in querying the data: in order to answer the simplest, select-star question of displaying actors and their current ratings one has to write outer joins:

Display actors, with their names and current ratings:

select ac_anchor.AC_ID, ACNAM_Name, ACDOB_birthdate, ACRAT_rating from ac_anchor left join ac_name on ac_anchor.AC_ID=ac_name.AC_ID left join ac_dob on ac_anchor.AC_ID=ac_dob.AC_ID left join ac_rating on (ac_anchor.AC_ID=ac_rating.AC_ID and ac_rating.ACRAT_fromdate = (select max(sub.ACRAT_fromdate) from ac_rating sub where sub.AC_ID = ac_rating.AC_ID)) 

We don't want to write the joins every time we need to access an actor's properties, so we’ll create a view:

create view actors as select ac_anchor.AC_ID, ACNAM_Name, ACDOB_birthdate, ACRAT_rating from <see the select above> 

This will allow us to access the data as if it was stored in a regular way:

select ACRAT_rating from actors where ACNAM_name='Gary Oldman' 

And this is where table elimination will be needed.



阅读(647) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~