1. Trang chủ
  2. » Vật lí lớp 12

Giáo trình thực hành SQL

8 12 0

Đang tải... (xem toàn văn)

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Định dạng
Số trang 8
Dung lượng 249,19 KB

Nội dung

Giáo trình. th ực h ành SQL.[r]

(1)

Giáo trình

thực hành SQL

(2)

7UDQJ

*,lv,7+,j€8

64/6WUXFWXUHG4XHU\/DQJXDJHODQJQQJÔFVGXÊQJFKRFDFKÊTXDQ WU F V G OLÊX TXDQ KÊ 1JQ QJ 64/ FKXQ ÔF D UD EL $16, $PHULFDQ 1DWLRQDO 6WDQGDUGV ,QVWLWXGH YD” ,62 ,QWHUQDWLRQDO 6WDQGDUGV 2UJDQL]DWLRQ Y—L SKL‹Q ED•Q P—L QK‰¢W KL‹£Q QD\ OD” SKL‹Q ED•Q 64/ SKL‹Q ED•Q ÔF D UD QP 0ÊFGXFRQKLXQJQQJNKDFQKDXÔFDUDFKRFDFKÊTXDQWU&6'/ TXDQKÊ64/ODQJQQJÔFVGXÊQJUÔQJUDLKLÊQQD\WURQJUÂWQKLXKÊWKÂQJ &6'/WKQJPDÊLQK2UDFOH64/6HUYHU'%0LFURVRIW$FFHVV7KQJTXD64/ QJL V GXÊQJ FR WK GĂ GDQJ QK QJKD ÔF G OLÊX WKDR WD—F Y—L GŽ– OL‹£X 0ˆ£W NKD—FŠ‰\OD”QJŒQQJŽ–FR—W’QKNKDLED—RQ‹QQR—G‹¡VŽ•GX£QJYD”FX–QJYY‰£\PD”WU•Q‹Q SKŒ EL‹¢Q

*LD—RWULQKQD”\QKˆšPFXQJF‰¢SFKRED£QWD”LOL‹£XWKDPNKD•RWŽQJŠŒ¢LŠ‰ž\ŠX•Y‹ž FD—F F‰X O‹£QK JLDR WD—F 64/ VŽ• GX£QJ FKR K‹£ TXD•Q WU“ &6'/ 0LFURVRIW 64/ 6HUYHU *LD—RWUQKEDRJŒžPEŒ¢QFKŽQJ

&KŽQJ JL—L WKLÊX PÔW V FX OÊQK V GXÊQJ WURQJ YLÊF QK QJKD FDF ÂL WÔQJGOLÊXQKEDQJGOLÊXNKXQJQKQYDFKPXÊF

