Database Objects VIEWS
Database Objects VIEWS
A view contains rows and columns, just like a real table. The fields in a view
are fields from one or more real tables in the database.
You can add SQL functions, WHERE, and JOIN statements to a view and
present the data as if the data were coming from one single table.
SQL Server stores data in tables, but you can create objects, called views,
that you query just like tables. Views do not store data; they are just
saved query definitions. Developers can use views to simplify coding.
1
SQL CREATE VIEW Syntax
CREATE VIEW view_name AS
SELECT column_name(s)
FROM table_name
WHERE condition
Note: A view always shows up-to-date data! The database engine recreates
the data, using the view's SQL statement, every time a user queries a view.
The view "Current Product List" lists all active products (products that are not
discontinued) from the "Products" table. The view is created with the
following SQL:
Another view in the Northwind sample database selects every product in the
"Products" table with a unit price higher than the average unit price:
We can also add a condition to the query. Now we want to see the total sale
only for the category "Beverages":
Now we want to add the "Category" column to the "Current Product List"
view. We will update the view with the following SQL:
3
SQL Dropping a View
You can delete a view with the DROP VIEW command.
Uses
ManufacturingDate DATETIME,
ExpiryDate DATETIME,
Next, we have a Customer table which stores UserID and Password details
for customers.
4
LNmeVARCHAR(50) NOT NULL,
Lastly, I have created a BOOKING table which houses all the bookings
from different customers.
QTY INT
(1,'Biscuits','2011-09-01 00:00:00.000','2012-09-01
00:00:00.000',1,20),
VALUES
('Sara','Verma','[email protected]','S123'),
5
('Rick','Singh','[email protected]','G311'),
('Micky','Khera','[email protected]','M222')
VALUES
(1,1002,'2011-11-01 00:00:00.000',3),
(2,1004,GETDATE(),4),
(3,1006,'2011-10-01 00:00:00.000',2)
CustIDFNameLNmeUserIDPswd
(3 row(s) affected)
ProductIDProductDescManufacturingDateExpiryDateIsSalable Price
6
2 Butter 2010-09-01 00:00:00.000 2011-09-01 00:00:00.000
1 30.00
(3 row(s) affected)
BookingIDProductIDCustIDDateOfBooking QTY
(3 row(s) affected)
AS
SELECT C.FName
,C.LNme
,P.ProductDesc
,B.DateOfBooking
,P.Price
7
,B.QTY
,(B.QTY*P.Price) AS TotalAmountPayable
FROM BOOKING B
ON B.ProductID=P.ProductID
ON B.CustID=C.CustID;
-------------------------------------------------- --------------------------------
----
(3 row(s) affected)
• This is the most credible use of a VIEW; it can not only reduce
apparent complexity but also prevent redundant hosting of data in the
DB.
We create a View:
Select CustID
,UserID
FROM dbo.Customer
We have a created a View which can be used by the API to fetch customer
details –(Minus) the Password Column.
Views do not have a physical existence, but still they do return a set of
record set as a table does, the differences is it is simply an additional
layer which calls the underlying code which finally returns the record set.
Features
Views are only meant for reading data in a customized mode? Not really
views also facilitate DML (Insert/Update/Delete). But there is a set of
rules which needs to be adhered to enable DMLs.
If you are using a view to insert data, then your view should have a single
select and also all the mandatory columns of the “being edited” table
must be included in the view unless the table has a default values for all
NOT NULL columns of the table.
Select CustID
,UserID
FROM dbo.Customer
Insert
VALUES ('Gurum','Ramaswamy','[email protected]')
The insert happened because though the columns CustID and Pswd are
mandatory butCustID is IDENTITY and PSWD has a DEFAULT. All the
other mandatory data was supplied in the insert query.
CustIDFNameLNmeUserIDPswd
(4 row(s) affected)
Update
11
UPDATE CustomerInfo_V
CustIDFNameLNmeUserIDPswd
(4 row(s) affected)
Delete
CustIDFNameLNmeUserIDPswd
Refreshing Views
Just in case we are working with a non-schema bound view and there is
some change in the underlying table, to prevent the view from producing
unexpected results, we have an option to refresh the view with:
WITH ENCRYPTION
AS
SELECT C.FName
,C.LNme
,P.ProductDesc
,B.DateOfBooking
12
,P.Price
,B.QTY
,(B.QTY*P.Price) AS TotalAmountPayable
FROM dbo.BOOKING B
ON B.ProductID=P.ProductID
ON B.CustID=C.CustID;
13