Versions of SQL Server 2005 ¢ Enterprise only support Windows Server OS — Includes all of the features of SQL Server 2005 and meets the high demands of enterprise online transaction pro
Trang 1A Tutorial on
SQL Server 2005
CMPT 354
Trang 2Manage the Data
Import = Export = Backup the = Restore the
data data database database
ANTUtOnall On Ss SenvyerZ05
Trang 3AN TwitOMniall On| S@IES OS einven 2005
HM SI
IN
@† a
Database Ri | is run server On Server
Trang 4Versions of SQL Server 2005
¢ Enterprise (only support Windows Server OS)
— Includes all of the features of SQL Server 2005 and meets the high demands of enterprise online transaction processing and data warehousing applications
¢ Standard (mostly support Windows Server OS)
— Includes the essential functionality needed for e-commerce, data warehousing, and line-of-business solutions
¢ 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
¢ Developer (can support Windows XP OS)
— Includes all of the functionality of Enterprise Edition, but is licensed only for
development, test, and demo use
¢ 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/ms 144275.aspx
AN ULOnalon SOS SenwerZ00s)
Trang 5Administrator’s 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
AN ULOnalon SOS SenwerZ00s)
Trang 6Installing SQL Server
2005
Trang 7A Simplified Installation Process
¢ Starting from setup.exe
¢ Click buttons other than “Cancel” in the Wizard
(using most of the default setup)
— Select components to install:
¢ SQL Server Database Services
¢ Workstation components, books online and development tools
— 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
AN ULOnalon SOS SenwerZ00s)
Trang 8Create Database Objects
with Microsoft SQL Server
Management Studio
Trang 9Create 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
AN ULOnalon SOS SenwerZ00s)
Trang 10“File Edit View Tools Window Community Help
Dower Ob BSS Faag PORES,
Connecty | @2 = j3 7 |ra a # 7 iz]- 8:-o: -
a l8 OWNER-0IC12F22A (SQL 5erver 9.0,1399 - OWR
EI E4 Databases ge eae i Databases
(5) Database Snapshots OWNER-00C12F224\Databases 4 Item(s)
@ (9 AdventureWorks
w AdventurewWorksDW 5
I#i C Security = New Database mị
8 (id Server Objects [i Replication Bg 2272 (System Databases khu kalb L2 C§ Soipt + [D4 Hep : : i
8 N Hanegement AdventureWorks
w 3B sal Notification Services (eee AdventureWorksDW Database name: MyDB _ |
Owner: |<default> | Gad [] Use full-text indexing
Database files:
LogicalName FieType Filegroup Initial Size (MB) | Autogrowth
MyDB Data PRIMARY j3 - | By 1 MB, unrestricted growth
MyDB_log Log Not Applicable 1 By 10 percent, unrestricted growth
Trang 11Create A Table
¢ Open Management Studio, drill down to
the DB354 database, and expand tt
¢ 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
AN ULOnalon SOS SenwerZ00s)
Trang 12File Edit View Project TableDesigner Tools Window Community Help
‘Divo e Sib shld PORRA,
8.3 z30188
in: Table - dbo.Table_1*) Summary |
Connectx | 81 =m |3 Column Name Data Type Allow Nulls [Tbl] dbo.Table _1 bd
& [gb OWNER-00C12F22A (SQL Server 9.01399 - OWN user_id O II
(9 System Databases H H (dentity)
a DB354 Server Name owner-O0c12F22a (49 Database Diagrams Table Designer
(4 Tables Identity Column —_user_id
Ba Programmabilty Replicated No
` Service Broker Row GUID Column
Storage Text/Image Filegrot PRIMARY
Security frase Fat
& (9 Security
H (9 Server Objects
& (9 Replication
& (9 Management
& (9 Notification Services
(8) SQL Server Agent (Agent XPs disabled)
Enter a name for the table:
& Identity Specification No
Trang 13Create 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
AN ULOnalon SOS SenwerZ00s)
Trang 14_ File Edit View Project TableDesigner Tools Window Community Help
: 88 Nssesr | [à Eồ tò lồ ([à pở @ là ñ1 Bà 3 „
“3n † z3 PH 8D m1
Table - dbo.user* ˆ 5ummary
Connect’ | 32 Y Celumn Name Data Type AlowNUls -
E¡ |, OWNER-00C12F2ZA (5QL Server 9.0.1399 - OWN user_id int L]
=) (Sy Databases xấu nchar(10) I4
ao pee ee Selected Check Constraint:
(Gi Constraints Eat
@ Ga Server Objects Check Existing Data On Cree
& (9 Replication Enforce For INSERTs And UF
& (9 Management Enforce For Replication Yes
& (39 Notification Services
(8) SQL Server Agent (Agent XPs disabled)
Editing properties For existing check constraint
Trang 15
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
AN ULOnalon SOS SenwerZ00s)
Trang 16File Edt View Project Query Designer Tools Window Community Hep
2 Dinew query | Oy | GB SOs | ola | GB,
Trang 17User Management
Trang 18Security 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
¢ Mixed Mode (SQL Server and Windows)
— The user logs on to their network, Windows or
otherwise
— Next, the user opens a non-trusted connection to SQL
Server using a separate user name and password
— The user name and password should be verified by
SQL Server
AN ULOnalon SOS SenwerZ00s)
Trang 19Create a standard login
¢ Open Management Studio and expand your server
¢ Expand Security and then click Logins
¢ Right-click Logins and select New Login from the context menu
¢ Inthe Logic name box, type Cmpt354
¢ Select SQL Server Authentication mode
¢« Inthe Password text box, tyoe a complex string and
confirm it
¢ Uncheck “User must change password at next login”
¢ Under Default database, select your target database as the default database
¢ Click the OK button
AN ULOnalon SOS SenwerZ00s)
Trang 20File Edit View Project QueryDesigner Tools Window Community Help
Dewey |b BRS BFA @ | PERBRBA,
(ae 8 | ? 8d | tN
(Gy Database Snapshots (Name) user_name
- AdventturetWorksDW m LOBNN “PEW Allow Nulls Yes
_MyDB Select a page _ oe - Collation Chinese_PRC_CI_AS
& |g DB35¢ = General 3 Sat Hee _ Data Type nchar
(3 Database Diagrams > Server Roles - Length 10
(9 Tables 7 User Mapping Login name: ¡Cmpt354 | | Search _ Precisian 0
Views “#8? Securables " / / - Scale 0
8 (Gy System Views & Status © lindows authentication lạ, 20
_ 2 mg © SOL Server authentication
Ca Programmability Password: [eeccceee |
— Confirm password: i |
5ecurity
C 5ecurity
=i Cñ Logins [#] Enforce password policy a
BUILTIN\Administrators IEIm| [v] Enforce password expiiation (a)
NT AUTHORITY\SYSTEM c “alias [¥] User must change password at next login
OWNER-0C12F22A15QL5erver2005R LG Ly -
OWNER-0C12F22ã\5QL5erver2005R » 0000006 \_) Mapped to certificate
> ER HN mhưnn VC user_name Cettificate | |
(Gi Server Roles ) Mapped to asymmatiic key
ese
@ (2) Server Objects — Key name: | |
= ¬ ane OWNER-00C12F224 "
& (9 Notification Services Connection: 3 Sun :
3) SQL Server Agent (Agent xPs disabled) OWNER-O0C12F224\Owner Default language:
TẾ soi ri Vu 32 View connection properties
J °IIDit:oii¡ils:ll| on) ©)09)IP, Senver 2005
Trang 21Creating Database User
menu, select New User
Inout a User name
Click the button at the right of Login name box, then
browse all the available names
select the target name (Cmpt354, the one you just
created)
Click OK
AN ULOnalon SOS SenwerZ00s)
Trang 22R E ee =
> ÀÄjCrosofÍt SNe ee ee Studio
File Edit View Project Query Designer Tools Window Community Help
Daw query || Bb eda |b OBB,
else a OWNER-00C12F224 (SQL Server 9,0,1399 - OWh |
© (Gy Databases | * (All Columns)
"Adventure Works eee El View Designer
- AdventureWorksDW chi Allow Nulls Yes
_MyDB Select a page Pec » FRE Collation Chinese_PRC_CI_AS
Ei | DB354 = General SS Sein 9E Data Type nchar
(49 Database Diagrams ers bị Lenath 10
(9 Tables (> Extended Properties User name: | Cmpt354 | Precision Oo
Eã views Login name: Cmpt354 Scale 0
Ca Programmability © + Eartilicate name: | |
Service Broker - -
a Storage (Key name: |
(39 Security ) Without login
(#i E8 Users
# (a Roles Default schema: | | Ca
aos Ẽ _ Schemas owned by this user: (vị
; OWNER-00C12F224\0wner
ST cu F22 DANE 3} View connection properties
FROM dbo [user]
Ready
AN TUKtOniall on) S al Senyer 2005
Trang 23Granting, Revoking, and Denying
Permissions
¢ Open Management Studio, expand your server and
Databases, then select the target database
¢« Expand the database, then expand Security and Users
¢ Double-click the target user, and select the Securables page from the dialog window
¢ In Securables section, click Add, and in the Add Objects window click OK
¢ Inthe Select Objects window, click Object Types, then check Tables and click OK
¢ Browse available table and check the target table, then click OK
¢ If necessary, define more detailed permissions on the
target table
¢ Click OK to return to Enterprise Manager
AN ULOnalon SOS SenwerZ00s)
Trang 24File Edit View Project Query Designer Tools Window Community Help
Dawe |S RB SA@ PRES,
AdventureWorks aro F 4 Database Name DB354
; tabase User - Cmpt35 aan
- AdventureWorksDW i Description
& |g DB354 = General SB Soiet Hợp Server Name owrter-Ic12f22a
& (j Tables > Extended Properties User name: | Cmpt354 _BindToSchema No
(3) Views Securabl | Deterministic Yes
(39 Synonyms Cane IT _ Distinct Values Na
8 Programmabiity SE Su _ GROUP BY Extensio <None>
(i Service Broker GSCU ME: Output All Columns No
(Gj Symmetric Keys Explicit permissions for dbo.user:
DA rain fee fest [Hhaer [Det
E Dã Replication Server: Control dbo Oo O Oo
@ Ga OWNER-00C12F224 Delete dbo oO Oo oO
& (2 Notification Services Connection: Insert dbo oO oO oO
(8) SQL Server Agent (Agent xPs disabled) OWNER-00C12F224\0wner References dbo wm oO Oo
aa ener 0n Slj View connection properties Select dbo 8l [#l oO
Take ownership dbo fF oO oO
J °IIDit:oii¡ils:ll| on) ©)09)IP, Senver 2005
Trang 25Query the Database
Trang 26Query 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 lableName
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
AN ULOnalon SOS SenwerZ00s)
Trang 27
| File Edit View Query Projet Tools Window Community Help
¡8| wwowery là Eồ tà lồ [à đc H @Ø &à ñ là #„ ep foe
— TEỊ create vom 34 l2 A2/1° 84 r2 g3[G]Ø1i
Available Databases] OWNER-O0C12F2 QLQuery3.sql* | Summary |
i select * from users
a (i OWNER-00C12F22A (SQL Server 9.0,1399 - OWN
& (By Databases
Trang 28Save the Query as a Script File
¢ Click “File”
¢ Select “Save SQLQuery1.sql as ”
¢ Type in the file name you want
¢ Click “Save”
AN ULOnalon SOS SenwerZ00s)
Trang 30How to Use T-SQL (cont.)
¢ Create a table with a constraint
CREATE TABLE Table354
(
Column1 int NULL,
Column2 char(10) Null,
CONSTRAINT chk_id CHECK (Columni BETWEEN 0 and 100)
)
AN ULOnalon SOS SenwerZ00s)
Trang 31How to Use T-SQL (cont.)
¢ Create a view
USE DB354 CREATE VIEW view354 ON dbo Table354
AS
SELECT Column1 FROM Table354
¢ Execute queries
USE DB354 SELECT * FROM Table354 WHERE Column1>50
AN ULOnalon SOS SenwerZ00s)
Trang 32Importing and Exporting
Your Data
Trang 33Exporting 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
AN ULOnalon SOS SenwerZ00s)