In this article you will come to know about the followings things,
  1. What is Views?
  2. Why we should use Views?
  3. What is System Views?
  4. What is User Defined Views?
  5. Syntax of User Views
  6. Step by Step View Creation & Execution.

    1. Single Table View
    2. Multiple Table View

What is Views?

As the name implies, VIEWS. SQL SELECT query is created only for viewing/display purposes. Views is a virtual table only used for viewing purpose. Views is created with the help of individual table or joining of tables. In short, in view we can join the fields from one or more database tables.
You can create multiple views of one table and one view with multiple tables.
View gives you the power to decide what column(s) / fields(s) display or not. We can easily maintain and display the column(s) as per privileges of user.

Why should we use views?

Views allow administrator and user to only view the records of tables. Insert, Update and Delete functionality restriction can be implemented with views very easily; that's why we should use VIEW.
By the way Single Table View can be used to Insert, Update and Delete the records. But multiple table VIEW cannot Update, Insert and Delete the records.

What is System Views?

Those views attached with system database and provide a detailed view of System Database called System Views.
Two types of System Views,
  • Information Schema
  • Catalog View

What is User Defined Views?

The View created by a user for specific tasksare called User Defined Views.
Mostly user defined views are created on user defined database not on system database.

Syntax of User Views

Single Table View
  1. CREATE VIEW <view_name> AS
  2. SELECT <column1>, <column2>. . .
  3. FROM <table>
  4. WHERE conditions;
MultiTable View
  1. CREATE VIEW <view_name> AS
  2. SELECT <column1>, <column2>. . .
  3. FROM <table> Join <Table>
  4. WHERE conditions;

Step by Step View Creation

Create Table
  1. ---tblEmployees Create Script
  2. USE [MbkTest]
  3. GO
  4. /****** Object: Table [dbo].[tblEmployees] Script Date: 26-Dec-19 12:07:27 PM ******/
  5. SET ANSI_NULLS ON
  6. GO
  7. SET QUOTED_IDENTIFIER ON
  8. GO
  9. SET ANSI_PADDING ON
  10. GO
  11. CREATE TABLE [dbo].[tblEmployees](
  12. [EmployeeID] [int] IDENTITY(1,1) NOT NULL,
  13. [PhoneNumber] [varchar](50) NULL,
  14. [SkillID] [int] NULL,
  15. [YearsExperience] [int] NULL,
  16. [EmployeeName] [nvarchar](50) NULL,
  17. PRIMARY KEY CLUSTERED
  18. (
  19. [EmployeeID] ASC
  20. )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
  21. ) ON [PRIMARY]
  22. GO
  23. SET ANSI_PADDING OFF
  24. GO
  25. ----tblSkills Create script
  26. /****** Object: Table [dbo].[tblSkills] Script Date: 26-Dec-19 3:54:52 PM ******/
  27. SET ANSI_NULLS ON
  28. GO
  29. SET QUOTED_IDENTIFIER ON
  30. GO
  31. SET ANSI_PADDING ON
  32. GO
  33. CREATE TABLE [dbo].[tblSkills](
  34. [SkillID] [int] IDENTITY(1,1) NOT NULL,
  35. [Title] [varchar](50) NULL,
  36. PRIMARY KEY CLUSTERED
  37. (
  38. [SkillID] ASC
  39. )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
  40. ) ON [PRIMARY]
  41. GO
  42. SET ANSI_PADDING OFF
  43. GO
