Sunteți pe pagina 1din 16

HÖÔÙNG DAÃN SÖÛ DUÏNG SQL SERVER

I. Caùc thaønh phaàn cuûa SQL server

1. Books Online
Coâng cuï cung caáp thoâng tin duøng ñeå tra cöùu toaøn dieän veà SQL Server
2000
2. Client Network Utility
Coâng cuï duøng ñeå thieát laäp, tham khaûo caùc caáu hình maïng, giao thöùc truyeàn
cho Client, caùc ñöôøng daãn tham khaûo ñeán thö vieän.
3. Configure SQL XML Support in IIS
SQL XML hoã trôï trong IIS

Microsoft Management Console


Microsoft Management Console (MMC) hosts administrative tools that you can use to
administer networks, computers, services, and other system components.

 For help with specific tasks, see How To.


 For general background information, see Concepts.
 For information about accessibility features, see Accessibility for MMC.
 For problem-solving instructions, see Troubleshooting MMC

IIS Virtual Directory Management for


SQL Server Utility Help
The documentation for IIS Virtual Directory Management for SQL Server Utility is
included in SQL Server Books Online. If you installed SQL Server Books Online during
Setup, it will open now.
4. Enterprise Manager
Thaønh phaàn chính cuûa SQL Server, duøng ñeå :
- Quaûn trò caùc CSDL.
- Taïo caùc CSDL vaø caùc thaønh phaàn beân trong CSDL.
- Ñaêng kyù laøm vieäc vôùi caùc SQL Server khaùc.
- Coù theå goïi laøm vieäc vôùi caùc SQL Server khaùc.

5. Import and Export Data


Coâng cuï hoã trôï cho pheùp deã daøng thöïc hieän vieäc chyeån döõ lieäu töø
ngoaøi vaøo SQL Server vaø töø SQL Server ra ngoaøi, döõ lieäu bao goàm caùc daïng
nhö : database, preadsheet vaø text file.

6. Profiler

Monitoring with SQL Profiler


SQL Profiler is a graphical tool that allows system administrators to monitor events in an
instance of Microsoft® SQL Server™. You can capture and save data about each event to
a file or SQL Server table to analyze later. For example, you can monitor a production
environment to see which stored procedures are hampering performance by executing too
slowly.
Use SQL Profiler to monitor only the events in which you are interested. If traces are
becoming too large, you can filter them based on the information you want, so that only a
subset of the event data is collected. Monitoring too many events adds overhead to the
server and the monitoring process and can cause the trace file or trace table to grow very
large, especially when the monitoring process takes place over a long period of time.

After you have traced events, SQL Profiler allows captured event data to be replayed
against an instance of SQL Server, thereby effectively reexecuting the saved events as
they occurred originally.

Use SQL Profiler to:

 Monitor the performance of an instance of SQL Server.

 Debug Transact-SQL statements and stored procedures.


 Identify slow-executing queries.
 Test SQL statements and stored procedures in the development phase of a project
by single-stepping through statements to confirm that the code works as expected.
 Troubleshoot problems in SQL Server by capturing events on a production system
and replaying them on a test system. This is useful for testing or debugging
purposes and allows users to continue using the production system without
interference.
 Audit and review activity that occurred on an instance of SQL Server. This allows
a security administrator to review any of the auditing events, including the
success and failure of a login attempt and the success and failure of permissions in
accessing statements and objects.

SQL Profiler provides a graphical user interface to a set of stored procedures that can be
used to monitor an instance of SQL Server. For example, it is possible to create your own
application that uses SQL Profiler stored procedures to monitor SQL Server.

You must have at least 10 megabytes (MB) of free space to run SQL Profiler. If free
space drops below 10 MB while you are using SQL Profiler, all SQL Profiler functions
will stop.

Starting SQL Profiler

SQL Profiler is started from the Microsoft® Windows NT® 4.0, Microsoft Windows®
2000 or Microsoft Windows 98 Start menu, or from SQL Server Enterprise Manager.

