Você está na página 1de 13

Module 1: Creating Databases and Database Files

1-15

Practice: Creating Databases

The purpose of this practice is to enable you to create a database by using SQL Server
Management Studio and by using Transact-SQL. In this practice, you will use both
techniques to create a new database.

Objectives
In this practice, you will:

Create a database by using SQL Server Management Studio.

Create a database by using Transact-SQL.

Instructions

Start the 2779B-MIA-SQL-01 virtual machine.

Log on to the virtual machine with the user name Student and the password
Pa$$w0rd.

Create a database by using SQL Server Management Studio


1. Click Start, point to All Programs, point to Microsoft SQL Server 2005, and then
click SQL Server Management Studio.
2. In the Connect to Server dialog box, specify the values in the following table, and
then click Connect.
Property

Value

Server type

Database Engine

Server name

MIAMI

Authentication

Windows Authentication

1-16

Module 1: Creating Databases and Database Files

3. If Object Explorer is not visible, on the View menu, click Object Explorer.
4. In Object Explorer, right-click Databases, and then click New Database.
5. In the New Database dialog box, specify the details in the following table.
Property

Value

Database name

TestDB

Initial Size for TestDB

20

6. In the Autogrowth column of the TestDB entry, click the ellipsis button (...).
7. In the Change Autogrowth for TestDB dialog box, clear the Enable Autogrowth
check box, and then click OK.
8. For the TestDB_log entry, change the Initial Size setting to 5 MB.
9. In the Autogrowth column of the TestDB_log entry, click the ellipsis button (...).
10. In the Change Autogrowth for TestDB_log dialog box, clear the Enable
Autogrowth check box, and then click OK.
11. In the New Database dialog box, click OK to create the database.
12. In Object Explorer, expand the Databases folder to verify that TestDB has been
created. If TestDB is not listed, right-click the Databases folder, and then click
Refresh.
13. Keep SQL Server Management Studio open. You will use it in the next procedure.

Create a database by using Transact-SQL


1. In SQL Server Management Studio, on the toolbar, click New Query.
2. In the new, blank query window, type the following Transact-SQL code (each
FILENAME parameter should be on a single line).
CREATE DATABASE TransactTestDB
ON (NAME = 'TransactTestDB',
FILENAME = 'C:\Program Files\Microsoft SQL
Server\MSSQL.1\MSSQL\DATA\TransactTestDB.mdf',
SIZE = 20 MB,
FILEGROWTH = 0)
LOG ON (NAME = 'TransactTestDB_Log',
FILENAME = 'C:\Program Files\Microsoft SQL
Server\MSSQL.1\MSSQL\DATA\TransactTestDB_Log.ldf',
SIZE = 5 MB,
FILEGROWTH = 0)

3. On the toolbar, click Execute.


4. When the command has completed successfully, in Object Explorer, right-click the
Databases folder, and then click Refresh to verify that the TransactTestDB
database has been created.
5. Close SQL Server Management Studio. Click No if prompted to save any files.

Module 1: Creating Databases and Database Files

1-23

Practice: Creating Filegroups

The purpose of this practice is to enable you to create filegroups by using SQL Server
Management Studio and by using Transact-SQL. In this practice, you will use both
techniques to create a new database filegroup.

Objectives
In this practice, you will:

Create a filegroup by using SQL Server Management Studio.

Create a filegroup by using Transact-SQL.


.

Instructions

Return to the 2779B-MIA-SQL-01 virtual machine.

Ensure that you are logged on to the virtual machine with the user name Student and
the password Pa$$w0rd.

Important: This demonstration requires you to have completed the preceding


practice, "Creating Databases."

Create a filegroup by using SQL Server Management Studio


1. Click Start, point to All Programs, point to Microsoft SQL Server 2005, and then
click SQL Server Management Studio.
2. In the Connect to Server dialog box, specify the values in the following table, and
then click Connect.

1-24

Module 1: Creating Databases and Database Files

Property

Value

Server type

Database Engine

Server name

MIAMI

Authentication

Windows Authentication

3. If Object Explorer is not visible, on the View menu, click Object Explorer.
4. In Object Explorer, expand Databases, right-click TestDB, and then click
Properties.
5. In the Database Properties - TestDB dialog box, in the Select a page pane, click
Filegroups, and then click Add.
6. Enter the settings in the following table.
Property

Value or action

Name

SECONDARY

Default

Selected

7. In the Select a page pane, click Files, and then click Add.
8. Type in the settings in the following table.
Property

Value

Logical Name

TestDB2

File Type

Data

Filegroup

SECONDARY

9. In the Database Properties - TestDB dialog box, click OK to create the new file and
filegroup.
10. In Object Explorer, if the TestDB node is not selected, click TestDB.
11. On the Summary tab, click Report.
12. When the report is displayed, expand Disk Space Used by Data Files. The report
should show the two filegroups and the list of files for each group.
13. Keep SQL Server Management Studio open. You will use it in the next procedure.

Create a filegroup by using Transact-SQL


1. In SQL Server Management Studio, on the toolbar, click New Query.
2. In the new, blank query window, type the following Transact-SQL code.
ALTER DATABASE [TransactTestDB]
ADD FILEGROUP [SECONDARY]
GO
ALTER DATABASE [TransactTestDB]
ADD FILE (
NAME = N'Test2', FILENAME =
N'C:\Program Files\Microsoft SQL Server\MSSQL.1\MSSQL\Data\Test2.ndf')

Module 1: Creating Databases and Database Files

1-25

TO FILEGROUP [SECONDARY]
GO
ALTER DATABASE [TransactTestDB] MODIFY FILEGROUP [SECONDARY] DEFAULT
GO

3. On the toolbar, click Execute.


4. In Object Explorer, if the TransactTestDB node is not selected, click
TransactTestDB.
5. Click the Summary tab, and then click Report.
6. When the report is displayed, expand Disk Space Used by Data Files. The report
should show the two filegroups and the list of files for each group.
7. Close SQL Server Management Studio. Click No if prompted to save any files.
After you complete the practice, leave the 2779B-MIA-SQL-01 virtual machine running.
You will use the databases you created in the Creating Databases practice in the next
practice.

Module 1: Creating Databases and Database Files

1-31

Practice: Creating a Schema

The purpose of this practice is to enable you to create schemas by using SQL Server
Management Studio and by using Transact-SQL. In this practice, you will use both
techniques to create a new schema.

Objectives
In this practice, you will:

Create a schema by using SQL Server Management Studio.

Create a schema by using Transact-SQL.

Instructions

Return to the 2779B-MIA-SQL-01 virtual machine.

Ensure that you are logged on to the virtual machine with the user name Student and
the password Pa$$w0rd.

Important: This demonstration requires you to have completed the first practice in
this module, "Creating Databases."

Create a schema by using SQL Server Management Studio


1. Click Start, point to All Programs, point to Microsoft SQL Server 2005, and then
click SQL Server Management Studio.
2. In the Connect to Server dialog box, specify the values in the following table, and
then click Connect.

1-32

Module 1: Creating Databases and Database Files

Property

Value

Server type

Database Engine

Server name

MIAMI

Authentication

Windows Authentication

3. If Object Explorer is not visible, on the View menu, click Object Explorer.
4. In Object Explorer, expand Databases, expand TestDB, expand Security, right-click
Schemas, and then click New Schema.
5. In the Schema - New dialog box, in the Schema name box, type Sales, and then
click OK.
6. In Object Explorer, expand Schemas, and then confirm that the Sales schema exists.
7. Keep SQL Server Management Studio open. You will use it in the next procedure.

Create a schema by using Transact-SQL


1. In SQL Server Management Studio, on the toolbar, click New Query.
2. In the new, blank query window, type the following Transact-SQL code.
Use TransactTestDB
GO
CREATE SCHEMA [Marketing]
GO

3. On the toolbar, click Execute.


4. In Object Explorer, expand Databases, expand TransactTestDB, expand Security,
expand Schemas, and then confirm that the Marketing schema exists.
5. Close SQL Server Management Studio. Click No if prompted to save any files.
After you complete the practice, leave the 2779B-MIA-SQL-01 virtual machine running.
You will use it in the next practice.

