What is the difference between a stored procedure and a view?

后端 未结 10 1972
星月不相逢
星月不相逢 2020-12-04 04:59

I am confused about a few points:

  1. What is the difference between a stored procedure and a view?

  2. When should I use stored procedures, and whe

10条回答
  •  广开言路
    2020-12-04 05:45

    A view is a simple way to save a complex SELECT in the database.

    A store procedure is used when simple SQL just isn't enough. Store procedures contain variables, loops and calls to other stored procedures. It's a programming language, not a query language.

    1. Views are static. Think of them as new tables with a certain layout and the data in them is created on the fly using the query you created it with. As with any SQL table, you can sort and filter it with WHERE, GROUP BY and ORDER BY.

    2. The depends on what you do.

    3. The depends on the database. Simple views just run the query and filter the result. But databases like Oracle allow to create a "materialized" view which is basically a table which is updated automatically when the underlying data of the view changes.

      A materialized view allows you to create indexes on the columns of the view (especially on the computed columns which don't exist anywhere in the database).

    4. I don't understand what you're talking about.

提交回复
热议问题