क्रिश्चियन शॉ
  • होम
  • Blog
    • प्रोग्रामिंग
      • C#
      • पॉवरशेल
      • Python
      • SQL
    • वर्डप्रेस
      • ट्युटोरियल
    • क्लाउड
    • होम ऑटोमेशन
      • होम असिस्टेंट
        • Node-Red
    • Career
  • सेवाएं
  • शब्दकोष
  • About
No Result
View All Result
क्रिश्चियन शॉ
  • होम
  • Blog
    • प्रोग्रामिंग
      • C#
      • पॉवरशेल
      • Python
      • SQL
    • वर्डप्रेस
      • ट्युटोरियल
    • क्लाउड
    • होम ऑटोमेशन
      • होम असिस्टेंट
        • Node-Red
    • Career
  • सेवाएं
  • शब्दकोष
  • About
No Result
View All Result
क्रिश्चियन शॉ
No Result
View All Result
Home प्रोग्रामिंग SQL
sql views

SQL Views – A Complete 101 guide to work with SQL Views

by ईसाई
2022-7-अगस्त
in SQL
0

When we are working with SQL we often need a temporary place to store our data for making further queries to get just the right data. To do this we can use something called SQL Views.

The great thing about SQL Views is that you are able to pick data from several tables and then join these data one more time with other tables stored in your database. By the end of this article, you will be able to create new SQL Views, update them and insert new data into them.

विषय-सूची
  1. What are SQL Views?
  2. How to create a SQL View?
    • (demo) – Create a new table
    • (demo) – Insert data into the new table
    • Create the movie view
  3. Update your SQL Views
  4. Insert a new record into SQL Views
  5. Summary

What are SQL Views?

You can think of a SQL View as a virtual table. This new virtual table can show our selected data and be joined with extra fields from other tables. Selected data from other tables, will be stored just as we know it from a regular query. The data is shown inside rows with columns from your select statement. The difference is that the rows do not exist in the database – they are virtual.

How to create a SQL View?

It is very easy to create a new SQL View. Below is an example of how to create a new view in your Microsoft SQL database.

CREATE VIEW <Sql_View_Name> AS
  SELECT Col1, Col2 FROM TableN
  WHERE <Your_Condition>

Let’s take a look at how this would work out in real life with some read data. First, we have to create a new table.

(demo) – Create a new table

This is just a simple table to hold some data about movies. It is really simple and there are no relations to other tables.

CREATE TABLE "Movies"
(
    Id UNIQUEIDENTIFIER NOT NULL PRIMARY KEY default NEWID(),
    Title varchar(80),
    Description varchar(250),
    Rating int
);

(demo) – Insert data into the new table

In order to show how to create a new view and select some data that will be presented in the view, we have to add some data. These will be a few records just for demo purposes.

INSERT INTO Movies VALUES (default,'Top Gun: Maverick','After more than thirty years of service as one of the Navys top aviators, Pete Mitchell is where he belongs, pushing the envelope as a courageous test pilot and dodging the advancement in r...',9);
INSERT INTO Movies VALUES (default,'Thor: Love and Thunder','Thor enlists the help of Valkyrie, Korg and ex-girlfriend Jane Foster to fight Gorr the God Butcher, who intends to make the gods extinct.',7);
INSERT INTO Movies VALUES (default,'Elvis','Elvis in Baz Luhrmanns biopic of Elvis Presley, from his childhood to becoming a rock and movie star in the 1950s while maintaining a complex relationship with his manager, Colonel Tom Park..',8);
INSERT INTO Movies VALUES (default,'Minions: The Rise of Gru','The untold story of one twelve-year-olds dream to become the worlds greatest supervillain.',7);
INSERT INTO Movies VALUES (default,'Amsterdam','Set in the 30s, it follows three friends who witness a murder, become suspects themselves, and uncover one of the most outrageous plots in American history.',5);
INSERT INTO Movies VALUES (default,'Doctor Strange in the Multiverse of Madness','Doctor Strange teams up with a mysterious teenage girl from his dreams who can travel across multiverses, to battle multiple threats, including other-universe versions of himself, which thre...',7);

Create the movie view

With the data in our database, we are now ready to create a new SQL View to showcase our movie data. This is done as below:

CREATE VIEW Movie_View AS
  SELECT Id, title, description, rating FROM Movies
  WHERE rating > 7

Let’s call our newly created SQL View named Movie_View using the following query: select * from movie_view.

sql views
SQL View – Movie_View

Great! We got our view as expected. This is returning movies with the columns we have decided to include in the view. What if we would like to change what columns are included in our SQL View without having to delete them? It’s easy – we can simply perform an UPDATE of our SQL View.

