<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: TrilochanSahoo</title>
    <description>The latest articles on DEV Community by TrilochanSahoo (@trilochansahoo).</description>
    <link>https://dev.to/trilochansahoo</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F802516%2F1a0401d9-0d8d-47ad-ab6c-2a2707f3b48d.jpg</url>
      <title>DEV Community: TrilochanSahoo</title>
      <link>https://dev.to/trilochansahoo</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/trilochansahoo"/>
    <language>en</language>
    <item>
      <title>Views in SQL Server</title>
      <dc:creator>TrilochanSahoo</dc:creator>
      <pubDate>Mon, 24 Jan 2022 17:30:12 +0000</pubDate>
      <link>https://dev.to/trilochansahoo/views-in-sql-server-4006</link>
      <guid>https://dev.to/trilochansahoo/views-in-sql-server-4006</guid>
      <description>&lt;p&gt;The view is a significant part of SQL and interesting to learn. However, it is kind of hard to understand for beginners. The view is the whole module within it. Hence, the attempt to simplify the module as much as possible.&lt;/p&gt;

&lt;p&gt;So let's get started.&lt;/p&gt;

&lt;h2&gt;
  
  
  The View
&lt;/h2&gt;

&lt;p&gt;In general, &lt;strong&gt;view&lt;/strong&gt; means sight or outlook of a particular object or place or something. In SQL, View is a &lt;strong&gt;virtual table&lt;/strong&gt; which is the &lt;strong&gt;result set of stored queries&lt;/strong&gt;. Put differently, a SQL table stores data in the database and takes memory to store the data. When we create a view from an original table or a base table, we add some columns and rows according to our requirements. After the execution of the query, the view shows the only results of the query in a table-like format. It only shows the output of the query rather than storing the data of the base table.&lt;/p&gt;

&lt;p&gt;Then arises one question, what happens when we operate on both sides? As an answer, when we perform any operation and change the actual data in the base table, it will also change the value of the view as it is dependent on the base table. But vice versa, it depends upon us. We can put restrictions on performing various operations according to our requirements. If we restrict all DML (Data Manipulation Language) commands on the view then, it acts as a &lt;strong&gt;read-only view&lt;/strong&gt; and does not affect the view as well as the base table.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--IrpXi7IJ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/92de3zpyq2jlrba6v3wr.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--IrpXi7IJ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/92de3zpyq2jlrba6v3wr.png" alt="Diagram of View" width="880" height="368"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Advantages
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Restricting the access:&lt;/strong&gt; Views provide an additional level of security by restricting access to a predetermined set of rows and columns of a table. &lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;em&gt;&lt;strong&gt;Example:&lt;/strong&gt; In a lecture table of a university database, it may contain many columns. But we can create 5-6 columns and give them to the user. As a result, we create an additional level of security on the top of the view.&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Simplify Complex queries for users:&lt;/strong&gt; Using views, we select information from multiple tables without the help of Joins.&lt;/li&gt;
&lt;/ul&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Provide Data Independence:&lt;/strong&gt; If we do any changes in the conceptual level view of the data, then the user-level view of the data would not be affected.&lt;/li&gt;
&lt;/ul&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Different Views on the same data:&lt;/strong&gt; We create different views on the same table and add different restrictions.&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;em&gt;&lt;strong&gt;Example:&lt;/strong&gt; In the university for student marks table, we can create different views based on lecture and student. The lecture has the privilege to change or update and add records where the student only read that data.&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  Managing Views
&lt;/h2&gt;

&lt;p&gt;There are different aspects related to managing views, which are given below.&lt;/p&gt;

&lt;h3&gt;
  
  
  Creating view:
&lt;/h3&gt;

&lt;p&gt;Views can be created by using the “create view” statement.&lt;br&gt;
&lt;em&gt;&lt;strong&gt;Syntax:-&lt;/strong&gt;&lt;/em&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;CREATE VIEW view_name AS
SELECT column1, column2, …
FROM table_name
WHERE condition;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;&lt;em&gt;Code Snippet:&lt;/em&gt;&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;CREATE VIEW [India Customers] AS
SELECT CustomerID, CustomerName
FROM Customers
WHERE Country = ‘India’;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Modifying View:
&lt;/h3&gt;

&lt;p&gt;By using the “Alter View” command allows us to modify a view.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;&lt;strong&gt;Syntax:-&lt;/strong&gt;&lt;/em&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;ALTER VIEW view_name AS
SELECT column1, column2, …
FROM table_name
WHERE condition;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;&lt;em&gt;Code Snippet:&lt;/em&gt;&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;ALTER VIEW [India Customers] 
AS 
SELECT CustomerID, CustomerName, CustomerAddress
FROM Customers
WHERE Country = ‘India’;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Removing View:
&lt;/h3&gt;

&lt;p&gt;View can be removed by using the “Drop view” statement.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;&lt;strong&gt;Syntax:-&lt;/strong&gt;&lt;/em&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;DROP VIEW view_name;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;&lt;em&gt;Code Snippet:&lt;/em&gt;&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;DROP VIEW [India Customers];
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That's the view in a nutshell.&lt;/p&gt;

&lt;p&gt;Thanks for reading the blog. Feel free to give suggestions for any areas of improvement. :)&lt;/p&gt;

</description>
      <category>beginners</category>
      <category>sql</category>
      <category>programming</category>
      <category>database</category>
    </item>
  </channel>
</rss>