With Windows Authentication mode, the user account that runs SQL Profiler must be
granted permission to connect to an instance of SQL Server. The login account also must
be granted permissions to execute SQL Profiler stored procedures. For more information,
see System Stored Procedures.

To start SQL Profiler


Enterprise Manager

How to start SQL Profiler (Enterprise Manager)

To start SQL Profiler

1. On the Start menu, point to Programs/Microsoft SQL Server, and then click
Enterprise Manager.

2. On the Tools menu, click SQL Profiler.

7. Query Analyse
Coâng cuï duøng ñeå thi haønh caùc caâu leänh hoaëc caùc taäp tin chöùa caùc
caâu leänh SQL. Vôùi giao dieän ñoà hoïa theå hieän caùc phaân tích giaù thaønh khi
thöïc hieän truy vaán döõ lieäu.

8. Server Network Utility


Coâng cuï duøng ñeå thieát laäp laïi caùc caáu hình cho server vaø hieån thò
thoâng tin ñöôøng daãn tham chieáu thö vieän laøm vieäc.
9. Server Manager
Duøng ñeå khôûi ñoäng, taïm döøng hoaëc keát thuùc phuïc vuï cuûa SQL Sever.

2. Taïo CSDL treân SQL server


2.1. Caùc ñoái töôïng lieân quan ñeán moät CSDL treân SQL Server
2.1.1. Servers
Laø thaønh phaàn chöùa Database vaø caùc coâng cuï quaûn trò ñoái vôùi Server.
Moãi teân Server seõ töông öùng vôùi moät SQL Server maø noù ñaêng kyù, nhö vaäy
töø moät maùy ta coù theå thöïc hieän coâng vieäc quaûn trò ñoái vôùi nhieàu SQL
Server.

2.1.1.1 Ñaêng kyù Server


2.1.1 2 Loaïi boû ñaêng kyù Server
2.1.2 Keát hôïp taøi khoaûn ngöôøi duøng treân Windows NT vaø Server
2.1.3 Ñieàu khieån khôûi ñoäng, taïm döøng vaø tieáp tuïc hoaït ñoäng treân moät
heä phuïc vuï SQL Server

I.2 DATABASE
Moãi SQL Server coù theå chöùa nhieàu Database. Moät Darabase bao goàm taäp hôïp
caùc table vaø caùc ñoái töôïng khaùc nhö : diagrams, views, stored procedure, trigger,…
Coù 3 loaïi taäp tin ñöôïc söû duïng ñeå löu tröõ database :
- Moãi database coù moät taäp tin döõ lieäu cô sôû (primary data file) duøng ñeå
chöùa döõ lieäu vaø caùc thoâng tin khôûi ñoäng database.
- Ngoaøi taäp tin cô sôû, moät database coøn coù chöùa taát caû döõ lieäu lieân
quan nhöng khoâng ñöôïc ñaët beân trong taäp tin döõ lieäu cô sôû. Neáu taäp tin
cô sôû coù theå löu giöõ taát caû caùc döõ lieäu trong database thì database seõ
khoâng caàn ñeán taäp tin döõ lieäu phuï (secondary data files).
- Moät database coù ít nhaát moät taäp tin löu veát (log file) chöùa caùc thoâng tin
giao dòch cuûa database duøng ñeå phuïc hoài döõ lieäu. Kích thöôùc toái thieåu
cuûa moät log file laø 512KB.

Moät heä phuïc vuï SQL Server coù toái thieåu 32767 database, moãi database coù kích
thöôùc toái thieåu laø 1MB.
User taïo database seõ trôû thaønh chuû sôû höõu cuûa database ñoù.

II.2.1. Taïo môùi database


Ñeå taïo môùi moät database chuùng ta laàn löôït thöïc hieän caùc böôùc sau :
Böôùc 1 :
Töø cöûa soå SQL Server Enterprise Manager  kích choïn Server  kích nuùt phaûi
chuoät treân ñoái töôïng Databases  choïn new database.

