Click here to load reader

Advance SQL: SQL Performance Tuning SQL Views · PDF file12/16/1996 · Advance SQL: SQL Performance Tuning SQL Views ... If you have the Northwind database you can see that it has

  • View
    263

  • Download
    8

Embed Size (px)

Text of Advance SQL: SQL Performance Tuning SQL Views · PDF file12/16/1996 · Advance SQL:...

  • Advance SQL: SQL Performance Tuning

    SQL Views

    A view is nothing more than a SQL statement that is stored in the database with an associated

    name. A view is actually a composition of a table in the form of a predefined SQL query.

    A view can contain all rows of a table or select rows from a table. A view can be created from

    one or many tables which depends on the written SQL query to create a view.

    Views, which are kind of virtual tables, allow users to do the following:

    Structure data in a way that users or classes of users find natural or intuitive.

    Restrict access to the data such that a user can see and (sometimes) modify exactly what

    they need and no more.

    Summarize data from various tables which can be used to generate reports.

    SQL CREATE VIEW Statement

    In SQL, a view is a virtual table based on the result-set of an SQL statement.

    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 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.

    SQL CREATE VIEW Examples

    If you have the Northwind database you can see that it has several views installed by default.

  • 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:

    CREATE VIEW [Current Product List] AS

    SELECT ProductID,ProductName

    FROM Products

    WHERE Discontinued=No

    We can query the view above as follows:

    SELECT * FROM [Current Product List]

    Another view in the Northwind sample database selects every product in the "Products" table

    with a unit price higher than the average unit price:

    CREATE VIEW [Products Above Average Price] AS

    SELECT ProductName,UnitPrice

    FROM Products

    WHERE UnitPrice>(SELECT AVG(UnitPrice) FROM Products)

    We can query the view above as follows:

    SELECT * FROM [Products Above Average Price]

    Another view in the Northwind database calculates the total sale for each category in 1997. Note

    that this view selects its data from another view called "Product Sales for 1997":

    CREATE VIEW [Category Sales For 1997] AS

    SELECT DISTINCT CategoryName,Sum(ProductSales) AS CategorySales

    FROM [Product Sales for 1997]

    GROUP BY CategoryName

    We can query the view above as follows:

    SELECT * FROM [Category Sales For 1997]

    We can also add a condition to the query. Now we want to see the total sale only for the category

    "Beverages":

    SELECT * FROM [Category Sales For 1997]

    WHERE CategoryName='Beverages'

  • WITH CHECK OPTION:

    The WITH CHECK OPTION is a CREATE VIEW statement option. The purpose of the WITH

    CHECK OPTION is to ensure that all UPDATE and INSERTs satisfy the condition(s) in the

    view definition.

    If they do not satisfy the condition(s), the UPDATE or INSERT returns an error.

    The following is an example of creating same view CUSTOMERS_VIEW with the WITH

    CHECK OPTION:

    CREATE VIEW CUSTOMERS_VIEW AS

    SELECT name, age

    FROM CUSTOMERS

    WHERE age IS NOT NULL

    WITH CHECK OPTION;

    The WITH CHECK OPTION in this case should deny the entry of any NULL values in the

    view's AGE column, because the view is defined by data that does not have a NULL value in

    the AGE column.

    SQL Updating a View

    You can update a view by using the following syntax:

    SQL CREATE OR REPLACE VIEW Syntax

    CREATE OR REPLACE VIEW view_name AS

    SELECT column_name(s)

    FROM table_name

    WHERE condition

    Now we want to add the "Category" column to the "Current Product List" view. We will update

    the view with the following SQL:

  • CREATE OR REPLACE VIEW [Current Product List] AS

    SELECT ProductID,ProductName,Category

    FROM Products

    WHERE Discontinued=No

    Updating a View:

    A view can be updated under certain conditions:

    The SELECT clause may not contain the keyword DISTINCT.

    The SELECT clause may not contain summary functions.

    The SELECT clause may not contain set functions.

    The SELECT clause may not contain set operators.

    The SELECT clause may not contain an ORDER BY clause.

    The FROM clause may not contain multiple tables.

    The WHERE clause may not contain subqueries.

    The query may not contain GROUP BY or HAVING.

    Calculated columns may not be updated.

    All NOT NULL columns from the base table must be included in the view in order for

    the INSERT query to function.

    SQL > UPDATE CUSTOMERS_VIEW

    SET AGE = 35

    WHERE name='Ramesh';

    Inserting Rows into a View:

    Rows of data can be inserted into a view. The same rules that apply to the UPDATE command

    also apply to the INSERT command.

    Here we can not insert rows in CUSTOMERS_VIEW because we have not included all the

    NOT NULL columns in this view, otherwise you can insert rows in a view in similar way as

    you insert them in a table.

  • Deleting Rows into a View:

    Rows of data can be deleted from a view. The same rules that apply to the UPDATE and

    INSERT commands apply to the DELETE command.

    Following is an example to delete a record having AGE= 22.

    SQL > DELETE FROM CUSTOMERS_VIEW

    WHERE age = 22;

    This would ultimately delete a row from the base table CUSTOMERS and same would reflect

    in the view itself. Now, try to query base table, and SELECT statement would produce the

    following result:

    +----+----------+-----+-----------+----------+

    | ID | NAME | AGE | ADDRESS | SALARY |

    +----+----------+-----+-----------+----------+

    | 1 | Ramesh | 35 | Ahmedabad | 2000.00 |

    | 2 | Khilan | 25 | Delhi | 1500.00 |

    | 3 | kaushik | 23 | Kota | 2000.00 |

    | 4 | Chaitali | 25 | Mumbai | 6500.00 |

    | 5 | Hardik | 27 | Bhopal | 8500.00 |

    | 7 | Muffy | 24 | Indore | 10000.00 |

    +----+----------+-----+-----------+----------+

    SQL Dropping a View

    You can delete a view with the DROP VIEW command.

    SQL DROP VIEW Syntax

    DROP VIEW view_name

    Read-Only View

    We can create a view with read-only option to restrict access to the view.

  • Syntax to create a view with Read-Only Access

    CREATE or REPLACE force view view_name AS

    SELECT column_name(s)

    FROM table_name

    WHERE condition with read-only

    The above syntax will create view for read-only purpose, we cannot Update or Insert data into

    read-only view. It will throw an error.

    Types of View

    There are two types of view,

    Simple View

    Complex View

    Simple View Complex View

    Created from one table Created from one or more table

    Does not contain functions Contain functions

    Does not contain groups of data Contains groups of data

    SQL Joins

    SQL joins are used to combine rows from two or more tables.

  • An SQL JOIN clause is used to combine rows from two or more tables, based on a common field

    between them.

    The most common type of join is: SQL INNER JOIN (simple join). An SQL INNER JOIN

    returns all rows from multiple tables where the join condition is met.

    Let's look at a selection from the "Orders" table:

    OrderID CustomerID OrderDate

    10308 2 1996-09-18

    10309 37 1996-09-19

    10310 77 1996-09-20

    Then, have a look at a selection from the "Customers" table:

    CustomerID CustomerName ContactName Country

    1 Alfreds Futterkiste Maria Anders Germany

    2 Ana Trujillo Emparedados y helados Ana Trujillo Mexico

    3 Antonio Moreno Taquera Antonio Moreno Mexico

  • Example

    SELECT Orders.OrderID, Customers.CustomerName, Orders.OrderDate

    FROM Orders

    INNER JOIN Customers

    ON Orders.CustomerID=Customers.CustomerID;

    it will produce something like this:

    OrderID CustomerName OrderDate

    10308 Ana Trujillo Emparedados y helados 9/18/1996

    10365 Antonio Moreno Taquera 11/27/1996

    10383 Around the Horn 12/16/1996

    10355 Around the Horn 11/15/1996

    10278 Berglunds snabbkp 8/12/1996

    Different SQL JOINs

    Before we continue with examples, we will list the types of the different SQL JOINs you can

    use:

    INNER JOIN: Returns all rows when there is at least one match in BOTH tables

    LEFT JOIN: Return all rows from the left table, and the matched rows from the right

    table

    RIGHT JOIN: Return all rows from the right table, and the matched rows from the left

    table

    FULL JOIN: Return all rows when there is a match in ONE of the tables

  • SQL INNER JOIN Keyword

    The INNER JOIN keyword selects all rows from both tables as long as there is a match between

    the columns in both tables.

    SQL INNER JOIN Syntax

    SELECT column_name(s)

    FROM table1

    INNER JOIN table2

    ON table1.column_name=table2.column_name;

    or:

    SELECT column_name(s)

    FROM table1

    JOIN table2

    ON table1.co

Search related