Module 1: Creating Databases and Database Files

1-39

Lab: Creating Databases and Database Files

After completing this lab, you will be able to:

Create a new database with options and filegroups.

Create a schema.

Create a database snapshot.

Estimated time to complete this lab: 30 minutes

Lab Setup
For this lab, you will use the available virtual machine environment. Before you begin the
lab, you must:

Start the 2779B-MIA-SQL-01 virtual machine.

Log on to the virtual machine with the user name Student and the password
Pa$$w0rd.

Lab Scenario
The senior database developer at Adventure Works has created a specification for a new
database in which details of the information technology (IT) assets used by the company
will be stored. You must use the specification to create the appropriate filegroups, create
the database with the necessary options, create the required schemas, and create a
database snapshot.
The senior database developer has provided you with the following requirements for the
new database:

The new database name is AW_IT_Assets.

1-40

Module 1: Creating Databases and Database Files

The database should consist of two data files and one log file with the names
AW_IT_Assets_Data1, AW_IT_Assets_Data2, and AW_IT_Assets_log. These
files should be created in the C:\Program Files\Microsoft SQL
Server\MSSQL.1\MSSQL\Data folder.

The database should use two filegroups, the primary filegroup and a filegroup named
SECONDARY, which should be the default filegroup. The AW_IT_Assets_Data2
file should be placed in the SECONDARY filegroup.

Both data files should have an initial size of 20 megabytes (MB) each and should not
grow automatically. The log file should have an initial size of 5 MB and should not
grow automatically.

The database should automatically close when no users are connected and should
automatically shrink when appropriate.

The database should have one user-defined schema named TechSupport.

A script for creating and populating a table has been generated and must be executed
in the database.

The user MIAMI\Katie should use the TechSupport schema as her default schema.

A database snapshot named AW_IT_Assets_Snapshot1 must be created for


reporting purposes.

The files for the database snapshot must be created in the C:\Program
Files\Microsoft SQL Server\MSSQL.1\MSSQL\Data\ folder.

Two data files, AW_IT_Assets_Data1 and AW_IT_Assets_Data2, with


physical filenames AWIT1 and AWIT2 respectively, must be used.

Use SQL Server Management Studio to create scripts for each action, and then save
these scripts in a SQL Server Scripts project in the D:\Labfiles\Starter folder. This
project can be used to document the database configuration and to re-create the
database if necessary.

Scripts to create tables and users have been provided in the D:\Labfiles\Starter folder.

Additional Information
When performing database development tasks, it can be helpful to use SQL Server
Management Studio to create a SQL Server Scripts project, and use it to document the
Transact-SQL code necessary to re-create the solution if necessary.
Use the following procedure to create a SQL Server Scripts project:
1. Open SQL Server Management Studio, connecting to the server you want to manage.
2. On the File menu, point to New, and then click Project.
3. Select the SQL Server Scripts template and enter a suitable name and location for
the project. Note that you can create a solution that contains multiple projects, but in
many cases a single project per solution is appropriate.

Module 1: Creating Databases and Database Files

1-41

Use the following procedure to add a query file to a project:


1. On the Project menu, click New Query, or in Solution Explorer, right-click the
Queries folder, and then click New Query. If Solution Explorer is not visible, on the
View menu, click Solution Explorer.
2. When prompted, connect to the server on which you want to execute the query. This
will add a connection object to the project.
3. To change the name of the query file from the default name (SQLQuery1.sql), rightclick it in Solution Explorer and click Rename.
Although you can perform all database development tasks by executing Transact-SQL
statements, it is often easier to use the graphical user interface in SQL Server
Management Studio. However, you should generate the corresponding Transact-SQL
scripts and save them in the project for future reference.
Often, you can generate the Transact-SQL script for an action before clicking OK in the
Properties dialog box used to perform the action. Many Properties dialog boxes include a
Script list with which you can script the action to a new query window, a file, the
Clipboard, or a SQL Server Agent job. A common technique is to add a blank query file
to a project, script each action to the Clipboard as it is performed, and then paste the
generated script into the query file.
You can also generate scripts for many existing objects, such as databases and tables. To
generate a script, right-click the object in Object Explorer and script the CREATE action.
If Object Explorer is not visible, on the View menu, click Object Explorer.