Update your SQL Views

It is not possible to update a SQL View with more than one view (just a head up). Below is the syntax for updating an already existing view:

ALTER VIEW <Name_Of_View> AS
  SELECT Col1, Col2, Col3 FROM TableN
  WHERE <Your_Condition>

Below is the SQL code for updating the already existing view we created before to show our movies in the demo table.

ALTER VIEW Movie_View AS
  SELECT title, description, rating FROM Movies
  WHERE rating > 7

Let’s check the output of our ALTERED table in our SQL View.

Altered SQL View

Perfect! Our SQL View got updated and now only shows: the title, description, and rating. Just as we wanted it to. Let’s move on to the last part of this article where we will insert a new movie into the view.

Insert a new record into SQL Views

From time to time we have to insert new data into our views – not only select them (view them). In the code snippet below I have written the syntax for how to insert new data into a view.

INSERT INTO <Name_Of_View>(Col1, Col2, Col3)
  VALUES(Val1, Val2, Val3);

To insert a new movie into our previously created SQL View, we can execute the following SQL code:

INSERT INTO Movie_View(title, description, rating)
  VALUES('The Dark Knight','When the menace known as the Joker wreaks havoc and chaos on the people of Gotham, Batman must accept one of the greatest psychological and physical tests of his ability to fight injustice.',9);

Let’s select all records in our view to verify that our new movie got added to the view.

sql views, insert new data
Select statement for table and view to show the difference

Awesome! The movie data got inserted into our SQL View and is now a part of the table.

Summary

In this article you learned how to create, view, alter and add new data to your SQL Views. SQL Views are really simple to work with and give a lot of power in terms of modifying and altering data before processing them further in our other applications.

I hope you learned something new or had your problems solved. If you got any questions, please let me know in the comments. Thank you for reading – until next time, happy coding!

Tags: MicrosoftMS SQLSQLSQL ViewViews
Previous Post

How to do pattern matching in switch statements – C# version >= 7.0

Next Post

How to Compose an ASP.NET Core Web API (.NET 6) with an MS SQL Server 2022 on Linux in Docker

ईसाई

ईसाई

Hello 👋 My name is Christian and I am 26 years old. I'm an educated Software Developer with a primary focus on C#, .NET Core, Python, and PowerShell. Currently, I'm expanding my skills in Software Robots and Cloud Architecture. In some of my spare time, I share my knowledge about tech stuff on my blog.

Related Posts

Dockerize ASP.NET Core
Docker

How to Compose an ASP.NET Core Web API (.NET 6) with an MS SQL Server 2022 on Linux in Docker

by ईसाई
2022-19-जुलाई
0

In this quick-start guide, I will teach you how to set up and run a simple ASP.NET Core Web API...

Read more
Next Post
Dockerize ASP.NET Core

How to Compose an ASP.NET Core Web API (.NET 6) with an MS SQL Server 2022 on Linux in Docker

प्रातिक्रिया दे जवाब रद्द करें

आपका ईमेल पता प्रकाशित नहीं किया जाएगा. आवश्यक फ़ील्ड चिह्नित हैं *

क्रिश्चियन शॉ

क्रिश्चियन शॉ

Software Developer

Hello - my name is Christian and I am 26 years old. I'm an educated Software Developer with a primary focus on C#, .NET Core, Python, and PowerShell. Currently, I'm expanding my skills in Software Robots and Cloud Architecture. In some of my spare time, I share my knowledge about tech stuff on my blog.

Recent articles

personal website
Career

Top 6 things to add on your personal website to get hired for a tech job

by ईसाई
2022-7-अगस्त
0

Back in the days before the internet was a thing like it is today, we used to have business cards...

Read more
watchdog

The #1 guide to show real-time .NET 6 logs for Web Apps and APIs in a modern way using WatchDog for Free

2022-13-अगस्त
get hired for a tech job

5 tips to help you get hired for a tech job

2022-31-जुलाई
restful web api

How to build a RESTful Web API using ASP.NET Core and Entity Framework Core (.NET 6)

2022-25-जुलाई
dynamically register entities

How to Dynamically Register Entities in DbContext by Extending ModelBuilder?

2022-23-जुलाई

क्रिश्चियन शॉ

Software Developer

Hello - my name is Christian and I am 26 years old. I'm an educated Software Developer with a primary focus on C#, .NET Core, Python, and PowerShell. Currently, I'm expanding my skills in Software Robots and Cloud Architecture. In some of my spare time, I share my knowledge about tech stuff on my blog.

Recent articles

personal website

Top 6 things to add on your personal website to get hired for a tech job

2022-7-अगस्त
watchdog