Böôùc 2 :
Taïi muïc Name goõ vaøo teân cuûa database môùi. Database cô sôû vaø log file ñöôïc
taïo seõ söû duïng teân database môùi goõ vaøo gaén theâm chuoãi “_data” ñoái vôùi
taäp tin cô sôû vaø chuoãi “_Log” ñoái vôùi log file.
Kích thöôùc ban ñaàu cuûa database vaø log file seõ gioáng nhö kích thöôùc ñaõ ñònh
cho model database.

Goõ teân
database
 Kích Automatically grow file ñeå caáp theâm khoâng gian caàn cho taäp tin
ñang ñöôïc choïn.
 Kích In megabytes neáu muoán töï ñoäng taêng kích thöôùc taäp tin theo soá gia
(increment) qui ñònh tröôùc, giaù trò maëc ñònh cuûa noù laø 1MB.
 Kích By percent neáu muoán töï ñoäng taêng kích thöôùc taäp tin theo phaàn
traêm qui ñònh tröôùc, giaù trò maëc ñònh cuûa noù laø 10%.
 Kích choïn Unrestricted filegrowth neáu muoán kích thöôùc taäp tin töï ñoäng
taêng khi caàn thieát.
 Kích choïn Restricted filegrowth (MB) neáu muoán xaùc ñònh giôùi haïn kích
thöôùc taäp tin taêng khi caàn thieát, goõ vaøo giaù trò giôùi haïn (ñôn vò tính laø
MB).

Böôùc 3 :
Ñeå thay ñoåi giaù trò maëc ñònh cho taäp tin cô sôû  kích vaøo theû Data Files 
kích vaøo caùc oâ töông öùng trong muïc File properties ñeå goõ vaøo caùc giaù trò
môùi.

Kích vaøo ñeå


xaùc ñònh laïi
thö muïc
chöùa
Töông töï, kích vaøo theû Transaction Log  kích vaøo caùc oâ töông öùng trong muïc
File properties ñeå goõ vaøo caùc giaù trò maëc ñònh cho Log file.
Kích vaøo ñeå
xaùc ñònh laïi
thö muïc chöùa

Böôùc 4 :
Kích nuùt leänh OK ñeå taïo môùi database.

II.2.3 Hieån thò thoâng tin vaø söûa ñoåi caùc thuoäc tính cuûa moät database
Ñeå hieån thò thoâng tin vaø söûa ñoåi caùc thuoäc tính cuûa moät database chuùng ta
thöïc hieän caùc böôùc nhö sau :
Böôùc 1 : Kích phaûi chuoät vaøo teân database, choïn properties
Böôùc 2 : Töø cöûa soå properties chuùng ta coù theå hieäu chænh laïi caùc thuoäc tính
trong database.
Kích phaûi chuoät
vaøo teân database,
choïn properties

II.3 Tables
 Table laø nôi löu tröõ döõ lieäu thaät söï cuûa database. Döõ lieäu ñöôïc löu tröõ
treân table theo daïng baûng (caùc coät vaø caùc haøng).
 Moãi moät database treân SQL Server coù theå chöùa ñeán 2 tæ table.
 Moãi table coù toái ña 1024 coät (moãi coät öùng vôùi moät thuoäc tính).
 Soá löôïng haøng (rows) vaø toång kích thöôùc cuûa table ñöôïc giôùi haïn bôûi
dung löôïng cho pheùp cuûa kho löu tröõ.
 Kích thöôùc toái ña cuûa moãi haøng (row) laø 8060 bytes.
 Moãi table coù chöùa ñeán 249 chæ muïc loaïi nonclustered vaø moät chæ muïc
loaïi clustered.

II.3.1. Caùch taïo Table :


II.3.1. Söû duïng coâng cuï hoã trôï Enterprise Manager:
Ñeå taïo môùi moät table baèng coâng cuï hoã trôï Enterprise Manager chuùng ta thöïc
hieän caùc böôùc nhö sau :

Böôùc 1 : Kích chuoät vaøo teân database  kích choïn Tables  kích phaûi chuoät
choïn New Table
Böôùc 2 : Treân cöûa soå New Table thöïc hieän theo höôùng daãn sau :

 Column Name : goõ vaøo teân cuûa column.


 Datatype : choïn kieåu döõ lieäu cho column.
 Length : kích thöôùc döõ lieäu.
 Allow Null : xaùc ñònh column coù chaáp nhaän giaù trò Null.
