{"id":315,"date":"2016-05-24T21:41:26","date_gmt":"2016-05-25T04:41:26","guid":{"rendered":"https:\/\/database.guide\/?p=315"},"modified":"2023-02-14T12:46:00","modified_gmt":"2023-02-14T02:46:00","slug":"what-is-a-view","status":"publish","type":"post","link":"https:\/\/database.guide\/what-is-a-view\/","title":{"rendered":"What is a View?"},"content":{"rendered":"<p>In the world of\u00a0databases,\u00a0a <dfn>view<\/dfn> is a query that&#8217;s stored on a\u00a0<a href=\"https:\/\/database.guide\/what-is-a-database\/\">database<\/a>.<\/p>\n<p>The term can also be used to\u00a0refer to\u00a0the <em>result set<\/em> of a\u00a0stored\u00a0query.<\/p>\n<p>To create a view, you write a query, then save\u00a0it as a view.<\/p>\n<p>To run\u00a0a\u00a0view, you\u00a0query it, just like you&#8217;d query\u00a0a table. The difference is that, the view itself is a query.\u00a0So\u00a0when you query the view, you&#8217;re effectively querying a query. \u00a0This enables you to save complex queries as views, then run\u00a0simple queries against those views.<\/p>\n<p><!--more--><\/p>\n<p>Views are sometimes referred to as virtual tables, because they present data in the form of a <a href=\"https:\/\/database.guide\/what-is-a-table\/\">table<\/a>, but without such a table existing in the database.<\/p>\n<p>A view can present data from multiple tables and present it\u00a0as though it&#8217;s in a single table (just like any other <code>SELECT<\/code> query). \u00a0So when creating a view (just as creating any <code>SELECT<\/code> query), you specify which <a href=\"https:\/\/database.guide\/what-is-a-column\/\">columns<\/a> to display.<\/p>\n<h2>Creating a View<\/h2>\n<p>Views are created using the (SQL) <code>CREATE VIEW<\/code> statement. So for example, to create a view called say, &#8220;NewCustomers&#8221; you would start with:<\/p>\n<pre>CREATE VIEW NewCustomers AS<\/pre>\n<p>Followed by the query that makes up the view.<\/p>\n<p>Here&#8217;s an example of a <a href=\"https:\/\/database.guide\/how-to-create-a-view-in-sql-server\/\">view that has just been created in SQL Server<\/a>:<\/p>\n<figure id=\"attachment_307\" aria-describedby=\"caption-attachment-307\" style=\"width: 590px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/database.guide\/wp-content\/uploads\/2016\/05\/sql_server_2014_views_1.png\"><img loading=\"lazy\" decoding=\"async\" class=\"wp-image-307 size-full\" src=\"https:\/\/database.guide\/wp-content\/uploads\/2016\/05\/sql_server_2014_views_1.png\" alt=\"Screenshot of a SQL view script in SQL Server 2014\" width=\"600\" height=\"495\" srcset=\"https:\/\/database.guide\/wp-content\/uploads\/2016\/05\/sql_server_2014_views_1.png 600w, https:\/\/database.guide\/wp-content\/uploads\/2016\/05\/sql_server_2014_views_1-300x248.png 300w\" sizes=\"auto, (max-width: 600px) 100vw, 600px\" \/><\/a><figcaption id=\"caption-attachment-307\" class=\"wp-caption-text\">A SQL view has just been created, and now appears in the Object Explorer under the &#8220;Views&#8221; node.<\/figcaption><\/figure>\n<h2>Querying\u00a0a View<\/h2>\n<p>You &#8220;run&#8221; a view by\u00a0querying it, just as you&#8217;d query a table. So, for example, you could perform a <code>SELECT<\/code> query against the NewCustomers view like this:<\/p>\n<pre>SELECT * FROM NewCustomers<\/pre>\n<p>You can also narrow the results down, just as you can with a normal query. So, you could also do this, for example:<\/p>\n<pre>SELECT * FROM NewCustomers\r\n\r\nWHERE OrderValue &gt; $150\r\n<\/pre>\n<p>Here&#8217;s an example of querying the\u00a0view that was created in the previous screenshot:<\/p>\n<figure id=\"attachment_308\" aria-describedby=\"caption-attachment-308\" style=\"width: 423px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/database.guide\/wp-content\/uploads\/2016\/05\/sql_server_2014_views_2.png\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-308\" src=\"https:\/\/database.guide\/wp-content\/uploads\/2016\/05\/sql_server_2014_views_2.png\" alt=\"Screenshot of running a SQL view in SQL Server 2014\" width=\"433\" height=\"320\" srcset=\"https:\/\/database.guide\/wp-content\/uploads\/2016\/05\/sql_server_2014_views_2.png 433w, https:\/\/database.guide\/wp-content\/uploads\/2016\/05\/sql_server_2014_views_2-300x222.png 300w\" sizes=\"auto, (max-width: 433px) 100vw, 433px\" \/><\/a><figcaption id=\"caption-attachment-308\" class=\"wp-caption-text\">Run a view by using a SELECT statement.<\/figcaption><\/figure>\n<h2>Updatable Views<\/h2>\n<p>Some views can be used to update data. So in this case,\u00a0you can use an <code>INSERT<\/code>, <code>UPDATE<\/code>, or <code>DELETE<\/code> statement to modify the data in the underlying base tables.<\/p>\n<p>However, there are many cases where you can&#8217;t\u00a0update data through a view, for example, if it&#8217;s returning aggregate data (such as <code>SUM()<\/code>, <code>AVG()<\/code>, etc).\u00a0It&#8217;s best to check\u00a0the\u00a0documentation for your DBMS to see what\u00a0restrictions are placed\u00a0on updatable views.<\/p>\n<h2>Advantages of Views<\/h2>\n<p>Views provide several\u00a0advantages over tables. Here are some of the main benefits of views over tables:<\/p>\n<h3>Simplified Code<\/h3>\n<p>Views\u00a0can\u00a0allow you to write simpler queries. So, rather than running a complex query against a table, you can run a simple query against a view (which contains the complex query).<\/p>\n<h3>Enhanced Security<\/h3>\n<p>Views can restrict access to only the data that a given user is authorised to access.<\/p>\n<p>For example, you could give a user access to a view, but not the underlying table\/s that the view queries. Therefore, the user will only have access to the columns defined in the view.By excluding certain columns in the view, you are denying the user access to the data in those columns.<\/p>\n<h3>Better Usability<\/h3>\n<p>Views can present data in a simplified way.\u00a0So data can be presented in a way that makes more sense to the user\u00a0(rather than imposing the underlying table structure on them).<\/p>\n<h3>Better\u00a0Maintainability<\/h3>\n<p>Views can also help in cases where the table structure is changed.<\/p>\n<p>If a\u00a0database&#8217;s table structure is changed, a view can make this seamless to the user. Users can continue to query the view without knowing the underlying tables have been changed.\u00a0In this case, the view could be updated to query the new table structure, while returning the same &#8220;virtual table&#8221; to the user, along with the same column names.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In the world of\u00a0databases,\u00a0a view is a query that&#8217;s stored on a\u00a0database. The term can also be used to\u00a0refer to\u00a0the result set of a\u00a0stored\u00a0query. To create a view, you write a query, then save\u00a0it as a view. To run\u00a0a\u00a0view, you\u00a0query it, just like you&#8217;d query\u00a0a table. The difference is that, the view itself is a &#8230; <a title=\"What is a View?\" class=\"read-more\" href=\"https:\/\/database.guide\/what-is-a-view\/\" aria-label=\"Read more about What is a View?\">Read more<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[17],"tags":[18,166,167,93,24,20],"class_list":["post-315","post","type-post","status-publish","format-standard","hentry","category-database-concepts","tag-database-terms","tag-define","tag-definition","tag-functions","tag-views","tag-what-is"],"_links":{"self":[{"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/posts\/315","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/comments?post=315"}],"version-history":[{"count":9,"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/posts\/315\/revisions"}],"predecessor-version":[{"id":472,"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/posts\/315\/revisions\/472"}],"wp:attachment":[{"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/media?parent=315"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/categories?post=315"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/tags?post=315"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}