1. Trang chủ
  2. » Công Nghệ Thông Tin

Microsoft SQL Server 2008 R2 Unleashed- P80 ppsx

10 230 0

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

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Định dạng
Số trang 10
Dung lượng 486,96 KB

Nội dung

ptg 734 CHAPTER 23 Creating and Managing Databases Shrinking the Log File The data file most likely to grow beyond a normal size and require periodic shrinking is the transaction log file. If a user process issues a large update transaction, the log file grows to the size needed to hold the records generated by the transaction. This could be significantly larger than the normal growth of the transaction log. As with data files, shrinking of the log file in SQL Server 2008 can take place only from the end of the log file. However, you must first back up or truncate the log to remove the inactive log records and reduce the size of the logical log. You can then run the DBCC SHRINKFILE or DBCC SHRINKDATABASE command to release the unused space in the log file. Transaction log files are divided logically into segments, called virtual log files. The Database Engine chooses the size of the virtual log files dynamically while it is creating or extending log files. Transaction log files can only be shrunk to a virtual log file boundary. It is therefore not possible to shrink a log file to a size smaller than the size of a virtual log file, even if the space is not being used. The size of the virtual log files in a transaction log increase as the size of the log file increases. For example, a database defined with a log file of 1GB may have virtual log files 128MB in size. Therefore, the log can be shrunk to only about 128MB. Because of the overhead incurred when the autoshrink process attempts to shrink database files, it is not recommended that you enable this option for the transaction log because it could be triggered numerous times during the course of a business day. It is better to schedule the shrinking of the log file to be performed during normal daily maintenance, when production system activity is at a minimum. Using SSMS to Shrink Databases In addition to shrinking a database by using T-SQL, you can do so through SSMS. In the Object Explorer, you right-click the database you want to shrink, and then you choose Tasks, followed by Shrink. You can then select either Database or Files. Selecting the Database option displays the Shrink Database dialog (see Figure 23.5). The currently allo- cated size and free space for the database are shown. You have the option of selecting the Shrink Action and checking the Reorganize Files Before Releasing Unused Space check box. You can click the Script button to generate the T-SQL that will be used to perform the database shrink operation. When you do, a DBCC SHRINKDATABASE command is generated. If you want to shrink database files, you choose the Files option instead of Database. Figure 23.6 shows the Shrink File dialog displayed when you select Files. You can shrink one database file at a time using this window. If you choose the shrink option Release Unused Space, SMSS uses the DBCC SHRINKFILE command with the TRUNCATEONLY option. If you choose the Reorganize Pages Before Releasing Unused Space option, SMSS uses the DBCC SHRINKFILE command without the TRUNCATEONLY or NOTRUNCATE option. As mentioned earlier, this causes page movement to free as much space as possible. A TRUNCATE operation then releases the free space back to the operating system. Download from www.wowebook.com ptg 735 Managing Databases 23 FIGURE 23.5 Shrinking an entire database using SSMS. FIGURE 23.6 Shrinking database files in SSMS. Download from www.wowebook.com ptg 736 CHAPTER 23 Creating and Managing Databases Moving Databases Sometimes you need to move a database or database file. There are several ways to accom- plish this task: . Make a database backup and then restore it to a new location. . Alter the database, specifying a new location for the database file. . Detach the database and then reattach the database, specifying an alternate location. Restoring a Database Backup to a New Location The database backup option is fairly straightforward. You make a backup of the database and then write it to a file or files. The file is restored, and any changes to the location of the database files are made at that time. Backup and restoration are discussed in detail in Chapter 14. You can easily detach a database by right-clicking the database in the Object Explorer and choosing Tasks and then Detach. When the database is detached, you can move the file(s) to the desired location. You can then right-click on the database’s node and select Attach. The Attach Databases screen that appears allows you to select the .mdf file and change the file location for any of the related database files. The steps involved in detaching and attaching a database are discussed in detail in the later section “Detaching and Attaching Databases.” Using ALTER DATABASE The ALTER DATABASE option for moving user database files was added in SQL Server 2005. This option involves the following steps: 1. Take the database offline. 2. Manually move the file(s) to the new location. 3. Run the ALTER DATABASE command to set the FILENAME property to the new file location. 4. Bring the database online. The following example uses the ALTER DATABASE command to move the log file for the AdventureWorks2008 database to the root of the C: drive. ALTER DATABASE AdventureWorks2008 MODIFY FILE (NAME = AdventureWorks2008_Log, FILENAME = ‘C:\AdventureWorks2008_log.ldf’) Download from www.wowebook.com ptg 737 Managing Databases 23 CAUTION Use caution when specifying the FILENAME parameter to move a database log file. If the FILENAME setting specified in the ALTER DATABASE command is incorrect and the file does not exist, the command still completes successfully. When the database is brought back online, a message stating that the file can’t be found appears, and a new log file is created for you. This invalidates the old log file. Detaching and Attaching Databases A convenient way to move or copy database files is to detach and attach databases. Detaching database files removes the database from an instance of SQL Server but leaves the database files intact. After the database is detached, the files associated with the data- base (that is, .mdf, .ndf, and .ldf files) can be copied or moved to an alternate location. You can then reattach the relocated files by using the CREATE DATABASE command with the FOR ATTACH option. TIP The process of detaching and attaching a database is extremely fast. It is therefore a good alternative to BACKUP and RESTORE when you’re copying a database to another location. The catch with detaching a database is that all users must be disconnected from the database, and the database is unavailable during the detach and copy of the database files. To detach a database, you right-click the database in Object Explorer and select Tasks and then Detach. Figure 23.7 shows an example of the Detach Database dialog box for detach- ing the AdventureWorks2008 database. You can specify several options, including a handy option (called Drop Connections) to kill any user processes (SPIDs) that may still be connected to the database when the detach operation is running. If you do not select the Drop Connections option, and users are still connected to the database, the detach opera- tion fails. Other options available during the detach operation are also useful. The Update Statistics option updates out-of-date statistics for all the database tables before you detach the data- base. The statistics update can take some time on larger databases, so this slows down the overall detach operation. The other option, Keep Full Text Catalogs, is new to SQL Server 2008. It allows you to detach any full-text catalogs associated with the database. These detached full-text catalogs are then reattached along with the database when the files are attached. See chapter 56 for a more in-depth discussion of full-text catalogs. The attach operation is simple to execute through SMSS. In Object Explorer, you simply right-click the database’s node and select the Attach option. The Attach Databases dialog box appears, allowing you to specify the database file(s) you want to attach. You need to click the Add button to be able to select a database file for restoration. When you select the main .mdf file associated with the database, the associated file information for the other related database files is populated as well. Download from www.wowebook.com ptg 738 CHAPTER 23 Creating and Managing Databases FIGURE 23.7 Detaching a database by using SSMS. 23.8 shows the Attach Databases dialog box for the AdventureWorks2008 database. The top portion of the dialog box lists the main (.mdf) database file selected for the AdventureWorks2008 database. The bottom portion lists the related files. You have an option to attach the database with a different name by changing the Attach As name located at the top of the screen. You can also edit the database details at the bottom of the screen and enter the location of the database files that will be attached. The Current File Path column displays the original file locations determined from the .mdf file. If the files were moved to a new location, this is the place to change the current file path to the new location. You can also accomplish the detach and attach operations by using T-SQL. You perform the detach operation with the sp_detach_db system stored procedure. You perform the attach operation with the CREATE DATABASE command, using the FOR ATTACH option. The following is an example of T-SQL commands for detaching and attaching the AdventureWorks2008 database: Detach the database EXEC master.dbo.sp_detach_db @dbname = N’AdventureWorks2008’, @keepfulltextindexfile=N’false’ GO Attach the database CREATE DATABASE [AdventureWorks2008] ON ( FILENAME = ‘C:\Program Files\Microsoft SQL Download from www.wowebook.com ptg 739 Managing Databases 23 FIGURE 23.8 Attaching a database by using SSMS. NOTE You can use th e sp_attach_db procedure to attach a database, but Microsoft recom- mends that you use the CREATE DATABASE FOR ATTACH command instead. The sp_attach_db procedure has been deprecated and is slated for removal in a future release of SQL Server. SQL Server 2008 has the capability to attach a database without all the log files. You do this by using the ATTACH_REBUILD_LOG clause when creating the database. When you use this clause, SQL Server rebuilds the log files for you. This capability is useful on large data- bases that may have large logs that are not needed in the environment where the database files are attached. For example, a READ_ONLY database would not need the log files that may be associated with its production counterpart. The following example uses the ATTACH_REBUILD_LOG clause to create a copy of the AdventureWorks2008 database: CREATE DATABASE [AdventureWorks2008Temp] ON ( FILENAME = ‘C:\Temp\AdventureWorks2008_Data.mdf’ ) Server\MSSQL.1\MSSQL\Data\AdventureWorks2008_Data.mdf’ ), ( FILENAME = ‘C:\Program Files\Microsoft SQL Server\MSSQL.1\MSSQL\Data\AdventureWorks2008_log.LDF’ ) FOR ATTACH Download from www.wowebook.com ptg 740 CHAPTER 23 Creating and Managing Databases FOR ATTACH_REBUILD_LOG Summary The steps involved in creating and managing databases are by no means limited to the topics in this chapter. A database consists of many database objects and has a myriad of other features discussed throughout this book. The next chapter, “Creating and Managing Tables,” delves into one of the most basic elements of a database: the table. Download from www.wowebook.com ptg CHAPTER 24 Creating and Managing Tables IN THIS CHAPTER . What’s New in SQL Server 2008 . Creating Tables . Defining Columns . Defining Table Location . Defining Table Constraints . Modifying Tables . Dropping Tables . Using Partitioned Tables . Creating Temporary Tables Tables are logical constructs used for the storage and manipulation of data in databases. Tables contain columns, which describe data, and rows, which are instances of table data. Basic relational database design determines the table and column names as well as the distribution of columns within tables. This chapter gives you the administrative knowledge you need to create tables and manage them within your enter- prise. It focuses on the basic constructs for tables and the table-level features that can make your tables robust and efficient objects to house your data. What’s New in SQL Server 2008 Most of the new table-oriented features available with SQL Server 2008 are related to the columns that define a table. Columns can now be defined with several new data types that further expand the type of data that SQL Server can store and manage. Some of these new types, such as the FILESTREAM storage and the geometry and geography data types take SQL Server beyond the typical relational model. FILESTREAM storage provides a reference point to a flat file in the operating system where the data actually resides, and the geometry and geography data types bring you into the world of spatial data storage. The hierarchyid data type is system-provided. You use hierarchyid as a data type to create tables with a hierarchi- cal structure or to reference the hierarchical structure of Download from www.wowebook.com ptg 742 CHAPTER 24 Creating and Managing Tables data in another location. You use hierarchyid functions to query and perform work with hierarchical data by using Transact-SQL (T-SQL). There have also been additions to more traditional relational data types. For example, SQL Server 2008 now offers separate data types for date and time. The new date data type contains only the date, whereas the new time data type contains only the time portion. In the past, the datetime or smalldatetime data types combined the date and time compo- nents into a single data type. Other date and time data type enhancements are discussed later in the chapter. SQL Server 2008 also introduces new ways to organize or access columns. Sparse columns and column sets are example of this. A sparse column is a column where the majority of the column’s values contain null values. A sparse column is an ordinary column but is given the SPARSE keyword when it is added to a table so that the SQL Server Database Engine can optimize the storage of these types of columns. The related column set is another type of column that is XML-typed and identifies all the sparse columns added to a table. These data types and all the new data types are discussed later in this chapter. The good news is that the management of tables and their related columns has remained relatively unchanged in SQL Server 2008. You will find that the facilities available in the SQL Server Management Studio are as familiar and as easy to use as they were in SQL Server 2005. NOTE This chapter uses examples from Bigpubs2008 database and the Adventureworks2008 database. Instructions for installing the BigPubs2008 database are located in the Introduction chapter at the beginning of this book. Creating Tables SQL Server 2008 supports the creation of tables using T-SQL, the SQL Server Management Studio (SSMS) Object Explorer and the SSMS Database Diagram Editor. Regardless of the tool you choose, creating a table involves naming the table, defining the columns, and assigning properties to the columns. The visual tools (such as Object Explorer and data- base diagrams) are usually the best starting point for creating tables. These tools offer drop-down boxes that allow you to choose the data types for your columns and check boxes that allow you to define their nullability. This chapter first looks at the visual tools and then delves into the specific parameters related to the underlying T-SQL statements that ultimately create a table. Using Object Explorer to Create Tables The Object Explorer in SSMS has a Tables node under each database listed. You can add tables via the Object Explorer by right-clicking this Tables node. Figure 24.1 shows the New Table option displayed after you right-click the Tables node in Object Explorer. The Download from www.wowebook.com ptg 743 Creating Tables FIGURE 24.1 Using Object Explorer to create a table. top-right side of the screen shown in Figure 24.1 is the table creation screen that allows you to enter the column name and data type and to set the Allow Nulls option. 24 The data entry area under Column Name is a free-form area where you can define a column name. You can select the data type from the Data Type drop-down, which displays the data types available with SQL Server. The Allow Nulls option is Boolean in nature and is either checked or not checked. For each column selected, a Column Properties section is displayed in SSMS, providing a myriad of additional properties that you can assign to each column. These properties are discussed in more detail in the “Defining Columns” section, later in this chapter. Using Database Diagrams to Create Tables You can use the database diagrams for a more robust visual representation of your tables. You view them from within SSMS, and they give you the distinct advantage of being able to display multiple tables and the relationships between these tables. The Database Diagram Editor behaves similarly to other data modeling tools that allow you to move related tables around in the diagram and group them accordingly. Figure 24.2 shows several screens related to database diagrams. The left side of Figure 24.2 shows the Object Explorer and the resulting New Database Diagram option that is displayed if you right-click the Database Diagrams node. The right side of the screen shows the diagram design window. In this example, the existing Department table from Download from www.wowebook.com . of the AdventureWorks2008 database: CREATE DATABASE [AdventureWorks2008Temp] ON ( FILENAME = ‘C:TempAdventureWorks2008_Data.mdf’ ) Server MSSQL.1MSSQLDataAdventureWorks2008_Data.mdf’ ), (. relatively unchanged in SQL Server 2008. You will find that the facilities available in the SQL Server Management Studio are as familiar and as easy to use as they were in SQL Server 2005. NOTE This. and efficient objects to house your data. What’s New in SQL Server 2008 Most of the new table-oriented features available with SQL Server 2008 are related to the columns that define a table. Columns

Ngày đăng: 05/07/2014, 02:20

TỪ KHÓA LIÊN QUAN