Sei sulla pagina 1di 17

Introduction

The Create and Alter Tables Using Transact SQL Syntax module provides you with the instruction
and server hardware to develop your hands on skills in the defined topics. This module includes the
following exercises:
1) Working with Tables

Lab Diagram
During your session you will have access to the following lab configuration.

PLABSQL01
(SQL Server 1)
192.168.0.1 /24

PLABSQL02
(SQL Server 2)
192.168.0.2 /24

Connecting to your lab


In this module you will be working on the following equipment to carry out the steps defined in each
exercise.

PLABSQL01 (SQL Server 1)

Each exercise will detail which console you are required to work on to carry out the steps.
To start simply click on the named Server from the device list (located on the left hand side of the
screen) and click the Power on from the in tools bar. In some cases the devices may power on
automatically.
During the boot up process an activity indicator will be displayed in the name tab:

Black - Powered Off


Orange - Working on your request
Green - Ready to access

If the remote console is not displayed automatically in the main window (or popup) click the
Connect icon located in the tools bar to start your session.
If the remote console does not appear please try the following option:

Switch between the HTML 5 and Java client versions in the tools bar.

In the event this does not resolve your connectivity problems please visit our Help / Support pages
for additional resolution options.

Copyright Notice
This document and its content is copyright of Practice-IT - Practice-IT 2014. All rights reserved.
Any redistribution or reproduction of part or all of the contents in any form is prohibited other than
the following:
1) You may print or download to a local hard disk extracts for your personal and non-commercial use
only.
2) You may copy the content to individual third parties for their personal use, but only if you
acknowledge the website as the source of the material. You may not, except with our express
written permission, distribute or commercially exploit the content. Nor may you transmit it or store
it in any other website or other form of electronic retrieval system.

Exercise 1- Working with Tables


In this exercise, you will learn the following in SQL Server 2012:

Creating Tables
Altering Tables
Dropping Tables

To better understand this task, please refer to your course material, or visit
http://technet.microsoft.com/en-us/library/ff848799.aspx [DDL Statements]

Creating Tables
A table definition contains the table name, column names, and column data types. Therefore, before
creating a table, you must decide on the table name, the field names or column names and their
data types to be included in the table definition. A column name should be unique to a specific table.
However, the same column name can be used in different tables within the same database. You can
also specify whether a specific column can contain null values or not in the table definition. If you do
not specify NULL or NOT NULL constraint, the SQL Server 2012 default is NULL.
Step 1
From the Practice-Labs application, ensure that the PLABSQL01 device is powered and ready to
Connect.
Launch access to PLABSQL01 by clicking the Connect icon. Once the desktop is presented, doubleclick SQL Server Management Studio.
The Connect to Server dialog box is displayed.
Keep the default settings and click Connect.

Step 2
In the Object Explorer, expand Databases, click AdventureWorks2012 and select New Query from
the menu.
A new Query window opens. Type the following CREATE TABLE command in the query window. The
following query creates the dbo.Employee table by specifying the columns of the table and a data
type and NULL/NOT NULL constraint for each column.
Use AdventureWorks2012
Go
CREATE TABLE dbo.Employee(
[BusinessEntityID] [int] NOT NULL,
[NationalIDNumber] [nvarchar](15) NOT NULL,
[LoginID] [nvarchar](256) NOT NULL,
[JobTitle] [nvarchar](50) NOT NULL,
[BirthDate] [date] NOT NULL,
[MaritalStatus] [nchar](1) NOT NULL,
[Gender] [nchar](1) NOT NULL,
[HireDate] [date] NOT NULL,

[SalariedFlag] [dbo].[Flag] NOT NULL,


[VacationHours] [smallint] NOT NULL,
[SickLeaveHours] [smallint] NOT NULL
)
GO

Step 3
Press F5 to execute the query. Your query executes successfully and you will find the message
Command(s) completed successfully appearing in the Messages window.

Step 4
In the Object Explorer, click the Refresh button in the menu. Then, expand AdventureWorks2012
and expand the Table node and you will find the newly created dbo.Employee table displayed in the
list.

Close the query window without saving it and collapse the node AdventureWorks2012 in the Object
Explorer.

Adding New Columns


You can modify the definition of existing tables using the ALTER TABLE statement. For example, you
can use the ALTER TABLE statement to add a new column or modify the data type of the existing
column of a table.
This example alters the definition of the dbo.Employee by adding two new columns
"EmployeeAddress" and "NoOfYearsExp". To add new columns to the existing table, use the ADD
<column definition> option in the ALTER TABLE statement. The type of information that you specify
when you add new column to an existing table is similar to that of the information that you supply
when you create a table.
Step 1
Click New Query from the menu. Type the following ALTER statement in the new query window.
Use AdventureWorks2012
Go
Alter Table dbo.Employee
add EmployeeAddress varchar(150),

NoOfYearsExp tinyint
GO

Step 2
Press F5 to execute the query. The query executes successfully by adding the new columns to the
table dbo.Employee. You will find the message Command(s) completed successfully in the
Messages window.

Step 3
In the Object Explorer, press the Refresh button in the menu. Expand AdventureWorks2012 and
expand Tables dbo.Employee Columns. You will find the newly added columns appearing in
the list.

Close the query window without saving and collapse the AdventureWorks2012 database.

Modifying Existing Columns


This example modifies the data type of the existing column "EmployeeAddress" in the
dbo.Employee table. The example modifies the length of the "EmployeeAddress" column from
Varchar(150) to Varchar(250) using the ALTER COLUMN option.
Step 1
Click New Query from the menu. Type the following ALTER TABLE statement in the new query
window.
Use AdventureWorks2012
Go
Alter Table dbo.Employee
alter column EmployeeAddress varchar(250)
Go

Step 2
Press F5 to execute the query. The query executes successfully and you will see the message
Command(s) completed successfully in the Messages window.

Step 3
In the Object Explorer, click the Refresh button in the menu. Then select AdventureWorks2012
Tables Columns EmployeeAddress and find the length of the column modified to
varchar(250).

Close the query window without saving and collapse the node AdventureWorks2012.

Dropping Tables
When you drop a table, the SQL Server removes the table definition as well all the data contained in
that table. Before you drop a table, you should remove all the dependencies, if any, between that
table and other objects within the database.
Step 1
Click the New Query option in the menu. In the new query window that opens, type the following
Drop Table statement. This example drops the dbo.Employee table from the AdventureWorks2012
database.
Use AdventureWorks2012
Go
Drop table dbo.Employee
Go

Step 2
Press F5 to execute the query. Your query executes successfully and you will see the message
Command(s) completed successfully in the Messages window.

Step 3
In the Object Explorer, click the Refresh button in the menu. Expand AdventureWorks2012Tables
node. You will find the dbo.Employee table removed from the list.

Close the query window and collapse AdventureWorks2012 node.


Shutdown all virtual machines used in this lab, by using the power functions located in the Tools bar
before proceeding to the next module. Alternatively you can log out of the lab platform.

Summary
In this exercise, you learnt the following in SQL Server 2012:

Creating a table
Altering an existing table by adding new columns and modifying the length of an existing
column
Dropping a table

Also try
Using Transact SQL queries try to do the following:

Create a database using Transact SQL


Alter a database and modify the database options
Delete a database using Transact SQL

Potrebbero piacerti anche