Professional Documents
Culture Documents
Introduction To Microsoft SQL Server 2014: Module Overview
Introduction To Microsoft SQL Server 2014: Module Overview
Module 1
Introduction to Microsoft SQL Server 2014
Contents:
Module Overview 1-1
Module Overview
Before beginning to learn how to write queries with Microsoft SQL Server 2014, it is useful to understand
the overall SQL Server database platform, including its basic architecture, the various editions available for
SQL Server 2014, and the tools a query writer will use. This module will also prepare you to use SQL Server
Management Studio, SQL Server's primary development and administration tool, to connect to SQL Server
instances and create, organize, and execute queries.
Objectives
After completing this module, you will be able to:
Lesson 1
The Basic Architecture of SQL Server
In this lesson, you will learn about the basic architecture and concepts of Microsoft SQL Server 2014. You
will learn how instances, services, and databases interact, and how databases are structured. This will help
prepare you to begin working with SQL Server queries in upcoming modules.
Lesson Objectives
After completing this lesson, you will be able to describe:
• Relational databases and, specifically, the role and structure of SQL Server databases.
Relational Databases
Typical relational databases comprise of several
tables that relate to each other. Each table typically
represents a class of entity which might be
something tangible, such as an employee, or
something intangible, such as a sales order. In this
example, a very simple database might have an
employee table and an order table, with employees
able to place orders.
We find meaningful information by using joins,
which are possible when two tables share values. To
continue the example, each order has an employee
ID for the person who placed the order. It is also
possible to join several tables. Let’s extend the example and add customers. Now each order also contains
a customer ID that can be used to link it to the new customer table. We can now display an employee and
their customers by joining all three tables. It is not necessary to display any order information, we can just
use this as a bridge between the other two tables.
Databases in SQL Server are containers for data and objects, including tables, views, stored procedures,
user accounts, and other management objects. An SQL Server database is always a single logical entity,
backed by multiple physical files.
SQL Server supports two types of databases—system and user. TSQL, the sample database you will be
using to write queries, is a user database. SQL Server's system databases include:
• msdb, the configuration database for the SQL Server Agent service and other system services.
• tempdb, used by the database engine to store temporary data such as work tables. This database is
dropped and recreated each time SQL Server restarts. Never store anything you need to depend on in
it!
• Resource, a hidden system configuration database that provides system objects to other databases.
Querying Microsoft® SQL Server® 1-3
Database administrators and developers can create user databases to hold data and objects for
applications. You connect to a user database to execute your queries. You will need security credentials to
log on to SQL Server and a database account with permissions to access data objects in the database.
The TSQL2014 database is modeled to resemble a sales-tracking application for a small business. Some of
the tables you will use include:
• Sales.Orders, which stores data typically found in the header of an invoice (order ID, customer ID,
order date, and so on).
• Sales.OrderDetails, which stores transaction details about each order (parent order ID, product ID, unit
price, and so on).
• Sales.Customers, which stores information about customers (company name, contact details, and so
on).
• HR.Employees, which stores information about the company's employees (name, birthdate, hire date,
and so on).
Other tables are supplied to add context, such as additional product information, to these tables.
Whether the database engine is local, or you are connecting to it over a network, it makes no difference
to the T-SQL code that we write. On the logon screen, you just need to specify the server that you are
connecting to.
Because you can connect to instances of SQL Server over a network, you can also refer to other databases
in a T-SQL script. To do this, you need to refer to a table, or other object, using its four-part name. This
takes the format of Instance.Database.Schema.Object. For example, the orders table in the dbo
schema, in the sales database on the MIA-SQL server’s default instance, would be referred to as MIA-
SQL.sales.dbo.orders. To connect to a remote server in a T-SQL script, you should set up the remote
instance as a linked server. In T-SQL you can add a linked server using the sp_addlinkedserver stored
procedure. Although there are many arguments that can be supplied, in its most straightforward, default,
use you could connect to the server in the previous example using the statement exec
sp_addlinkedserver n’MIA-SQL’.
Queries
T-SQL is a set-based language. This means that it
does not go through records row-by-row, but
instead pulls data from the server one table at a
time, or a subset of the table if it is filtered. This
makes SQL Server very efficient to deal with large
volumes of data, although writing a seemingly
straightforward query to add five percent to the
preceding row is quite complicated. SQL Server
does not typically consider what row a record is on,
it looks at the data within that row.
Inside each file, the script can be ordered into batches, that are marked with the word GO at the end. It is
important to realize that each batch is run in its entirety before the next one is started. This is important if
things need to happen in a certain order. For example, if you had a script that created a table, and then
populated it with data, it would fail without batches. SQL Server would analyze the batch and reject the
statement that populated the table because the table does not currently exist. If you write the script to
create the table, type GO, and then write the script to populate the table. It will succeed because the table
exists when SQL Server assesses the second batch.
Querying Microsoft® SQL Server® 1-5
Lesson 2
SQL Server Editions and Versions
In this lesson, you will learn about the editions and versions of Microsoft SQL Server. You will learn which
editions of SQL Server 2014 are available, their distinguishing features, and which editions would be best
to use when planning a new deployment.
Lesson Objectives
After completing this lesson, you will be able to describe:
Question: Have you worked with any versions of SQL Server prior to SQL Server 2012?
1-6 Introduction to Microsoft SQL Server 2014
• Business Intelligence, which is a new edition. It provides the core database engine, full reporting
and analytics capabilities, and full BI services. However, like the Standard edition, it supports fewer
processor cores and does not offer all the availability, security, and data warehousing features.
SQL Server 2014 also offers other editions, such as Parallel Data Warehouse, Web, Developer, and Express,
each targeted for specific use cases.
This course uses core database engine features found in all editions.
Additional Reading: For more information on the use of T-SQL in Microsoft Azure SQL
Databases, go to the MSDN article Transact-SQL Support (Microsoft Azure SQL Database):
http://go.microsoft.com/fwlink/?LinkID=394805
Querying Microsoft® SQL Server® 1-7
Lesson 3
Getting Started with SQL Server Management Studio
In this lesson, you will learn how to use SQL Server Management Studio (SSMS) to connect to an instance
of SQL Server, explore the databases contained in the instance, and work with script files containing
T-SQL queries.
Lesson Objectives
After completing this lesson, you will be able to:
• Start SSMS.
Starting SSMS
SSMS is an integrated management, development,
and querying application with many features for
exploring and working with your databases. SSMS is
based on the Visual Studio shell. If you have
experience with Visual Studio, you will likely feel
comfortable with SSMS.
To start SSMS, you may:
By default, SSMS will display a Connect to Server dialog box you can use to specify the server (or instance)
name and your security credentials. If you use the Options button to access the Connection Properties
tab, you can also supply the database to which you wish to connect. However, you can explore many
SSMS features without initially connecting to an SQL Server instance, so you may also cancel the Connect
to Server box and link to a server later.
After SSMS is running, you may wish to explore some of its settings, such as those found in the Tools,
Options box. SSMS can be customized in many ways, such as setting a default font, enabling line numbers
for scripts, and controlling the behavior of its many windows.
For more information on using SQL Server Management Studio, go to Use SQL Server Management
Studio in Books Online:
Use SQL Server Management Studio
http://go.microsoft.com/fwlink/?LinkID=402707
1-8 Introduction to Microsoft SQL Server 2014
• The name of the database. If you do not specify this, you will be connected to the database
designated as your account's default by the database administrator, or to the master database if no
default has been specifically assigned. In Microsoft Azure, it is important to choose the correct
database as you may not change connections between user databases. You would need to disconnect
and reconnect to the desired database.
• The authentication mechanism required by the server. This may be Windows Authentication, in which
your Windows network credentials will be passed to SQL Server (no entry required), or SQL Server
Authentication, in which a username and password for your account must be created by a database
administrator (you enter them at connection time). SQL Server Authentication is the only mechanism
supported by Microsoft Azure.
Question: Which authentication method do you use to log on to SQL Server in your
organization?
SQL Server query writers most commonly use Object Explorer to learn about the structure and definition
of the data objects they want to use in their queries. For example, to learn the names of columns in a
table, you follow these steps:
4. Expand the Tables folder to view the list of tables in the database.
5. Locate the table you are interested in and expand it to find the Columns folder. The Columns folder
will display the names, data types, and other information about the columns that make up the table.
You can even drag the name of a database, table, or column into the query window to have it
entered and avoid typing it yourself.
Note: Selecting objects in the Object Explorer pane does not change any connections
made in other windows.
Solution None Top-level container for projects. Stored as a text file with an .ssmssln
extension, which references components contained within it. May contain
multiple projects. Displayed at the top of the object hierarchy in Solution
Explorer.
Project Solution Container for T-SQL scripts (called queries), stored database connection
metadata, and miscellaneous files. Stored as a text file with an .ssmssqlproj
extension, which references component scripts and other files.
Script Project T-SQL script file with an .sql extension. The core item of work in SSMS.
The benefits of using scripts organized in projects and solutions include the ability to immediately open
multiple script files in SSMS. You can open the solution or project file from within SSMS or Windows
Explorer.
To create a new solution, click the File menu and click New Project. (There is no “New Solution”
command.) Specify a name for the initial project, its parent solution, and whether you want the project to
be stored in a subfolder below the solution file in the location you indicate. Click OK to create the parent
objects.
To interact with Solution Explorer, open the pane (if necessary) from the View menu. To create a new
script that will be stored as part of the project, right-click the Queries folder in the project and click New
Query.
1-10 Introduction to Microsoft SQL Server 2014
Note: Using the New Query toolbar button or the new query commands on the File menu
will create a new script temporarily stored with the solution in the Miscellaneous Files folder. If
you wish to move an existing open query document into a solution currently open in Solution
Explorer, you will need to save the file. You can then drag the query into the project tree to save
it in the Queries folder. This will make a copy of the script file and place it in the solution.
It is important to remember to save the solution when exiting SSMS or opening another solution to
preserve changes to the file inventory. Saving a script using the Save toolbar button or the Save
<Queryname>.sql command on the File menu will only save changes to the current script file contents. To
save the entire solution and all its files, use the Save All command on the File menu or when prompted to
save the .ssmssln and .ssmssqlproj files on exit.
Executing Queries
To execute T-SQL code in SSMS, you first need to
open the .sql file that contains the queries, or type
your query into a new query window. Then decide
how much of the code in the script is to be
executed as follows:
• Select the code you wish to execute.
• Pressing the F5 key, the Alt+X keyboard shortcut, or the Ctrl+E keyboard shortcut.
By default, SSMS will display your results in a new pane of the query window. The location and
appearance of the results can be changed in the Options box, accessible from the Tools menu. To toggle
the results display and return to a full-screen T-SQL editor, use the Ctrl+R keyboard shortcut.
SSMS provides several formats for the display of query results:
• Grid, which is a spreadsheet-like view of the results, with row numbers and columns you can resize.
You can use Ctrl+D to select this before executing a query.
• Text, which is a Windows Notepad-like display that pads column widths. You can use Ctrl+T to select
this before executing a query.
• File, which allows you to directly save query results to a text file with an .rpt extension. Executing the
query will prompt a results file location. The file may then be opened by any application that can read
text files, such as Windows Notepad and SSMS. You can use Ctrl+Shift+F to select this before
executing a query.
Querying Microsoft® SQL Server® 1-11
The first time Help is invoked, you will be prompted to specify whether you wish to view Books Online
content online or locally.
For detailed instructions on how to download, install, and configure Books Online for local offline use, go
to the topic Get Started with Product Documentation for SQL Server:
Demonstration Steps
Use SSMS to connect to an on-premises instance of SQL Server 2014
1. Ensure that the 20461C-MIA-DC and 20461C-MIA-SQL virtual machines are both running, and then
log on to 20461C-MIA-SQL as ADVENTUREWORKS\Student with the password Pa$$w0rd.
7. Show the list of columns, and point out the data type information for the ModifiedDate column.
Work with T-SQL scripts
1. If the Solution Explorer pane is not visible, click View and click Solution Explorer. Initially, it will be
empty.
3. In the New Project box, under Installed Templates, click SQL Server Management Studio
Projects.
8. In the Solution Explorer pane, right-click Queries, then click New Query.
USE AdventureWorks;
GO
SELECT CustomerID, AccountNumber
FROM Sales.Customer;
14. Click File, click Recent Projects and Solutions, and then click Module 1 Demonstration.ssmssln.
16. Close SQL Server Management Studio without saving any files.
Querying Microsoft® SQL Server® 1-13
Objectives
After completing this lab, you will be able to:
Password: Pa$$w0rd
3. Using the View menu, show the Object Explorer and Solution Explorer windows in SSMS.
o Disable IntelliSense.
o Enable the option to include column headers when copying the result from the grid. Look under
Query Results, SQL Server, Results to Grid for the check box Include column headers when
copying or saving the results.
1-14 Introduction to Microsoft SQL Server 2014
Results: After this exercise, you should have opened SSMS and configured editor settings.
1. Create a Project
2. Add a new query file to the created project and name it MyFirstQueryFile.sql.
3. Save the project and the query file by clicking the Save All option.
2. Open Windows Explorer, navigate to the project folder, and observe the created files in the file
system.
3. Back in SSMS, using Solution Explorer, remove the query file MySecondQueryFile.sql from the created
project. (Choose the Remove option, not Delete.)
4. Again, look in the file system. Is the file MySecondQueryFile.sql still there?
5. Back in SSMS, remove the file MyFirstQueryFile.sql and choose the Delete option. Observe the files in
Windows Explorer. What is different this time?
2. Drag the query file MySecondQueryFile.sql from Windows Explorer to the Queries folder under the
project MyFirstProject in Solution Explorer. (Note: If the Solution Explorer window is not visible,
enable it as you did in exercise 1).
Results: After this exercise, you should have a basic understanding of how to create a project in SSMS and
add query files to it.
2. Configure Books Online to use the online option, not local help.
Results: After this exercise, you should have a basic understanding of how to find information in Books
Online.
1-16 Introduction to Microsoft SQL Server 2014
Question: If no T-SQL code is selected in a script window, which lines will be run when you
click the Execute button?