SQL Fundamentals - Tables v. Views

SQL Fundamentals - Tables v. Views

Something that gets overlooked a lot is knowing when the proper time to use a Table and when to use a View when working with data. They both use different methods of utilizing data and they both have different efficiencies when they are used. Using the right item can save you money or time.

Let's start with an example. We will use the following tables. The first table is a list of customers. The second table is a list of locations. The purpose of our table will be to get the Home Locations for the customers and provide their first name, last name, city, and state.

The code for creating tables and views are virtually identical. In these examples, the system was Teradata. Oracle and SQL Server versions will be slightly different. This first segment of code is to create a table.

CREATE MULTISET TABLE home_loc_tbl AS(
SELECT
  a.FName, 
  a.LName,
  b.City,
  b.State
FROM
  Customer AS a
  JOIN Location AS b
    ON a.CustNum=b.MbrNum
    AND b.LocType='Home'
)WITH DATA
PRIMARY INDEX(Lname, FName);



This next segment of code is how you would do the same thing,but creating a view instead.

CREATE VIEW home_loc_vw AS (
SELECT
  a.FName, 
  a.LName,
  b.City,
  b.State
FROM
  Customer AS a
  JOIN Location AS b
    ON a.CustNum=b.MbrNum
    AND b.LocType='Home'
);

With tables or views, they are queried or used in your SQL code identically. And when we query them both, it seems we are getting the same result.

Even though the results are the same in this situation, we will see this is not always the case. Let's now say that after our table and view have been created, Member Number 1 moves to from Texas to Arizona and has his information updated. This now gives us the source data of:

Now, when we query both the table and the view, we will get different results.

Why did this happen? When you create a table, it takes all of the data the query compiled and placed it into the table. The view on the other hand only stores the logical connection you used to gather the information. That means, any time you query a view, it is actually going back to the source tables and pulling the information each time.

This gives views certain advantages, and certain drawbacks.

Some of the good points are that it simplifies code. If you have ever tried to troubleshoot SQL code and the author had used subqueries and re-used the same alias names from the main parts of the query, that can be a headache. But having the entire subquery being a view makes it look like and work mostly like a table that most people are familiar with. Another bonus is that with it only being a logical connection, any time the source data changes, the data compiled by the view is the most up to date. And finally, views do not take up much space. They only need to store the code for the query used to generate it. This will save money for organizations that are being charged for space utilization.

Though, there are times when using a view is a drawback. You can't store historic data in a view. When you query it, it is always what it is at that moment. Another drawback is that when you query the view, it has to go through all of the processing to compile the data. So, while it saves on disk space, it will add to CPU utilization which can be another cost factor.

This then may make you wonder when to use what type of item.

Use Tables:

  • If you need permanent storage of information
  • If you access the set of data repeatedly and changes are infrequent. (Saves CPU Utilization)

Use Views:

  • If the data changes rapidly
  • If you infrequently access the view. (Saves Disk Utilization)

To view or add a comment, sign in

More articles by John Pates

  • Playing with ChatGPT

    Over the past couple months, I have been using ChatGPT to provide me with insight on how to do things. One of which is…

  • Visualization Considerations for Accessibility

    Today at work, there was someone complaining about a chart we receive from outside our company. The chart in question…

  • Graph Databases for Personal Education (World of Warcraft)

    I was working on something with both learning GraphDB and thought I could apply it to a problem in World of Warcraft…

  • Assignment Method (Hungarian Method)

    The Assignment Method is an Operations Management tool typically used for minimizing the amount of cost, time or effort…

    4 Comments
  • Wrapping up the first step of my Machine Learning journey

    I can't believe I have finally finished this step of my Machine Learning journey. A couple months ago, I had a notice…

  • Continuing My Machine Learning Journey

    I am continuing this class in Machine Learning(ML). It is a topic I have found really interesting, but it is hard to…

  • GraphDB day 2

    I've been continuing playing around with GraphDB using Neo4j. It is going slower as if I make a mistake, it takes me a…

  • Adventures with GraphDB day 1

    As part of my desire to keep learning, despite not having a work "use case" to try to learn GraphDB, I am actually…

    1 Comment
  • Playing with Python and Tableau

    For my personal education, I was playing around with Python. My long term goal is to create a way to have Python scrub…

  • SQL Fundamentals - Caution When Using Division

    This is not only a SQL item, but also applies to quite a few programming languages too. When using division, you have…

Others also viewed

Explore content categories