1-42

Module 1: Creating Databases and Database Files

Exercise 1: Creating a Database


In this exercise, you will create a new database using SQL Server Management Studio.
You will then save the database object as a script.
The principal tasks for this exercise are:

Create a SQL Server Management Studio Scripts project.

Create the new database with appropriate options and filegroups.

Tasks

Supporting information

1.

Start SQL Server Management Studio and connect to the MIAMI


server by using Windows authentication.

Create a new SQL Server Scripts project named AW_IT_Database in


the D:\Labfiles\Starter folder.

Add a new query to the project, connecting to MIAMI by using


Windows authentication when prompted.

Change the query file name to CreateAW_IT_Assets.sql.

2.

Create a SQL Server


Management Studio Scripts
project.

Create the new database with


appropriate options and

filegroups.

Use Object Explorer to create a new database.


Type the appropriate details for the AW_IT_Assets database:

Enter the appropriate details for the AW_IT_Assets_Data1 file.

Enter the appropriate details for the AW_IT_Assets_Data2 file.

Place the AW_IT_Assets_Data2 file on a new default filegroup


named SECONDARY.

Enter the appropriate details for the AW_IT_Assets_log file.

Script the action to the Clipboard.

Execute the action to create the database.

Paste the contents of the Clipboard into the CreateAW_IT_Assets.sql


script file, and then save the file.

Module 1: Creating Databases and Database Files

1-43

Exercise 2: Creating Schemas


In this exercise, you will add a schema to the database that you created in the previous
exercise. You will then create a new user login and assign a default schema.
The principal tasks for this exercise are:

Create the TechSupport schema.

Create a new table and login by using the provided script.

Verify that the user can select information from the Asset table only by qualifying
the table name with the schema.

Change the default schemas for the users, and retest access to the table within the
default schema.

Tasks

Supporting information

1.

Add a new query to the project and connect to the MIAMI instance by
using Windows authentication.

Rename the file to CreateAW_IT_Schemas.sql.

Use Object Explorer to create the TechSupport schema, scripting the


action to the Clipboard.

Execute the action to create the schema.

Paste the contents of the Clipboard into the


CreateAW_IT_Schemas.sql script file, and then save the file.

Add the D:\Labfiles\Starter\Create_AW_IT_Table_and_User.sql file to


the project and connect to the MIAMI instance by using Windows
authentication.

Execute the script to create the Asset table and the login for
MIAMI\Katie.

2.

3.

4.

Create the TechSupport


schema.

Create a new table and login


by using the provided script.

Verify that the user can select


information from the Asset
table only by qualifying the

table name with the schema.

Change the default schemas


for the users, and retest
access to the table within the

In a Command Prompt window, execute the following command:


runas /noprofile /user:MIAMI\Katie cmd
Set the password to Pa$$w0rd.
In the new command prompt window, execute the following
command:
sqlcmd -S MIAMI -E

Type Transact-SQL statements to select all of the data from the


Asset table without using a schema name. Execute the query.

An error message is displayed because the dbo schema does not


contain the Asset table.

Rewrite the query to use the two-part name for the table and execute
the query.

The table is displayed correctly.

Do not close the Command Prompt window.

In SQL Server Management Studio, use Object Explorer to change


the default schema of the MIAMI\Katie user to TechSupport.

Script the action to the Clipboard.

1-44

Module 1: Creating Databases and Database Files

Tasks
default schema.

Supporting information

Execute the action to change the default schema.

Paste the contents of the Clipboard into the


CreateAW_IT_Schemas.sql file.

Save all files in the project.

In a Command Prompt window, run sqlcmd, and then write TransactSQL statements to select all the data from the Asset table without
specifying the schema name. Notice that now the table is displayed
correctly.

Exit sqlcmd and close the Command Prompt window.

Você também pode gostar