Professional Documents
Culture Documents
Course 4
Querying SQL Server Databases
View Objects
View: Virtual representation of one or several tables used to
User roles
Server
Column T1
Column T2
Column T3
Column T4
ColumnT1
T1
Column T3
T3
ColumnR1
R1 Cmp
ColumnR2
R2 Cmp
Cmp
Cmp
Table 1
View - User 1
Column
Column
Column
Cmp
R1R1 Cmp
R2R2 Cmp
R3R3
Column T2
T2
Column T3
T3
Column R2
R2
Cmp
Cmp
Cmp
Table 2
View - User 2
views.
Data customization:
Different users may access the same data with a different presentation.
Types of views:
User-defined views: used according to the previously described
scenarios.
a database.
Catalog-type of views
o Example:
SELECT * FROM sys.tables
Views on the dynamics of server management data (Data Management View):
o Example:
SELECT session_id, client_net_address
FROM sys.dm_exec_connections
selected fields, source tables, filters etc. are specified using objects
which provide their visual representation.
Using the SQL syntax:
(CREATE | ALTER | DROP View )
Using views:
Data produced by views may be subjected to the same SQL clauses
Source
tables
selection
Criteria Pane
(field list, filters, sort order )
SQL Pane
Sorting options
DEFINING VIEWS
EDITING VIEWS
REMOVING VIEWS
fieldName1, fieldName2,
viewName
Filter criteria
Group fields
Filter criteria (Group fields/Aggregate expressions)
Sort fields ASC|DESC
Indexed Views
INDEX: mechanism for performing fast data searches on
a table or a view.
It is saved as a distinct structure, auxiliary to a table or
view, which helps improving query performance and
response time.
An INDEX consists of keys corresponding to the values
of one or several fields and allow fast identification of
records associated with each key.
Example:
Partitioned Views
Enable access to identically structured record
Frequently Used
SQL System Functions
CONVERT Function
converts the result produced by an expression to a specified data type
The syntax:
Usual codes for data_style argument -> specify input/output date/time formats:
Date style
Corresponding
Date/Time format
101
mm/dd/yyyy
103
dd/mm/yyyy
108
hh:mi:sec
113
GETDATE() Function
Example: server date and time
returns the current system date and time
NEWID() Function
returns an uniqueidentifier value
DAY (date_expression)
MONTH(date_expression)
YEAR(date_expression)
DATEPART Function
Syntax:
DATEPART (date_part, date/time_expression)
Returns the specified date part of a particular date/time value.
date_part
(predefined and
frequently used values)
year
month
day
dayofyear
quarter
week
hour
minute
second
DATEDIFF Function
Syntax:
DATEDIFF (date_part, starting date/time , ending date/time)
Returns the difference between two dates (starting date ending date);
the result is represented by a certain number of date_part intervals.
DATEADD Function
Syntax: