3 4
SQL Server provides three methods for creating a database: the Create Database Wizard, SQL Server Enterprise Manager, and T-SQL commands that can be saved in a file and run as a script. All three methods will be described in the sections that follow.
The Create Database Wizard has some limitations you should be aware of. The wizard places all data files it creates on a single drive in one folder that you specify. You cannot put data files in different physical locations (neither on different drives nor in different folders) if you use the wizard. You can place log files on a drive or in a folder separate from the data files but, again, in only one physical location. User-defined filegroups cannot be specified, and all files inherit the same growth options. Because of these limitations, the Create Database Wizard is the best choice if you need only a primary data file and a transaction log file in your database. (On the other hand, you can always add files and filegroups to the database later if you need them.)
You should use Enterprise Manager or a T-SQL script to create your database if you have secondary data files that you want to place on a disk drive different from that containing the primary data file, if you want to add user-defined filegroups, or if you need different growth options for the various files.
For novice database builders, the Create Database Wizard might be the easiest method; if it fits your needs despite the limitations mentioned earlier, by all means, use it. Follow these steps to use the Create Database Wizard to create a database:
Figure 9-4. The Select Wizard screen.
Figure 9-5. The Create Database Wizard welcome screen.
Figure 9-6. The Name The Database And Specify Its Location screen.
NOTE
The remainder of the figures in this section show the creation of a database named MyDB that has a primary data file on C:\mssql2k\MSSQL\data and one log file on D:\mssql2k\MSSQL\data.
In our example, we left the default primary file, MyDB_Data, and we added a secondary file, MyDB_Data2. Both these files will be placed in the same location you specified in step 3. (Do not continue if putting all data files on the same drive and in the same folder and filegroup is not what you want. Instead, use one of the other methods shown in the following sections for creating a database.) Click Next to continue.
Figure 9-7. The Name The Database Files screen.
Figure 9-8. The Define The Database File Growth screen.
As you did in step 4 for your database files, type the name and initial size for your transaction log. (Remember that a transaction log contains a record of all database modifications for recoverability in case of system failure.) The first transaction log file is automatically created and given the database name as a prefix in its name. You can accept this name or type a different one. The transaction log data is stored in a file with the .ldf extension. You can add more log files on different drives, if needed. If you have some idea of how big the transaction log will be, type a value now. Otherwise, keep the default size; you can modify it later using Enterprise Manager or the ALTER DATABASE command. Click Next to continue.
Figure 9-9. The Completing The Create Database Wizard screen.
SQL Server Enterprise Manager enables you to create more complex databases than does the Create Database Wizard. You can specify growth options for each file created, rather than for all the files as a group, and you can create user-defined filegroups. To create a database using Enterprise Manager, complete the steps outlined in the following list. In this example, we will create a sample database named MyDB, with a primary data file, three secondary data files (which reside in the same user-defined filegroup), and one log file.
Figure 9-10. The General tab of the Database Properties window.
Figure 9-11. The Data Files tab of the Database Properties window.
The location for each file is by default the folder on the drive on which SQL Server is installed. You can change this setting by typing a new path or by using the browse button.
You might prefer to create or alter your databases by using T-SQL commands or scripting rather than by using a graphical user interface (GUI). Creating your own scripts can be useful when you create databases. Let's say you create the database but then realize you specified the wrong location for a file. You can drop the database and start over. If you used a T-SQL script to create the database, you will be able to edit it and rerun it quickly, instead of having to reenter all the data into a GUI. You can also run the same script if you need to create the database on another system, such as a warm backup system.
On the other hand, Enterprise Manager can be used to generate T-SQL scripts for database creation (as well as for the creation of all database objects) but only after a database has already been created. The Enterprise Manager scripts will include all of the current database option settings, which can be useful; therefore, you might prefer to use the generated scripts. Whether you decide to write your own scripts or to use generated scripts, it is good to understand the T-SQL code used to create a database. In this section, we will review the T-SQL commands for creating a database. These commands can be typed into a file to create a script. Specifically how to create and run a script is explained in Chapter 13.
REAL WORLD A Simple Database
In the following example, we'll create a database named MyDB that contains a primary data file (MyDB_root); one secondary data file (MyDB_data1), which remains in the primary filegroup by default; and one transaction log file (Log_data1). The SQL statements for creating the MyDB database are shown here:
CREATE DATABASE MyDB ON (NAME = MyDB_root, --Primary data file FILENAME = 'c:\mssql2k\MSSQL\data\mydbroot.mdf', SIZE = 8MB, MAXSIZE = 9MB, FILEGROWTH = 100KB), (NAME = MyDB_data1, --Secondary data file FILENAME = 'c:\mssql2k\MSSQL\data\mydbdata1.ndf', SIZE = 1000MB, MAXSIZE = 1500MB, FILEGROWTH = 100MB) LOG ON (NAME = Log_data1, --Log file FILENAME = 'e:\log_files\logdata1.ldf', SIZE = 1000MB, MAXSIZE = 1500MB, FILEGROWTH = 100MB)
Notice that, in this example, the primary and secondary data files are both on the C drive and the log file is on the E drive. As mentioned, you should always physically separate data files from log files to improve disk I/O performance.
Also, notice that the recommended file extensions—.mdf, .ndf, and .ldf—were used. The SIZE, MAXSIZE, and FILEGROWTH options can be specified in kilobytes or megabytes; the default is megabytes.
MORE INFO
To learn more about the CREATE DATABASE statement, check the Books Online index for the topic "Create Database."
REAL WORLD A More Complex Database
The database example in this section is based on a system with several disk drives or several arrays of disks (in a RAID system). The term "disk" will be used to refer to either a single disk drive or an array of disk drives acting as a RAID volume. Several files are created, each on a separate disk. Each file is placed in one of two filegroups. Our database will be named "Sales" and will contain the following files:
- A primary data file, Sales_root.mdf
- Three secondary data files, customer_data1.ndf, customer_data2.ndf, and customer_data3.ndf, in the filegroup customers_group
- Two secondary data files, product_data1.ndf and product_data2.ndf, in the filegroup products_group
- One log file, log_data1.ldf
The code for creating the Sales database is shown here. Because we will be creating user-defined filegroups, we will include the keyword FILEGROUP with this CREATE DATABASE command.
CREATE DATABASE Sales ON PRIMARY --Explicitly states primary --filegroup (optional). (NAME = Salesroot, --Primary data file FILENAME = 'c:\mssql2k\MSSQL\data\salesroot.mdf', SIZE = 8MB, MAXSIZE = 10MB, FILEGROWTH = 1MB), FILEGROUP customers_group --Filegroup for next files (NAME = customer_data1, --Secondary data file FILENAME = 'd:\mssql2k\MSSQL\data\customerdata1.ndf', SIZE = 800MB, MAXSIZE = 1000MB, FILEGROWTH = 100MB), (NAME = customer_data2, --Secondary data file FILENAME = 'e:\mssql2k\MSSQL\data\customerdata2.ndf', SIZE = 800MB, MAXSIZE = 1000MB, FILEGROWTH = 100MB), (NAME = customer_data3, --Secondary data file FILENAME = 'f:\mssql2k\MSSQL\data\customerdata3.ndf', SIZE = 800MB, MAXSIZE = 1000MB, FILEGROWTH = 100MB), FILEGROUP products_group --Filegroup for next files (NAME = product_data1, --Secondary data file FILENAME = 'g:\mssql2k\MSSQL\data\product_data1.ndf', SIZE = 500MB, MAXSIZE = 700MB, FILEGROWTH = 100MB), (NAME = product_data2, --Secondary data file FILENAME = 'h:\mssql2k\MSSQL\data\product_data2.ndf', SIZE = 500MB, MAXSIZE = 700MB, FILEGROWTH = 100MB) LOG ON (NAME = logdata1, --Log file FILENAME = 'i:\log_files\logdata1.ldf', SIZE = 800MB, MAXSIZE = 1000MB, FILEGROWTH = 200MB)
As the comments in this code indicate, the primary filegroup can be explicitly stated before the primary data file is defined. The primary filegroup is the default. The other two filegroups, customers_group and products_group, are defined immediately before the files that will be placed in them. All of the files listed after a filegroup definition will be placed in that filegroup until another filegroup is defined or the LOG ON clause is reached.
Also, notice the drive letters for each file. Each file is created on a different disk to allow data to be spread across disks when the tables and indexes are created in the filegroups. For instance, a table created in the customers_group filegroup will have its data striped across the disks that hold the customer_data1.ndf, customer_data2.ndf, and customer_data3.ndf files. The log file is also on a separate disk, with no data files, to allow the log to perform sequential writes.
If you do not want SQL Server to automatically grow a file, set FILEGROWTH=0 so that the file will be allowed to fill only its initial size. You might choose this setting if you have a table that is static—in other words, a table that does not grow. In this case, MAXSIZE would not need to be specified in the statement. The maximum size would be the initial SIZE setting.
NOTE
If you're familiar with SQL Server versions 6.5 and earlier, you'll notice that you no longer use the DISK INIT command to create logical devices before creating the database. The use of files replaces logical devices.