Showing posts with label Views. Show all posts
Showing posts with label Views. Show all posts

Views in SQL Server with Example-Part1

Hello friends, today I am going to talk about one of my favorite topic “Views” in SQL Server. This topic is one of my favorite topics. Whenever I ask somebody why we need Views in SQL Server, I never get a satisfactory answer. Hence I thought to share my thoughts about this topic. Let’s see the definition of View first then we learn why we need it.

What is a View: - A View in SQL Server can be defined as a logical/virtual table structure whose content (rows and columns) is defined by a select statement of the View.

Need of View: - It is important to understand the need of Views, below are the points which define why a View is important.

Security Mechanism: - The most important fact of existence of a View is, its Security Mechanism. To understand the security mechanism of a View, let’s take an example. I have client’s financial database with me and I do not want to reveal all the sensitive information of their employees (in Employee table) to all of the developers in my development team. What would I do?

Solution :-  I will create a View on Employee table  with only limited set of columns(which are not confidential) of "Employee" table  and create a new login and grant permission to access only the View not the "Employee" table. To understand this lets take an example.

I have created "Employee" table using below SQL statement and inserted few rows into it.

Create table Employee(Eid int,Name varchar(50),Basic_Sal int,HRA int)

Insert into Employee values(101,'Neeraj',50000,10000)
Insert into Employee values(102,'Ram',30000,5000)
Insert into Employee values(103,'Rachit',24000,4900)
Insert into Employee values(104,'Akshay',49000,23000)
Insert into Employee values(105,'Ankit',38000,4300)

Let’s create a View using Below SQL Statement (Syntax of Creating a Simple View):-

Create view vw_Employeeà [Create View followed by View Name]
as
select Eid,Name from Employee à[Statement defining the View]

Let’s create a Login name “User” by following below steps:-

We will login to SQL Server using “Sa” credential or “Windows Authentication” as these two credentials are having full access to the Server. Then right click on the login folder and click on “New Login” menu as shown in the below screenshot.


Once we click on the “New Login” Menu, a New screen will appear as shown in the below screenshot.


In the above screenshot, there are five circles. In first circle, I have put the name of the Login “User”.In the second circle, I have selected the “SQL Server authentication” mode. In third circle, I have set the password and confirmed the password. In fourth circle, I have unchecked the “Enforced password policy”. 

Click on the Menu “User Mapping” (fifth circle) that will bring a new Screen as shown in the below screenshot.


Check all the "checkboxes" (encircled by blue color) to give access of the all databases and then check the "checkbox" db_owner encircled by red color.Click on the “OK” button which is at the bottom right corner.Since we have made the Login (User) as “Db_owner”, we will be able to access any object and can do any operation (Select,Insert,Update,Delete) on any object. Now we will revoke all the rights from the "Employee" table for login(User) by following below steps.

Right click on “Employee” tableàProperties as shown in the below screenshot.


Once we click on “properties” Menu, this action will bring a new screen to us as shown in the below screenshot.


Click on the “permissions” Menu, which will bring a new look to the current screen as shown in the below screenshot.


Click on “Search” button(encircled by red color), which will bring a new screen as shown below.


We will put the login name “User” in the text field and click on “Check names” button to verify the login name entered in the text field is correct and then press “OK” button. Once we click on the “OK” button, the current screen will be closed and we will be on the previous screen.


In the permission section for the user, we can choose which permission we want to give the user and which we do not. For convenience purpose, I have checked all the checkboxes of “Deny” column, to revoke all the permissions from the Login/User on “Employee” table. Finally click “OK”. Now lets check if the permission is revoked for table “Employee”. To check this, we need to login to the SQL using “User” login.


In the above screenshot, we can see that, I am logged in to the SQL Server using “User” login and I have run the select query on table “Employee” which is throwing an error message stating that “The select permission was denied on the object Employee, Database Demo, schema, dbo”.

Since the Login(User) is a db owner, It can access all the objects, except “Employee” table as we have explicitly removed all the rights from “Employee” table. Lets try to access the View.


Even though we do not have access to the base table “Employee”, we are able to access the data of the employee table using View.This is the most important feature of View which is ignored by most of us.

Conclusion: - Views are used to apply row level or column level security on the base tables. The example shown above was related to column level security. Similarly we can apply row level security by selecting the specific no of rows of table in a View.

I will explain rest of features of View in my next article. Please feel free to ask any question related to this topic and share your feedback on this topic.

Working with database objects and meta data stored in SQL Server

Good Morning friends.. In my article that I posted on 19th Jan 2014, I mentioned that we would talk about database objects and meta data stored in SQL Server in my next article. But I could not write that article about the database objects and in the mean time, I wrote few interesting articles those are quite popular topics in SQL Sever. So today I am going to continue from the last topic where we stopped(creation of user database).

As a database developer you will be working on following objects.

  • Table
  • Primary Key
  • Foreign Key
  • Unique Key
  • Default Constraint
  • Procedure
  • Function
  • Trigger
  • Views
  • Indexes
We will talk about all objects, mentioned above one by one.

Table :- Table is an object where information is stored in form of rows and columns in SQL Server.

Creating database table using T-SQL script.

Create table employee(Eid int,Name Varchar(100))




If we paste the highlighted script into the management studio window and click on "Execute" button(encircled with red color in the above image), a table with name "Employee" will be created in the database. If you see left side of the execute button you will see the database name selected as "Test". It means that the table will be created in the "Test" database. You can select any already created database from the list. This time I have chosen "Test".


Now lets See how to create a table using management studio.




For creating table using management studio, you need to expand the databases node that is right side in the above image and you find the test database that you have already created in past.Now you need to expand the "Test" node as well and in that node you will find tables node, just right click on the tables node. Now a menu will appear with option new table, you need to click on new table menu. As soon as you click on new table menu, a new type window will be opened in front of you.


Now lets have a look on the below snapshot.




In the above image, you can see three columns..

1)Column Name :- This column contain the names of the column that you have in your table.
2)Data type :- Data type of each column.
3)Allow Nulls :- You can specify if your column will contain null values or not.



I have created two columns in my table, now I want to save this table in SQL Server, To save this table into the database, we need to click on floppy icon, encircled by red color.




As soon as you click on save button(Floppy icon), a dialogue box will appear in front of you where we will enter the name of the table, I have put my table name as "Department", just click on "OK" button of the dialog box and the table will be created in the database.


I hope after this article you will be able to create a table using management studio as well as through T-SQL script. In the next article I will talk about few more things related to tables that will give you more clarity. We will also talk about data types and different properties available to set while creating the table. Till then take care and have a wonderful day.