Beginning SQL Server 2005 for Developers From Novice to Professional phần 4 ppt

53 263 0
Beginning SQL Server 2005 for Developers From Novice to Professional phần 4 ppt

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

Thông tin tài liệu

136 CHAPTER 5 ■ DEFINING TABLES Defining a Table: Using a Template SQL Server has a third method of building tables, although this is my least favored method. A large number of templates are built into SQL Server Management Studio for everyday tasks. It is also possible to build your own template for repetitive tasks, which is where I can see more power for developers in this area. Templates can be found in their own explorer window. Selecting View ➤ Template Explorer or pressing Ctrl+Alt+T brings up the Template Explorer window, displayed initially on the right-hand side of SQL Server Management Studio. Try It Out: Creating a Table Using a Template 1. Expand the Table node on the Template Explorer. About halfway down you will see a template called Create Table, as shown in Figure 5-13. Double-click this to open up a new Query Editor pane with the template for creating a table. Figure 5-13. List of templates Dewson_5882C05.fm Page 136 Monday, January 9, 2006 3:26 PM CHAPTER 5 ■ DEFINING TABLES 137 2. Take a close look at the following, which is the listing from the template. A template includes a number of parameters. These are enclosed by angle brackets (<>). ========================================= Create table template ========================================= USE <database, sysname, AdventureWorks> GO IF OBJECT_ID('<schema_name, sysname, dbo>.<table_name, sysname, sample_table>', 'U') IS NOT NULL DROP TABLE <schema_name, sysname, dbo>.<table_name, sysname,➥ sample_table> GO CREATE TABLE <schema_name, sysname, dbo>.<table_name, sysname, sample_table>( <column1_name, sysname, c1> <column1_datatype, , int> <column1_nullability,, NOT NULL>, <column2_name, sysname, c2> <column2_datatype, , char(10)> <column2_nullability,, NULL>, <column3_name, sysname, c3> <column3_datatype, , datetime> <column3_nullability,, NULL>, CONSTRAINT <contraint_name, sysname, PK_sample_table> PRIMARY KEY (<columns_in_primary_key, , c1>) ) GO 3. By pressing Ctrl+Shift+M, you can alter these parameters to make a set of meaningful code. Do this now, so that the parameters can be altered. Figure 5-14 shows most of our third table, TransactionDetails. TransactionTypes. The reason I say most is that our template code only deals with three columns, and our table has four columns. Before choosing to display this screen, you could have altered the code to include the fourth column, or you could modify the base template if you think that three columns are not enough. When you scroll down, you will see a parameter called CONSTRAINT. You can either leave the details as they are or blank them out; it doesn’t matter, as we will be removing that code in a moment. Dewson_5882C05.fm Page 137 Monday, January 9, 2006 3:26 PM 138 CHAPTER 5 ■ DEFINING TABLES Figure 5-14. Template parameters for TransactionTypes 4. After clicking OK, the code is as follows. The main point of interest is the IF statement after switching to the ApressFinancial database. This code queries SQL Server’s system tables to check for a TransactionTypes table within the dbo schema. If it does exist, then the DROP TABLE statement is executed. This statement will delete the table defined from SQL Server, if possible. An error message may be displayed if the table has links with other tables or if someone has a lock on it, thus preventing the deletion. We talk about locks in Chapter 8. ========================================= Create table template ========================================= USE ApressFinancial GO IF OBJECT_ID('dbo.TransactionTypes', 'U') IS NOT NULL DROP TABLE dbo.TransactionTypes GO CREATE TABLE dbo.TransactionTypes( TransactionTypeId int NOT NULL, TransactionDescription nvarchar(30) NOT NULL, CreditType bit NOT NULL, CONSTRAINT PRIMARY KEY () ) GO 5. The full code for the TransactionTypes table follows. Once you have entered it, you can execute it. Note that there are three changes here. First of all, we change the schema name from dbo to the correct schema, TransactionDetails, then we put in the IDENTITY details for the TransactionTypeId column, but we are not going to place the fourth column in at this time. We will add it when we take a look at how to alter a table in the section “The ALTER TABLE Command” later in this chapter. Finally, we remove the CONSTRAINT statement, as we are not creating a key at this time. ========================================= Dewson_5882C05.fm Page 138 Monday, January 9, 2006 3:26 PM CHAPTER 5 ■ DEFINING TABLES 139 Create table template ========================================= USE ApressFinancial GO IF OBJECT_ID('TransactionDetails.TransactionTypes', 'U') IS NOT NULL DROP TABLE TransactionDetails.TransactionTypes GO CREATE TABLE TransactionDetails.TransactionTypes( TransactionTypeId int IDENTITY(1,1) NOT NULL, TransactionDescription nvarchar(30) NOT NULL, CreditType bit NOT NULL ) GO Now that we have our third table, we can look at altering the template of the CREATE TEMPLATE, as it would be better to have the IDENTITY parameter there as well as four or five columns. Creating and Altering a Template The processes for creating and altering a template follow the same steps. All templates are stored in a central location and are available for every connection to SQL Server on that computer, therefore templates are not database or server restricted. The path to where they reside is C:\Program Files\Microsoft SQL Server\ 90\Tools\Binn\VSShell\Common7\IDE\sqlworkbenchnewitems\Sql It is also possible to create a new node for templates from within the Template Explorer by right clicking and selecting New ➤ Folder. ■Note Don’t create the folder directly in the Sql folder, as this is not picked up by SQL Server Management Studio until you exit and reenter the SQL Server Management Studio. You could create different formats of templates for slightly different actions on tables. We saw the CREATE TABLE template previously, but what if we wanted a template that included a CREATE TABLE specification with an IDENTITY column? This is possible by taking a current template and upgrading it for a new template. Dewson_5882C05.fm Page 139 Monday, January 9, 2006 3:26 PM 140 CHAPTER 5 ■ DEFINING TABLES Try It Out: Creating a Template from an Existing Template 1. From the Template Explorer, find the CREATE TABLE template, right-click it, and select Edit. This will display the template that we saw earlier. Change the comment and then we can start altering the code. 2. The first change is to add that the first column is an IDENTITY column. We know where this is located from our code earlier: it comes directly after the data type. To add a new parameter, input a set of angle brackets, then create the name of the parameter as the first option. The second option is the type of parameter this is, for example, sysname, defining that the parameter is a system name, which is just an alias for nvarchar(256). The third option is the value for the parameter; in this case we will be including the value of IDENTITY(1,1). The final set of code follows, where you can also see a fourth column has been defined with a bit data type. ■Tip You can check the alias by running the sp_help_sysname T-SQL command. ========================================= Create table template with IDENTITY ========================================= USE <database, sysname, AdventureWorks> GO IF OBJECT_ID('<schema_name, sysname, dbo>.<table_name, sysname,➥ sample_table>', 'U') IS NOT NULL DROP TABLE <schema_name, sysname, dbo>.<table_name, sysname, sample_table> GO CREATE TABLE <schema_name, sysname, dbo>.<table_name, sysname, sample_table>( <column1_name, sysname, c1> <column1_datatype, , int> ➥ <identity,,IDENTITY (1,1)> <column1_nullability,, NOT NULL>, <column2_name, sysname, c2> <column2_datatype, , char(10)> <column2_nullability,, NULL>, <column3_name, sysname, c3> <column3_datatype, , datetime> <column3_nullability,, NULL>, <column4_name, sysname, c4> <column4_datatype, , bit> <column4_nullability,, NOT NULL>, CONSTRAINT <contraint_name, sysname, PK_sample_table> PRIMARY KEY (<columns_in_primary_key, , c1>) ) GO Dewson_5882C05.fm Page 140 Monday, January 9, 2006 3:26 PM CHAPTER 5 ■ DEFINING TABLES 141 3. Now the code is built, but before we test it, we shall save this as a new template called CREATE TABLE with IDENTITY. From the menu, select File ➤ Save CREATE TABLE.sql As, and from the Save File As dialog box, save this as CREATE TABLE with IDENTITY.sql. This should update your Template Explorer, but if it doesn’t, try exiting and reentering SQL Server Management Studio, after which it will be avail- able to use. The ALTER TABLE Command If, when using the original template, we had created the table with only three columns, we would have an error to correct. One solution is to delete the table with DROP TABLE, but if we had placed some test data in the table before we realized we had missed the column, this would not be ideal. There is an alternative: the ALTER TABLE statement, which allows restrictive alterations to a table layout but keeps the contents. SQL Server Management Studio uses this statement when altering a table graphically, but here I will show you how to use it to add the missing fourth column for our TransactionTypes table. Columns can be added, removed, or modified using the ALTER TABLE command. Removing a column will simply remove the data within that column, but careful thought has to take place before adding or altering a column. There are two scenarios when adding a new column to a table: should it contain NULL values for all the existing rows, or should there be a default value instead? Any new columns created using the ALTER TABLE statement where a value is expected (or defined as NOT NULL) will take time to implement. This is because any existing data will have NULL values for the new column; after all, SQL Server has no way of knowing what value to enter. When altering a table and using NOT NULL, you need to complete a number of complex processes, which include moving data to an interim table and then moving it back. The easiest solution is to alter the table and define the column to allow NULLs, add in the default data values using the UPDATE T-SQL command, and alter the column to NOT NULL. ■Note It is common practice when creating columns to allow NULL values, as the default value may not be valid in some rows. Try It Out: Adding a Column 1. First of all, open up the Query Editor and ensure that you are pointing to the ApressFinancial data- base. Then write the code to alter the TransactionDetails.TransactionTypes table to add the new column. The format is very simple. We specify the table prefixed by the schema name we want to alter after the ALTER TABLE command. Next we use a comma-delimited list of the columns we wish to add. We define the name, the data type, the length if required, and finally whether we allow NULLs or not. As we don’t want the existing data to have any default values, we will have to define the column to allow NULL values. Dewson_5882C05.fm Page 141 Monday, January 9, 2006 3:26 PM 142 CHAPTER 5 ■ DEFINING TABLES ALTER TABLE TransactionDetails.TransactionTypes ADD AffectCashBalance bit NULL GO 2. Once we’ve altered the data as required, we then want to remove the ability for further rows of data to have a NULL value. This new column will take a value of 0 or 1. Again, we use the ALTER TABLE command, but this time we’ll add the ALTER COLUMN statement with the name of the column we wish to alter. After this statement are the alterations we wish to make. Although we are not altering the data type, it is a mandatory requirement to redefine the data type and data length. After this, we can inform SQL Server that the column will not allow NULL values. ALTER TABLE TransactionDetails.TransactionTypes ALTER COLUMN AffectCashBalance bit NOT NULL GO 3. Execute the preceding code to make the TransactionDetails.TransactionTypes table correct. Defining the Remaining Tables Now that three of the tables have been created, we need to create the remaining four tables. We will do this as code placed in Query Editor. There is nothing specifically new to cover in this next section, and therefore only the code is listed. Enter the following code and then execute it as before. You can then move into SQL Server Management Studio and refresh it, after which you should be able to see the new tables. USE ApressFinancial GO CREATE TABLE CustomerDetails.CustomerProducts( CustomerFinancialProductId bigint NOT NULL, CustomerId bigint NOT NULL, FinancialProductId bigint NOT NULL, AmountToCollect money NOT NULL, Frequency smallint NOT NULL, LastCollected datetime NOT NULL, LastCollection datetime NOT NULL, Renewable bit NOT NULL ) ON [PRIMARY] GO CREATE TABLE CustomerDetails.FinancialProducts( ProductId bigint NOT NULL, ProductName nvarchar(50) NOT NULL ) ON [PRIMARY] Dewson_5882C05.fm Page 142 Monday, January 9, 2006 3:26 PM CHAPTER 5 ■ DEFINING TABLES 143 GO CREATE TABLE ShareDetails.SharePrices( SharePriceId bigint IDENTITY(1,1) NOT NULL, ShareId bigint NOT NULL, Price numeric(18, 5) NOT NULL, PriceDate datetime NOT NULL ) ON [PRIMARY] GO CREATE TABLE ShareDetails.Shares( ShareId bigint IDENTITY(1,1) NOT NULL, ShareDesc nvarchar(50) NOT NULL, ShareTickerId nvarchar(50) NULL, CurrentPrice numeric(18, 5) NOT NULL ) ON [PRIMARY] GO Setting a Primary Key Setting a primary key can be completed in SQL Server Management Studio with just a couple of mouse clicks. This section will demonstrate how easy this actually is. For more on keys, see Chapter 3. Try It Out: Setting a Primary Key 1. Ensure that SQL Server Management Studio is running and that you have navigated to the ApressFinancial database. Find the ShareDetails.Shares table, and right-click and select Modify. Once in the Table Designer, select the ShareId column. This will be the column we are setting the primary key for. Right-click to bring up the pop-up menu shown in Figure 5-15. Figure 5-15. Defining a primary key Dewson_5882C05.fm Page 143 Monday, January 9, 2006 3:26 PM 144 CHAPTER 5 ■ DEFINING TABLES 2. Select the Set Primary Key option from the pop-up menu. This will then change the display to place a small key in the leftmost column details. Only one column has been defined as the primary key, as you see in Figure 5-16. Figure 5-16. Primary key defined 3. However, this is not all that happens, as you will see. Save the table modifications by clicking the Save button. Click the Manage Indexes/Keys button on the toolbar. This brings up the dialog box shown in Figure 5-17. Look at the Type, the third option down in the General section. It says Primary Key. Notice that a key definition has been created for you, with a name and the selected column, informing you that the index is unique and clustered (more on indexes and their relation to primary keys in Chapter 6). Figure 5-17. Indexes/Keys dialog box That’s all there is to creating and setting a primary key. A primary key has now been set up on the ShareDetails.Shares table. In this instance, any record added to this table will ensure that the data will be kept in ShareId ascending order (this is to do with the index, which you will see in Chapter 6), and it is impossible to insert a duplicate row of data. This key can then be used to link to other tables within the database at a later stage. Creating a Relationship We covered relationships in Chapter 3, but we’ve not created any. Now we will. The first relation- ship that we create will be between the customer and customer transactions tables. This will be Dewson_5882C05.fm Page 144 Monday, January 9, 2006 3:26 PM CHAPTER 5 ■ DEFINING TABLES 145 a one-to-many relationship where there is one customer record to many transaction records. Keep in mind that although a customer may have several customer records, one for each product he or she has bought, the relationship is a combination of customer and product to transactions because a new CustomerId will be generated for each product the customer buys. We will now build that first relationship. Try It Out: Building a Relationship 1. Ensure that SQL Server Management Studio is running, and that ApressFinancial database is selected and expanded. We need to add a primary key to CustomerDetails.Customers. Enter the code that follows and then execute it: ALTER TABLE CustomerDetails.Customers ADD CONSTRAINT PK_Customers PRIMARY KEY NONCLUSTERED ( CustomerId ) WITH( STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] GO 2. Find and select the TransactionDetails.Transactions table, and then right-click. Select Design Table to invoke the Table Designer. 3. Once in the Table Designer, right-click and select Relationships from the pop-up menu shown in Figure 5-18. Or click the Relationships button on the Table Designer toolbar. Figure 5-18. Building a relationship 4. This brings up the relationship designer. As it’s empty, you need to click Add. This will then populate the screen as shown in Figure 5-19. Dewson_5882C05.fm Page 145 Monday, January 9, 2006 3:26 PM [...]... cannot have data entered into it by any SQL command, as the data entered in to this column is completed automatically by SQL Server itself Also, in an identity column, by default no two rows can have the same value However, there is no automation to stop any attempt to create duplicate keys Therefore, there is still a need to inform SQL Server that the index will be unique Moving on to the Create As Clustered... and you don’t let SQL Server update the statistics on the table, then SQL Server could be using inaccurate information when it is trying to decide how to retrieve the data It could even mean that the index change you thought would improve performance has in fact made the performance much slower That said, it is not always prudent to let SQL Server recompute statistics automatically SQL Server will do... Values for Template Parameters option, which will make the index creating easier The button should be on the SQL Editor toolbar (see Figure 6-5) Figure 6-5 The Specify Values for Template Parameters button 4 Change the database to the example database, name the index (in this case, it has been named after the table), set schema_name to CustomerDetails, table_name to CustomerProducts, and column_name1 to. .. indexes, let’s proceed to create some in SQL Server There are many different ways to create indexes within SQL Server, as you might expect Those various methods are the focus of this section of the chapter, starting with how to use the table designer in SQL Server Management Studio The first index we’ll place into the database will be on the CustomerId field within the CustomerDetails.Customers table Dewson_5882C06.fm... what data exists To give a quick example of a referential constraint, say you have a customer who owns banking products A referential constraint would prevent the customer’s record from being deleted while those products existed SQL Server does not automatically create indexes on your foreign keys However, as the foreign key column values need to be identified by SQL Server when joining to the parent... and uses the information it gleans from these to figure out what indexes to place within the database and where improvements can be made At this point in the book, I haven’t actually covered working with data, so going through the use of this tool will just lead to confusion This powerful and advanced tool should be used only by experienced SQL Server 2005 developers or database administrators Getting... group from the base table If the file groups are located on separate disks, data retrieval can be enhanced for your queries as SQL Server can use parallel I/O operations to retrieve the data from the index and base tables concurrently When you are retrieving information from a table that has a nonclustered index, SQL Server finds the relevant row in the index If the information you want doesn’t form... user front-end for updating the remaining areas of the customer’s data, and it could also display specific customer details, but it would not know that the CustomerId requires incrementing for each new record, and it would not know the value to start from The first index created will be used to find the record to update with a customer’s information The application will have found the customer using a... the CustomerId and use this to find the record within SQL Server When completing the initial search, the CustomerId is returned as part of the set of values, so when the user selects the appropriate John J Doe, the application knows the appropriate CustomerId SQL Server will use this value to specifically locate the record to update In the following exercise, we’ll add this index to the Customers table... of those columns specified in the ORDER BY clause, SQL Server may be able to avoid performing an internal sort, resulting in improved query performance ■Tip If an index is only one column, SQL Server can read the index just as fast in a forward direction as it can backward 6 As indicated earlier, SQL Server generates the value of the CustomerId column to be the next number in a sequence when a record . templates are stored in a central location and are available for every connection to SQL Server on that computer, therefore templates are not database or server restricted. The path to where they. reside is C:Program FilesMicrosoft SQL Server 90ToolsBinnVSShellCommon7IDEsqlworkbenchnewitems Sql It is also possible to create a new node for templates from within the Template Explorer. T -SQL you can save the code and its ready for deployment to production servers when required. Try It Out: Using SQL to Build a Relationship 1. In a Query Editor pane, enter the following T-SQL

Ngày đăng: 14/08/2014, 10:22

Từ khóa liên quan

Mục lục

  • Beginning SQL Server 2005 for Developers

    • Chapter 6 Creating Indexes and Database Diagramming

    • Chapter 7 Database Backups, Recovery, and Maintenance

Tài liệu cùng người dùng

  • Đang cập nhật ...

Tài liệu liên quan