Presentation is loading. Please wait.

Presentation is loading. Please wait.

Working with SQL Server Database Objects Faculty: Nguyen Ngoc Tu.

Similar presentations


Presentation on theme: "Working with SQL Server Database Objects Faculty: Nguyen Ngoc Tu."— Presentation transcript:

1 Working with SQL Server Database Objects Faculty: Nguyen Ngoc Tu

2 A library, where books are arranged in sequence of Author names in the shelves What’ll happen if you want to request some books by its title? Clustered indexNon-clustered Indexes

3  Indexes can be created using the CREATE INDEX command.  An index that uses more than one column to index data is called a composite index.  The FILLFACTOR and PAD_INDEX options reserve space on index pages for future index expansion.  The sp_helpindex system store procedure is used to view index information.

4  New index features: online index operations, parallel index operations and locking options.  The ALTER INDEX statement is used to reorganize, disable and rebuild an index.  SQL Server 2005 allows non-key columns to be included in non-clustered indexes and allows you to create XML indexes on the XML columns in the table.  The various methods to optimize indexes include reorganizing, rebuilding, disabling and dropping indexes.  SQL Server 2005 creates index statistics that provide information about the distribution of values in a column or a group of columns. This information is used by the query optimizer to speed up the query process.

5 Objectives

6  A view is a virtual table that can be created to allow users access to specific columns of a table.  Views can include columns from multiple tables. A view retrieves data from multiple tables and presents it in such a way that the data appears to have been retrieved from a single table.  The tables from which the view is created are referred to as base tables. These tables can be from different databases.

7  A view does not get stored in the database unless you create a unique clustered index on it.  Once a unique clustered index is created, multiple non-clustered indexes can be created on the view.  An index can be created on a view only if:  The view does not include columns from other views.  The base tables are in the same database as of the view.

8  Views can be classified into three different types based on the parameters and options specified at the time of creating the view.  Standard View  Indexed View  Partitioned View

9  Standard View: A view created using columns from one or more tables is referred to as a standard view. Most of the advantages of views can be satisfied using standard views. These include providing specific access to data for viewing and manipulation.  Indexed View: A view defined with a unique clustered index is referred to as an indexed view. Indexed views are useful for queries that aggregate many rows. Indexed views are not suitable for tables that are frequently updated.  Partitioned View: A view created using horizontally partitioned data from one or more tables is referred to as a partitioned view. If the base tables reside in the same server, the view is referred to as a local partitioned view. If the base tables reside in different servers, the view is referred to as a distributed partitioned view.

10 Partitioned View Image resource://www.sqlskills.com/resources/Whitepapers/Partitioning%20in%20SQL%20Server%202005%20Beta%20II.htm

11  Views provide a personalized image of selected parts of a table. There are multiple advantages of views.  Security through Personalized Access: Views provide a data access system through which users can be allowed to access specific data columns.  Customized Display of Data: Using views, data derived from the same source can be viewed differently by different users depending on individual viewing requirements.

12  Merger of Data from Multiple Tables or Views: Views can merge columns from multiple tables and other views and display them together as if coming from a single table.  Operations on Records: Views can be used to update tables provided all the important columns of the table are contained in the view.  Integrity Constraint Checks: If data is added or modified through a view, SQL Server automatically checks the data to ensure that it meets the data integrity constraints for the respective columns.

13  The properties of an object such as a table or a view are stored in special system tables. These properties are referred to as metadata. All SQL objects produce metadata.  This metadata can be viewed using system views, which are predefined views of SQL Server 2005.  There are 230 different system views and these are automatically inserted into the user created database. These views are grouped into several different schemas (sub- databases).

14  A user can create a view using columns from tables or other views only if the user has permission to access these objects.  A view is created using the CREATE VIEW statement and it can be created only in the current database.  A view can have a maximum of 1024 columns.

15  Syntax: CREATE VIEW AS where  select_statement: specifies the SELECT statement that defines the view.  Example: CREATE VIEW Salary_Details AS SELECT EmpID, FirstName, LastName, Salary FROM Employee_Details To display the details of the Salary_Details view SELECT * FROM Salary_Details

16  To execute the CREATE VIEW statement, the user must be a member of the system administrators (sysadmin) role, database owner (db_owner), the data definition language administrator (db_ddladmin) or the user must have the CREATE VIEW permission.  Also, the user should have the SELECT permission on all tables or views that are references within the query.

