The difference between temporary tables and views in sql--understanding from the perspective of application scenarios

When learning SQL for the first time, the concepts of views and temporary tables are not easy to distinguish. The following are some application scenarios of views and temporary tables, hoping to understand the difference between the two.

1. View concept:

Simply put, the view is a virtual thing generated according to your sql statement, and it does not occupy the space of the database itself

For example, there was this table

create table table_1(id int,name varchar(100))

Then there is such a view

create view view_1 as select id from table_1

When the data in your table is added or deleted, the content in your view also changes.

In short, you cannot update or insert into the view

To put it bluntly, the change of the view changes with the change of the table

Unless you recreate or replace view_1, you can change the things in this view

 2. The concept of temporary table:

Temporary table is very simple, MySQL temporary table is very useful when we need to save some temporary data. Temporary tables are only visible in the current connection. When the connection is closed, Mysql will automatically delete the table and release all space. It means that the temporary table is stored in the memory, and the temporary created table, after you run the SQL statement and turn it off, it will be gone. Therefore, we often only use temporary tables when stored procedures or frequently operating statements. (The application scenarios for temporary tables are given below)

When to use views?

Application Scenario 1: Confidential work, for example, there is an employee salary table. If you only want the finance to see the employee salary field, but others cannot see the salary field, then use a view to filter out the sensitive field salary.

Application Scenario 2: There is a very complex query statement, there are about 100 rows, and sometimes I want to associate this huge select statement with other tables to get the results. It is very troublesome to write too much. You can use a view to replace these 100 row select statement, acting as a variable role

When to use a temporary table?

Application Scenario 1: You have a lot of DML operations in a short period of time, such as the shopping cart form of Jingdong Taobao and Amazon, putting things into the shopping cart (insert), changing the quantity (update), deleting the product (delete), once the money is settled, these data It is about to be cleared, and a temporary table is required at this time.

Scenario 2: When exporting data, you may not want to export the complete database or table, you may only want to export data that meets certain conditions, then you can create a temporary table, insert the select statement into the temporary table, and then export This temporary table, after the guide is finished, will automatically clean up these useless data by ending the session or transaction.

Application scenario 3: When you write a stored procedure, there are many connections. For example, you need to connect as many tables as A, B, C, D, E, F, G, and H to get your result table and do the connection at the same time. The consumption is too high, you can first put the result of A, B, C connection in the temporary table, and then connect this temporary table with D, E, F, and put it in the temporary table as a new result, and then put The temporary table is connected with G and H, and finally the temporary table data is obtained, which is inserted into the result table (permanent table) at one time.

Summarize:

Let’s talk about the difference first (because the difference is still very big, let’s focus on it)

The view is for the sake of security, not allowing users to see the structure of the table, in fact, the virtual table does not exist at all, and the presentation method is actually a piece of sql code

Temporary tables are basically encountered in the process of sql operation and assist us in the realization of sql statements. There is a scenario you need to remember to make you understand the concept of temporary tables:

After your sql query runs out of results, the subsequent operations need to use the results of your query above (that is to say, operate on the results of your query). In this case, we need to create a temporary table and save the results in the temporary table. middle! ! ! !

These are all detours encountered in the database of their own learning, purely personal opinions! What is wrong, hope to point out, thanks

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325522505&siteId=291194637