SQL Server 2008 Tutorial
SQL Server 2008 Tutorial
SQL Server is an enterprise database management system (DBMS) from Microsoft. It is used by
thousands of companies worldwide and is used by thousands, if not millions, of websites around the
world.
This tutorial explains what SQL Server 2014 is and If you don't feel quite ready to jump into the SQL
how to use it. You should have a basic knowledge Server tutorial or the SQL tutorial, start here. This
of relational database management systems and tutorial will explain what a database management
how they work. Based on SQL Server 2014.
system (DBMS) is, the typical features that make
up a DBMS, and how they are generally used.
SQL Tutorial
Similar to the SQL Server 2014 tutorial but based If you've completed the SQL Server tutorial, try
on SQL Server 2008.
this one next. This tutorial covers SQL (Structured
Query Language), and is essential if you need to
develop database driven websites.
Very similar to the SQL Server 2008 and 2014 This tutorial explains what Microsoft Access is and
tutorials, except this one is was written a really how to use it. You don't need to do the SQL
long time ago...
tutorial first, however, you should have a basic
knowledge of relational database management
systems and how they work.
Table of Contents
2.
3.
4.
5.
6.
7.
8.
9.
Generally, it's the application that provides the functionality to these visitors. It is the database that
stores the data and makes it available. Having said that, SQL Server does include some useful features
that can assist the application in providing its functionality.
The Editions
Here are the different editions available for SQL Server 2008.
Enterprise
Edition
Data management and business intelligence platform providing enterprise class scalability, high
availability, and security for running business-critical applications
Standard
Edition
Data management and business intelligence platform providing ease of use and manageability
for running departmental applications
Workgroup
Edition
Data management and reporting platform providing secure, remote synchronization, and
management capabilities for running branch applications
Developer
Edition
May be installed and used by one user to design, develop, test, and demonstrate your programs
on as many systems as needed
Web
Edition
A low-TCO, scalable, and manageable database option for web hosters and end customers
looking to deploy publicly facing web applications and services
Express
Edition
A free edition of SQL Server ideal for learning and building desktop and small server applications
and for redistribution by ISVs
Compact
Edition
A free, SQL Server embedded database ideal for building stand-alone and occasionally
connected applications for mobile devices, desktops, and web clients
Evaluation
Edition
This edition may be installed for demonstration and evaluation purposes until an expiration
period of 180 days.
SQL Server database engine - create, store, update and retrieve your data
SQL Server Management Studio Basic - visual database management tool for creating, editing
and managing databases
Reporting Services - integrated report creation and design environment to create reports
The Express edition also comes as SQL Server 2008 Express with Tools, which only includes the SQL
Server database engine and the SQL Server Management Studio Basic, and SQL Server 2008 Express
(Runtime Only) which doesn't include the SQL Server Management Studio.
While the free version of SQL Server does have its limitations, it is a good start for those starting out
with SQL Server.
You can download SQL Server 2008 Express from Microsoft's website.
The left pane contains the Object Explorer. The Object Explorer provides navigation to databases, server
objects (such as triggers), log files, and more.
The right pane allows you to write queries against the database and view the results. In this screenshot
I have opened a blank query by clicking the "New Query" button. You can also bring up other windows,
such as the Properties window.
Note that I have minimized the size of the window for this screenshot. Once maximized, you have much
more room to play with.
You can use SQL Server Management Studio to create as many databases as you like. You can also
connect to as many databases on as many servers as you like.
Most of the tasks performed with SQL Server Management Studio are initiated either from the top menu,
or by right-clicking on an icon/object.
Throughout most of this tutorial, we'll be looking at the various things you can do via SQL Server
Management Studio.
One of the first things we should look at with SQL Server/Management Studio is how to create a
database. After all, most of the tasks you perform with SQL Server will evolve around one or more
databases.
System Databases
If you've only just installed SQL Server, you might notice that some databases have already been
created. These are system databases.
Databas
e
Type
Description
master
System
databas
e
System
This database is used as a template for all other databases that are created.
model
databas
e
msdb
System
Used by the SQL Server Agent for configuring alerts and scheduled jobs etc
databas
e
tempdb
System
Holds all temporary tables, temporary stored procedures, and any other
databas
e
1.
Right
click
on
the
"Databases"
icon
and
select
"New
Database...":
2.
Name
your
database
and
click
"OK":
Other Options
We have just created a database using the default options. When we created the database, a "Data File"
and a "Transaction Log" were created. They were created in the default location for our server.
If we'd wanted to, we could have specified a different location for these files. We also could have
changed specifications on whether to allow the file to grow automatically (as it stores more and more
data), and if so, how that growth should be managed. We could have done that at step 2. But all is not
lost. We can still do it now that we've created the database. We can do it via the Properties dialog box.
To view or change the database properties, simply right click on the database and select "Properties":
The Properties dialog contains a large number of options for changing the configuration of your
database. For now, we can leave everything at its default setting.
1.
Ensuring you have the right database expanded, right click on the "Tables" icon and select "New
Table...":
2.
Using the values in the screenshot, complete the details in the "Column Name" column,
the "Data Type" column, and "Allow Nulls" column.
b.
Make the IndividualId column an "identity column", by setting "Is Identity" to "Yes"
(this option is under the "Identity Specification" section in the bottom pane). Note that
to set values in the bottom pane, you need to select the column name in the top pane
first. This column is going to be an auto-number column - it will contain an
incrementing number for each record that is created.
c.
(This will
What we are doing at this stage, is creating the column names, specifying the type of data that
can be entered into them, and setting default values. Restricting the data type for each column
is very important and helps maintain data integrity. For example, it can prevent us from
accidentally entering an email address into a field for storing the current date.
3.
Save
the
table
by
selecting File
>
Save
Table_1:
4.
When
prompted,
name
your
table:
We can use the "Edit Top 200 Rows" option to add data to our table.
1.
To use this option, right click on the table you wish to open, and select "Edit Top 200 Rows":
2.
You can now start entering the data directly into your table.
Note that you don't need to enter data into the IndividualId and DateCreated columns. This is
because the they will be populated automatically (remember, we set IndividualId to "Is Identity"
and DateCreated to "(GetDate())")
can simply run your script to enter the data. If you have multiple environments, once again you can run
your script against each environment.
Once you get used to writing and running scripts, you will probably find it quicker than entering data
directly into the table.
1.
2.
The following screenshot shows an example of using a SQL 'select' statement to select data from a
database:
As you can see, the results of the query are displayed in the bottom pane.
As a shortcut, you can click on a table in the left pane and drag it to the right pane when building your
query. This can save you time - especially if you have many tables to add to your query.
The above 'select' statement is an example of a SQL query. Apart from the occasional example, SQL
queries are outside the scope of this tutorial. If you'd like to learn more about writing SQL queries, check
out the SQL tutorial.
In the previous lesson, we created a SQL script using SQL Server Management Studio (SSMS). In this
lesson, we will look at how to write SQL scripts using the graphical query designer.
Select Query
>
Design
Query
in
Editor...:
2.
Add the tables you want to run the query against. In this case, we only have one table to
choose
from.
3.
Select
the
column/s
you
want
to
display
in
your
query:
4.
Click "OK"
Once you've clicked OK, you will find the query has been added to your workspace. You can then run it
as you would any other query.
Benefits of Views
A view can be useful when there are multiple users with different levels of access, who all need to see
portions of the data in the database (but not necessarily all of the data). Views can do the following:
Join columns from multiple tables and present them as though they are part of a single table
Accessing Views
Any view that you create ends up being located under the "Views" folder of your database.
The following screenshot shows a number of views that are located within the "AdventureWorks2008"
database:
Creating a View
You create a view by using the CREATE VIEW statement, followed by the SELECT statement.
Copy to Clipboard
Modifing a View
You can modify an existing view by using using ALTER instead or CREATE.
Example:
Copy to Clipboard
You can also right click on the view and select "Design".
Running a View
You run a view by using a SELECT statement.
Copy to Clipboard
As you can see, it looks just like you've selected rows from a table. The difference is that, each column
could potentially be coming from a different table.
Explanation of benefit
Modular
programming
You can write a stored procedure once, then call it from multiple places in your
application.
Performance
Stored procedures provide faster code execution and reduce network traffic.
Faster execution: Stored procedures are parsed and optimized as soon as they
are created and the stored procedure is stored in memory. This means that it
will execute a lot faster than sending many lines of SQL code from your
application to the SQL Server. Doing that requires SQL Server to compile and
optimze your SQL code every time it runs.
Reduced network traffic: If you send many lines of SQL code over the network
to your SQL Server, this will impact on network performance. This is especially
true if you have hundreds of lines of SQL code and/or you have lots of activity
on your application. Running the code on the SQL Server (as a stored
procedure) eliminates the need to send this code over the network. The only
network traffic will be the parameters supplied and the results of any query.
Security
Users can execute a stored procedure without needing to execute any of the
statements directly. Therefore, a stored procedure can provide advanced database
functionality for users who wouldn't normally have access to these tasks, but this
functionality is made available in a tightly controlled way.
create
stored
procedures
in
the
SQL
Server
Management
Studio
using
the CREATE
PROCEDURE statement, followed by the code that makes up the stored procedure.
Copy to Clipboard
If you need to modify an existing stored procedure, you simply replace the CREATE with ALTER.
Copy to Clipboard
EXEC MyStoredProcedure
If the stored procedure has spaces in its name, enclose it between double quotes:
Copy to Clipboard
2.
Right
click
on
the
stored
procedure
and
select
"Execute
Stored
Procedure...":
3.
dialog
4.
Click "OK"
5.
SQL
Server
will
will
appear.
now
generate
Enter
the
your
SQL
code
chosen
and
parameter
execute
the
values
stored
etc:
procedure:
Parameters
A parameter is a value that your stored procedure uses in order to perform it's task. When you write a
stored procedure, you can specify the parameters that need to be supplied from the user. For example, if
you write a stored procedure to select the address details about an individual, your stored procedure
needs to know which individual to select. In this case, the user can provide an IndividualId or UserId to
tell the stored procedure which individual to look up.
configure replication
Using SQL Server Management Studio, expand the "Security" option and right click on "Logins"
2.
Click
3.
Complete the login properties in the "General" tab by providing a name for the login, choosing
on
"New
Login"
the Authentication method (providing a password if you choose "SQL Server authentication"),
and selecting the database to use as a default. If you don't choose a language, it will use the
default for the current installation of SQL Server.
If you get an error that reads "The MUST_CHANGE option is not supported by this version of
Microsoft Windows", simply uncheck the "User must change password at next login" option. The
error occurs because your operating system doesn't support this option.
4.
Click the "Server Roles" tab if you need to apply any server-wide security privileges.
5.
Click the "User Mapping" tab to specify which databases this user account is allowed to access.
By default, the login will be assigned to the "Public" role, which provides the login with basic
access. If the login needs more access in one or more databases, it can be assigned to another
role with greater privileges.
Note that these roles are "Database Roles" and are different to the server roles in the previous
tab. Server roles are for administering the SQL Server. Database roles are created within each
database and specify what the login can do within that database.
You view the properties of a server role by right clicking on it. You can then add users to the server role
by clicking Add. In the screenshot below, Homer has been added to the securityadmin role.
Description
sysadmin
serveradmin
Can set server-wide configuration options, can shut down the server.
setupadmin
securityadmi
n
Can manage logins and database permissions, read logs, change passwords.
diskadmin
bulkadmin
Public
Every SQL Server user account belongs to this server role. When a server principal has
not been granted or denied specific permissions on a securable object, the user inherits
the permissions granted to public on that object. Only assign public permissions on an
object when you want the object to be available to all users.
As you can see, some of these roles allow very specific tasks to be performed. If you don't have many
technical users, it's likely that you'll only use one or two of these roles (including sysadmin).
2.
Right
click
on Schemas and
select New
Schema....
Like
this:
3.
Complete the details in the General tab for the new schema. In this example, the schema name
is
"person"
and
the
schema
owner
is
"Homer".
4.
Add
users
to
the
schema
as
required
and
5.
6.
Click OK.
set
their
permissions:
In
Object
Explorer,
right
click
on
the
table
2.
name
and
select
"Design":
3.
From
the
Properties
4.
Close
5.
Design
View
window,
by
change
right
the
clicking
schema
the
tab
to
the
and
desired
schema:
selecting
"Close":
1.
Refresh
the
Object
Browser
view:
2.
You will now see that Object Browser displays the new schema for the table (person.Individual):
2.
Right
3.
Complete the details for the linked server. In this example, Data source refers to the name of
click
on Linked
Servers and
select New
Linked
Server....
Like
this:
your
requirements.
Distributed Queries
Once you have configured your linked server, you will be able to run queries etc against it. When you run
a query against a linked server, it is referred to as adistributed query.
When you execute a distributed query against a linked server, you must include a fully qualified, fourpart table name for each data source to query.
form linked_server_name.catalog.schema.object_name.
This
four-part
name
should
be
in
the
Here's an example:
Copy to Clipboard
Barts_database_server.Barts_database.Person.Enemy
This example is based on the linked server example above. It assumes that the remote database has a
schema called "Person" and a table called "Enemy".
In SQL Server 2005 and higher, Data Transformation Services (DTS) is no longer installed with SQL
Server. All DTS related tasks are now performed with SSIS.
Backwards Compatibility
For backwards compatibility, Microsoft has provided installation files that provide run-time support for
DTS packages. This will be useful if you have already created DTS packages in an earlier version of SQL
Server. For more information see Support for Data Transformation Services (DTS) in SQL Server 2008 on
the MSDN website.
Created databases
and more
What Next?
If you haven't already, check out the SQL Server 2014 tutorial.
Also, there were a couple of areas we didn't touch on during this tutorial. This is mainly due to the fact
that it is aimed at the free SQL Server Express version, which has its limitations.
All is not lost though. My SQL Server 2000 tutorial covers some of the areas that weren't included here.
For example, you can learn about the SQL Server Agentfor scheduling jobs to run automatically. You can
also read about the SQL Profiler, which allows you to monitor events within your SQL Server.
The most logical next step from learning SQL Server is to learn SQL itself (if you haven't already done
this that is). SQL stands for Structured Query Language and enables you to perform database related
tasks programatically. Most of the tasks in this tutorial can be done programatically using SQL.
Probably the most common use for SQL is to work with the data in the database. For example, to insert
data, select data, update data, or to delete data. Any database driven website will have many SQL
statements doing this stuff.
To learn more about SQL, check out the SQL tutorial.