Database refactoring: Replace table with view

When working on replacement projects, I often find I need to make minor changes to an existing database that is still in use by one of several other applications. Initially, it may seem like situation will force you to conform to the current database schema. But there are other options, even though they may not be for those who are faint of heart.

The general pattern when you want to evolve a database that is in use by legacy system, is to make sure that the legacy system sees the same data structure when it reads from or writes to the database. You can achieve this by using database views and “instead-of” triggers that will execute custom SQL code when insert, update or delete statements for the view are executed.

Here is an example: I was once given the challenge of centralizing a database that the customer currently installed one per site. The rub: I could not change the current application. And the new solution should treat some information as shared across the sites and some information as exclusive per site.

Here is how I solved the problem for an example table (let’s say CUSTOMER, with shared column BONUS_LEVEL and site-exclusive column LAST_VISIT):

  1. I created the tables CUSTOMER_SHARED, with column BONUS_LEVEL, and CUSTOMER_SITE with columns SITE and LAST_VISIT
  2. I populated the tables with the union of the current site databases.
  3. I dropped the existing CUSTOMER table
  4. I created a new CUSTOMER view as “SELECT … FROM customer_site INNER JOIN customer_shared WHERE = “. Now, the existing applications can see, but not update data.
  5. I created “instead-of triggers” for insert, update and delete on customer. The existing applications can now update data as if nothing had changed.

Instead-of triggers is a very handy mechanism for views. They are executed when insert, update or delete statements are executed on the table. Some examples of such a triggers are:

This sort of transformation is not without risk, and should be carefully tested and scripted to make sure it behaves the same when you test and when you deploy it.

A final word of warning: Both the function and the performance of instead-of triggers can depend a lot on your database vendor. Make sure you test a solution that use these features with a realistic amount of data.

I’d like to hear from readers who either found the SQL code intimidating or enlightening. I’d really like to know whether my readers are as frightened by SQL-code as non-programmers are by Java. ;-)

So remember: When you’re faced with a seemingly impossible task: Think inside a bigger box!

About Johannes Brodwall

Johannes is Principal Software Engineer in SopraSteria. In his spare time he likes to coach teams and developers on better coding, collaboration, planning and product understanding.
This entry was posted in Agile Release Patterns, English, Software Development. Bookmark the permalink.

10 Responses to Database refactoring: Replace table with view

  1. Karianne Berg says:

    After working with the Smidig2010 app without having a proper admin interface, SQL does not scare me at all. :) Never seen “instead-of” triggers before, though. Very cool, will have to try.

  2. Anders S says:

    Good stuff. Last resort, but tools everyone should know.

  3. Anonymous says:

    Social Media University trains companies and individuals how to connect and drive business using the latest in social networking techniques.
    Social Media University provides online interactive classrooms, previously recorded training for the privacy of your home or office viewing, individual and group training on social media strategy and implementation. Social Media University teaches its students how to leverage the power of social media to build and maintain their clientele.
    For more please visit:

  4. I have not read it myself, but a former colleague of mine read the book ‘Database refactorings’ and found it interesting:

    I see that it contains a lot online as well with references that for people with some experience could find useful.

  5. Salman Shaik07 says:

    Good one.
    If we think of refactoring of the database by applying different methods like replacing tables with views…etc..
    What to you think should be the strategy / execution plan of the QA team to test that the functionality works fine even after re-factoring ?

    Though testing the front end is important ?
    What should be the back end database verifications which needs to eb done ?

    Would be very very help ful if you could post some help.

  6. Salman Shaik07 says:

    Please ignore typos, used mobile qwerty to type this :)

  7. Hi, Salman

    I think it’s critical to have automated tests that exercise the database schema in realistic ways. For a low-risk refactoring like replace table with view, I would trust the automated tests to catch anything serious.

    The release as a whole would of course be part of the overall test plan, but I would not take any extra action because of the database refactorings. Provided that we have good tests!

  8. Salman Shaik07 says:

    I agree with your comments.
    But, as the release date been closer, I cannot have automated tests in place now.

    Apart from having few regression checks via front end screen which access that particular table/view, what else do you think would matter testing in the database directly.

    For example, if we have dropped two existing tables and replaced it with one table and then created two views in place of existing two tables. I would mind to check
    1) If all the contsraints which were there in earlier tables hold good.

    2) If the DML hits on that views/table go fine without any error.
    3) I would check miscellaneous things like permission/grants or user specific roles on that particular table or view.

    But, would there be any scenario where replacing two tables with one table and creating two view instead of that two table mights fail ???

    I know explaining the scenario is quite difficult without pictorial representation, but would expect some great replies.

    Thanks in advance

  9. Hi, Salman

    At the level you’re asking, the question of “how much testing” is a matter of risk assessment. What are the worst things that could happen, what are their consequences, how likely are they, and how much do we want to invest in detecting the problem up front? The answer most to these questions are context specific.

    In the case of the view replacing two tables:
    * Syntax error (likely to happen without testing, easy to test)
    * The view is wrong (returns the wrong data) (likely to happen without testing, easy to test)
    * Corner cases are treated wrong (for select)
    * Insert/Update/Delete triggers contain errors
    * Corner cases are treated wrong (for Insert/Delete/Update)

    The level of testing should correspond to the complexity of the change and the risk for something going wrong.

  10. Salman Shaik07 says:

    I got the context, let me check all the possibilities until I reach the threshold of exhaustive testing for better satisfaction :)

Comments are closed.