Caùc giaù trò trong theû column :
 Description : dieãn giaûi teân cuûa column.
 Default value : xaùc ñònh giaù trò maëc ñònh.
 Precision : laø soá theå hieän kyù soá cuûa column coù kieåu döõ lieäu
daïng soá. Ví duï : Column Name : hocphi, Datatype : float , Length : 8,
Precision : 53 (coù söï töông quan giöõa Length vaø Precision)
 Scale laø soá theå hieän soá kyù soá thaäp phaân cuûa column coù kieåu
döõ lieäu laø soá thöïc. Ví duï : soá 123.45 coù Precision laø : 5 vaø Scale
laø : 2.
 Indentify : laø Yes neáu muoán column laø thuoäc tính nhaän daïng coù
tính duy nhaát. Ñieàu kieän : column naøy phaûi coù kieåu döõ lieäu laø :
int, smallint, tinyint, decimal hoaëc numeric ñoàng thôøi giaù trò taïi
coät Scale =0 vaø Not Null. Khoâng theå qui ñònh giaù trò maëc nhieân
cho column loaïi identify.
 Indentify seed : giaù trò khôûi ñaàu cuûa column ñöôïc xaùc ñònh laø
indentify (giaù trò maëc ñònh laø 1).
 Indentify Increment : giaù trò xaùc ñònh böôùc taêng keá tieáp keå töø
giaù trò Indentify seed cuûa column ñöôïc xaùc ñònh laø indentify (giaù
trò maëc ñònh laø 1).
Chuù yù :
Moät column daïng Indentify seõ gioáng nhö thuoäc tính treân table cuûa
Microsoft Access coù kieåu döõ lieäu AutoNumber.
 IS RowGuid : laø Yes ñeå taïo moät column coù tính chaát global unique
indentifier. Ñieàu kieän laø : column naøy phaûi coù kieåu döõ lieäu laø
uniqueidentifier.
Moät column hoaëc moät bieán cuïc boä coù kieåu döõ lieäu uniqueindentifier
coù theå ñöôïc gaùn giaù trò baèng 2 caùch :
 Söû duïng keát quaû traû veà töø haøm NEWID, keát quaû traû
veà cuûa haøm naøy coù daïng chuoãi caùc nhoùm soá thaäp luïc
phaân. Ví duï : EDAC-6DCA1-24FEC.
 Bieán ñoåi moät chuoåi xaùc ñònh coù daïng xxxx-xxx-xxxx-
xxxxx, vôùi moãi x laø moät soá thaäp luïc phaân 0-9 hoaëc töø A
– F. Ví duï : D45C-ACD-125F-23CBA.

Böôùc 3: Queùt choïn column laøm khoùa chính  kích choïn bieåu töôïng chìa khoùa.
Chuù yù :
- Neáu coù töø 2 column trôû leân tham gia khoùa chính  moät tay giöõ phím
Ctrl + moät tay queùt choïn caùc column laøm khoùa chính  kích choïn bieåu
töôïng chìa khoùa.
- Chó nhöõng coät khoâng chaáp nhaän Null môùi coù theå laøm thaønh vieân cuûa
khoùa chính.

Böôùc 4: Kích nuùt leänh Save ñeå keát thuùc vieäc taïo môùi table.

Baûng moâ taû kieåu döõ lieäu :

