You are on page 1of 5

Module 2 Lab: Creating Data Types and Tables

Exercise 1: Creating Data Types


Lab answer key

Creating a SQL Server Scripts project


You must perform the following procedure to create a SQL Server Scripts project:

1. Click Start, point to All Programs, point to Microsoft SQL Server 2005, and 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

3. On the File menu, point to New, and then click Project.


4. In the New Project dialog box, specify the values in the following table, and then click OK.
Property Value
Name AW_Tables
Location D:\Labfiles\Starter
Create directory for Unselected
solution

5. On the Project menu, click New Query. When prompted, use Windows authentication to
connect to MIAMI.
6. If Solution Explorer is not visible, click Solution Explorer on the View menu.
Creating the ShortDescription and CashValue data types
You must use the following procedure to create the ShortDescription and CashValue
data types:

1. In Solution Explorer, right-click SQLQuery1.sql, and then click Rename. Rename the file to
CreateDataTypes.sql.
2. In the query window, type the following Transact-SQL code to create the ShortDescription
data type.

USE AdventureWorks
CREATE TYPE dbo.ShortDescription
FROM nvarchar(100)
NULL

3. Add the following Transact-SQL code to create the CashValue data type.

CREATE TYPE dbo.CashValue


FROM decimal(8,2)
NULL

4. Click the Execute button on the toolbar to execute the query.


5. On the File menu, click Save All.
6. If Object Explorer is not visible, click Object Explorer on the View menu.
7. In Object Explorer, expand Databases, AdventureWorks, Programmability, Types, and
User-defined Data Types, and then verify that the dbo.ShortDescription and
dbo.CashValue data types are listed.
8. Keep SQL Server Management Studio open; you will use it in the next exercise.
Exercise 2: Creating Tables

Lab answer key

Creating a new query file


You must perform the following steps to create a new query file:

1. On the Project menu, click New Query. When prompted, use Windows authentication to
connect to MIAMI.
2. If Solution Explorer is not visible, click Solution Explorer on the View menu.
3. In Solution Explorer, right-click SQLQuery1.sql, and then click Rename. Rename the file to
CreateTables.sql.

Creating the ReturnedGoods and Refunds tables


You must perform the following procedure to create the tables:
1. In the query window, type the following Transact-SQL code to create the ReturnedGoods
table.
USE AdventureWorks
CREATE TABLE Sales.ReturnedGoods
(ReturnID int IDENTITY NOT NULL,
ProductID int NOT NULL,
CustomerID int NOT NULL,
ReturnDate datetime NOT NULL,
ReturnReason ShortDescription NULL)
2. Add the following Transact-SQL code to create the Refunds table.
CREATE TABLE Sales.Refunds
(RefundID int IDENTITY NOT NULL,
ReturnID int NOT NULL,
Amount CashValue NOT NULL)

3. Click the Execute button on the toolbar to execute the query.


4. On the File menu, click Save All.
5. If Object Explorer is not visible, click Object Explorer on the View menu.
6. In Object Explorer, expand Databases, AdventureWorks, and Tables, and then verify that
the Sales.ReturnedGoods and Sales.Refunds tables are listed.
7. Keep SQL Server Management Studio open; you will use it in the next exercise.
Exercise 3: Creating Partitioned Tables

Lab answer key

Creating a partition function


You must perform the following steps to create a partition function:

1. On the Project menu, click New Query. When prompted, use Windows authentication to
connect to MIAMI.
2. If Solution Explorer is not visible, click Solution Explorer on the View menu.
3. In Solution Explorer, right-click SQLQuery1.sql, and then click Rename. Rename the file to
CreatePartitionedTable.sql.
4. In the query window, type the following Transact-SQL code to create the pf_ReturnDate
partition function.

USE AdventureWorks
CREATE PARTITION FUNCTION pf_ReturnDate (datetime)
AS RANGE RIGHT
FOR VALUES ('01/01/2005', '01/01/2006')

5. Click the Execute button on the toolbar to execute the query.

Creating a partition scheme


You must perform the following procedure to create a partition scheme:

1. Add the following Transact-SQL code to create the ps_ReturnDate partition scheme.

CREATE PARTITION SCHEME ps_ReturnDate


AS PARTITION pf_ReturnDate
ALL TO ([PRIMARY])

2. Select the CREATE PARTITION SCHEME statement, and then click the Execute button on
the toolbar to execute it.
Creating a partitioned table
You must perform the following procedure to create a partitioned table:

1. Add the following Transact-SQL code to create the ReturnsArchive table.

CREATE TABLE Sales.ReturnsArchive


(ReturnID int IDENTITY NOT NULL,
ProductID int NOT NULL,
CustomerID int NOT NULL,
ReturnDate datetime NOT NULL,
ReturnReason ShortDescription NULL)
ON ps_ReturnDate(ReturnDate)

2. Select the CREATE TABLE statement, and then click the Execute button on the toolbar to
execute it.
3. On the File menu, click Save All.

Inserting data into the partitioned table


You must perform the following procedure to insert data into the partitioned table:

1. On the Project menu, click Add Existing Item, and then add the
D:\Labfiles\Starter\InsertReturnsArchive.sql script file. When prompted, use Windows
authentication to connect to MIAMI.
2. Click the Execute button on the toolbar (or press F5) to execute the query. This inserts some
test data into the ReturnsArchive table.

Viewing partition usage


You must perform the following procedure to view partition usage:

1. Under the existing code, add the following Transact-SQL statement to retrieve partition usage
information.
SELECT ReturnID, ReturnDate, $Partition.pf_ReturnDate(ReturnDate)
PartitionNo
FROM Sales.ReturnsArchive
2. Select the SELECT statement, and then click the Execute button on the toolbar to execute it.
When all rows have been returned, scroll through the results and verify that the data is spread
across three partitions.
3. On the File menu, click Save All.
4. Close SQL Server Management Studio.

You might also like