&KQJ WUQK ED\ EÂQ F‰X O‹£QK WKDR WD—F GŽ– OL‹£X OD” 6(/(&7 ,16(57 83'$7(YD”'(/(7(WURQJŠR—W‰£SWUXQJFKX•\‹¢X•F‰XO‹£QK6(/(&7

&KŽQJŠ‹žF‰£SŠ‹¢QKDLF‰XO‹£QKŠL‹žXNKL‹ QOD”*5$17YD”5(92.(VŽ•GX£QJ WURQJYL‹£FF‰¢SSKD—WYD”KX\•ER•TX\‹žQFX•DQJŽ”LVŽ•GX£QJ&6'/

&KŽQJ JL—L WKL‹£X Y‹ž WKX• WXÊF OX WU YD WULJJHU j\ OD QKQJ ÂL WÔQJ &6'/ÔFVGXÊQJQKPWQJKLÊXQQJNKLVGXÊQJ&6'/

7URQJFKQJSKXÊOXÊFFKXQJWLJLLWKLÊXFÂXWUXFYDGOLÊXFXDFDFEDQJV GXÊQJWURQJFDFYGXÊFKQJEDÊQWLÊQWUDFXYDÂLFKLÂXYLFDFYGXÊDQX 1JRDLUDWURQJFKQJQD\FRQFRFDFKDPWKQJVGXÊQJWURQJ64/6HUYHUFDF EDÊQWKDPNKDRWURQJWKÊFKDQK

(3)

C

Chhổổồồnngg11::NNGGNNNNGGặặẻẻậậNNHHNNGGHHẫẫAA DDặặẻẻLLIIUU

1JQQJQKQJKDGOLÊXEDRJPFDFFXOÊQKFKRSKHSQJLVGXÊQJQK QJKD&6'/YDFDFÂLWÔQJWURQJ&6'/QKFDFEDQJFDFNKXQJQKQFKPXÊF

7DREDQJGOLX

'OLÊXEQWURQJPÔW&6'/ÔFWFKFOXWUWURQJFDFEDQJ%QWURQJFDF EDQJGOLÊXÔFWFKFGLGDÊQJFDFGRQJYDFDFFÔW0ĂLPÔW GRQJ ELX GLĂQ PÔWEDQJKLGX\QKÂWYDPĂLPÔWFÔWELXGLĂQFKRPÔWWUQJ

&DFWKXFWQKOLQTXDQQEDQJ

.KL WD£R YD” OD”P YL‹£F Y—L FD—F ED•QJ GŽ– OL‹£X WD FQ SKDL \ ÂQ FDF WKXÔF WQK NKDFWUQEDQJQKNLX G OLÊX FDF UDQJ EXÔF FDF NKRD FDF TXL WF &DF WKXÔF WQKQD\ÔFVGXÊQJQKPWDÊRUDFDFUDQJEXÔFWRDQYHÊQWUQFDFFÔWWUQJWUQ EDQJFXQJQKWDÊRUDFDFWRDQYHÊQWKDPFKLÂXJLDFDFEDQJGOLÊXWURQJ&6'/

D.LXGOLX

0ĂL PÔW FÔW WUQJ FXD PÔW EDQJ X SKDL WKXÔF YDR PÔW NLX G OLÊX QKÂW QK D ÔF QK QJKD W WUF 0ĂL PÔW NLX G OLÊX TXL QK FDF JLD WU G OLÊX ÔFFKRSKHSÂLYLFÔWR&DFKÊTXDQWU&6'/WKQJFXQJFÂSFDFNLXGOLÊX FKXQQJRDLUDFRQFRWKFKRSKHSQJLVGXÊQJQKQJKDFDFNLXGOLÊXNKDF GÊDWUQFDFNLXGOLÊXDFR

'L\ODPÔWVÂNLXGOLÊXWKQJÔFVGXÊQJWURQJJLDRWDF64/

Binary Bit Char Datetime Decimal Float Image

Int Money Nchar Ntext Nvarchar Real

Smalldatetime

Smallint Smallmoney Text

Tinyint Varbinary Varchar

E&DFUDQJEXF&21675$,176

7UQFDFEDQJGOLÊXFDFUDQJEXÔFÔFVGXÊQJQKPFDFPXÊFFKVDX

(4)

7UDQJ

ã 7DÊRQQWQKWRDQYHÊQWKÊFWKWURQJPÔWEDQJGOLÊXYDWRDQYHÊQWKDPFKLÂXJLD FDF EDQJ G OLÊX WURQJ &6'/ UDQJ EXÔF 35,0$5< (< 81,48( YD )25(,*1.(<

&KXQJWDVHWPKLXFKLWLÂWKQYFDFUDQJEXÔFQD\SKQWUQKED\YFXOÊQK &5($7(7$%/(

7DREDQJEQJWUX\YQ64/

7DÊR FDF EDQJ OD PÔW NKX TXDQ WURQJ WURÔQJ WURQJ TXD WUQK WKLÂW NÂ YD FDL GÊW FDF&6'/%QWURQJFDF&6'/PĂLPÔWEDQJWKQJÔFVGXÊQJQKPELXGLĂQ WKQJWKLYFDFÂLWÔQJWURQJWKÂJLLWKÊFYDKRÊFELXGLĂQPÂLTXDQKÊJLDFDF ÂLWÔQJRjFRWKWFKFWÂWPÔWEDQJGOLÊXEDÊQWQKÂWFQ SKDL [DF QK ÔFFDF\XFXVDX

ã %DQJÔFVGXÊQJQKPPXÊFFKJYDFRYDLWURQKWKÂQDREQWURQJ&6'/"

ã %DQJVHEDRJPQKQJFÔWQDRYDNLXGOLÊXFKRFDFFÔWRODJ"

ã 1KQJFÔWQDRFKRSKHSFKÂSQKÊQJLDWU18//

ã &RVGXÊQJFDFUDQJEXÔFFDFPÊFQKKD\NKQJYDQÂXFRWKVGXÊQJXYD QKXWKÂQDR"

ã 1KQJFÔWQDRVHRQJYDLWURODNKRDFKQKNKRDQJRDLNKRDGX\QKÂW"1KQJ GDÊQJFKPXÊFQDRODFQWKLÂWYDFQX

D7DREDQJGOLX

j WDÊR PÔW EDQJ WURQJ &6'/ ED£Q VŽ• GX£QJ F‰X O‹£QK &5($7( 7$%/( FR— FX— SKD—SQKŽVDX

CREATE TABLE table_name (

{colname_1 col_1_properties [constraints_1 ] [,{colname_2 col_2_properties [constraints_2 ]]

[,{colname_N col_N_properties [constraints_N ]] [table_constraints]

)

7URQJ„R‘

- table_name: 7‹Q ED•QJ F‰žQ WD£R 7‹Q FX•D ED•QJ SKD•L GX\ QK‰¢W

WURQJPŒ¡L&6'/YD”SKD•LWX‰QWKHRFD—FTXLWˆFY‹ž Š“QKGDQK

(5)

ED•QJSKD•LFR—W‹QNKD—FQKDXYD”SKD•LWX‰QWKHRFD—F TXL WˆF Y‹ž Š“QK GDQK 0ĂL PÔW EDQJ SKDL FR W QKÂWPÔWFÔW

- col_i_properties: &DFWKXÔFWQKFXDFÔWWKLTXLQKNLXGOLÊX

FXD FÔW YD FK QK FÔW FR FKR SKHS FKÂS QKÊQ JLDWU18//KD\NKQJ

- constraints_i: &DF UDQJ EXÔF QÂX FR WUQ FÔW WK L QK FDF

UDQJEXÔFYNKRDFDFPÊFQKFDFTXLQKY NKXQGDÊQJGOLÊX

- table_constraint: &DFUDQJEXÔFWUQEDQJGOLÊX

9GX&XOÊQKGL\WKÊFKLÊQKYLÊFWDÊREDQJ1+$19,(1EDRJPFDFFÔW 0$19+27(11*$<6,1+',$&+,',(17+2$,

CREATE TABLE nhanvien (

manv char(10) not null, hoten char(30) not null, ngaysinh datetime null, diachi char(50) null, dienthoai char(6) null )

E6ˆGX›QJFD‘FUDŽQJEX†œFWURQJEDQJGˆOL…›X

D D D

D5DŽQJEX†œF&+(&.

5DQJEXÔF&+(&.ÔFVGXÊQJFKQKFDFJLDWUKD\NKXQGDÊQJGOLÊX FR WK ÔF FKÂS QKÊQ ÂL YL PÔW FÔW 7UQ PÔW FÔW FR WK FR QKLX UDQJ EXÔF &+(&.jNKDLEDRPÔWUDQJEXÔF&+(&.ÂLYLPÔWFÔWQDRRWDVGXÊQJFX SKDSQKVDX

[CONSTRAINT constraint_name] CHECK (expression)

7URQJ RH[SUHVVLRQ OD PÔW ELX WKF ORJLF TXL QK JLD WU KD\ NKXQ GDÊQJ FXD G OLÊXÔFFKRSKHS.KLRFKQKQJJLDWUGOLÊXQDRODPFKRH[SUHVVLRQQKÊQJLD WUXQJPLÔFFKÂSQKÊQ

9Œ GX› j‹  TXL Š“QK ŠL‹£Q WKRD£L FX•D QKQ YLQ SKDL FR GDÊQJ FKQJ KDÊQ FXOÊQKYGXÊÔFYLÂWQKVDX

(6)

&XSKDS'$7($''GDWHSDUWQXPEHUGDWH

&KF QQJ +DP WUD Y PÔW JLD WU NLX 'DWH7LPH EQJ FDFK FÔQJ WKP PÔW NKRDQJJLDWUODQXPEHUYDRQJD\GDWHÔFFKQK

'DWHSDUW WKDP VÂ FK QK WKDQK SKQ VH ÔF FÔQJ ÂL YL JLD WUGDWH EDR JP

'DWHSDUW 9LWWW

\HDU \\\\\\ TXDUWHU TTT PRQWK PPP GD\RI\HDU G\\ GD\ GGG ZHHN ZNZZ KRXU KK PLQXWH PLQ VHFRQG VVV PLOOLVHFRQG PV

E+DP'$7(',))

&XSKDS'$7(',))GDWHSDUWVWDUWGDWHHQGGDWH

&KFQQJ+DPWUDYNKRDQJWKLJLDQJLDKDLJLDWUNLXQD\ÔFFKQK WX\WKXÔFYDRWKDPVÂGDWHSDUW

9GXKDP'DWHGLII\HDUFKRNÂWTXDOD

F+DŽP'$7(3$57

&X‘SKD‘S'$7(3$57GDWHSDUWGDWH

&Kˆ‘F Q‚QJ +D”P WUD• Y‹ž PÔW V QJX\Q ÔF WUFK UD W WKDQK SKQ ÔF FK QKELWKDPVÂSDUWGDWHWURQJJLDWUNLXQJD\ÔFFKQK

9GX+DP'DWH3DUW\HDUFKRNÂWTXDOD

G+DP*(7'$7(

&XSKDS*(7'$7(

&KFQQJ+DPWUDYQJD\KLÊQWDÊL

H+DŽP'$<0217+<($5

&X‘SKD‘S'$<GDWH0217+GDWH<($5GDWH

&Kˆ‘F Q‚QJ +D”P WUD• Y‹ž JLD— WU“ QJD”\ WŽQJ Ž—QJ WKD—QJ QˆP FX•D JLD— WU“ NLX QJD\ÔFFKQK

(7)

D+DP/()7

&XSKDS/()7VWULQJQ

&KŽ—FQˆQJ+D”PWU’FKUDWŽ”FKXŒ¡LVWULQJQN\—WŽ£W’QKWŽ”E‹QWUD—L

E+DŽP5,*+7

&X‘SKD‘S/()7VWULQJQ

&Kˆ‘FQ‚QJ+D”PWU’FKUDWŽ”FKXŒ¡LVWULQJQN\—WŽ£W’QKWŽ”E‹QSKD•L

F+DŽP68%675,1*

&X‘SKD‘S68%675,1*VWULQJPQ

&Kˆ‘FQ‚QJ+D”PWU’FKUDWŽ”FKXŒ¡LVWULQJQN\—WŽ£W’QKWŽ”N\—WŽ£WKŽ—P

G+DŽP/75,0575,0

&X‘SKD‘S/75,0VWULQJ575,0VWULQJ

&Kˆ‘FQ‚QJ+D”PFˆWER•FD—FNKRD•QJWUˆQJWKŽ”DE‹QWUD—LE‹QSKD•LFKXŒ¡LVWULQJ

H+DŽP/(1

&X‘SKD‘S/(1VWULQJ

(8)

M

MUUÛCÛCLLUUÛCÛC

&+ml1*1*k11*muir1+1*+p$'mu/,j8

7$2%$u1*'nv/,k8

&DFWKXÔFWQKOLQTXDQÂQEDQJ 7DÊREDQJEQJWUX\YÂQ64/ 6DLEDQJ

&+p08ƒ&,1'(;

.+81*1+o19,(:

&+ml1*1*k11*mu7+$27$v&'mu/,j€8

758<;8i‚7'nv/,kƒ8

1.1 Xác định bảng mệnh đề FROM 12

1.2 Mệnh WHERE 13

1.3 Danh sách chọn câu lệnh SELECT 14

1.4 Tính toán giá trị câu lệnh SELECT 16

1.5 Từ khoá DISTINCT 16

1.6 Tạo bảng câu lệnh SELECT INTO 17

1.7 Sắp xếp kết truy vấn ORDER BY 17

1.8 Phép hợp to¸n tư UNION 18

1.9 PhÐp nèi 20

1.10 Tạo dòng thống kê liệu với COMPUTE BY 24

1.11 Thống kê liệu với GROUP BY vµ HAVING 26

1.12 Truy vấn (subquery) 27

%l€681*&iƒ31+iƒ79$t;2$w'nv/,kƒ8 %Œ VXQJGŽ–OL‹£X &‰£SQK‰£WGŽ–OL‹£X ;RD—GŽ–OL‹£X &+ml1*1*k11*mui,j|8.+,j}1 &i8/k1+*5$17 &i8/k1+5(92.( &+ml1*7+8t78&/m875mu9$s75,**(5 ,6nu'81*7+8u78&/n875nv6725('352&('85( I.1 Tạo thủ tục lưu trữ 35

I.2 Thông tin trả từ thủ tục lưu trữ 36

I.3 Các qui t¾c sư dơng cho sp 38

I.4 Xác định tên bên thủ tục 38

I.5 §ỉi tên thủ tục: 39

I.6 Xoá thủ tục: 39

II Sệ DễNG CáC TRIGGER II.1 Tạo trigger 40

II.2 Các giá trị null ngầm định hiển (implicit and explicit null values) 40

II.3 Việc đổi tên trigger 41

II.4 HiÓn thị thông tin trigger 41

II.5 Xoá trigger 42

Ngày đăng: 09/03/2021, 03:39

w