Kieåu döõ lieäu Kích thöôùc Mieàn giaù trò YÙ nghóa söû duïng
Binary 8 KB “0”..”9”,”a”..”f”, Chöùa caùc bit thoâng tin
”A”..”F”
Varbinary 8 KB “0”..”9”,”a”..”f”, Chöùa caùc bit thoâng tin
”A”..”F” (caáp phaùt ñoäng)
Bit 1 Byte 0 hoaëc 1 Döõ lieäu coù moät trong
hai traïng thaùi 0 hoaëc 1
Char 255 Bytes 4000 kyù töï Kyù töï hoaëc chuoåi kyù
töï
Varchar 255 Bytes 4000 kyù töï Gioáng nhö Char tuy
nhieân kích thöôùc byte
caáp phaùt theo ñoä daøi
chuoåi thöïc teâ(
Nchar 1..8000 kyù töï Kyù töï hoaëc chuoåi kyù
töï
Nvarchar 1..8000 kyù töï Kyù töï hoaëc chuoåi kyù
töï
Text 2,147,483,647 Bytes 231-1
(2,147,483,647) kyù töï
Ntext 230-1
(1,073,741,823) kyù töï
Datetime 8 Bytes 1/1/1753  31/12/9999 Daïng chöùa chuoåi bieåu
dieãn ngaøy giôø
Smalldatetime 4 Bytes 1/1/1990  6/6/2079 Daïng chöùa chuoåi bieåu
dieãn ngaøy giôø
Decimal 17 Bytes -10 -1  10 -1
38 38
Soá thöïc
Numeric 17 Bytes -10 -1  10 -1
38 38
Soá thöïc
Float 8 Bytes -1.79E +308  Soá thöïc
1.79E+308
Real 4 Bytes -3.40E +38 Soá thöïc
3.40E+38
Int 4 Bytes -231  231-1 Soá nguyeân
(-2,147,483,648 
+ 2,147,483,647)
Smallint 2 Bytes -215  215-1 Soá nguyeân
(-32768  32767)
Tinyint 1 Bytes 0..255 Soá nguyeân

Image 231 -1 (2,147,483,647) 231 -1 Döõ lieäu hình


Bytes (2,147,483,648 bytes)
Money 8 Bytes -263  + 263 Döõ lieäu tieàn teä
Smallmoney -2,147,483,648  Döõ lieäu tieàn te
+ 2,147,483,647
Timestamp 8 Bytes Chuoåi coù daïng : Theo doõi coù maãu tin
0x000000100000a90 naøo thay ñoåi döõ lieäu
Uniqueidentifier 16 Bytes Soá thaäp luïc phaân

II.3.2. Taïo môùi moät table söû duïng T-SQL:

Cuù phaùp :
CREATE TABLE [database_name.[owner].| owner.]table_name()

Giaûi thích :
- Database_name : laø teân cuûa database chöùa table. Neáu teân cuûa database
khoâng ñöôïc ñònh roõ, heä thoáng seõ laáy teân cuûa database hieän haønh.
- Owner : laø teân cuûa user ID chuû sôû höõu cuûa table môùi seõ taïo. Owner
phaûi laø moät user ID ñang toàn taïi trong database vöøa chæ ra ôû muïc
databse_name.
- Table_name : laø teân cuûa table môùi. Teân cuûa Table khoâng ñöôïc truøng
vôùi teân cuûa table khaùc ñang toàn taïi trong database (teân cuûa table trong
moät database phaûi laø duy nhaát). Teân cuûa table giôùi haïn trong 256 kyù töï.
- Column_name : laø teân cuûa moät column trong table. Teân cuûa column trong
moät table phaûi laø duy nhaát. Teân cuûa moät column coù theå ñöôïc boû qua
ñoái vôùi nhöõng column ñöôïc taïo vôùi kieåu döõ lieäu laø timestamp.
- Computed_column_expression : laø moät bieåu thöùc xaùc ñònh giaù trò cuûa
moät column tính toaùn. Moät column tính toaùn laø moät column aûo, coù nghóa
laø noù khoâng ñöôïc löu tröõ vaät lyù trong table. Noù laø keát quaû tính toaùn
töø caùc column khaùc trong table.

Taïo môùi moät table söû duïng T-SQL theo caùc böôùc sau :
Taïo database ñeå chöùa caùc table seõ taïo.
Böôùc 1 : Khôûi ñoäng coâng cuï SQL Sever Query Analyzer.
Böôùc 2 : Goõ leänh SQL : CREATE TABLE tablename(fieldname size(),…)
Böôùc 3 : Goïi thöïc hieän leänh SQL naøy

S-ar putea să vă placă și