The #1 guide to show real-time .NET 6 logs for Web Apps and APIs in a modern way using WatchDog for Free

2022-13-अगस्त
get hired for a tech job

5 tips to help you get hired for a tech job

2022-31-जुलाई
  • hi_INहिन्दी
    • da_DKDansk
    • en_USEnglish
    • de_DEDeutsch
    • pt_BRPortuguês do Brasil
  • Contact
  • गोपनीयता नीति
  • सेवा की शर्तें

© 2022 क्रिश्चियन शॉ - All rights reserved.

No Result
View All Result
  • होम
  • Blog
    • प्रोग्रामिंग
      • C#
      • पॉवरशेल
      • Python
      • SQL
    • वर्डप्रेस
      • ट्युटोरियल
    • क्लाउड
    • होम ऑटोमेशन
      • होम असिस्टेंट
    • Career
  • सेवाएं
  • शब्दकोष
  • About

© 2022 क्रिश्चियन शॉ - All rights reserved.

मैं आपकी वरीयताओं को याद करके और बार-बार आने वाली यात्राओं को याद करके आपको सबसे अधिक प्रासंगिक अनुभव देने के लिए अपनी वेबसाइट पर कुकीज़ का उपयोग करता हूं। “स्वीकार करें” पर क्लिक करके, आप सभी कुकीज़ के उपयोग के लिए सहमति देते हैं।
मेरी निजी जानकारी न बेचें.
कुकी सेटिंगACCEPT
गोपनीयता और कुकीज़ नीति

गोपनीयता अवलोकन

This website uses cookies to improve your experience while you navigate through the website. Out of these cookies, the cookies that are categorized as necessary are stored on your browser as they are essential for the working of basic functionalities of the website. We also use third-party cookies that help us analyze and understand how you use this website. These cookies will be stored in your browser only with your consent. You also have the option to opt-out of these cookies. But opting out of some of these cookies may have an effect on your browsing experience.
Necessary
Always Enabled
Necessary cookies are absolutely essential for the website to function properly. This category only includes cookies that ensures basic functionalities and security features of the website. These cookies do not store any personal information.
Functional
Functional cookies help to perform certain functionalities like sharing the content of the website on social media platforms, collect feedbacks, and other third-party features.
Performance
Performance cookies are used to understand and analyze the key performance indexes of the website which helps in delivering a better user experience for the visitors.
Analytics
Analytical cookies are used to understand how visitors interact with the website. These cookies help provide information on metrics the number of visitors, bounce rate, traffic source, etc.
CookieDurationDescription
__gads1 year 24 daysThe __gads cookie, set by Google, is stored under DoubleClick domain and tracks the number of times users see an advert, measures the success of the campaign and calculates its revenue. This cookie can only be read from the domain they are set on and will not track any data while browsing through other sites.
_ga2 yearsThe _ga cookie, installed by Google Analytics, calculates visitor, session and campaign data and also keeps track of site usage for the site's analytics report. The cookie stores information anonymously and assigns a randomly generated number to recognize unique visitors.
_ga_0J2F6JVWSD2 yearsThis cookie is installed by Google Analytics.
_gat_gtag_UA_84232734_11 minuteSet by Google to distinguish users.
_gid1 dayInstalled by Google Analytics, _gid cookie stores information on how visitors use a website, while also creating an analytics report of the website's performance. Some of the data that are collected include the number of visitors, their source, and the pages they visit anonymously.
YouTube2 yearsYouTube sets this cookie via embedded youtube-videos and registers anonymous statistical data. I embed YouTube videos in my articles/tutorials - you won't get the full experience of the articles if this is deactivated.
Advertisement
Advertisement cookies are used to provide visitors with relevant ads and marketing campaigns. These cookies track visitors across websites and collect information to provide customized ads.
CookieDurationDescription
IDE1 year 24 daysGoogle DoubleClick IDE cookies are used to store information about how the user uses the website to present them with relevant ads and according to the user profile.
test_cookie15 minutesThe test_cookie is set by doubleclick.net and is used to determine if the user's browser supports cookies.
VISITOR_INFO1_LIVE5 months 27 daysA cookie set by YouTube to measure bandwidth that determines whether the user gets the new or old player interface.
YSCsessionYSC cookie is set by Youtube and is used to track the views of embedded videos on Youtube pages.
yt-remote-connected-devicesneverYouTube sets this cookie to store the video preferences of the user using embedded YouTube video.
yt-remote-device-idneverYouTube sets this cookie to store the video preferences of the user using embedded YouTube video.
Others
Other uncategorized cookies are those that are being analyzed and have not been classified into a category as yet.
SAVE & ACCEPT
Powered by CookieYes Logo