Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống
1
/ 20 trang
THÔNG TIN TÀI LIỆU
Thông tin cơ bản
Định dạng
Số trang
20
Dung lượng
255,61 KB
Nội dung
Chapter 2: Developing with T-SQL 39 NONCLUSTERED Indexes A nonclustered index on a table or view is an index where the order of the index does not depend on the physical order of the data rows. In other words, the columns do not have to be next to each other to make up the index. You can create up to 249 nonclustered indexes for each table in your database. NONCLUSTERED is the default mode when no keyword is specified in the CREATE INDEX statement. Included Columns in Indexes In some cases, you may find that you are frequently querying a column in a table that is not a key column. In previous versions of SQL Server, you would generally create an indexed view to handle this situation. However, one of the restrictions to using an indexed view is that the index must be unique. SQL Server 2005 resolves this by allowing the inclusion of nonkey columns in a nonclustered index. This allows the query optimizer to locate all the required information from an index scan; the table or clustered index need not be accessed. SQL Server 2005 allows up to 1023 columns to be included as nonkey columns. The following shows an example of creating a nonclustered index, including nonkey columns: CREATE NONCLUSTERED INDEX IdxTerritoryID_Date ON Sales.SalesPerson (TerritoryID) INCLUDE (ModifiedDate) CLUSTERED Indexes A clustered index has the index order the same as the physical order of the rows, and the table data is stored with the index. If you regularly access rows in your table in a particular order, a clustered index can significantly improve the speed of your queries. SQL Server allows you to create only one clustered index on each table. The following code shows creating a clustered index on the Sales .SalesPerson table: CREATE CLUSTERED INDEX IdxPersonTerr ON Sales.SalesPerson (SalesPersonID, TerritoryID) UNIQUE Indexes You can create a unique index on a column to guarantee that the data in the column will not be duplicated on an Insert operation. The database engine checks for duplicate values each time data is added by an insert operation on a unique index column, and if a duplicate is found, the key values are rolled back and the database engine displays an error message. Indexed Views You can create a unique clustered index on a view to improve query performance. The view is stored in the database in the same way a table with a clustered index is stored. The query optimizer will automatically consider scanning the view index even though the view is not referenced in the query. 40 Microsoft SQL Server 2005 Developer’s Guide XML Indexes XML data type columns can hold up to 2GB of data. You can query portions of the XML in these data types, so it’s a good idea to create indexes for them. There are two types of indexes you can create for XML data: primary and secondary. A primary XML index covers all the elements in the column, and a secondary XML index covers the paths, values, and properties. The following code shows an example of how to create a primary index: CREATE PRIMARY XML INDEX IdxXmlData ON Sales.SalesPerson(xml_Data) Defaults A default is bound to a column or alias data type and specifies a default value for the column or columns, when no value is supplied. The following example restricts the information that can be placed into the column to only the values in the rule list: CREATE DEFAULT OrderQty AS '100' NOTE The CREATE RULE statement will be removed in later versions of SQL Server. It is recommended that you use the DEFAULT keyword for defining values when you create a table. Rules A rule is bound to a column or alias data type to specify the acceptable values that can be contained in that column. The following example restricts the information that can be placed into the column to only the values in the rule list. CREATE RULE OrderQty AS @list IN ('100', '250', '500') NOTE The CREATE RULE statement will be removed in later versions of SQL Server. It is recommended that you create a CHECK constraint as part of the table definition when you create a table. Views Views are virtual tables that allow you to represent data in an alternate way. You can create a view only in the current database, and if you are creating a view in a batch query, the CREATE VIEW must be the first statement in the query. The following Chapter 2: Developing with T-SQL 41 code creates a view called StorePersonnel based on the SalesPersonId and the name of the store from the Sales.Store table in the Adventureworks database: CREATE VIEW StorePersonnel AS SELECT SalesPersonID, Name FROM AdventureWorks.Sales.Store WHERE SalesPersonID > 250 You can create a view with a maximum of 1024 columns. When a view is queried, the database engine checks for the existence of database objects and the validity of all objects referenced in the SELECT statement. If a table or view structure changes, the view dependent on that table or view needs to be dropped and re-created. When you create a view, information about the view is stored in three catalog views: sys.view, sys.columns, and sys.sql_dependencies, and the text of the CREATE VIEW statement used to create the view is stored in the sys.sql_modules catalog view. Synonyms Synonyms are aliases you can create for your objects. They help you simplify the naming of remote objects or objects that are in another database or schema. Synonyms allow you to exchange underlying objects without affecting the code that references the objects. The following command creates a synonym called RetailLocation for the Sales.Store table in the AdventureWorks database: CREATE SYNONYM RetailLocation FOR AdventureWorks.Sales.Store The base object need not exist at the time the synonym is created, as SQL Server checks for the existence of the base object at runtime, instead of creation time. You can create synonyms for tables, temporary tables, views, procedures, and functions. Stored Procedures The CREATE PROCEDURE statement can be used to create a standard T-SQL stored procedure, which is a saved collection of T-SQL statements, or it can be used to create a stored procedure implemented through a class of an assembly in the Microsoft .NET Framework common language runtime (CLR). This example shows creating a simple stored procedure to return the SalesPersonID and Name from the Sales.Store table: CREATE PROCEDURE Sales.usp_GetSalesPerson AS SELECT SalesPersonID, Name FROM Sales.Store 42 Microsoft SQL Server 2005 Developer’s Guide The following example shows calling the new stored procedure usp_GetSalesPerson and the returned results: EXECUTE Sales.usp_GetSalesPerson SalesPersonID Name 280 A Bike Store 283 Progressive Sports 277 Advanced Bike Components 277 Modular Cycle Systems 281 Metropolitan Sports Supply 276 Aerobic Exercise Company In many cases, you will want to pass parameters to your stored procedures and return results. A parameter name begins with @ and can be any data type allowed for columns. A stored procedure can have as many as 2100 parameters. The OUTPUT keyword designates a parameter as an output parameter. The following code creates a stored procedure named usp_GetOneStore: CREATE PROCEDURE Sales.usp_GetOneStore (@InID int, @OutName nvarchar(50) OUTPUT) AS Set @OutName = (SELECT Name FROM Sales.Store WHERE CustomerID = @inID) Notice that the procedure takes an input parameter named @InID and an output parameter named @OutName. The Set keyword sets the @OutName output parameter with the returned value of the SELECT statement. The next listing shows calling the usp_GetOneStore stored procedure and its results: DECLARE @StoreName nvarchar(50) EXECUTE Sales.usp_GetOneStore 28, @StoreName Output print @StoreName Commuter Bicycle Store First you need to declare a variable for the output of the stored procedure. In this example, the @StoreName variable is declared as an nvarchar with a length of 50. Next the stored procedure is called with 28 as the input parameter and the Chapter 2: Developing with T-SQL 43 @StoreName variable as the output argument. The Output keyword must be used on the output argument on the EXECUTE statement. To create a stored procedure from a CLR assembly, you must first register the assembly in SQL Server using the CREATE ASSEMBLY statement. You can then use the CREATE PROCEDURE statement to create the stored procedure. The following listing shows creating an assembly and then creating a stored procedure based on that assembly: CREATE ASSEMBLY usp_GetSalesPerson FROM 'C:\temp\usp_GetSalesPerson.dll' CREATE PROCEDURE usp_GetSalesPerson EXTERNAL NAME usp_GetSalesPerson. [usp_GetSalesPerson.StoredProcedures].usp_GetSalesPerson A full description of creating an assembly for a stored procedure and deploying it to the server is covered in Chapter 3. Functions The CREATE FUNCTION statement can be used to create a standard T-SQL function, which is a saved collection of T-SQL statements, or it can be used to create a user- defined function (UDF) implemented through a class of an assembly in the Microsoft .NET Framework common language runtime (CLR). Two types of functions can be created: scalar-valued functions and table-valued functions. Functions that are scalar- valued return one of the scalar data types, whereas the RETURN clause of table-valued functions specifies TABLE. When creating a function, you need to specify the function name and the RETURNS clause. Other options that can be included in the CREATE FUNCTION statement include a schema name and parameters. You can create a function with a maximum of 1024 parameters. Scalar-Valued Functions This example shows creating a simple scalar-valued function that returns the HouseName from the Sales.Warehouse table: CREATE FUNCTION ufnGetHouseName ( @House int ) RETURNS char(50) AS BEGIN RETURN (SELECT HouseName FROM Sales.Warehouse WHERE HouseID > @House) END 44 Microsoft SQL Server 2005 Developer’s Guide The following example shows calling the new function ufnGetHouseName and the return value: SELECT dbo.ufnGetHouseName (1) (No column name) Warehouse02 Table-Valued Functions This next example shows creating a simple table-valued function that returns a table containing the SalesPersonID column of the Sales.Store table: CREATE FUNCTION Sales.fn_PersonPerStore (@PersonID int) RETURNS TABLE AS RETURN (SELECT * FROM Sales.Store WHERE SalesPersonID = @PersonID) Here you see calling the new function fn_PersonPerStore and the returned table results: SELECT * FROM Sales.fn_PersonPerStore ('279') CustomerID Name SalesPersonID 8 Exemplary Cycles 279 9 Tandem Bicycle Store 279 26 Stylish Department Stores 279 27 Sports Sales and Rental 279 45 Every Bike Shop 279 62 Manufacturers Inc 279 63 Metro Bike Mart 279 To create a UDF from a CLR assembly, you must first register the assembly in SQL Server using the CREATE ASSEMBLY statement. You can then use the CREATE FUNCTION statement to create the UDF. The following listing shows creating an assembly and then creating a UDF based on that assembly: CREATE ASSEMBLY ufn_GetDataAsString FROM 'C:\temp\ufn_GetDataAsString.dll' CREATE FUNCTION ufn_GetDateAsString() RETURNS nvarchar(256) EXTERNAL NAME ufn_GetDateAsString.UserDefinedFunctions.ufn_GetDateAsString Chapter 2: Developing with T-SQL 45 A full description of creating an assembly for a UDF and deploying it to the server is covered in Chapter 3. Triggers A trigger is a kind of stored procedure that executes when an event occurs in the server. Data Manipulation Language (DML) triggers execute when a user tries to modify data. DML triggers are carried out on DML events such as INSERT, UPDATE, or DELETE statements. DML triggers are discussed in the next section of this chapter. DDL Triggers Earlier versions of SQL Server allowed triggers to be used only with DML events. SQL Server 2005 extends trigger usage by allowing triggers to be placed on Data Definition Language (DDL) events, including creating and dropping database objects such as tables, views, procedures, and logins. DDL triggers can be associated with CREATE, ALTER, and DROP statements. This enables the DBA to place restrictions on the type of DDL operations that can be performed in a given database, or you can use these triggers to send notification messages regarding important schema changes that take place in the database. The following example shows how to add a DDL trigger named NoTableUpdate to the DROP TABLE and ALTER TABLE DDL statements: CREATE TRIGGER NoTableUpdate ON DATABASE FOR DROP_TABLE, ALTER_TABLE AS PRINT 'DROP TABLE and ALTER TABLE statements are not allowed' ROLLBACK Here you can see how the new DDL trigger can be used to restrict the use of the DROP TABLE and ALTER TABLE statements. If an ALTER TABLE or DROP TABLE statement is issued, the NoTableUpdate trigger will print an error message and roll back the attempted DDL operation. An attempt to issue an ALTER TABLE statement in the database containing the NoTableUpdate trigger is shown here: DROP TABLE and ALTER TABLE statements are not allowed .Net SqlClient Data Provider: Msg 3609, Level 16, State 2, Line 1 Transaction ended in trigger. Batch has been aborted. To make alterations to the tables in a database after this trigger is in place, you will first need to drop the DDL trigger. 46 Microsoft SQL Server 2005 Developer’s Guide Security Securing a database from unwanted access is a must in any organization. With SQL Server 2005, the database server is in locked-down mode by default, which means each service and feature must be explicitly activated. You can use the following T-SQL statements to set up authority and rights to your users for access to SQL Server 2005. Logins Logins are created to allow users admission to the server. For users to access the databases in the server, you need to create User objects, as described later in this chapter. There are four types of logins you can specify for gaining access to the server: SQL Server logins, Windows logins, certificate-mapped logins, and asymmetric key- mapped logins. Logins from certificates or asymmetric keys can be created only if the certificate or asymmetric key already exists in the master database. The following listing is an example of creating a login with a password: CREATE LOGIN TecaGuest WITH PASSWORD = 'iMsoiLwR4E' MUST_CHANGE In this example, the MUST_CHANGE option requires the user to change the password the first time they connect to the server. Credentials A credential is associated with a login, as it is a record that contains authentication information when SQL Server is used in Mixed authentication mode. The following listing creates a credential for AlternateGuest with a Windows user identity of Teca01Guest: CREATE CREDENTIAL AlternateGuest WITH IDENTITY = 'Teca01Guest' After you create a credential, you can map it to a SQL Server login by using CREATE LOGIN or ALTER LOGIN. CREATE LOGIN Teca02Guest WITH PASSWORD = 'MBSim1tl', CREDENTIAL = AlternateGuest Users The User object is used to allow users access to the databases on the server. The CREATE USER statement maps a new database user to a login. The new user can also be restricted from mapping to a login. The following example uses the WITHOUT Chapter 2: Developing with T-SQL 47 LOGIN clause, which creates a user that is restricted to their own database. The user is not allowed to connect to other databases and cannot be mapped to any login: CREATE USER TecaRestrictedUser WITHOUT LOGIN Roles Roles are database-level objects used for granting permissions to a group of role members. For example, you can create a role for the payroll department of your organization, configure the database-level permissions of the role, and then add only the payroll personnel to the role. The following code creates a Payroll role. CREATE ROLE Payroll Schemas Schemas are objects that you can use to logically group together database objects like tables and views, and to set access rights to those objects. The CREATE SCHEMA statement can create a schema in the current database, as well as tables and views within the new schema. The following creates a schema named MonthSales: CREATE SCHEMA MonthSales Master Key Each database can have a single master key that is a root encryption object for all keys, certificates, and data in the database. The following shows creating a master key: CREATE MASTER KEY ENCRYPTION BY PASSWORD = 'l1y47l%9dwvyb2ayup9#$Nn' The created master key is encrypted with the triple DES algorithm and stored in two places. One storage location is the sys.symmetric_keys database table and encrypted by the supplied password; the second location is the sys.databases table in the master database and encrypted using the Service Master Key. You can use the master key to create three other types of keys: asymmetric keys, certificates, or symmetric keys. Asymmetric keys are used for public key cryptography pairing a public and private key, certificates are basically wrappers for a public key, and symmetric keys are used for shared secrets where the same key both encrypts and decrypts data. Asymmetric Keys An asymmetric key is a security entity that uses the RSA algorithm with key sizes of 512, 1024, or 2048 bits. In its default form, the asymmetric key 48 Microsoft SQL Server 2005 Developer’s Guide contains a public key and a private key, and the private key is managed and protected by the database master key. You can also specify a password-protected private key that you manage. The following shows the creation of an asymmetric key that is protected by the database master key: CREATE ASYMMETRIC KEY AsymKeySales WITH ALGORITHM = RSA_2048 Certificates A certificate is a security file or assembly that uses the X.509 standard encryption algorithm and supports X.509 V1 fields. The CREATE CERTIFICATE statement can load a certificate from either a file or an assembly. The following example creates a certificate from the master database: CREATE CERTIFICATE TecaCert09 WITH SUBJECT = 'TCert08 certificate in master database', EXPIRY_DATE = '01/31/2008' You can then create a login mapped to the certificate. CREATE LOGIN TCert08 FROM CERTIFICATE TecaCert08; Symmetric Keys The symmetric key security entity must be encrypted by using at least one certificate, password, symmetric key, or asymmetric key. It can be encrypted by using multiple certificates, passwords, symmetric keys, and asymmetric keys at the same time. With symmetric keys, only one key is used for encryption and decryption, and both participants in the encrypting/decrypting action must know this key, but its performance is much faster than that of asymmetric keys. SQL Server supports the most widely used symmetric key algorithms, including DES, triple DES, RC2, RC4, DESX, AES_128, AES_192, and AES_256. The following listing shows creating a symmetric key: CREATE SYMMETRIC KEY SymKeySales WITH ALGORITHM = AES_256 ENCRYPTION BY PASSWORD 'cNIu284ry$bd%JDqT' Storage for Searching SQL Server contains full-text searching capabilities that allow you to search data that isn’t necessarily an exact match to the full text of a column or a part of a column. For example, you can search for two words that are near each other, or you can perform a “fuzzy” search where SQL Server matches a word or phrase that is close to the search word or phrase. Full-text searching is accomplished with the Microsoft Full-Text Engine for SQL Server (MSFTESQL) that runs as a service on the operating system. The MSFTESQL [...]... Querying and Updating with T -SQL DML In the next section of this chapter you’ll see how T -SQL can be used to query and update SQL Server databases A full explanation of using T -SQL is beyond the scope of this chapter Writing SQL queries is a topic that’s big enough to warrant its own book, and in fact several books have been written on the topic This chapter will present the core T -SQL concepts that you’ll... percentage of the result set In SQL Server 2000 you were forced to use a constant value in conjunction with the TOP clause In other words, you could select the TOP 5 or TOP 10 rows, where the value of 5 or 10 was a constant With SQL Server 2005 the TOP function now enables the use of an expression in conjunction with the TOP clause An expression can be any allowed T -SQL expression, including a variable... you’ll need to get started writing T -SQL queries and to better understand how they work Select and Joins The SELECT statement is undoubtedly the key building block for using T -SQL as a basis for queries from your data access applications and T -SQL scripts, stored procedures, and functions This is true even for client and n-tiered applications that connect to SQL Server using ODBC, OLE DB, and ADO.NET... object-oriented data access framework that makes it easy for applications to work with the data retrieved from a relational database, but at their core they all still submit T -SQL commands to retrieve and update data from the SQL Server database 49 50 M i c r o s o f t S Q L S e r v e r 2 0 0 5 D e v e l o p e r ’s G u i d e Building Queries Using the SELECT Statement In its most basic form, the SELECT...C h a p t e r 2 : D e v e l o p i n g w i t h T- S Q L service is installed by default when you install SQL Server, but it runs only when fulltext search is being used MSFTESQL handles the actions of full-text searching, such as filtering and word breaking, as well as memory resources Any indexes you build for full-text searching are kept... also handle much more complex requirements by using the JOIN clause to join together rows from multiple tables, producing a single result set Using joins is common in a relational database system like SQL Server, as the data composing the database tables is typically normalized to various degrees Therefore related data is typically stored in several different tables that are intended to be joined together... HumanResources.Contacts table uses an alias of c, and the HumanResources.Department column uses an alias of d While the SELECT statement defines the result set that will be returned, the join conditions that tell SQL Server how to retrieve the data are specified in the FROM clause In this example the HumanResources.Employee table is joined to the Person.Contact table on the Contact ID to retrieve the employee name... the join condition are included in the result set Any columns that correspond to the other table are set to NULL Table 2-2 Common Join Types While this example illustrates the use of the inner join, SQL Server supports a number of additional join conditions The common join types are listed in Table 2-2 A complete list can be found in Books On-Line Combining Related Data Using UNIONs The UNION statement . DDL trigger. 46 Microsoft SQL Server 2005 Developer’s Guide Security Securing a database from unwanted access is a must in any organization. With SQL Server 2005, the database server is in locked-down. database, but at their core they all still submit T -SQL commands to retrieve and update data from the SQL Server database. 50 Microsoft SQL Server 2005 Developer’s Guide Building Queries Using the. Administration (5 row(s) affected) 56 Microsoft SQL Server 2005 Developer’s Guide Using the TOP Clause The Top clause can be used to return a given percentage of the result set. In SQL Server 2000 you were forced