Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
107 views
in Technique[技术] by (71.8m points)

sql - Views can be used to provide a backward compatible interface

Views can be used to provide a backward compatible interface to emulate a table that used to exist but whose schema has changed.

Sql Server - Database Design - Views

What does that mean, can somebody explain?

Is it saying, when there is a change in the schema of tables that are used in creating views, doesn't alter the view?

question from:https://stackoverflow.com/questions/65868821/views-can-be-used-to-provide-a-backward-compatible-interface

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

One of the big problems in working with databases is handling changes over time. The issue is that downstream users depend on the data model they are using.

You can isolate downstream users by having them rely on views, rather than directly accessing the base tables.

For instance, you might start with a table that has a user table with an address column. Folks downstream access the table as:

select u.*
from users u;

Later, you realize that addresses can change and you want to implement a type-2 table for addresses -- that is, a separate table with an effective and end date on each record. Well, existing code will break.

Instead, if the downstream users are using:

select u.*
from v_users u;

Then the view only needs to change from:

create view v_users as
    select u.user_id, u.address
    from users u;

to:

create view v_users as
     select u.user_id
     from users u join
          user_addresses ua
          on ua.user_id = u.user_id and
             current_timestamp >= ua.eff_ts and
             (current_timestamp < ua.end_ts or ua.end_ts is null);

Voila! Nothing breaks downstream.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...