6_Tables of a Database

download 6_Tables of a Database

of 30

Transcript of 6_Tables of a Database

  • 7/25/2019 6_Tables of a Database

    1/30

    TABLE OF ADATABASE

    RAYMOND S. BERMUDEZ

    Instructor, College of Computer Studies - MSEUF

    LESSON 5

  • 7/25/2019 6_Tables of a Database

    2/30

    BY: Mr. RAYM!" S. BERMU"E#, - Instructor, College of Computer Studies, MSEUF $ %ucen& Cit'

    A t&(le is prim&ril' & list of items or &group of lists. )o m&n&ge suc* & list, its*ould (e meticulousl' org&ni+ed. )oorg&ni+e t*is inform&tion, it is diided insections. ere is &n e&mple:

    )&(le of & d&t&(&se

  • 7/25/2019 6_Tables of a Database

    3/30

    BY: Mr. RAYM!" S. BERMU"E#, - Instructor, College of Computer Studies, MSEUF $ %ucen& Cit'

    )o complete t*e cre&tion of & t&(le, 'oumust s&e it. If 'ou &re fres*l' cre&ting& t&(le &nd decide to s&e it, 'ou /ould(e prompted to n&me it. )*e n&me of &t&(le:

    C&n (e m&de of digits onl'. Fore&mple 'ou c&n *&e & t&(le

    c&lled 012C&n st&rt /it* & digit, & letter, or

    &n underscore

    C&n (e m&de of letters, digits, &ndsp&ces

    )&(le !&me

  • 7/25/2019 6_Tables of a Database

    4/30

    BY: Mr. RAYM!" S. BERMU"E#, - Instructor, College of Computer Studies, MSEUF $ %ucen& Cit'

    Besides t*ese rules, 'ou c&n m&3e up'ours. )o &oid confusion, *ere &re t*erules /e /ill use to n&me our t&(les:A n&me /ill st&rt /it* & letter. E&mples &re

    actor Second

    After t*e 4rst c*&r&cter &s &n underscore or& letter, t*e n&me /ill *&e com(in&tions ofunderscores, letters, &nd digits. E&mples

    &re _n24, act_52_tUnless st&ted ot*er/ise, & n&me /ill not

    include speci&l c*&r&cters suc* &s 5, 6, 7, 8,9, , ;, or ,D2 to >,DD > ('tes

    int ->0 to >0 -0 1 ('tes

    (igint -> to > -0 2 ('tes

    decim&l@p,snumeric@p,s

    -0?2 to 0?2 -0 to 0D ('tes

    sm&llmone' ->01,D12.12 to >01,D12.1D 1 ('tes

    mone' ->>,D,>?,2,1DD.2?2 to>>,D,>?,2,1DD.2?D

    2 ('tes

    re&l -.12 to -0.022 , ?, &nd 0.022

    to .121 ('tes

    Flo&t -0.D?2 to ->.>?2 , ?, &nd>.>?2 to 0.D?2

    1 ('tes or 2('tes

  • 7/25/2019 6_Tables of a Database

    8/30BY: Mr. RAYM!" S. BERMU"E#, - Instructor, College of Computer Studies, MSEUF $ %ucen& Cit'

    *+aacte data t%&e

    c*&r@n - 0 ('te per c*&r&cterde4ned (' n up to & m&imum of

    2??? ('tes

    &rc*&r@n -0 ('te per c*&r&cterstored up to & m&imum of 2???('tes

    "&t& )'pe

  • 7/25/2019 6_Tables of a Database

    9/30BY: Mr. RAYM!" S. BERMU"E#, - Instructor, College of Computer Studies, MSEUF $ %ucen& Cit'

    *+aacte data t%&e

    tet -0 ('te per c*&r&cter stored upto & m&imum of > GB

    nc*&r@n -> ('tes per c*&r&cterde4ned (' n up to & m&imum of1??? ('tes

    "&t& )'pe

  • 7/25/2019 6_Tables of a Database

    10/30BY: Mr. RAYM!" S. BERMU"E#, - Instructor, College of Computer Studies, MSEUF $ %ucen& Cit'

    *+aacte data t%&e

    n&rc*&r@n- > ('tes per c*&r&ctersstored up to & m&imum of 1???

    ('tes

    ntet- > ('tes per c*&r&cter storedup to & m&imum of > GB

    "&t& )'pe

  • 7/25/2019 6_Tables of a Database

    11/30BY: Mr. RAYM!" S. BERMU"E#, - Instructor, College of Computer Studies, MSEUF $ %ucen& Cit'

    *+aacte data t%&e

    sm&lld&tetime ?0H?0H0??to ?H?H>?D

    d&tetime ?0H?0H0D to

    0>H0H

    d&te ?0H?0H???0 to

    0>H0H

    time ??:??:??.???????

    to >::.

    "&t& )'pe

  • 7/25/2019 6_Tables of a Database

    12/30BY: Mr. RAYM!" S. BERMU"E#, - Instructor, College of Computer Studies, MSEUF $ %ucen& Cit'

    B#na% Data

    (it !ull, ? &nd 0 0 (it

    "&t& )'pe

    )* CREA)E )AB%E

  • 7/25/2019 6_Tables of a Database

    13/30BY: Mr. RAYM!" S. BERMU"E#, - Instructor, College of Computer Studies, MSEUF $ %ucen& Cit'

    )*e CREA)E )AB%E st&tement isused to cre&te & t&(le in & d&t&(&se.

    CREATE TABLE table_name(column_name1 data_type,column_name2 data_type,

    column_name3 data_type,....)

    )*e CREA)E )AB%ESt&tement

    )* CREA)E )AB%E

  • 7/25/2019 6_Tables of a Database

    14/30BY: Mr. RAYM!" S. BERMU"E#, - Instructor, College of Computer Studies, MSEUF $ %ucen& Cit'

    Example

    *REATE TABLE ,e!on!

    -,_d #nt/La!tName 0ac+a-2551/F#!tName 0ac+a-2551/

    Adde!! 0ac+a-2551/*#t% 0ac+a-25511

    )*e CREA)E )AB%ESt&tement

  • 7/25/2019 6_Tables of a Database

    15/30BY: Mr. RAYM!" S. BERMU"E#, - Instructor, College of Computer Studies, MSEUF $ %ucen& Cit'

    )*e !) !U%% constr&int enforces &column to !) &ccept !U%% &lues.

    )*e !) !U%% constr&int enforces &4eld to &l/&'s cont&in & &lue. )*isme&ns t*&t 'ou c&nnot insert & ne/record, or upd&te & record /it*out

    &dding & &lue to t*is 4eld.

    S=% !) !U%% Constr&int

  • 7/25/2019 6_Tables of a Database

    16/30BY: Mr. RAYM!" S. BERMU"E#, - Instructor, College of Computer Studies, MSEUF $ %ucen& Cit'

    CREA)E )AB%E ersons@JId int NOT NULL,

    %&st!&me &rc*&r@> NOTNULL,First!&me &rc*&r@>,Address &rc*&r@>,Cit' &rc*&r@>

    S=% !) !U%% Constr&int

  • 7/25/2019 6_Tables of a Database

    17/30

    BY: Mr. RAYM!" S. BERMU"E#, - Instructor, College of Computer Studies, MSEUF $ %ucen& Cit'

    )*e RIMARY KEY constr&intuniLuel' identi4es e&c* record in &d&t&(&se t&(le.

    rim&r' 3e's must cont&in uniLue&lues.

    A prim&r' 3e' column c&nnotcont&in !U%% &lues.

    E&c* t&(le s*ould *&e & prim&r'

    3e', &nd e&c* t&(le c&n *&e onl'!E rim&r 3e .

    S=% RIMARY KEY Constr&int

  • 7/25/2019 6_Tables of a Database

    18/30

    BY: Mr. RAYM!" S. BERMU"E#, - Instructor, College of Computer Studies, MSEUF $ %ucen& Cit'

    CREA)E )AB%E ersons@

    JId int !) !U%% ,RMARY EY,%&st!&me &rc*&r@> !) !U%%,First!&me &rc*&r@>,Address &rc*&r@>,

    Cit' &rc*&r@>

    S=% RIMARY KEY Constr&int

    S=% RIMARY KEY C i

  • 7/25/2019 6_Tables of a Database

    19/30

    BY: Mr. RAYM!" S. BERMU"E#, - Instructor, College of Computer Studies, MSEUF $ %ucen& Cit'

    )o cre&te & RIMARY KEY constr&inton t*e JId column /*en t*e t&(leis &lre&d' cre&ted, use t*e follo/ing

    S=%:

    ALTER TABLE ,e!on!ADD ,RMARY EY -,_d1

    S=% RIMARY KEY Constr&inton A%)ER )AB%E

    "R RIMARY KEY

  • 7/25/2019 6_Tables of a Database

    20/30

    BY: Mr. RAYM!" S. BERMU"E#, - Instructor, College of Computer Studies, MSEUF $ %ucen& Cit'

    )o drop & RIMARY KEY constr&int,use t*e follo/ing S=%:

    ALTER TABLE ,e!on!DRO, ,RMARY EY

    "R & RIMARY KEYConstr&int

  • 7/25/2019 6_Tables of a Database

    21/30

    BY: Mr. RAYM!" S. BERMU"E#, - Instructor, College of Computer Studies, MSEUF $ %ucen& Cit'

    )*e "R )AB%E st&tement is usedto delete & t&(le.

    DRO, TABLE tab$e_name

    )*e "R )AB%E St&tement

    T+ ALTER TABLE

  • 7/25/2019 6_Tables of a Database

    22/30

    BY: Mr. RAYM!" S. BERMU"E#, - Instructor, College of Computer Studies, MSEUF $ %ucen& Cit'

    )*e A%)ER )AB%E st&tement is usedto &dd, delete, or modif' columns in&n eisting t&(le.

    S3L ALTER TABLE S%nta

    )o &dd & column in & t&(le, use t*efollo/ing s'nt&:

    ALTER TABLE tab$e_nameADD co$'mn_name datat%&e

    T+e ALTER TABLEStatement

    T+ ALTER TABLE

  • 7/25/2019 6_Tables of a Database

    23/30

    BY: Mr. RAYM!" S. BERMU"E#, - Instructor, College of Computer Studies, MSEUF $ %ucen& Cit'

    )o delete & column in & t&(le, uset*e follo/ing s'nt& @notice t*&tsome d&t&(&se s'stems donNt &llo/

    deleting & column:ALTER TABLE tab$e_nameDRO, *OLUMN co$'mn_name

    )o c*&nge t*e d&t& t'pe of & columnin & t&(le, use t*e follo/ing s'nt&:

    ALTER TABLE tab$e_name

    ALTER *OLUMN co$'mn_name

    T+e ALTER TABLEStatement

    S3L AUTO

  • 7/25/2019 6_Tables of a Database

    24/30

    BY: Mr. RAYM!" S. BERMU"E#, - Instructor, College of Computer Studies, MSEUF $ %ucen& Cit'

    Auto-increment &llo/s & uniLuenum(er to (e gener&ted /*en & ne/record is inserted into & t&(le.

    )*e MS S=% Serer uses t*eI"E!)I)Y 3e'/ord to perform &n&uto-increment fe&ture.

    B' def&ult, t*e st&rting &lue forI"E!)I)Y is 0, &nd it /ill increment(' 0 for e&c* ne/ record.

    S3L AUTON*REMENT F#e$d

    S3L AUTO

  • 7/25/2019 6_Tables of a Database

    25/30

    BY: Mr. RAYM!" S. BERMU"E#, - Instructor, College of Computer Studies, MSEUF $ %ucen& Cit'

    E&mple*REATE TABLE ,e!on!-,_d #nt ,RMARY EY DENTTY/

    La!tName 0ac+a-2551 NOT NULL/F#!tName 0ac+a-2551/Adde!! 0ac+a-2551/*#t% 0ac+a-2551

    1!ote:To specify the column (for example PID)should start at value 10 and increment by 5chan!e the identity to IDE"TIT#(105)$

    S3L AUTON*REMENT F#e$d

  • 7/25/2019 6_Tables of a Database

    26/30

    BY: Mr. RAYM!" S. BERMU"E#, - Instructor, College of Computer Studies, MSEUF $ %ucen& Cit'

    )*e NSERT NTO !tatement isused to insert ne/ records in & t&(le.

    )*e I!SER) I!) st&tement is usedto insert & ne/ ro/ in & t&(le.

    NSERT NTO !tatement

  • 7/25/2019 6_Tables of a Database

    27/30

    BY: Mr. RAYM!" S. BERMU"E#, - Instructor, College of Computer Studies, MSEUF $ %ucen& Cit'

    SQL !SERT !T" Synta#

    )*e 4rst form doesnNt specif' t*ecolumn n&mes /*ere t*e d&t& /ill (e

    inserted, onl' t*eir &lues:!SERT !T" table_name$AL%ES (&alue1, &alue2,

    &alue3,...)

    )*e second form speci4es (ot* t*ecolumn n&mes &nd t*e &lues to (einserted:

    !SERT !T" table_name

    NSERT NTO !tatement

  • 7/25/2019 6_Tables of a Database

    28/30

    BY: Mr. RAYM!" S. BERMU"E#, - Instructor, College of Computer Studies, MSEUF $ %ucen& Cit'

    EOAM%E:

    Pe *&e t*e follo/ing ersonst&(le:

    !o/ /e /&nt to insert & ne/ ro/ int*e ersons t&(le.

    Pe use t*e follo/ing S=% st&tement:!SERT !T" 'eon

    $AL%ES (*,+!len+, +-oan+, +Ba//en 2+,+Sta&an0e+)

    NSERT NTO !tatement

  • 7/25/2019 6_Tables of a Database

    29/30

    BY: Mr. RAYM!" S. BERMU"E#, - Instructor, College of Computer Studies, MSEUF $ %ucen& Cit'

    n!et Data On$% #n S&ec#6ed*o$'mn!

    It is &lso possi(le to onl' &dd d&t& in

    speci4c columns.)*e follo/ing S=% st&tement /ill &dd

    & ne/ ro/, (ut onl' &dd d&t& in t*eJId, %&st!&me &nd t*e

    First!&me columns:!SERT !T" 'eon ('_d,Lat!ame, t!ame)

    $AL%ES (, +Teem+, +-a/ob+)

    NSERT NTO !tatement

  • 7/25/2019 6_Tables of a Database

    30/30

    TABLE OF ADATABASE

    RAYMOND S. BERMUDEZ

    Instructor, College of Computer Studies - MSEUF

    LESSON 5