You are on page 1of 13

CHNG 4 TNG QUAN V SQL SERVER 2005 Bi thc hnh b sung

To CSDL n gin bng SQL Server Management Studio


Mc tiu :
Kt thc chng ny, bng cch s dng Management Studio, bn s c th: To CSDL Contacts qun l danh b Nhp d liu v chnh sa d liu trong bng Trong bi ny, bn s c hng dn cch thc to mt CSDL n gin c tn Contacts qun l thng tin danh b c dng cho cc cng vic lin h thng ngy ca bn. CSDL ny n gin ch cha mt bng Contacts vi cc thng tin danh b c lu tr gm: H v tn (First_Name, Last_Name, Middle_Name), Cng ty(Company), Ngh nghip (Job_Title) , S in thoi (Business_Phone, Home_Phone, Mobile_Phone), S fax (Fax_Number), a ch (Address), Tnh\Thnh (City\Province), Th in t (email), trang web (webpage). Cc danh b c phn loi thnh 3 loi (Category) l Business (cng vic), Personal (Ring t) v Family (Gia nh).

thc hin vic to CSDL Contacts, bn cn thc hin tun t cc bc sau:

1. Khi ng Management Studio 2. Kt ni vo Database Engine 3. To CSDL Contacts 4. To Bng Contacts 5. Nhp d liu v chnh sa d liu trong bng Contacts

1. Bc 1: Khi ng Management Studio

khi ng Management Studio,vo Start\ All Programs\ Microsoft SQL Server 2005\ SQL Server Management Studio.

2. Bc 2: Kt ni vo Database Engine Sau khi khi ng Management Studio, hp thoi Connect to Server hin ra, bn cn nhp chnh xc cc thng tin c th kt ni ti SQL Server. Cc thng tin bao gm: Server type: nh mc nh (l Database Engine) Server name: ch n SQL Server 2005 Database Engine m bn kt ni n. Lu , trn my c th tn ti nhiu Engine (c gi l Instance), bn cn ch r c Instance name v host name kt ni thnh cng. Nh trong hnh di y, chng ta s kt ni vo my ch c tn l srv (y chnh l hostname, thng trng vi tn my tnh, hoc tn min); instance c kt ni c tn SQLSERVER.

Authentication: Chn loi xc thc, bn c th dng Windows Authentication (nh trong hnh) hoc SQL Server Authentication. Trong trng hp bn dng SQL Server Authentication, hy hi qun tr h thng bit ti khon ng nhp vo SQL Server.

Giao din Management Studio sau khi ng nhp thnh cng:

3. Bc 3: To CSDL Contacts
to mi CSDL, nhn phi chut vo nt Databases ri chn New Database.

Trong hp thoi New Database, nhp tn (Database name) cho CSDL, t tn CSDL l Contacts; ri nhn OK.

4. Bc 4: To Bng Contacts
Sau khi to xong CSDL Contacts, trong ca s Object Browser, m nt Contacts (nhn vo du +), ri nhn phi chut vo nt Tables, chn New Table.

4.1 Thm cc ct cho bng Contacts Ca s Table Designer s hin ra nh hnh di, bn nhp tn cc ct (Column Name) v kiu d liu (Data Type) tng ng cho ct thit k bng Contacts. Trc ht, bn t tn ct u tin ca bng l id, kiu d liu l int. Ct ny s c dng lu tr m s cho tng danh b, n s c SQL Server t sinh t ng.

thit lp c ch sinh t ng cho ct id, trong ca s Column Properties bn di, m nt Identity Specification, t gi tr Is Identity l Yes.

Hnh: t identity cho ct id

Hnh: ct id c thit lp identity Bn nhp thm cc ct cn li ca bng Contacts nh trong hnh di y:

Lu , trong khi nhp kiu d liu cho tng ct, bn cn ch r kch thc tng ct. V d, khai bo kiu d liu cho Last_Name l nvarchar(50) c ngha l ct Last_Name c kiu d liu l nvarchar, ct ny c th lu tr ti 50 k t unicode. ngha chi tit ca tng loi kiu d liu CSDL s c ch r k trong cc gi hc sau. Sau khi nhp xong cc ct v kiu d liu tng ng, trc khi lu bng Contacts, ta s thit lp mt s rng buc n gin cho cc ct c bit ca bng Contacts (K thut thit lp rng buc nng cao cho cc trng d liu ca SQL Server s c cp trong cc bi sau).

4.2 t kha chnh cho ct id: nhn phi chut vo ct id, chn Set Primary Key.

4.4 Gii hn gi tr nhp vo ct Category Ct Category c dng phn loi cc danh b. Nh trn ni, cc contact c phn thnh 3 loi l Personal, Business v Family; do , cc gi tr nhp vo ct Category ch c th l Personal, Business hoc Family. thit lp c rng buc nh vy, ta thc hin cc bc: chn ct Category, nhn phi chut, chn Check Constraints;

trong hp thoi Check Constraints hin ra, nhn nt Add;

trong phn Expression ca General, nhn nt

nhp biu thc nh trong hnh di y, ri nhn OK:

Lu , nu bn nhp sai biu thc, SQL Server s thng bo li nh hnh di y:

4.5 Yu cu nhp d liu vi mt s trng chnh: b du kim (check) trong ct Allow Nulls trn cc ct Last_Name, First_Name v eMail. Khi lu d liu vo bng Contacts, ta khng c b trng (null) cc ct ny.

4.6 Lu bng Contacts Nhn nt Save trn thanh cng c, hoc nhn Ctrl+S lu bng.

Nhp tn Contacts vo hp thoi Choose Name t tn cho bng, ri nhn OK lu bng.

4.7 Chn thm mt ct vo bng Bn c th thy trong danh sch cc ct ca Contacts c to ra trong cc bc trn cn thiu mt ct Middle_Name. Ta s chn thm ct ny vo sau ct Last_Name v trc ct First_Name. thc hin c vic ny, chn ct First_Name, nhn phi chut, chn Insert Column;

Ngay sau khi nhn Insert Column, mt ct mi c chn thm vo gia hai ct. Nhp Middle_Name vo Column Name v nvarchar(50) vo ct Data Type.

Bng Contacts hon chnh:

5. Bc 5 - Nhp d liu v chnh sa d liu trong bng Contacts nhp d liu vo bng, ta cn m ca s nhp liu bng cch nhn phi chut vo bng Contacts, chn Open Table.

Ca s nhp liu hin ra, ta c th nhp d liu vo trong cc tng ng vi cc ct. Khi nhp xong mt dng, nhn Tab chuyn sang dng mi, d liu s t ng c lu vo trong CSDL.

Trn ca s nhp liu, ta c th chnh sa d liu trn mt bt k, cng nh xa mt dng d liu.