SQL Server 2005 Tutorial
SQL Server 2005 Tutorial
Road Map
Create Database Objects Create a database Create a table Set a constraint Create a view Create a user
Query
Manage the Data Import data Export data Backup the database Restore the database
2
Client-Server Architecture
User runs a query
1
Workstation
5
Workgroup
Includes the core database features of the SQL Server product line, and is the data management solution for small organizations that need a database with no limits on size or number of users
Express (free)
A free, easy-to-use, lightweight, and embeddable version of SQL Server 2005, includes powerful features such as SQL Server 2005 Reporting Services and SQL Server 2005 Management Studio Express
Compact
A free, easy-to-use embedded database engine that lets developers build robust Windows Desktop and mobile applications that run on all Windows platforms
Reference: http://technet.microsoft.com/en-us/library/ms144275.aspx
4
Administrators Duties
Install and configure SQL Server 2005 Plan and create databases Back up the databases Restore the databases when necessary Set up and manage users for SQL Server Manage security for new users and existing users Import and export data Set up and manage tasks, alerts, and operators Manage the replication environment Tune the SQL Server system for the optimal performance Troubleshoot any SQL Server problems
5
User Advanced option to setup installation path and include sample databases Create a default instance Use the built-in System account: Local System User Windows Authentication Mode
7
Create A Database
Start the Management Studio Connect to your SQL Server Right-click the Databases folder in the console tree, choose New Database from the context menu Fill in the boxes in the database properties sheet Click OK when you are finished.
9
10
Create A Table
Open Management Studio, drill down to the DB354 database, and expand it Right-click on Tables and select New Table Type the column name and data type, and setup column properties (in the window at the bottom of the screen) Click on the Save button, enter a name for the table and click OK
11
12
Create A Constraint
Open Management Studio and drill down to target table and expand it Right-click on Constraints and select New Constraint In the Check Constraint dialog box type the constraint expression Click OK to create the constraint
13
14
Create Views
Open Management Studio and drill down to the target database Expand the database and locate View Right-click on View and select New View In Tables page, select target table and click Add Edit the view definition in the appearing GUI Click the Save button Name the view and save it
15
16
User Management
Security Modes
Windows Authentication Mode
The user logs on to a Windows domain; the user name and password are verified by Windows The user then opens a trusted connection with SQL Server Since this is a trusted connection, SQL does not need to verify the user password
20
22
23
24
Query Analyzer
Different than SQL Server 2000, the Query Analyzer is integrated in Management Studio From the Management Studio menu, select File New Query with Current Connection In the appearing page, enter the following:
SELECT * FROM TargetDatabase..TableName
Click Execute button or press Ctrl+E or F5 The query will be executed and gives you results OR From the Available Databases listbox, select the target database Run the query: SELECT * FROM TableName You will get the same result set
26
27
28
Dropping databases
DROP DATABASE DB354
29
30
Execute queries
USE DB354 SELECT * FROM Table354 WHERE Column1>50
31
Exporting A Table
From Management Studio, locate the target database and select it Right-click on the database, then select Tasks Export Data from the context menu Use the Wizard to setup data source, server name, authentication mode, and database (use the default ones), then Next Setup data destination, such as a flat file (file path and name need to be specified), then Next to copy data from a table Choose a table and use default delimiter option Execute immediately
33
Importing a Table
Use Import Data Wizard Specify data source first Then specify data destination
The table to which data is imported needs to be specified
34
35
Why Backups?
Data can be corrupted by a variety of problems:
Failure of the hard disk drive Failure of the hard disk controller Motherboard failure Power outage or spike Virus attack Accidental change or deletion of data Malicious change or deletion of data
37
You can set the mode by using the Options tab of the database property sheet
38
Backup Choices
Full database backups: The entire database is backed up Transaction log backups: Add all the changes in the transaction log to your full database backups Differential database backups: Back up only data that has changed since the last full backup
For example, if a persons bank account changed 10 times in one day, the transaction log backup would contain all 10 changes but the differential backup would contain just the final amount
Filegroup backups: Allow you to back up different pieces of the database, based on the various files that make up the database
39
Backing Up Databases
Highlight the target database. Open the Backup dialog box by right-clicking and choosing Tasks Back Up User default setup to do a simple backup Click OK to start the backup After the backup completes, click OK on the Confirmation screen to close the Backup dialog box
40
41
42
43
What to Submit
Write a pure SQL query for each problem Put all the 5 queries in ONE script file Use your student # as the script file name Use /* comments */ for comments An example script file to submit
/* Q1 */ SELECT * FROM Customer /* Q2 */ Put query here /* Q3 */ Put query here /* Q4 /* Put query here /* Q5 /* Put query here 45
If your student # is 999999999, save the script file as 9999999.sql, then submit this file
How to Submit
For submission details, please follow the submission instruction on the submission web server
https://submit.cs.sfu.ca/
46
References
SQL Server Books Online Microsoft Developer Network
http://msdn.microsoft.com/
Thank you!