17  Syntax: CREATE VIEW AS SELECT * FROM table_name1 JOIN table_name2 ON table_name1.column_name = table_name2.column_name  Example: CREATE VIEW EmpDetails AS SELECT EmpID, FirstName, LastName, DeptName FROM Department JOIN Employee ON Department.DeptID = Employee. DeptID

18  The CREATE VIEW statement cannot include the COMPUTE or COMPUTE BY clauses.  The CREATE VIEW statement can include the ORDER BY clause only if the TOP keyword is used.  Views cannot reference more than 1024 columns.  The CREATE VIEW statement cannot include the INTO keyword.  A view cannot reference a temporary table.  The CREATE VIEW statement cannot be combined with other Transact-SQL statements in a single batch.

19  Syntax: ALTER VIEW AS  Example: ALTER VIEW Salary_Details AS SELECT EmpID, FirstName, LastName, City, Salary FROM Employee_Details WHERE City = ‘New York’ To display the details of the Salary_Details view SELECT * FROM Salary_Details

20  A view can be removed from the database if it is no longer needed. This is done using the DROP VIEW statement. When a view is dropped, the data in the base tables remains unaffected.  The definition of the view and other information associated with the view is deleted from the system catalog. All permissions for the view are also deleted.  If a user queries any view that references the dropped view, the user receives an error message.

21  There are certain system stored procedures that help retrieve view definitions: sp_helptext  Syntax: sp_helptext  Example: sp_helptext ‘DeptEmpView’  Information about the definition of a view can be obtained if such information is not encrypted.

22  Syntax: sp_depends where,  objname: specifies the name of the database object for depencencies.  Example: sp_depends ‘Salary_Details’

23  Syntax: CREATE VIEW WITH ENCRYPTION AS  Example: CREATE VIEW Salary_Details WITH ENCRYPTION AS SELECT EmpID, FirstName, LastName, Salary FROM Employee_Details  If a user tries to see the view definition, an error message will be displayed stating the definition is encrypted.

24  A view is a virtual table generated using selected columns from one or more tables.  There are three types of views namely, Standard, Indexed and Partitioned.  Views are used to grant users limited access to database tables depending on individual user requirements.  System views are predefined created views for extracting SQL Server metadata.

25  The CREATE VIEW statement is used to create a view.  The view definition cannot include the COMPUTE, COMPUTE BY, ORDER BY or OPTION clauses.  In SQL Server 2005, you can create views having extracted information from xml data type columns of a table. This extraction is done using the value() method.  After creating a view, you can also alter or delete it.

26 Objectives

27  It is used to ensure that all the updates in the view satisfy the conditions mentioned in the view definition. If the conditions are not satisfied, the database engine returns an error.  Thus, the CHECK OPTION is used to enforce domain integrity; it checks the definition of the view to see that the WHERE conditions in the SELECT statement is not violated.

28  Syntax: CREATE VIEW AS [WITH CHECK OPTION] where: WITH CHECK OPTION: specifies that the modified data in the view continues to satisfy the view definition.  Example: CREATE VIEW Emp_Salary_Details AS SELECT EmpID, FirstName, LastName, City, Salary FROM Employee_Details WHERE Salary < 35000 WITH CHECK OPTION

29  CREATE VIEW authors_CA AS ( SELECT * FROM Authors WHERE state='CA' )  If you now issue the following command: UPDATE authors_CA SET state='NJ'  This is perfectly legal, but can create subtle programming errors  CREATE VIEW authors_CA AS ( SELECT * FROM Authors WHERE state='CA' ) WITH CHECK OPTION Now any insert or update operation that makes a record disappear from the view raises a trappable runtime error.

30  A view can be bound to the schema of the base table using the SCHEMABINDING option. This option can be used with CREATE VIEW or ALTER VIEW statements.  When SCHEMABINDING option is specified, the base table or tables cannot be modified that would affect the view definition.  The view definition must be first modified or deleted to remove dependencies on the table that is to be modified.  While using the SCHEMABINDING option in a view, you must specify the schema name along with the object name in the SELECT statement.

31  Syntax: CREATE VIEW [WITH SCHEMABINDING] AS where: WITH SCHEMABINDING : specifies that the view must be bound to a schema.  Example: CREATE VIEW Employee_View WITH SCHEMABINDING AS SELECT EmpID, FirstName, LastName, City FROM dbo.Employee_Details

