51
Mc Lc Bài 1 CÁC KHÁI NIỆM CƠ BẢN ................................................................................ 1 I. Các khái nim ....................................................................................................... 1 1. Csdliu ..................................................................................................... 1 2. Hqun trcsdliu ................................................................................... 1 II. Thiết kế cơ sở dliu ........................................................................................... 1 1. Cơ sở dliu quan h....................................................................................... 1 2. Quy trình thiết kế cơ sở dliu ........................................................................ 2 3. Các bước chun hoá .......................................................................................... 4 Bài 2 TNG QUAN VCSDL MYSQL ...................................................................... 6 I. Tng quan vMySQL .......................................................................................... 6 1. Mô hình Client/Server ...................................................................................... 6 2. Quy trình cài đặt (trên Windows) ..................................................................... 6 3. Khởi động & tt dch vMySQL ................................................................... 13 4. Thao tác ti client ............................................................................................ 13 5. Thay đổi password cho tài khon root ............................................................ 14 II. Các thao tác cơ bản trên CSDL .......................................................................... 14 1. Xem các cơ sở dliu ..................................................................................... 14 2. Tạo cơ sở dliu mi ..................................................................................... 15 3. Xoá cơ sở dliu ............................................................................................ 15 4. Thêm tài khoản người dùng (user) .................................................................. 15 Bài 3 BNG - TABLE................................................................................................. 16 I. Định nghĩa .......................................................................................................... 16 II. Các kiu dliu .................................................................................................. 16 1. Kiu ds(numeric) ...................................................................................... 16 2. Kiu chui kí t(string) .................................................................................. 16 3. Kiu hn hp (Miscellaneous) ........................................................................ 17 4. Kiu ngày gi.................................................................................................. 17 5. Các tkhoá định nghĩa cột (modifiers) .......................................................... 18 III. Các thao tác trên bng ..................................................................................... 18 1. To bng ......................................................................................................... 18 2. Xem thông tin ca CSDL, Bng ..................................................................... 19 3. Xoá bng ......................................................................................................... 19 4. Thay đổi cu trúc bng.................................................................................... 19 5. Chèn dliu vào bng .................................................................................... 19

Giao Trinh MySQL

Embed Size (px)