Sample Datas
  1. ---tblEmployees Sample Records
  2. GO
  3. SET IDENTITY_INSERT [dbo].[tblEmployees] ON
  4. INSERT [dbo].[tblEmployees] ([EmployeeID], [PhoneNumber], [SkillID], [YearsExperience], [EmployeeName]) VALUES (1, N'9869569634', 2, 11, N'Suhana Kalla')
  5. INSERT [dbo].[tblEmployees] ([EmployeeID], [PhoneNumber], [SkillID], [YearsExperience], [EmployeeName]) VALUES (2, N'9869166077', 8, 14, N'Ashish Kalla')
  6. INSERT [dbo].[tblEmployees] ([EmployeeID], [PhoneNumber], [SkillID], [YearsExperience], [EmployeeName]) VALUES (3, N'9869569634', 1, 24, N'Manoj Kalla')
  7. INSERT [dbo].[tblEmployees] ([EmployeeID], [PhoneNumber], [SkillID], [YearsExperience], [EmployeeName]) VALUES (4, N'9969359746', 6, 20, N'Nirupama Kalla')
  8. INSERT [dbo].[tblEmployees] ([EmployeeID], [PhoneNumber], [SkillID], [YearsExperience], [EmployeeName]) VALUES (5, N'9869166012', 7, 28, N'Rajesh Bohra')
  9. INSERT [dbo].[tblEmployees] ([EmployeeID], [PhoneNumber], [SkillID], [YearsExperience], [EmployeeName]) VALUES (6, N'9261166012', 5, 18, N'Murli Vyas')
  10. INSERT [dbo].[tblEmployees] ([EmployeeID], [PhoneNumber], [SkillID], [YearsExperience], [EmployeeName]) VALUES (7, N'9161569634', 2, 5, N'Magan Shukla')
  11. INSERT [dbo].[tblEmployees] ([EmployeeID], [PhoneNumber], [SkillID], [YearsExperience], [EmployeeName]) VALUES (8, N'9219166077', 4, 7, N'Jagat Pratap')
  12. INSERT [dbo].[tblEmployees] ([EmployeeID], [PhoneNumber], [SkillID], [YearsExperience], [EmployeeName]) VALUES (9, N'9459569634', 6, 10, N'Suresh Kamalkar')
  13. INSERT [dbo].[tblEmployees] ([EmployeeID], [PhoneNumber], [SkillID], [YearsExperience], [EmployeeName]) VALUES (10, N'9687359746', 8, 2, N'Hari Vidhan')
  14. SET IDENTITY_INSERT [dbo].[tblEmployees] OFF
  15. ---tblSkills Sample Records
  16. SET IDENTITY_INSERT [dbo].[tblSkills] ON
  17. INSERT [dbo].[tblSkills] ([SkillID], [Title]) VALUES (1, N'Visual Foxpro')
  18. INSERT [dbo].[tblSkills] ([SkillID], [Title]) VALUES (2, N'C#')
  19. INSERT [dbo].[tblSkills] ([SkillID], [Title]) VALUES (3, N'VB.NET')
  20. INSERT [dbo].[tblSkills] ([SkillID], [Title]) VALUES (4, N'Delphi')
  21. INSERT [dbo].[tblSkills] ([SkillID], [Title]) VALUES (5, N'Java')
  22. INSERT [dbo].[tblSkills] ([SkillID], [Title]) VALUES (6, N'Power Builder')
  23. INSERT [dbo].[tblSkills] ([SkillID], [Title]) VALUES (7, N'COBOL')
  24. INSERT [dbo].[tblSkills] ([SkillID], [Title]) VALUES (8, N'Python')
  25. SET IDENTITY_INSERT [dbo].[tblSkills] OFF

Creating Single Table View

You can see in view we had used a single table called tblEmployees and only three columns are shown in output. Columns are EmployeeID, EmployeeName, and YearsExperience.
VIEW COMMAND
  1. CREATE VIEW YearExperienceMoreThan5YearsLess10Years
  2. AS
  3. SELECT EmployeeID, EmployeeName,YearsExperience
  4. FROM tblEmployees a
  5. WHERE a.YearsExperience >= 5 and a.YearsExperience <= 9
Views In SQL Server
Views In SQL Server
Refresh your database
Views In SQL Server
Output - Testing View
You can see all records of tblEmployees :
Command
select * from tblEmployees
Views In SQL Server
Now we execute/run our VIEW called,
Command
select * from [dbo].[YearExperienceMoreThan5YearsLess10Years]
Views In SQL Server

Creating Multiple Table View

You can see in view we had used two tables,
  • tblEmployees
  • tblSkills
We had shown the following columns,
  • EmployeeID
  • EmployeeName
  • YearsExperience
  • Skills
VIEW COMMAND
  1. CREATE VIEW EmployeeWithSkillDetail
  2. AS
  3. SELECT a.EmployeeID, a.EmployeeName,a.YearsExperience,Skill = b.Title
  4. FROM tblEmployees a
  5. INNER JOIN tblSkills b
  6. ON a.SkillID = b.SkillID
Views In SQL Server
Refresh Database and see view is created successfully,
Views In SQL Server
OUTPUT
Now we execute/run our VIEW called,
Command
select * from [dbo].[EmployeeWithSkillDetail]
Views In SQL Server
Happy Coding. .