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

Slides:



Advertisements
Similar presentations
Yukon – What is New Rajesh Gala. Yukon – What is new.NET Framework Programming Data Types Exception Handling Batches Databases Database Engine Administration.
Advertisements

Virtual training week 4 structured query language (SQL)
Working with SQL Server Database Objects
Introduction to Structured Query Language (SQL)
A Guide to Oracle9i1 Advanced SQL And PL/SQL Topics Chapter 9.
A Guide to MySQL 7. 2 Objectives Understand, define, and drop views Recognize the benefits of using views Use a view to update data Grant and revoke users’
Introduction to Structured Query Language (SQL)
A Guide to SQL, Seventh Edition. Objectives Understand, create, and drop views Recognize the benefits of using views Grant and revoke user’s database.
Database Constraints. Database constraints are restrictions on the contents of the database or on database operations Database constraints provide a way.
DAY 21: MICROSOFT ACCESS – CHAPTER 5 MICROSOFT ACCESS – CHAPTER 6 MICROSOFT ACCESS – CHAPTER 7 Akhila Kondai October 30, 2013.
Module 9: Managing Schema Objects. Overview Naming guidelines for identifiers in schema object definitions Storage and structure of schema objects Implementing.
Database Technical Session By: Prof. Adarsh Patel.
Views: Limiting Access to Data A view is a named select statement that is stored in a database as an object. It allows you to view a subset of rows or.
DBSQL 14-1 Copyright © Genetic Computer School 2009 Chapter 14 Microsoft SQL Server.
11 Copyright © 2007, Oracle. All rights reserved. Creating Other Schema Objects.
CPS120: Introduction to Computer Science Lecture 19 Introduction to SQL.
Chapter 6 Database Administration
Views Lesson 7.
Module 4 Designing and Implementing Views. Module Overview Introduction to Views Creating and Managing Views Performance Considerations for Views.
Chapter 2 Views. Objectives ◦ Create simple and complex views ◦ Creating a view with a check constraint ◦ Retrieve data from views ◦ Data manipulation.
Database Systems Design, Implementation, and Management Coronel | Morris 11e ©2015 Cengage Learning. All Rights Reserved. May not be scanned, copied or.
SQL Basics. What is SQL? SQL stands for Structured Query Language. SQL lets you access and manipulate databases.
SQL Basic. What is SQL? SQL (pronounced "ess-que-el") stands for Structured Query Language. SQL is used to communicate with a database.
Indexes / Session 2/ 1 of 36 Session 2 Module 3: Types of Indexes Module 4: Maintaining Indexes.
Chapter 9 Constraints. Chapter Objectives  Explain the purpose of constraints in a table  Distinguish among PRIMARY KEY, FOREIGN KEY, UNIQUE, CHECK,
Oracle 11g: SQL Chapter 4 Constraints.
Database Lab Lecture 1. Database Languages Data definition language ( DDL ) Data definition language –defines data types and the relationships among them.
Chapter 4 Constraints Oracle 10g: SQL. Oracle 10g: SQL 2 Objectives Explain the purpose of constraints in a table Distinguish among PRIMARY KEY, FOREIGN.
Managing Constraints. 2 home back first prev next last What Will I Learn? Four different functions that the ALTER statement can perform on constraints.
Database Programming Sections 11 & 12 –Sequences, Indexes, and Synonymns.
AL-MAAREFA COLLEGE FOR SCIENCE AND TECHNOLOGY INFO 232: DATABASE SYSTEMS CHAPTER 7 (Part II) INTRODUCTION TO STRUCTURED QUERY LANGUAGE (SQL) Instructor.
Indexes and Views Unit 7.
Database Security. Multi-user database systems like Oracle include security to control how the database is accessed and used for example security Mechanisms:
SQL/Lesson 7/Slide 1 of 32 Implementing Indexes Objectives In this lesson, you will learn to: * Create a clustered index * Create a nonclustered index.
Constraints Lesson 8. Skills Matrix Constraints Domain Integrity: A domain refers to a column in a table. Domain integrity includes data types, rules,
Chapter 5 : Integrity And Security  Domain Constraints  Referential Integrity  Security  Triggers  Authorization  Authorization in SQL  Views 
Session 1 Module 1: Introduction to Data Integrity
Altering Tables and Constraints Database Systems Objectives Add and modify columns. Add, enable, disable, or remove constraints. Drop a table. Remove.
Creating Indexes on Tables An index provides quick access to data in a table, based on the values in specified columns. A table can have more than one.
Relational Database Management System(RDBMS) Structured Query Language(SQL)
SQL Server 2012 Session: 1 Session: 12 Triggers Data Management Using Microsoft SQL Server.
Stored Procedures / Session 4/ 1 of 41 Session 4 Module 7: Introducing stored procedures Module 8: More about stored procedures.
Manipulating Data Lesson 3. Objectives Queries The SELECT query to retrieve or extract data from one table, how to retrieve or extract data by using.
Constraints and Views Chap. 3-5 continued (7 th ed. 5-7)
Retele de senzori Curs 2 - 1st edition UNIVERSITATEA „ TRANSILVANIA ” DIN BRAŞOV FACULTATEA DE INGINERIE ELECTRICĂ ŞI ŞTIINŢA CALCULATOARELOR.
1 Database Fundamentals Introduction to SQL. 2 SQL Overview Structured Query Language The standard for relational database management systems (RDBMS)
MICROSOFT ACCESS – CHAPTER 5 MICROSOFT ACCESS – CHAPTER 6 MICROSOFT ACCESS – CHAPTER 7 Sravanthi Lakkimsety Mar 14,2016.
SQL Triggers, Functions & Stored Procedures Programming Operations.
 CONACT UC:  Magnific training   
SQL Basics Review Reviewing what we’ve learned so far…….
Module 6: Creating and Maintaining Indexes. Overview Creating Indexes Understanding Index Creation Options Maintaining Indexes Introducing Statistics.
In this session, you will learn to: Create and manage views Implement a full-text search Implement batches Objectives.
Views / Session 3/ 1 of 40 Session 3 Module 5: Implementing Views Module 6: Managing Views.
Data Integrity & Indexes / Session 1/ 1 of 37 Session 1 Module 1: Introduction to Data Integrity Module 2: Introduction to Indexes.
Database Constraints Ashima Wadhwa. Database Constraints Database constraints are restrictions on the contents of the database or on database operations.
SQL IMPLEMENTATION & ADMINISTRATION Indexing & Views.
Getting started with Accurately Storing Data
Fundamentals of DBMS Notes-1.
Implementing Views Advanced Database Dr. AlaaEddin Almabhouh.
SQL Creating and Managing Tables
Module 7: Implementing Views
SQL Creating and Managing Tables
SQL Creating and Managing Tables
Chapter 2 Views.
SQL DATA CONSTRAINTS.
A Guide to SQL, Eighth Edition
Chapter 2 Views.
Introduction To Structured Query Language (SQL)
IST 318 Database Administration
Manipulating Data Lesson 3.
Presentation transcript:

Working with SQL Server Database Objects Faculty: Nguyen Ngoc Tu

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

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

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

Objectives

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

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

 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

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

Partitioned View Image resource://

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

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

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

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

 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

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

 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

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

 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

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

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

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

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

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

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

Objectives

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

 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 < WITH CHECK OPTION

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

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

 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

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

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

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

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

 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’

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

 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’

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

 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’)

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

 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’

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