Citation preview

  • Mc Lc

    Bi 1 CC KHI NIM C BN ................................................................................ 1

    I. Cc khi nim ....................................................................................................... 1

    1. C s d liu ..................................................................................................... 1

    2. H qun tr c s d liu ................................................................................... 1

    II. Thit k c s d liu ........................................................................................... 1

    1. C s d liu quan h ....................................................................................... 1

    2. Quy trnh thit k c s d liu ........................................................................ 2

    3. Cc bc chun ho .......................................................................................... 4

    Bi 2 TNG QUAN V CSDL MYSQL ...................................................................... 6

    I. Tng quan v MySQL .......................................................................................... 6

    1. M hnh Client/Server ...................................................................................... 6

    2. Quy trnh ci t (trn Windows) ..................................................................... 6

    3. Khi ng & tt dch v MySQL ................................................................... 13

    4. Thao tc ti client ............................................................................................ 13

    5. Thay i password cho ti khon root ............................................................ 14

    II. Cc thao tc c bn trn CSDL .......................................................................... 14

    1. Xem cc c s d liu ..................................................................................... 14

    2. To c s d liu mi ..................................................................................... 15

    3. Xo c s d liu ............................................................................................ 15

    4. Thm ti khon ngi dng (user) .................................................................. 15

    Bi 3 BNG - TABLE ................................................................................................. 16

    I. nh ngha .......................................................................................................... 16

    II. Cc kiu d liu .................................................................................................. 16

    1. Kiu d s (numeric) ...................................................................................... 16

    2. Kiu chui k t (string) .................................................................................. 16

    3. Kiu hn hp (Miscellaneous) ........................................................................ 17

    4. Kiu ngy gi .................................................................................................. 17

    5. Cc t kho nh ngha ct (modifiers) .......................................................... 18

    III. Cc thao tc trn bng ..................................................................................... 18

    1. To bng ......................................................................................................... 18

    2. Xem thng tin ca CSDL, Bng ..................................................................... 19

    3. Xo bng ......................................................................................................... 19

    4. Thay i cu trc bng .................................................................................... 19

    5. Chn d liu vo bng .................................................................................... 19

  • Bi 4 THAO TC TRN CSDL ................................................................................. 21

    I. Import d liu ..................................................................................................... 21

    1. Import t file text ............................................................................................ 21

    2. Import t file sql ............................................................................................. 22

    3. Import t file Access ....................................................................................... 23

    II. Export d liu ..................................................................................................... 25

    1. Chng trnh mysqldump ............................................................................... 25

    2. Lnh Select Into Outfile ............................................................................. 25

    3. Chng trnh mysql client .............................................................................. 25

    Bi 5 TRUY VN QUERY ...................................................................................... 26

    I. nh ngha .......................................................................................................... 26

    1. nh ngha ....................................................................................................... 26

    2. Cc t kho ..................................................................................................... 26

    II. Cc thao tc truy vn d liu .............................................................................. 26

    1. Truy vn chn d liu ..................................................................................... 26

    2. Truy vn thng k d liu ............................................................................... 27

    3. Truy vn lng .................................................................................................. 28

    4. Mnh Join ................................................................................................... 28

    5. Truy vn chn d liu ..................................................................................... 28

    6. Truy vn xo d liu ....................................................................................... 28

    7. Truy vn cp nht d liu ............................................................................... 28

    III. Ton t & hm ................................................................................................ 28

    1. Ton t ............................................................................................................ 28

    2. Hm ton hc .................................................................................................. 29

    3. Hm iu kin ................................................................................................. 29

    4. Hm logic ........................................................................................................ 29

    5. Hm chui ....................................................................................................... 30

    6. Hm thi gian ................................................................................................. 30

    Bi 6 FUNCTION PROCEDURE TRIGGER ....................................................... 33

    I. Function .............................................................................................................. 33

    1. Cu trc to Function ...................................................................................... 33

    2. S dng Function ............................................................................................ 33

    II. Procedure ............................................................................................................ 34

    1. Cu trc to Procedure .................................................................................... 34

    2. S dng Procedure .......................................................................................... 34

    III. Trigger ............................................................................................................ 34

  • Bi 7 BO MT V QUN TR ............................................................................... 36

    I. Bo mt CSDL ................................................................................................... 36

    1. Phng thc bo mt trong MySQL ............................................................... 36

    2. To ti khon ngi dng ............................................................................... 36

    3. Xo ti khon ngi dng ............................................................................... 36

    4. Cp quyn & xo quyn cho ti khon ngi dng ........................................ 36

    II. Qun tr ............................................................................................................... 37

    1. Backup (sao lu) d liu ................................................................................. 37

    2. Restore (phc hi) d liu ............................................................................... 37

    3. Cc hm trn h thng ca MySQL ................................................................ 37

    4. S dng mysqladmin ...................................................................................... 37

    Bi 8 S DNG CNG C GIAO DIN HO - MySQL GUI TOOLS ........... 39

    I. Gii thiu: ........................................................................................................... 39

    II. MySQL Administrator ........................................................................................ 39

    III. MySQL Query Browser .................................................................................. 40

    Bi Tp ........................................................................................................................... 42

  • Gio trnh C s d liu MySQL

    Bin son Nguyn Minh Thnh 1

    Bi 1 CC KHI NIM C BN

    I. Cc khi nim

    1. C s d liu

    C s d liu (CSDL) hay cn gi l database l tp hp d liu c t chc mt cch

    c cu trc phc v cho nhiu mc ch khc ca ngi dng.

    Vd : Cng ty c tp tin lu tr danh sch nhn vin trn my tnh, cng lc ban gim

    c cn xem xt cc nhn vin khen thng phng ti v li cn lp bng lng thng

    cho cc nhn vin.

    Nh vy danh sch nhn vin c c ban gim c v phng ti v khai thc cng

    mt lc, d nhin thng tin v nhn vin phi nht qun ngha l d u - ban gim c

    hay phng ti v - thng tin y l phi nh nhau.

    Nh vy, vic qun tr mt cng ty c s nht qun ta cn phi t chc d liu,

    thng tin ca cng ty mt cch ng nht - ngha l phi t chc CSDL cho cng ty.

    2. H qun tr c s d liu

    H qun tr CSDL l cc phn mm cung cp cc cng c xy dng CSDL, v cc

    thao tc trn cc CSDL . V d nh Foxpro, Access, Microsoft SQL Server, MySQL..

    II. Thit k c s d liu

    1. C s d liu quan h

    a. nh ngha

    Mt c s d liu quan h l mt c s d liu c to nn bi cc bng. Mi

    bng bao gm nhiu ct, v cc bng c mi quan h vi nhau da vo nhng gi tr kho.

    Vd : mt c s d liu v bn hng s c mt bng l n t Hng bao gm cc

    thng tin nh M s n t hng, ngy t hng, ngy giao hng, V mt bng khc l

    Khch Hng bao gm cc thng tin nh h tn, a ch, Hai bng ny s c lin quan vi

    nhau, v khng th mt n t hng m khng c khch hng.

  • Gio trnh C s d liu MySQL

    Bin son Nguyn Minh Thnh 2

    b. Thc th & Thuc tnh

    Thc th l mt i tng c th hay tru tng trong th gii thc v c

    chuyn i vo trong CSDL thnh mt bng. Trong mt CSDL ta phi qun l nhiu i

    tng d liu, mi i tng c chuyn i t cc thc th trong th gii thc vo

    CSDL.

    Vd : Trong mt trng hc c cc thc th: gio vin, hc sinh, mn hc Trong

    mt th vin c cc thc th: sch, loi sch, nh xut bn Trong mt cng ty bun bn c

    cc thc th: mt hng (c th), cng n (tru tng). Mi mt thc th nh vy s l mt

    bng trong CSDL.

    Mi thc th c cc tnh cht ring gi l cc thuc tnh.

    Vd : thc th hc sinh c cc thuc tnh nh : h tn, ngy sinh, qu qun

    Mi mt thuc tnh s c th hin thnh mt ct tng ng trong bng th hin

    thc th .

    c. Kho

    phn bit cc i tng (thnh phn) trong cng mt thc th, ta s chn ra

    nhng thuc tnh dng phn bit chng, nhng thuc tnh gi l kha.

    Ta c th chn ra trong cc thuc tnh ca thc th mt hoc nhiu thuc tnh lm

    kho hoc cng c th t ra mt thuc tnh mi (thng c gi l M) lm kho cho

    thc th .

    Vd : phn bit gia cc sinh vin trong cng mt thc th sinh vin, ta t ra

    thuc tnh MaSV, v mi sinh vin c mt m s duy nht phn bit vi cc sinh vin

    khc. Do , MaSV l kha ca thc th SinhVien.

    d. Mi quan h

    Mi quan h (hay cn gi l rng buc) dng th hin s lin quan d liu gia

    cc bng vi nhau.

    C 3 loi quan h (s c trnh by chi tit phn sau):

    Quan h 1 1

    Quan h 1 n

    Quan h n n

    2. Quy trnh thit k c s d liu

    Bc 1: Xc nh mc ch ca CSDL

    Xc nh c mc ch ca CSDL, s gip chng ta quyt nh a nhng thng

    tin no vo CSDL

    V d: Mt cng ty cn qun l nhng thng tin v bun bn ca cng ty. Nh vy,

    cc thng tin cn lu tr l n t hng, mt hng, khch hng, nhn vin bn hng.

    Bc 2: xc nh cc bng (table) cn thit.

    Khi xc nh c mc ch ca CSDL, chng ta c th chia thng tin theo cc

    ch phn bit. Mi ch l mt bng (table) trong CSDL.

  • Gio trnh C s d liu MySQL

    Bin son Nguyn Minh Thnh 3

    V d: vi nhng thng tin ca cng ty trn chng ta c th to cc bng nh sau:

    HA N, CHI TIT HA N, HNG HA, KHCH HNG, NHN VIN

    BN HNG

    Bc 3: Xc nh cc field (hay cn gi l trng hoc field) cho mi bng.

    Khi xc nh cc field cho bng ta cn ch mt s iu: Khng nn to field cha

    thng tin c th rt ra t nhng field khc, hoc cha kt qu c th tnh ton c.

    Nn tch thng tin ra phn nh nht; Cc field phi ph c cc thng tin cn thit.

    Xc nh kha chnh trong bng (Primary Key): Mt field l kha chnh khi m gi

    tr ca n trong bng l duy nht, khng trng lp. Trong mt bng c th c hai field

    (hoc hn) kt hp li to thnh mt kha chnh, gi tr hp ca cc field ny trong

    bng l duy nht.

    Field kha chnh phi c kch thc nh, tc truy xut CSDL c nhanh.

    Field kha chnh khng chp nhn gi tr rng.

    V d: trong bng HA N field M ha n l field kha chnh, v mi ha n

    c mt m ring.

    Bc 4: Xc nh mi quan h gia cc bng

    Chng ta lu tr cc d liu c quan h vi nhau trn nhiu bng ring l khc

    nhau. V vy chng ta cn xc nh mi quan h (Relationship) gia cc bng. t

    quan h gia hai bng: bng A v bng B, chng ta thm kha chnh ca mt bng n

    bng cn li, v vy kha ny xut hin trong c hai bng. Nhng chng ta phi xc

    nh s dng kha chnh ca bng no? xc nh mi quan h cho ng, chng ta

    phi hiu r cc loi quan h ca chng. C 3 loi quan h gia hai bng.

    Quan h 1-nhiu (One to Many): y l mi quan h ph bin trong CSDL

    quan h. Trong quan h ny mt mu tin trong bng A c th c quan h vi nhiu

    mu tin trong bng B.

    V d: Trong mt c quan, mi mt tnh thnh (ni sinh ca nhn vin) c nhiu

    nhn vin. Nhng mt nhn vin ch thuc mt tnh thnh (ni sinh)

    Quan h nhiu - nhiu (Many-to-Many): Mi mu tin trong bng A c quan h

    vi nhiu mu tin trong bng B, v ngc li mt mu tin trong bng B, cng c quan

    h vi nhiu mu tin trong bng A. Quan h ny kh th hin trong thit k. Trong

    trng hp ny ta phi to ra thm mt bng trung gian C. V chng ta s to mi

    quan h 1-nhiu gia A v C, v mi quan h 1-nhiu gia B v C.

  • Gio trnh C s d liu MySQL

    Bin son Nguyn Minh Thnh 4

    Quan h 1-1 (One-to-One): Mt mu tin trong bng A ch c quan h vi mt

    mu tin trong bng B. V trong mt mu tin trong bng B ch c mt mu tin trong

    bng A. y l trng hp bt thng trong quan h, bi v hu nh thng tin quan h

    theo kiu ny c th s c lu tr trn cng mt bng.

    Trong trng hp to quan h 1-1, trc ht bn xem c th a cc d liu vo

    cng mt bng c hay khng. Nu v mt l do no m khng th lm c iu

    , sau y l mt vi cch t quan h:

    Nu hai bng c cng ch , ta lp mi quan h bng cch s dng kha chnh

    cho c hai bng.

    Nu hai bng c ch khc nhau vi kha chnh khc nhau, ta chn mt trong hai

    bng v t field dng lm kha chnh ca n trong bng cn li nh l kha ngoi.

    Bc 5: Tinh ch li thit k

    Sau khi ta to cc bng, cc field trong tng bng, thit lp quan h gia cc bng,

    chng ta cn xem xt li mt s vn sau:

    - Nhp mt s mu tin kim tra sai st nh c thiu st hoc d tha hoc trng

    lp thng tin trong CSDL khng?

    - Xem c thiu field no khng

    - chn kha chnh cho mi bng cha

    - Kim tra nu c trng lp thng tin trong mt bng khng. Nu c, v c th chia

    bng ra thnh hai bng vi quan h 1- nhiu.

    C bng no nhiu field, m t mu tin hay khng v c field no b trng trong

    mt s mu tin hay khng. Nu c hy thit k li

    3. Cc bc chun ho

    thc hin tt vic tnh ch li thit k, ta s da vo 3 nguyn tc chun ho sau.

    Phng thc chun ho 1 (1NF First Normal Form)

  • Gio trnh C s d liu MySQL

    Bin son Nguyn Minh Thnh 5

    Quan h l 1NF nu ko cha cc thuc tnh lp, cc thuc tnh phi l n, ngha

    l gi tr ca cc l giao ca hng v ct phi c gi tr n, nh vy, mi quan h u l

    1NF. Nu bng d liu cha cc thuc tnh lp th ko phi quan h, chuyn bng d liu c

    lp thnh quan h, c th tch cc thuc tnh lp thnh mt hoc nhiu bng khc v nu cn

    thit th tng cng kha cho cc bng mi ny. Tip tc xem xt cac sbng mi m bo

    sao cho cc bng ny cng l quan h, tc l t chun 1.

    Phng thc chun ho 2 (2NF Second Normal Form)

    Mt quan h R l dng chun 2(2NF) nu n l 1NF v cc ph thuc hm gia

    cc thuc tnh ngoi kha v kha u l cc ph thuc hm s ng, ni cch khc, mi

    thuc tnh ngoi kha u ko c ph thuc hm vo b phn ca kha.Nu quan h R cha

    nhng thuc tnh c ph thuc hm vo mt b phn ca kha th cn tch cc nhm thuc

    tnh ph thuc vo b phn ca kha v b sung thm cho cc nhm ny mt phn kha m

    chng c ph thuc hm, thnh quan h. Nhm cn li ta thnh mt quan h vi kha nh

    c. Cc quan h c to lp u l 2NF.

    Phng thc chun ho 3 (3NF Third Normal Form)

    Mt quan h R l dng chun 3 (3NF) nu n l 2NF v cc ph thuc hm gia

    cc thuc tnh kha ngoi v kha u l cc ph thuc hm trc tip-ngha l ko tn ti

    nhng ph thuc hm ngoi kha.. Nu R khng phi l 3NF, ngha l trong R tn ti thuc

    tnh khng ph thuc hm trc tip vo kha, th tch cc nhm thuc tnh c ph hm vo

    thuc tnh kha thnh mt quan h. kha ca quan h mi ny chnh l thuc tnh m chng

    c ph thuc hm.

  • Gio trnh C s d liu MySQL

    Bin son Nguyn Minh Thnh 6

    Bi 2 TNG QUAN V CSDL MYSQL

    I. Tng quan v MySQL

    MySQL l mt h qun tr c s d liu a lung m ngun m theo m hnh

    client/server, v mc chuyn dng cho doanh nghip. MySQL c pht trin bi mt

    cng ty t vn v pht trin ng dng ca Thu in c tn l TcX.

    MySQL l mt h qun tr c s d liu c tc truy xut rt nhanh v uyn chuyn.

    MySQL c pht trin ph bin cho h iu hnh Linux, tuy nhin, vi cc phin bn mi

    hin nay, n c th s dng tt trn ca h iu hnh Windows.

    Chng ta cn phn bit gia MySQL v SQL, SQL l ngn ng dng truy xut c s

    d liu c hng phn mm IBM pht trin v c s dng a s cc h qun tr CSDL

    hin nay nh MySQL, Microsoft SQL Server, DB2, Sysbase Adapter Server, SQL

    Lite,Oraccle

    1. M hnh Client/Server

    M hnh client server (Client server model) l mt m hnh c s dng kh lu

    trong lnh vc lp trnh v pht trin ng dng, m hnh mng trong mt cng ty, c quan

    Trong m hnh ny, s c mt my ch (server) c cu hnh kh mnh v kh nng lu tr, b

    nh, tc x l c t lm trung tm lu tr tt c cc d liu, qun l cc my

    con trong c quan, cng ty hay quy trnh pht trin ng dng no .

    V tnh cht c th ca nhiu nhim v khc nhau nn my ch c chia lm nhiu

    loi : Database server (my ch yu lu tr, qun l c s d liu), Web server (my ch lu

    tr, qun l ng dng web), Network server (my ch qun l h thng mng) y,

    chng ta s ch bn n Database server.

    Cc Database server s c ci t mt hoc nhiu h qun tr CSDl khc nhau, dng

    lu tr v x l cc tin trnh truy cp, truy xut, thng k lin quan n d liu. Khi

    ny, client (my con, my khch) s ng vai tr gi cc yu cu, cu hi n server, khi

    server s tip nhn v x l cc yu cu v gi kt qu v cho client. M hnh ny cn c

    th gi l m hnh 2 tng (2-tiers).

    2. Quy trnh ci t (trn Windows)

    a. Download MySQL

    ti ng dng MySQL ta truy cp vo a ch trang web

    http://dev.mysql.com/downloads/. Hin nay, ti thi im vit ti liu ny, phin bn mi nht

    ca MySQL l 5.4 beta, tuy nhin cc bn nn s dng phin bn 5.1 (phin bn c khuyn

    co s dng recommend), trong gio trnh ny s s dng phin bn 5.1.

    Bn phi chn phin bn ph hp vi h iu hnh (32bit hay 64bit). Nu bn s

    dng Windows 32bit (nh Win2000, WinXP, Win7 32bit, WinVista 32bit) th hy chn h

    iu hnh Windows, ngc li chn Windows x64.

    Sau khi chn h iu hnh, bn hy ti file Windows MSI Installer (file *.msi)

    cho tin vic ci t.

    b. Quy trnh ci t

  • Gio trnh C s d liu MySQL

    Bin son Nguyn Minh Thnh 7

    Sau khi ti v, bn s double click vo file mysql-essential-5.1.39-win32.msi

    tin hnh ci t.

    Mn hnh cho ca qu trnh ci t, click Next.

    Mn hnh chn phng thc ci t, ta chn Typical (ci t mc nh), chng

    trnh s c ci t vo C:\Program Files\MySQL\MySQL Server 5.1. Click Next.

    Mn hnh tin trnh ci t.

  • Gio trnh C s d liu MySQL

    Bin son Nguyn Minh Thnh 8

    Mn hnh gii thiu cc tnh nng ca MySQL, click Next.

    Mn hnh tu chn sau ci t.

    o Configure MySQL Server now : tip tc tin hnh cu hnh server sau

    khi ci t xong.

    o Register the MySQL Server now : tip tc tin hnh ng k vi

    MySQL.

    Bn chn tu chn 1, b tu chn 2, click Next.

    Mn hnh tin hnh cu hnh Server, click Next

  • Gio trnh C s d liu MySQL

    Bin son Nguyn Minh Thnh 9

    Mn hnh tu chn cch cu hnh :

    o Detailed Configuration : cu hnh chi tit server.

    o Standard Configuration : cu hnh theo chun mc nh.

    Chn tu chn 1, click Next.

    Mn hnh la chn loi server thch hp :

    o Developer Machine : la chn ny MySQL s s dng t b nh nht,

    thch hp cho cc my pht trin cc ng dng, website ca cc lp

    trnh vin.

    o Server Machine : la chn ny thch hp cho cc hosting server, chuyn

    cung cp dch v lu tr CSDl online.

    o Deticated MySQL Server Machine : tu chn ny ch thch hp cho cc

    server chuyn chy MySQL, khng chy thm dch v no khc.

    MySQL s s dng trn b nh ca my tng tc truy xut CSDL.

    Bn chn tu chn 1, click Next.

  • Gio trnh C s d liu MySQL

    Bin son Nguyn Minh Thnh 10

    Mn hnh tu chn phng thc ti u ho CSDL:

    o Multifunctional Database : phng thc ti u ho chung, thch hp cho

    CSDL s dng vi nhiu mc ch khc nhau.

    o Transactional Database Only : Ti u ho s dng kiu bng

    InnoDB,thch hp cho CSDL s dng nhiu n Transaction.

    o Non-Transactional Database Only : ti u ho s dng bng kiu

    MyISAM, thch hp cho cc CSDL lu v phn tch log.

    Bn chn tu chn 1, click Next.

    Mn hnh la chn ni lu tr CSDL, thng s c lu tr trong th mc

    MySQL Datafiles trong th mc ci MySQL (C:\Program

    Files\MySQL\MySQL Server 5.1\MySQL Datafiles).

    Bn c th i v tr lu bng cch click du ()

  • Gio trnh C s d liu MySQL

    Bin son Nguyn Minh Thnh 11

    Mn hnh la chn s lng kt ni n server. Bn hy tu chn theo s lng

    m bn c lng, tuy nhin s lng hp l l khong 15 20 cho cc CSDL

    ginh cho cc ng dng (nh Website).

    Mn hnh tu chn phng thc kt ni n server, mc nh, click Next.

    Mn hnh tu chn b k t (charset) cho CSDL. Chn tu chn Best Support

    for Multilingualism thch hp cho lu ting Vit (h tr UTF-8).

  • Gio trnh C s d liu MySQL

    Bin son Nguyn Minh Thnh 12

    Mn hnh la chn phng thc chy MySQL :

    o Install As Windows Service : chy MySQL nh l mt dch chy n.

    o Include Bin Directory in Windows PATH : chn ng dn gc ca

    MySQL vo cc bin mi trng ca Windows, khng cn khai bo

    ng dn khi chy.

    Chn c 2 tu chn, click Next.

    Chn password cho ti khon cao nht ca MySQL (ti khon root). Hy nh

    tht k password ny.Khng chn tu chn 2 v l do bo mt.

    Click Execute chy v khi ng dch v.

  • Gio trnh C s d liu MySQL

    Bin son Nguyn Minh Thnh 13

    3. Khi ng & tt dch v MySQL

    Sau khi ci t, MySQL Server s chy nh l mt dch v (chng trnh chy n)

    trn Windows. By gi, my ca bn va ng vai tr l mt server va ng vai tr l mt

    client, v vy tn server ca my s l localhost.

    tt dch v MySQL Server ta s vo m Task Manager, chn th Proccess, tm

    n tin trnh mysqld, click chn v nhn End Task tt dch v.

    khi ng li dch v, ta vo th mc ci t MySQL (C:\Program

    Files\MySQL\MySQL Server 5.1\bin), double click vo file MySQLInstanceConfig.exe,

    thc hin li tin trnh cu hnh Server v chy dch v.

    4. Thao tc ti client

    Trong th mc ci t, th mc Bin cha tt

    c cc file thc thi, cc file lnh ca MySQL, cc file

    ny thng c chy giao din dng lnh (command

    line).

    chy cc thao tc pha client ta thc thi

    file mysql.exe command line vi cu lnh :

    mysql u root p

    (hoc vo Start Programs MySQL

    MySQL Command Line Client).

  • Gio trnh C s d liu MySQL

    Bin son Nguyn Minh Thnh 14

    Nhp password ng nhp MySQL server, do mi ch c ti khon root c to,

    nn bn hy nhp password ca root khi ci t ng nhp server vi ti khon root.

    Mn hnh sau khi ng nhp thnh cng s l cu cho welcome

    thc hin truy vn CSDL ta g lnh v kt thc bng u ";". Cc lnh ca

    MySQL s c trnh by chi tit phn sau.

    Vd : thc hin lnh thot chng trnh ta g : quit;

    Hoc lnh : exit;

    Ch : trong MySQL cu khng phn bit ch hoa, ch thng.

    5. Thay i password cho ti khon root

    Mt khu ca root l rt quan trng, v root l ti khon cao nht ca MySQL Server,

    do nu mt khu b ngi khc bit s c th truy cp d liu v lm tt c cc thao tc trn

    . V vy vic thay i mt khu root khi cn thit l vic cn lm. thc hin thay i mt

    khu root, ta thc hin cu lnh sau :

    set password for root@localhost = password("new_password");

    thay i password cho cc ti khon khc, ta thc hin lnh :

    set password for acount_name@server_name = password("new_password");

    Trong :

    account_name : tn ti khon.

    server_name : tn server mun truy cp, nu server cng l my cc b, ta s

    dng localhost.

    xem tnh trng ca server, ta thc hin lnh

    status;

    II. Cc thao tc c bn trn CSDL

    1. Xem cc c s d liu

    xem cc c s d liu hin c trn server, ta dng lnh :

    show database;

  • Gio trnh C s d liu MySQL

    Bin son Nguyn Minh Thnh 15

    2. To c s d liu mi

    Sau khi phn tch v thit k c s d liu, ta c th xy dng c s d liu

    trn mt h qun tr. xy dng, thao tc u tin l to c s d liu, CSDL trn

    MySQL ta dng lnh :

    create database database_name;

    Trong : database_name l tn CSDL mun to.

    Vd : create database QLBanHang;

    Sau khi to CSDL, thc hin truy xut ln CSDL no , ta dng lnh :

    use database_name;

    vd : use QLBanHang;

    3. Xo c s d liu

    xo mt CSDL, ta dng lnh :

    drop database database_name;

    vd : drop QLBanHang;

    4. Thm ti khon ngi dng (user)

    Sau khi to c c s d liu, ta c th to thm cc ti khon ngi dng khc

    s dng CSDL (khng nn ch ti khon root s dng). thm c ti khon

    ngi dng ln mt CSDL ta phi hiu cch MySQL qun l user trn cc CSDL nh th no.

    MySQL c mt CSDL mc nh cng mang tn mysql. Trong CSDL ny bao gm 3

    bng : user, db, host.

    Bng user : dng qun l cc ti khon ngi dng, mt khu, thuc

    nhng host no, v ti khon c nhng quyn hn g.

    Bng db : dng qun l cc ti khon ngi dng c php truy cp

    nhng CSDL no.

    Bng host : dng qun l cc database thuc nhng hostname no.

    Trc tin, ta phi thm vo bng host dng d liu qun l CSDL va to c truy

    cp trn nhng hostname no.

    Vd : CSDL vi tn l QLDonDatHang, c to trn server localhost.

    use mysql;

    insert into host values('localhost','QLDonDatHang','Y','Y','Y','Y','Y','Y','Y','Y',

    'Y','Y','Y','Y','Y','Y','Y','Y','Y','Y');

    Tip theo, ta phi thm vo bng user ti khon mun to.

    insert into user values('localhost','inzaghithanh',password('9967722'),'Y','Y','Y',

    'Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y',

    'Y','Y','Y' ,'', '', '', '',20,20,20,20);

    Sau cng, ta s thm vo bng db cho php ti khon va to s dng CSDL.

    insert into db values('localhost','QLDonDatHang','inzaghithanh','Y','Y','Y','Y',

    'Y', 'Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y');

  • Gio trnh C s d liu MySQL

    Bin son Nguyn Minh Thnh 16

    Bi 3 BNG - TABLE

    I. nh ngha

    Bng l ni lu tr d liu. Trc khi thc hin truy vn v s dng d liu, th nhim v

    u tin l phi lu tr n.

    Bng c th cha d liu v cc i tng thng tin nh HNG HA, NHN VIN.

    Mi dng (row) ca bng cha mt th hin ring ca i tng no . V d thng tin v

    mt hng ha, thng tin v mt nhn vin. Dng c thnh lp t cc ct (column), mi

    ct l mt thng tin v i tng c lu tr trong bn. V d trong bng NHANVIEN

    chng ta c cc field nh sau: Field MaNV(m nhn vin), field HoNV (h v ch lt), field

    TenNV (tn), field Nu (nam/n), field NgaySinh (ngy sinh)..

    II. Cc kiu d liu

    MySQL cung cp cc kiu d liu sau :

    1. Kiu d s (numeric)

    Tn kiu

    (type name)

    B nh

    (memory space)

    bytes

    Vng gi tr

    (value range)

    Vng gi tr dng

    (Unsigned)

    Tinyint 1 -128 172 0 255

    Smallint 2 -32768 32767 0 65535

    Mediumint 3 -8388608 8388607 0 16777215

    Int 4 -2147483648

    2147483647 0 4294967295

    Bigint 8

    -9223372036854775808

    9223372036854775807

    0

    18446744073709550

    615

    Float (M,D) 4

    Double (M,D) 8

    Decimal (M,D) M + 2

    2. Kiu chui k t (string)

    Tn kiu Kch thc ti a

    (bytes)

    Khong lu tr

    (bytes)

    Char(x) 255 X

    Varchar(x) 255 X + 1

    Tinytext 255 X + 1

    Tinyblob 255 X + 2

    Text 65535 X + 2

    Blob 65535 X + 2

  • Gio trnh C s d liu MySQL

    Bin son Nguyn Minh Thnh 17

    Mediumtext 1.6 MB X + 3

    Mediumblog 1.6 MB X + 3

    Longtext 4.2 GB X + 4

    Longblob 4.2 GB X + 4

    3. Kiu hn hp (Miscellaneous)

    Enum : kiu d liu lit k, cho php nh ngha trc cc gi tr cho mt ct, ct s

    ch lu tr mt trong cc gi tr nh sn .

    Vd :

    CREATE TABLE Test(

    Return ENUM('Y','N') DEFAULT 'N',

    Size ENUM('S','M','L','XL','XXL'),

    Color ENUM('Black','Red','White')

    )

    Set : kiu d liu lit k, tng t enum nhng cho php ct lu tr nhiu gi tr

    trong cc gi tr nh sn, mi gi tr cch nhau bi du ', '

    Vd :

    CREATE Table Test(

    Advertiser SET('Web Page','Television','Newspaper')

    )

    4. Kiu ngy gi

    Tn kiu nh dng chun Gi tr 0

    DATETIME YYYY-MM-DD

    HH:MM:SS

    0000-00-00

    00:00:00

    DATE YYYY-MM-DD 0000-00-00

    TIME HH:MM:SS 00:00:0

    YEAR YYYY 0000

    TIMESTAMP (Xem phn sau) 00000000000000

    (di nht)

    nh dng kiu TIMESTAMP

    Tn kiu nh dng

    TIMESTAMP(14) YYYYMMDDHHMMSS

    TIMESTAMP(12) YYMMDDHHMMSS

    TIMESTAMP(10) YYMMDDHHMM

    TIMESTAMP(8) YYYYMMDD

    TIMESTAMP(6) YYMMDD

    TIMESTAMP(4) YYMM

    TIMESTAMP(2) YY

  • Gio trnh C s d liu MySQL

    Bin son Nguyn Minh Thnh 18

    5. Cc t kho nh ngha ct (modifiers)

    T kho Kiu d liu

    thch hp ngha

    Auto_Increment Int T ng tng d liu ca ct

    Binary Char, Varchar Thit lp lu tr chui nh phn

    (phn bit ch hoa thng)

    Default Tt c

    tr text, blob Thit lp gi tr mc nh cho ct

    Not null Tt c Khng cho php gi tr null

    Null Tt c Cho php gi tr null

    Primary key Tt c Thit kho chnh cho ct

    Unique Tt c Gi tr duy nht

    Unsigned Kiu numeric Ch lu gi tr s nguyn

    Zerofill Kiu numeric in gi tr 0 cho chiu di s

    III. Cc thao tc trn bng

    1. To bng

    To bng mi

    Create table table_name ( column_names datatypes modifiers)

    Vd : To bng Customers (khch hng)

    CREATE TABLE Customers (Customer_ID INT NOT NULL

    PRIMARY KEY AUTO_INCREMENT, First_Name VARCHAR(20)

    NOT NULL, Last_Name VARCHAR(30) NOT NULL,

    Address VARCHAR(50), City VARCHAR(20),

    State VARCHAR(2), Zip VARCHAR(20),

    E_Mail VARCHAR(20), Age INT, Race VARCHAR(20),

    Gender ENUM('M', 'F') DEFAULT 'F',

    Eye_Color VARCHAR(10), Hair_Color VARCHAR(10),

    Favorite_Activity ENUM('Programming', 'Eating', 'Biking', 'Running', 'None')

    DEFAULT 'None',

    Favorite_Movie VARCHAR(50),Occupation VARCHAR(30), Smoker CHAR(0));

    To bng tm (bng ph) mi

    Create temporary table table_name (column_names datatypes modifiers)

    To bng tm (bng ph) t mt cu truy vn

    Create temporary table select column_name from table_name

    vd : create temporary table select * from Customers

    To bng sao chp t mt bng khc

    Create table table_name select column_name from table_name_1

    vd : create table Customers_copy select * from Customers

    Kim tra s tn ti ca bng trc khi to

  • Gio trnh C s d liu MySQL

    Bin son Nguyn Minh Thnh 19

    Create table If not Exists table_name (column_names datatypes modifiers)

    2. Xem thng tin ca CSDL, Bng

    Xem cc bng ca CSDL

    Show tables [from database_name];

    Xem cc ct ca bng

    Show columns [from table_name];

    Xem cu trc ca bng

    Discribe table_name [from database_name];

    3. Xo bng

    Drop table table_name [from database_name];

    4. Thay i cu trc bng

    Thay i tn ct

    Alter table table_name change old_column_name

    new_column_name old_datatype;

    Vd : ALTER TABLE Customers

    CHANGE First_Name FirstName VARCHAR(20);

    Thay i kiu d liu

    Alter table table_name change column_name column_name new_datatype;

    Vd : ALTER TABLE Customers

    CHANGE Last_Name Last_Name VARCHAR(50);

    i tn bng

    Alter table table_name Rename new_table_name;

    Vd : ALTER TABLE Customers RENAME Customer_Table;

    Thm ct vo bng

    Alter table table_name add column_name datatype;

    Vd : ALTER TABLE Customer ADD Last_Name VARCHAR(30);

    Xo mt ct

    Alter table table_name Drop column_name;

    Vd : ALTER TABLE Customers DROP Last_Name;

    Thm kho chnh

    Alter table table_name Add Primary Key (column_names);

    Vd : ALTER TABLE Customers ADD PRIMARY KEY (Customer_ID);

    Xo kho chnh

    Alter table table_name Drop Primary Key;

    5. Chn d liu vo bng

    Chn mt dng d liu

  • Gio trnh C s d liu MySQL

    Bin son Nguyn Minh Thnh 20

    Insert into table_name (column_names) values (column_values);

    Vd : To bng mt bng c tn Test_Table v chn d liu

    CREATE TABLE Test_Table

    (Test_ID INT NOT NULL PRIMARY KEY AUTO_INCREMENT,

    Test_Name VARCHAR(30),

    Test_Date DATETIME,

    Test_Giver VARCHAR(30));

    INSERT INTO Test_Table (Test_ID, Test_Name, Test_Date, Test_Giver)

    VALUES (NULL, 'Test','2000-01-01','Glen');

    Chn nhiu dng d liu

    Insert into table_name (column_names) values (column_values),

    (column_values), ();

    Vd :

    INSERT INTO Test_Table (Test_ID, Test_Name, Test_Date, Test_Giver)

    VALUES (NULL, 'John','2000-01-01','Glen'),

    (NULL, 'Thomas','2000-01-01','Jose');

  • Gio trnh C s d liu MySQL

    Bin son Nguyn Minh Thnh 21

    Bi 4 THAO TC TRN CSDL

    I. Import d liu

    Sau khi thit k v to CSDL, ta c th nhp d liu vo cc bng bng cc dng lnh,

    nhng phng php s khng kh thi nu chng ta c mt ngun d kh ln t trc.

    Ngoi ra, nu ta mun ly d liu t mt h qun tr khc, th ta cng khng th nhp tng

    dng lnh. V vy, cc h qun tr lun c chc nng Import cho php ngi dng chn mt

    lng d liu ln c trc vo trong CSDL mt cch nhanh nht v t tn thi gian.

    1. Import t file text

    thc hin Import d liu t mt file text, ta s dng chng trnh mysqlimport.exe

    trong th mc Bin ci t MySQL. Cu lnh import command line nh sau :

    Mysqlimport.exe database_name table_name.txt

    Vd : mysqlimport QLBanHang Customer.txt

    D liu trong file text s c ti vo bng c tn cng vi tn file text. Nu bng cha

    tn ti, chng trnh s t to bng mi. nh dng ca file text phi c trnh by theo quy

    nh sau :

    Mi dng d liu c trnh by trn 1 dng.

    Gi tr text phi c ng bng du nhy n (') hoc nhy kp (").

    Cc gi tr cch bi du phy (,).

    Cc gi tr phi c sp theo th t tng ng

    Vd : khi import vo bng c cc ct sau Customer_ID int, Last_Name varchar(25),

    First_Name varchar(15), d liu file text phi theo mu sau :

    1, "Nguyen Minh","Thanh"

    Cc tu chn ca chng trnh mysqlimport

    Tu chn ngha

    -d hoc --delete Xo tt c d liu ang c trong bng trc khi

    chn d liu mi

    -f hoc --force Tip tc chn d liu khi gp li

    -i hoc --ignore B qua nhng dng d liu chn ging vi nhng

    dng c d liu duy nht

    -L hoc -local Ch nh s dng file import trn my cc b

    -l hoc -lock-tables Kho cc bng trc khi chn

    -r hoc -replace Thay th cc dng d liu duy nht bng dng d

    liu chn

    --fields-enclosed-by=char Ch nh k t ng d liu ( vd : 'ABC')

    --fields-escaped-by=char Ch nh k t loi tr cho cc k t c bit

    --fields-optionally-terminated-

    by=char

    Ch nh k t phn chia cc gi tr

    --fields-terminated-by=char Ch nh k t phn chia cc gi tr

    --lines-enclosed-by=str Ch nh k t kt thc mt dng d liu

  • Gio trnh C s d liu MySQL

    Bin son Nguyn Minh Thnh 22

    V d : mt file text c tn Orders.txt (Ho n) vi d liu theo nh dng sau :

    "1", "ORD89876", "1 Dozen Roses", "19991226"

    Cu lnh import c cu trc sau :

    Mysqlimport.exe frl fields-enclosed-by="

    --fields-terminated-by=, QLBanHang Orders.txt

    2. Import t file sql

    Mt cch khc import d liu l thc thi hng loi cc cu lnh sql t mt file

    *.sql (hay cn gi l batching).

    Mt mu v d v file data.sql nh sau :

    USE QLBanHang;

    INSERT INTO Customers (Customer_ID, Last_Name, First_Name)

    VALUES(NULL, "Nguyen Minh","Thanh");

    INSERT INTO Customers (Customer_ID, Last_Name, First_Name)

    VALUES(NULL, "Nguyen Thien","Nam");

    INSERT INTO Customers (Customer_ID, Last_Name, First_Name)

    VALUES(NULL, "Nguyen Khoa","Danh");

    thc thi file sql ta s s dng lnh sau :

    Load Data Infile filename.sql Into Table table_name;

    Vd : LOAD DATA INFILE "C:\MyDocs\data.sql" INTO TABLE Orders;

    Nu mun ch nh file sql nm trn my cc b, c nhn :

    Load Data Local Infile filename.sql Into Table table_name;

    thay th cc dng gi tr trng nhau :

    Load Data Local Infile filename.sql Replace Into Table table_name;

    Tuy nhin, ta cng c th s dng phng thc Load Data ny cho cc file text

    Load Data Infile filename.txt Into Table table_name;

    Cc tu chn c dng thm khi ti d liu t file text (c dng sau t kho

    Fields) :

    Terminated by char

    Enclosed by char

    Escaped by char

    Vd :

    LOAD DATA INFILE "Orders.txt" REPLACE INTO TABLE Orders

    FIELDS TERMINATED BY ',' ENCLOSED BY '"';

    Ch nh cc ct c ti d liu:

    vd : LOAD DATA INFILE "/home/Order.txt"

    INTO TABLE Orders

    (Order_Number, Order_Date, Customer_ID);

  • Gio trnh C s d liu MySQL

    Bin son Nguyn Minh Thnh 23

    3. Import t file Access

    import d liu t mt file Access, ta s dng phn mm Access xut d liu ra

    mt file text trc, ri dng file text ti vo trong MySQL.

    V d ta c mt bng d liu Orders sau trong Access :

    Ta s dng chc nng Export (trong menu File), ca s Export s xut hin :

    Nhp tn file trong filename v click vo nt chn Save Formatted lu gi cc

    nh dng ca d liu.

    Tip theo, ta s chn du phn cch gia cc gi tr (Delimited : phn cch bng du

    phy, Fixed Width : phn cch bng khong trng v c canh l).

  • Gio trnh C s d liu MySQL

    Bin son Nguyn Minh Thnh 24

    Chn ng dn cho file xut

    Kt qu xut nh sau :

    S dng cc tho tc import t file text import file d liu va xut.

  • Gio trnh C s d liu MySQL

    Bin son Nguyn Minh Thnh 25

    II. Export d liu

    1. Chng trnh mysqldump

    export cu trc nh ngha v c d liu trong bng ra file .txt ta dng chng

    trnh mysqldump trong th mc Bin ca MySQL. File kt xut s nm trn server.

    Mysqldump.exe u username p database_name table_name > filename.txt

    vd : mysqldump u root p qlBanHang Customers > C:\Customer.txt

    Sau khi g lnh, ta s nhp password ca ti khon s dng. Nu bn khng mun

    to tng file .txt cho tng bng, m mun lm cho c CSDL, ta s dng lnh

    Mysqldump.exe u username p database_name > filename.txt

    2. Lnh Select Into Outfile

    thc hin kt xut d liu t mt cu truy vn (s trnh by chng sau) ra file

    .txt trn server, ta thc hin cu lnh sau :

    Select column_names from table_name Into Outfile filename.txt

    [ Fields

    Terminated by char

    Enclosed by char

    Line Terminated by char ]

    vd :

    SELECT * FROM Customer INTO OUTFILE 'c:\customers.txt'

    FIELDS TERMINATED BY ',' ENCLOSED BY ' " '

    LINES TERMINATED BY '\r\n';

    Tuy nhin, cu lnh ny s kt xut file trn server.

    3. Chng trnh mysql client

    Hai thao tc trn u kt xut file trn server, nu bn pht trin ng dng trn my

    cc b (local) th my s va ng vai tr l mt server va ng vai tr l mt client. Tuy

    nhin, nu bn lm vic thc s trong mi trng server/client, th vic kt xut ny s lm

    mt thi gian l bn phi chp file v my.

    thc hin thao tc kt xut trc tip ln my client ta s dng chng trnh mysql

    nhng khng ng nhp theo cch chng ta hay lm. Cu lnh thc thi nh sau :

    Mysql.exe e "Select column_names from table_name"

    --skip-column-names \ database_name > filename.txt

    Vd :

    Mysql.exe -e "SELECT * FROM Customers"

    --skip-column-names \ QLBanHang > customers.txt

  • Gio trnh C s d liu MySQL

    Bin son Nguyn Minh Thnh 26

    Bi 5 TRUY VN QUERY

    I. nh ngha

    1. nh ngha

    Truy vn l s dng cc cu lnh ca ngn ng SQL (Structured Query Language

    ngn ng truy vn c cu trc) thc hin vic to (create), thm (insert), chn (select), xo

    (delete) v sa (Update, Alter) d liu, bng, ct, dng

    Cc bi trc chng ta cng s dng mt s cu lnh v to v chnh sa cu trc

    bng, lnh to CSDL v chn d liu bi ny, chng ta s tp chung vo vn truy vn

    d liu, tc l chn, thng k, xo v cp nht d liu.

    2. Cc t kho

    Cc t kho ca ngn ng truy vn SQL

    Create On Limit Into Or

    Alter Order by Drop Where Like

    Insert Join Delete Group by As

    Select Cross Join Update Left Join

    From Right Join Set And

    Cu trc ca mt cu lnh truy vn d liu nh sau :

    SELECT * | column_names

    FROM table_names

    WHERE criterias

    GROUP BY column_names

    ORDER BY column_names

    II. Cc thao tc truy vn d liu

    1. Truy vn chn d liu

    Chn tt c ct trong bng

    SELECT * FROM table_name

    Vd : select * from Customers;

    Chn mt vi ct trong bng

    SELECT column_names FROM table_name

    Vd : select Lastname, Firstname from Customers;

    Chn d liu vi iu kin trch lc d liu

    SELECT * | column_names FROM table_name

    WHERE criteria_1 and|or criteria_2

    Vd : Select * from Customers

    Where Fistname="Thnh" or Fistname="Nam";

  • Gio trnh C s d liu MySQL

    Bin son Nguyn Minh Thnh 27

    Truy vn gn ging vi chui k t, ta s dng k t thay th (%)

    Vd : Select * from Customers

    Where Lastname like ="Nguyn%";

    Gii hn s lng dng kt qu

    SELECT * | column_names FROM table_name

    WHERE criterias LIMIT number?

    Vd : Select * from Customers Limit 5;

    Chn d liu t nhiu bng

    SELECT * | column_names FROM table_name_1, table_name_2

    WHERE table_relationship

    Vd : Select * from Customers C, Orders O

    Where C.Customer_ID = O. Customer_ID;

    Sp xp d liu

    SELECT *|column_names FROM table_names

    WHERE table_relationships and|or criterias

    ORDER BY column_names ASC|DESC

    Vd : Select * from Customers C, Orders O

    Where C.Customer_ID = O. Customer_ID;

    Order by OrderID;

    t tn mi cho ct - bng :

    Table_name AS new_table_name

    Column_name AS new_column_name

    Vd : Select Lastname as HoLot, Firstname as Ten, Order_ID as MaHoaDon

    From Customers As C, Orders As O

    Where C.Customer_ID = O. Customer_ID;

    Order by Order_ID;

    2. Truy vn thng k d liu

    SELECT column_names, Aggregate_function(#column_name)

    FROM table_names

    where table_relationships and|or criterias

    GROUP BY column_names

    Vd : Select Customer_ID, Count(Order_ID)

    From Customers C, Orders O

    Where C.Customer_ID = O.Customer_ID

    Group by Customer_ID;

    Cc Aggregate_function (hm thng k) : Max(), Min(), Sum(), Count(Distinct),

    Avg(), Std().

  • Gio trnh C s d liu MySQL

    Bin son Nguyn Minh Thnh 28

    Mnh Group by trong truy vn thng k c tc dng gom nhm cc d liu ging

    nhau ca cc ct v cc hm thng k s thng k trn cc nhm d liu .

    3. Truy vn lng

    Khi mt truy vn c s dng trong mt truy vn khc, th l cu truy vn lng.

    Cu truy vn bn trong gi l truy vn con (subquery).

    Vd : Select Customer_ID, Max(TongTri) as TongTriLonNhat

    From (Select Customer_ID, Sum(ThanhTien) As TongTri From Orders

    Group by Customer_ID) as A

    Group by Customer_ID;

    4. Mnh Join

    Dng thit lp quan h gia cc bng khi thc hin truy vn.

    Inner Join : cc dng d liu c 2 bng s c gi li lm kt qu. y l mnh

    thng c dng nht.

    Vd : Select Lastname, Firstname, Order_ID

    From Customer as C (INNER) Join Order as O On C.Customer_ID =

    O.Customer_ID;

    Left Join : tr v cc dng d liu ca bng bn tri v cc dng c kt ni bn

    bng bn phi. Nu cc dng bn tri khng c kt ni vi ct no bn phi, th cc ct ca

    bng bn phi s l null.

    Vd : Select Lastname, Firstname, Order_ID

    From Customer as C LEFT Join Order as O On C.Customer_ID =

    O.Customer_ID;

    5. Truy vn chn d liu

    INSERT INTO table_name(column_names) VALUES(column_values)

    Vd : Insert Into Customers(Customer_ID, Firstname, Lastname)

    Values(null, "Thnh","Nguyn Minh");

    6. Truy vn xo d liu

    DELETE FROM table_names WHERE criterias

    Vd : Delete From Customers Where Customer_ID=2;

    7. Truy vn cp nht d liu

    UPDATE table_name SET column_name=value, WHERE criterias

    Vd : Update From Customers Set Firstname="Danh" Where Customer_ID=3;

    III. Ton t & hm

    1. Ton t

    Php cng / tr / nhn / chia : dng tnh ton 2 ct d liu s

    Vd_1 : Select ThanhTien + Thue as TongSoTien From Orders as HoaDon;

    Vd_2 : Select ThanhTien - GiamGia as TongSoTien From Orders as HoaDon;

  • Gio trnh C s d liu MySQL

    Bin son Nguyn Minh Thnh 29

    Vd_3 : Select SoLuong * DonGia as ThanhTien From Orders as HoaDon;

    Php ghp chui :

    Vd : Select Lastname & Firstname as Fullname From Customers;

    Php IN : xc nh mt gi tr c nm trong mt tp hp

    Vd : Select Customer_ID, Lastname, Firstname From Customer

    Where Customer_ID In (Select Customer_ID, Count(Order_ID) From

    Customer C, Order O Where C.Customer_ID = O.Customer_ID

    Group by Customer_ID Order By Count(Order_ID))

    2. Hm ton hc

    Mod (s b chia, s chia) : ly phn d ca php chia.

    Vd : Select Mod(ThanhTien,2) From Orders as HoaDon;

    Round(s, v tr lm trn) : hm lm trn s.

    Vd : Select Round(ThanhTien,1) From Orders as HoaDon;

    V tr lm trn :

    2 : 2 s thp phn

    1 : 1 s thp phn

    0 : 0 s thp phn

    -1 : hng n v

    -2 : hng chc

    3. Hm iu kin

    IF(logic_expression,true_result,false_result) : hm kim tra iu kin ng/sai.

    Vd : Select If(SoLuong>20,5%,2%) as GiamGia From Orders;

    IFNULL(result_1,result_2) : hm tr v kt qu result_1 nu n khng null

    ngc li s tr v result_2.

    Vd : Select IfNull(10/0,1) as Exam

    CASE value WHEN expression THEN result_1 ELSE result_2 : hm tr v

    result_1 khi expression ng, ngc li tr v result_2.

    Vd : Select CASE 1 WHEN Column1="Y" THEN 1 WHEN Column2="Y" THEN

    2 WHEN Column3="Y" THEN 3 ELSE "NONE";

    4. Hm logic

    Cc hm logic c dng trn cc biu thc iu kin (logic_expression).

    AND : php v.

    OR : php hoc.

    NOT : php ph nh.

    Vd : Select * From Orders O, OrderDetails D, Products P

    Where O.Order_ID = D.Order_ID AND D.Product_ID = P. Product_ID;

  • Gio trnh C s d liu MySQL

    Bin son Nguyn Minh Thnh 30

    5. Hm chui

    LTRIM, RTRIM, TRIM : ct b nhng khong trng tha.

    Vd : Select LTRIM(" ABC") "ABC"

    Vd : Select RTRIM("ABC ") "ABC"

    Vd : Select TRIM(" ABC ") "ABC"

    Ta cng c th s dng ct b chui k t c ch nh vi cc t kho (

    SUBSTRING(chui, v tr bt u) : ly mt chui con t v tr bt u

    Vd : Select Substring("Hello World",7) "World"

    LOCATE(chui 1, chui 2, v tr bt u) : xc nh v tr chui 1 trong chui 2

    t v tr bt u.

    Vd : Select Locate("lo","Hello World",1) 4

    REPLACE(chui 1, chui 2, chui 3) : thay th chui 2 bng chui 3 trong

    chui 1.

    Vd : select Replace("Filename.xxx","xxx","123") "Filename.123"

    UCASE, LCASE : chuyn ch thng thnh in hoa v ngc li.

    REVERSE : o ngc chui.

    6. Hm thi gian

    MONTHNAME(date) : tr v tn thng ca date.

    Vd : Select monthname(20000105) January

    DAYOFYEAR(date) : tr v s ngy tnh t u nm n date.

    Vd : Select dayofyear(20000201) 32

    Vd : Select dayofyear("2000/02/01") 32

    DAYOFMONTH(date) : tr v s ngy tnh t u thng n date.

    Vd : Select dayofyear(20000201) 1

    Vd : Select dayofyear("2000/02/01") 1

    Vd : Select dayofyear("00-02-01") 1

    DAYOFWEEK(date) : tr v s ngy tnh t u tun n date.

    Vd : Select dayofyear(20000201) 3

    Vd : Select dayofyear("0-2-1") 3

    YEAR(date) : tr v nm ca date.

    QUARTER(date) : tr v qu ca date.

    MONTH(date) : tr v thng ca date.

    DAY(date) : tr v ngy ca date.

    WEEK(date) : tr v s tun ca date tnh t ngy u tin ca nm.

    YEARWEEK(date) : tr v s tun ca date tnh t ngy u tin ca nm.

    NOW() , SYSDATE(), CURRENT_TIMESTAMP : tr v ngy gi h thng.

    Vd : Select NOW() 2009-10-28 18:05:11

  • Gio trnh C s d liu MySQL

    Bin son Nguyn Minh Thnh 31

    Vd : Select NOW()+0 20091028180511

    CURDATE() , CURRENT_DATE : tr v ngy ca h thng.

    CURTIME() , CURRENT_TIME : tr v ngy ca h thng.

    HOUR(time) : tr v gi ca time.

    MINUTE(time) : tr v pht ca time.

    SECOND(time) : tr v giy ca time.

    DATE_FORMAT(date,format) : nh dng date theo format.

    TIME_FORMAT(time,format) : nh dng date theo format.

    Bng tham s nh dng

    %r 12-gi (hh:mm:ss AM|PM)

    %T 24-gi (hh:mm:ss AM|PM)

    %Y nh dng nm, 4 ch s

    %y nh dng nm, 2 ch s

    %m Gn s 0 vo u thng (01, 02)

    %c B s 0 u thng (1, 2)

    %M Tn thng (y )

    %b Tn thng (vit tt)

    %D Ngy (1st, 2

    nd)

    %d Ngy c s 0 u (01, 02, 03)

    %e Ngy khng c s 0 u (1, 2, 3)

    %W Ngy trong tun (Sunday, Monday)

    %a Ngy trong tun (vit tt)

    %H Gi (00, 0123)

    %k Gi (0, 1, 2,23)

    %h Gi (01, 02, 12)

    %L Gi (01, 02, 12)

    %I Gi (1, 2, 12)

    %i Pht (00, 01,59)

    %S Giy (00, 01,59)

    %s Giy (00, 01,59)

    %P AM | PM

    %U S tun trong nm (vi Sunday l ngy u tin)

    $u S tun trong nm (vi Monday l ngy u tin)

    %X&%V Nm v s tun (vi Sunday l ngy u tin)

    %x&%v Nm v s tun (vi Month l ngy u tin)

    %j S ngy trong nm (001, 002,366)

    %w S ngy trong tun (0 = Sunday, 1 = Monday)

    %% nh dng %

    Vd : Select Date_Format("1999-12-31 23:00:00","%r on % W")

    kt qu : 11:00:00 PM on Friday

    TO_DAYS(date) : tr v s ngy t ngy u tin sau Cng Nguyn n date.

    Vd : TO_DAYS("2000-01-01") 730485

  • Gio trnh C s d liu MySQL

    Bin son Nguyn Minh Thnh 32

    FROM_DAYS(number) : tr v ngy sau Cng Nguyn number ngy.

    Vd : FROM_DAYS(1000000) 2737-11-28

    TIME_TO_SEC(time) : tr v thi gian tnh bng giy t 0:00:00 n time.

    SEC_TO_TIME(seconds) : tr v thi im sau 0:00:00 mt khong thi gian l

    seconds giy.

    DATE_ADD(date, INTERVAL expression type) : cng thm mt khong thi

    gian vo date.

    ADDDATE(date, INTERVAL expression type) : cng thm mt khong thi

    gian vo date.

    DATE_SUB(date, INTERVAL expression type) : bt i mt khong thi gian

    trong date.

    SUBDATE(date, INTERVAL expression type) : bt i mt khong thi gian

    trong date.

    Vd: ADDDATE("1980-07-04",INTERVAL 15 YEAR) 1995-07-04

    Vd: SUBDATE(20001201, INTERVAL "10 4" YEAR_MONTH) 1990-08-01

    Bng cc gi tr Type trong cng thc

    Type nh dng d liu

    SECOND Giy

    MINUTE Pht

    HOUR Gi

    DAY Ngy

    MONTH Thng

    YEAR Nm

    MINUTE_SECOND "Pht : Giy"

    HOUR_MINUTE "Gi : Pht"

    DAY_HOUR "Ngy Gi"

    YEAR_MONTH "Nm Thng"

    HOUR_SECOND "Gi : Pht : Giy"

    DAY_MINUTE "Ngy Gi : Pht"

    DAY_SECOND "Ngy Gi : Pht : Giy"

  • Gio trnh C s d liu MySQL

    Bin son Nguyn Minh Thnh 33

    Bi 6 FUNCTION PROCEDURE

    TRIGGER

    I. Function

    Function l phng thc dng nh ngha ra cc hm mi do chnh ngi qun tr

    CSDL to ra. Function dng to ra cc hm tng t nh nhng hm c trnh by

    trn (NOW(), TRIM()), Function s c nh ngha bi mt chng trnh (cc dng lnh

    ca MySQL ghp li, mi dng lnh s c kt thc bng du ;) thc hin mt yu cu

    tnh ton no v tr v mt gi tr (kt qu) m ta cn. to c Function bn cn c

    quyn CREATE ROUTINE.

    Vd : to hm tnh gi tr tui trung bnh ca khch hng trong Customers.

    1. Cu trc to Function

    CREATE FUNCTION function_name(arguments)

    RETURNS datatype characteristics

    BEGIN

    RETURN value;

    END

    Trong :

    Function_name : tn function mun to

    Arguments : danh sch cc tham s cn thit cho function

    Datatype : kiu d liu m function tr v

    Characteristics : cc c trng ca function

    DETERMINISTIC, NO SQL, READS SQL DATA

    RETURN : kt qu tr v ca function.

    Vd : to hm tnh gi tr tui trung bnh ca khch hng trong Customers.

    CREATE FUNCTION TuoiTB()

    RETURNS Float Reads SQL Data

    BEGIN

    RETURN select avg(Year(Birthday)) From Customer;

    END

    2. S dng Function

    Ta c th dng Function mi to nh cc function c sn ca MySQL ( bi 3).

    Function s c lu li trong chnh CSDL ang s dng. Tuy nhin, chng ta khng th lit

    k danh sch cc Function, v vy chng ta phi nh cc Function to.

    Vd : Select TuoiTB();

  • Gio trnh C s d liu MySQL

    Bin son Nguyn Minh Thnh 34

    II. Procedure

    Procedure l phng thc dng to ra cc hm x l tuy nhin Procedure khng tr

    v gi tr (kt qu) nh Function. Procedure thng c dng thao tc ln CSDL nhiu

    hn. i vi Function, ta khng th chnh sa d liu, nh i vi Procedure ta c th to ra

    cc hm dng chnh sa, xod liu. Kt qu ca Procedute khng phi l mt gi tr

    nh Function, m l mt b gi tr (kt qu ca mt truy vn)

    1. Cu trc to Procedure

    CREATE PROCEDURE procedure_name(IN | OUT | INOUT arguments)

    BEGIN

    .

    END

    Trong :

    Procedure_name : tn Procedure mun to

    Arguments : cc tham s cn thit cho procedure, cc tham s phi s

    dng cc t kho In, Out, InOut ch nh loi argument (In tham s

    ch cho gi tr vo, Out tham s ch cho gi tr ra, InOut tham s cho

    gi tr vo v ra).

    Vd : lit k danh sch cc khch hng theo phi.

    CREATE PROCEDURE GetCustomersByGender(IN gen varchar(1))

    BEGIN

    Select * From Customer Where Gender = gen;

    END

    2. S dng Procedure

    thc thi mt Procedure c to ta s dng t kho Call nh sau :

    CALL procedure_name(argument_values);

    Vd : Call GetCustomersByGender(F);

    III. Trigger

    Trigger l phng thc to cc rng buc cho CSDL khi CSDL c s thay i nh

    thm, xo, sa d liu. Cc trigger s t ng c CSDL thc thi khi nhng hnh ng thay

    i xy ra.

    Cu trc to Trigger :

    CREATE TRIGGER trigger_name

    [BEFORE | AFTER] [INSERT | UPDATE | DELETE]

    FOR EACH ROW

    BEGIN

    END

  • Gio trnh C s d liu MySQL

    Bin son Nguyn Minh Thnh 35

    Trong :

    Trigger_name : tn trigger.

    Before | After : thao tc trc hay sau.

    Insert | Update | Delete : thao tc khi chn mi | cp nht | xo

    o NEW : ch nh dng mi c chn, cp nht.

    o OLD : ch nh dng mi c xo.

    Vd: Trong bng Customers c mt ct AGE (tui), ct ny khng nhp nhng s c

    t tnh t ct Birthday khi chn thng tin mt khch hng mi

    CREATE TRIGGER Update_Age_Customer AFTER INSERT

    FOR EACH ROW

    BEGIN

    Update Customers Set Age = Year(NEW.Birthday)

    WHERE Customer_ID = New.Customer_ID;

    END

  • Gio trnh C s d liu MySQL

    Bin son Nguyn Minh Thnh 36

    Bi 7 BO MT V QUN TR

    I. Bo mt CSDL

    1. Phng thc bo mt trong MySQL

    Nh trnh by trong bi 2 (phn II.4), MySQL c mt CSDL c bit c tn

    mysql. Trong CSDL ny c cc bng cha thng tin v server, c bit chng ta bit n 3

    bng l db,host, user. Ngoi ra cn cc bng nh func, column_priv, tables_priv miu t v

    quyn hn v chc nng ca cc ti khon ngi dng. Ngi duy nht c truy cp vo cc

    bng ny l ngi qun tr CSDL hay ngi nm ti khon root. Tt c cc d liu trn server

    c lu tr trong th mc data (c cha trong th mc ci t mysql).

    2. To ti khon ngi dng

    Ti khon vi tt c cc quyn truy cp trn my cc b.

    GRANT ALL PRIVILEGES ON *.* | database.table

    TO username@localhost IDENTIFIED BY "password"

    Vd : GRANT ALL PRIVILEDES ON *.*

    TO inzaghithanh@localhost IDENTIFIED BY "9967722";

    Ti khon vi tt c cc quyn truy cp t xa.

    GRANT ALL PRIVILEGES ON *.* | database.table

    TO username@host IDENTIFIED BY "password"

    Vd : GRANT ALL PRIVILEDES ON *.*

    TO inzaghithanh@"saoviet.edu.vn" IDENTIFIED BY "9967722";

    Vd : GRANT ALL PRIVILEDES ON *.*

    TO inzaghithanh@"%" IDENTIFIED BY "9967722";

    Vd : GRANT ALL PRIVILEDES ON *.*

    TO inzaghithanh@"203.162.4.190" IDENTIFIED BY "9967722";

    3. Xo ti khon ngi dng

    Xo ti khon trong bng user

    DELETE FROM user WHERE user="value" and host="value"

    Vd : DELETE FROM user WHERE user="inzaghithanh" and host="localhost";

    4. Cp quyn & xo quyn cho ti khon ngi dng

    Cp quyn :

    GRANT privilege_list ON *.* | database.table

    TO username@host IDENTIFIED BY "password"

    Vd : GRANT select, insert On *.*

    TO khoadanh@localhost IDENTIFIED BY "9967722";

    Xo quyn :

    REVOKE ALL | PRIVILEGE_TYPE

  • Gio trnh C s d liu MySQL

    Bin son Nguyn Minh Thnh 37

    ON *.* | Database.Table From user@host

    Vd : REVOKE ALL ON *.* From khoadanh@localhost

    II. Qun tr

    1. Backup (sao lu) d liu

    sao lu li c s d liu, ta s dng thao tc Export d liu (bi 4 phn II) ra mt

    file text hay file sql v lu tr li.

    2. Restore (phc hi) d liu

    phc hi li c s d liu, ta s dng thao tc Import d liu (bi 4 phn I) t mt

    file text hay file sql a d liu vo.

    3. Cc hm trn h thng ca MySQL

    DATABASE() : tr v CSDL ang s dng.

    USER(), SYSTEM_USER(), SESSION_USER() : tr v ti khon ang s

    dng.

    VERSION() : xem thng tin phin bn MySQL

    PASSWORD(string) : tr v gi tr string c m ho.

    4. S dng mysqladmin

    Chng trnh mysqladmin cha cc lnh qun l CSDL MySQL, chng trnh

    c thc thi ch dng lnh

    Cc lnh ca mysqladmin

    Create database_name : to CSDL vi tn ch nh.

    Drop database_name : xo CSDL vi tn ch nh.

    Status : xem tnh trng server.

    Version : xem phin bn MySQL.

    Extended-status : xem thng tin m rng ca server.

    Variables : xem cc bin thng s c gi tr.

    Processlist : xem cc kt ni n server.

    Password new_password : thay i password.

    Flush-hosts : lu tt c cc host trong b m.

    Flush-logs : lu tt c cc nht k.

    Flush-tables : lu tt c cc bng.

    Flush-privileges : cp nht li cc bng cp quyn.

    Kill id1, id2 : tt cc lung x l ca MySQL.

    Ping : gi tnh hiu n MySQL kim tra cc hm.

    Reload : ti li cc quyn t cc bng lu tr quyn.

    Refresh : lu li tt c cc bng quyn, ng li v m cc file nht k.

    Shutdown : tt server.

  • Gio trnh C s d liu MySQL

    Bin son Nguyn Minh Thnh 38

    Cc tham s ca mysqladmin

    -#, --debug : xut ra file nht k kim tra.

    -f, --force : xo

    -u, --user=username : ch nh ti khon s dng mysqladmin.

    -p, --password[=password] : ch nh password s dng mysqladmin.

    -h, --host=hostname : kt ni n mt host ch nh.

    -C, --compress : s dng giao thc nn gia client/server.

    -P, --port[=portname] : ch nh port cho kt ni.

    -?, --help : hng dn, gip .

    -V, --version : xem phin bn v thot.

    Vd : mysqladmin u root p create qlBanhang

    Vd : mysqladmin status

  • Gio trnh C s d liu MySQL

    Bin son Nguyn Minh Thnh 39

    Bi 8 S DNG CNG C GIAO DIN

    HO - MySQL GUI TOOLS

    I. Gii thiu:

    MySQL GUI Tool l b cng c vi giao din ho thn thin, gip ngi s d dng

    thao tc trn MySQL hn so vi thao tc bng giao din cu lnh.

    Ta c th ti MySQL GUI Tools trn trang web chnh thc ca MySQL

    (http://dev.mysql.com/downloads/gui-tools/5.0.html). B cng c bao gm 3 cng c chnh :

    MySQL Administrator : cng c qun tr MySQL Server.

    MySQL Query Browser : cng c thc hin cc cu truy vn trc quan.

    MySQL Migration Toolkit : cng c cho php chuyn i gia cc h CSDL

    khc nhau.

    II. MySQL Administrator

    Sau khi ci t MySQL GUI Tools, ta vo :

    Start Program MySQL MySQL Administrators

    Mn hnh ng nhp h thng server :

    Server Host : nhp a ch MySQL server mun ng nhp

    Username : ti khon ng nhp.

    Password : mt khu ng nhp.

    Mn hnh chnh ca chng trnh :

    Server Information : thng tin v server.

    Service Control : qun l dch v MySQL chy trn Server.

    Startup Variables : qun l cc bin thng s t chy.

    User Administrator : qun l ti khon ngi dng.

  • Gio trnh C s d liu MySQL

    Bin son Nguyn Minh Thnh 40

    Server Connections : qun l cc kt ni trn n server.

    Health : qun l ti nguyn trn server.

    Server Logs : nht k ghi nhn trn server.

    Replication Status : qun l tnh trng tr li t server cho cc client.

    Backup : thc hin sao lu d liu.

    Restore : thc hin phc hi d liu.

    Catalogs : danh mc cc CSDL trn server.

    III. MySQL Query Browser

    Sau khi ci t MySQL GUI Tools, ta vo :

    Start Program MySQL MySQL Administrators

    Mn hnh ng nhp h thng server :

  • Gio trnh C s d liu MySQL

    Bin son Nguyn Minh Thnh 41

    Server Host : nhp a ch MySQL server mun ng nhp

    Username : ti khon ng nhp.

    Password : mt khu ng nhp.

    Default Schema : C s d liu s dng mc nh.

    Mn hnh chnh ca chng trnh :

    s dng, ta nhp cu truy vn vo textbox pha trn v nhn phm Execute thc

    thi. Ngoi ra, ta cng c th chn Procedure ca s Schemata thc thi cu Procedure .

  • Gio trnh C s d liu MySQL

    Bin son Nguyn Minh Thnh 42

    Bi Tp Bi Tp Thc Hnh S 1

    1. Ci t MySQL 5.1 trn Windows vi cc yu cu sau :

    - Chn th mc cha d liu MySQL Datafiles trong th mc gc ci t MySQL.

    - Thit lp password cho root@localhost l : saovietadmin.

    2. S dng MySQL Command Line Client, ng nhp Server vi ti khon root.

    Xem cc CSDL hin c trn Server.

    Xem tnh trng ca Server.

    i password ca root@localhost l : saoviet.

    Thot khi chng trnh.

    3. S dng cmd (command line) ca Windows ng nhp MySQL Server vi ti khon

    root.

    To CSDL mi c tn l : Test.

    S dng CSDL va to.

    Xo CSDL va to.

    Bi Tp Thc Hnh S 2 : To Bng

    1. To CSDL c tn : QuanLyBanHang.

    2. S dng CSDL QuanLyBanHang, to cc bng sau :

    Bng TinhTP

    Tn ct Kiu d liu Thuc tnh

    MaTP CHAR(2) Primary key, Not Null

    TenTP VARCHAR(30) Not Null, Default ' '

    Bng DonVi

    Tn ct Kiu d liu Thuc tnh

    MaDV CHAR(2) Primary key, Not Null

    TenDV VARCHAR(30) Not Null, Unique

    Bng NhaCungCap

    Tn ct Kiu d liu Thuc tnh

    MaNCC INT Primary key, Not Null, Auto_Increment

    TenNCC VARCHAR(30) Not Null, Unique

    DiaChi VARCHAR(60) Not Null, Default ' '

    DienThoai VARCHAR(11) Not Null, Default ' '

    Bng MatHang

    Tn ct Kiu d liu Thuc tnh

    MaMH INT Primary key, Not Null, Auto_Increment

  • Gio trnh C s d liu MySQL

    Bin son Nguyn Minh Thnh 43

    TenMH VARCHAR(30) Not Null, Unique

    DonViTinh VARCHAR(20) Not Null, Default ' '

    DonGia INT Not Null, Unsigned, Default 0

    HinhSP TEXT Null

    MaNCC INT Not Null

    Bng KhachHang

    Tn ct Kiu d liu Thuc tnh

    MaKH VARCHAR(10) Primary key, Not Null

    TenKH VARCHAR(40) Not Null, Unique, Default ' '

    DiaChi VARCHAR(60) Not Null, Default ' '

    DienThoai VARCHAR(11) Not Null, Default ' '

    MaTP CHAR(2) Not Null

    Bng NhanVien

    Tn ct Kiu d liu Thuc tnh

    MaNV INT Primary key, Not Null, Auto_Increment

    HoLot VARCHAR(30) Not Null, Default ' '

    TenNV VARCHAR(20) Not Null, Default ' '

    Phai ENUM('F','M') Not Null, Default 'F'

    LuongCB INT Not Null, Unsigned, Default 0

    NgayNViec DATE Not Null

    DiaChiNV VARCHAR(60) Not Null, Default ' '

    DienThoaiNV VARCHAR(11) Null, Default ' '

    HinhNV TEXT Null

    Bng DonDatHang

    Tn ct Kiu d liu Thuc tnh

    MaDDH CHAR(5) Primary key, Not Null

    MaKH VARCHAR(10) Not Null

    MaNV INT Not Null

    NgayDH DATE Not Null

    NgayGiao DATE

    Bng ChiTietDDH

    Tn ct Kiu d liu Thuc tnh

    MaDDH CHAR(5) Primary key, Not Null

    MaMH INT Primary key, Not Null

    SoLuong INT Not Null

    GiamGia DATE Default 0

    Bng ChamCong

  • Gio trnh C s d liu MySQL

    Bin son Nguyn Minh Thnh 44

    Tn ct Kiu d liu Thuc tnh

    Thang TINYINT Primary key, Not Null, Unsigned

    Nam SMALLINT Primary key, Not Null, Unsigned

    MaNV INT Primary key, Not Null

    NgayCong TINYINT Default 0

    3. M hnh quan h gia cc bng :

    4. Thc hin mt s cu lnh trn bng :

    Xem danh sch cc bng ca CSDL QuanlyBanHang.

    Xem danh sch ca cc ct trong tng bng.

    Xem cu trc ca tng bng.

    5. Nhp d liu vo bng

    Bng TinhTP

    Bng DonVi

  • Gio trnh C s d liu MySQL

    Bin son Nguyn Minh Thnh 45

    Bng NhaCungCap

    Bng MatHang

    Bng KhachHang

    Bang NhanVien (Import t file NhanVien.txt)

    Bang ChamCong (Import t file ChamCong.txt)

    Bng DonDatHang (Import t file DonDatHang.txt)

    Bng ChiTietDDH (Import t file ChiTietDDH.txt)

    Bi Tp Thc Hnh S 3 : Truy vn

    Thc hin cc cu truy vn chn sau :

    1. Lit k danh sch cc mt hng, bao gm cc thng tin sau : MaMH, TenMH,

    DonViTinh, DonGia.

    2. Lit k cc mt hng c n gi >=62 thng tin gm cc ct sau: MaMH, TenMH,

    DonViTinh, DonGia.

    3. Lit k cc mt hng m nh cung cp c MaNCC l 4 hoc 2 thng tin gm cc ct

    sau: MaNCC, TenNCC, MaMH, TenMH, DonViTinh, DonGia.

  • Gio trnh C s d liu MySQL

    Bin son Nguyn Minh Thnh 46

    4. Lit k cc mt hng m nh cung cp c a ch ti TP.HCM thng tin gm cc ct

    sau: MaNCC, TenNCC, DiaChi, MaMH, TenMH, DonViTinh, DonGia.

    5. Lit k cc n hng trong khong thi gian t ngy 01/01/1997 n ngy

    31/01/1997 thng tin gm cc trng sau: MaDDH, NgayDH, MaKH, TenKH,

    MaMH, TenMH, SoLuong.

    6. Lit k cc n hng trong thng 2 nm 1997 thng tin gm cc trng sau : MaDDH,

    NgayDH, NgayGiao, MaNV, HoTenNV.

    7. Lit k cc n t hng giao trc ngy 01/03/1997 v nhn vin phc v c MaNV

    l 4 hay 5. Thng tin gm cc trng sau : MaDDH, NgayDH, MaKH, TenKH,

    MaNV, HoTenNV.

    8. Lit k cc nhn vin thuc phi Nam. Thng tin gm cc trng sau: MaNV, HoLot,

    TenNV, DiaChi, NgayNViec, Phai

    9. Lit k cc nhn vin nhn vic t ngy 01/07/1991 n ngy 30/11/1992 v lng c

    bn

  • Gio trnh C s d liu MySQL

    Bin son Nguyn Minh Thnh 47

    21. Tnh tng thnh tin ca tng khch hng trong tng nm. Thng tin gm : Nam,

    MaKH, TenKH, TongTien

    22. Lit k danh sch khch hng c tng tr t hng nhiu nht trong nm 1998. thng

    tin gm : MaKH, TenKH, Nam, TongTienMax.

    23. Lit k danh sch cc mt hng c tng gi tr t hng nhiu nht trong 1997 thng

    tin gm : MaMH, TenMH, Nam, TongTienMax

    24. Cui nm cng ty mun tng lch cho khch hng vi quy nh nh sau: 1 n t

    hng 2 cun lch. To truy vn gm cc thng tin sau: MaKH, TenKH, DiaChi,

    SoDDH, SoLichTang.

    25. Thng k tng lng nhn vin ca tng n v trong nm 1999. thng tin gm :

    MaDV, TenDV, Thang, TongLuong.

    26. Li t k n v c tng lng trong nm 1999. ln nh t thng tin gm : MaDV,

    TenDV, Thang, TongLuongMax.

    Thc hin cc cu truy vn hnh ng sau :

    27. To Table LuuDDH gm cc n t hng trong nm 1997,1998 v 1999 gm cc

    trng sau : MaDDH, MaKH, NgayDH, NgayGiao, MaMH, SoLuong, GiamGia.

    28. Xa Trong Table LuuDDH cc n t hng 1997

    29. Tng 2% gim gi trong Table LuuDDH cho cc n t hng trong nm 1999

    30. To table Luong9901 (bng lng thng 1 nm 1999) gm cc Field sau: Thang, Nam,

    MaNV, HoTen, LuongCB, NgayCong, Luong. (bit lng = Lung cn bn * Ngy

    cng)

    31. Tng lng cc nhn vin trong bng lng Luong9901 tng 10% i vi cc nhn

    vin Nam v 20% i vi cc nhn vin n v iu kin l c ngy cng > 20 ngy.

    32. Lit k cc mt hng khng c t hng trong khong thi gian [t 1/5/1997] [n

    1/5/1998]

    Bi Tp Thc Hnh S 3 : Function Procedure - Trigger

    1. To Function tnh tng doanh thu ca nm 1997.

    2. To Function tnh tng doanh thu ca mt nm (c truyn tham s).

    3. To Function tnh tng thnh tin ca mt n t hng (c truyn tham s).

    4. To Function tnh tng doanh thu ca mt nhn vin (c truyn tham s).

    5. Thc hin li cc cu truy vn trn vi Procedure.

    6. To Procedure lit k danh sch nhn vin theo phi (c truyn tham s).

    7. To Procedure lit k danh sch mt hng theo n t hng (c truyn tham s).

    8. To Procedure lit k danh sch mt hng theo n t hng (c truyn tham s).

    9. To Procedure lit k danh sch mt hng theo nh cung cp (c truyn tham s).

    10. To Procedure lit k danh sch n t hng c t t ngy A n ngy B (A, B

    c truyn tham s).

  • Gio trnh C s d liu MySQL

    Bin son Nguyn Minh Thnh 48

    Bi Tp Thc Hnh S 4 : Bo Mt

    1. To ti khon mi (ch truy cp trn local) vi y quyn c

    tn : saoviet

    mt khu : saoviet.

    2. Thc hin ng nhp vi ti khon saoviet.

    3. Thc hin cho php ti khon saoviet c th truy cp bt k u.

    4. To ti khon mi (ch truy cp trn local) ch c quyn chn (select) :

    tn : abc

    mt khu : 123456.

    5. ng nhp vi ti khon abc. V thc hin chn, chn d liu vo bng MatHang.

    6. ng nhp vi ti khon root, xo ti khon abc.

    7. S dng cc hm h thng xem thng tin Server.

    8. Thc hin Backup ton b d liu ca QuanLyBanHang ra file QuanLyBanHang.sql