32  Tables that are schema-bound to a view cannot be dropped unless the view is dropped or changed such that it no longer has schema binding. If the view is not dropped or changed and you attempt to drop the table, the Database Engine returns an error message.  Also, when an ALTER TABLE statement affects the view definition of a schema-bound view, the ALTER TABLE statement fails.

33  An indexed view is a view that has a UNIQUE CLUSTERED index created on it. Unlike regular views, which are generated only at runtime, an indexed view stores the result set of a view in the database.  After an index is created on a view, if any modifications are made to the data in the base tables, they are automatically updated in the indexed view.  Indexed views improve performance due to sorting of data. It works best for queries that contain a large number of rows.  To create an index on a view, it is necessary to first bind the view to a schema. This is done using the SCHEMABINDING option.

34  Syntax: CREATE UNIQUE CLUSTERED INDEX ON (column_name,…)  Example: CREATE UNIQUE CLUSTERED INDEX IX_EmpID ON Employee_View (EmpID)  Indexed views should not be used for tables that are frequently updated as this puts additional strain on resources for maintaining the view.

35  The following guidelines should be considered before creating an indexed view: The speed of retrieving the data must outweigh the maintenance cost. The underlying data should have minimal updating requirements. Indexes should be used in queries that include a significant amount of joins and aggregations processing many rows and performed frequently. The first index created on the view must be a unique clustered index. Create the view with the SCHEMABINDING option. An indexed view can reference base tables, but not other views.

36  Syntax: sp_rename ‘ ’, ‘ ’, ‘ ’ where: object_type: specifies the type of object being renamed. For example, COLUMN, DATABASE, INDEX, OBJECT and USERDATATYPE.  Example: sp_rename ‘Employee_View’, ‘DeptEmpView_NEW’

37  Views can be used to modify data in database tables. When modifying data through a view, the following points should be considered: Modifications can be carried out in columns from a single base table. Data cannot be modified if the column is created using aggregate functions. When the WITH CHECK OPTION clause is used, values cannot be modified that causes them to violate the view definition. Records can be modified only if the view contains all the columns of the base table that have constraints defined on them.

38  Data in rows of a view can be updated using the UPDATE statement. These updates can be carried out on a single row, group of rows, or on the entire table. When a view is updated, corresponding updates are made in the base table.  Syntax: UPDATE SET =, = WHERE  Example: UPDATE CustDetails SET City=‘San Francisco’ WHERE CustID=‘C0004’

39  You can insert or update a multi-table view if: The view has no WITH CHECK OPTION clause. All columns being inserted or updated belong to the same base table.  The value of a column with an IDENTITY property cannot be updated.  Records cannot be updated if the base table contains a TIMESTAMP column.  While updating a row, if a constraint or rule gets violated, the statement is terminated, an error is returned and no records are updated.  When there is a self join with the same view or base table, the UPDATE statement does not work.

40  SQL Server 2005 enables you to insert rows into a view. New rows can be inserted using the INSERT statement. When rows are inserted into the view, the base table get accordingly updated.  Syntax: INSERT INTO (, ) VALUES (, )  Example: INSERT INTO CustDetails (CustID,AccNo,AccName,City) VALUES (‘C0005’, 5, ‘Joes’, ‘Sacramento’)

41  The INSERT statement must specify values for all columns in a view in the underlying table that do not allow null values and have no DEFAULT definitions.  When there is a self-join with the same view or base table, the INSERT statement does not work.

42  SQL Server 2005 enables you to delete rows from a view. Rows can be deleted from the view using the DELETE statement. When rows are deleted from a view, corresponding rows get deleted from the base table.  Syntax: DELETE FROM WHERE ( )  Example: DELETE FROM CustDetails WHERE CustID=‘C0004’

43  A view can be indexed by defining a UNIQUE CLUSTERED index on it.  The CHECK OPTION prevents data in a view from being modified such that the modified data violates the view definition condition.  The SCHEMABINDING option is used to bind the view to the schema of the base table.  Data can be modified through a view using the UPDATE, INSERT and DELETE statements.  For modifying data through a view, all important columns in the base table must be included in the view.


Download ppt "Working with SQL Server Database Objects Faculty: Nguyen Ngoc Tu."

Similar presentations


Ads by Google