Giáo trình. th ực h ành SQL.[r]
(1)
Giáo trình
thực hành SQL
(2)7UDQJ
*,lv,7+,j8
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 YL SKLQ EDQ PL QK¢W KL£Q QD\ OD SKLQ EDQ 64/ SKLQ EDQ Ô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 WDF YL G OL£X 0£W NKDF\ODQJQQJFRWQKNKDLEDRQQQRG¡VGX£QJYDFXQJYY£\PDWUQQ SK EL¢Q
*LDRWULQKQD\QKPFXQJF¢SFKRED£QWDLOL£XWKDPNKDRWQJ¢L\XY FDF FX O£QK JLDR WDF 64/ V GX£QJ FKR K£ TXDQ WU &6'/ 0LFURVRIW 64/ 6HUYHU *LDRWUQKEDRJPE¢QFKQJ
&KQJ JLL 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 FX O£QK WKDR WDF G OL£X OD 6(/(&7 ,16(57 83'$7(YD'(/(7(WURQJRW£SWUXQJFKX\¢XFXO£QK6(/(&7
&KQJF£S¢QKDLFXO£QKLXNKL QOD*5$17YD5(92.(VGX£QJ WURQJYL£FF¢SSKDWYDKX\ERTX\QFXDQJLVGX£QJ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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 FX O£QK &5($7( 7$%/( FR FX SKDSQKVDX
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]
)
7URQJR
- table_name: 7Q EDQJ FQ WD£R 7Q FXD EDQJ SKDL GX\ QK¢W
WURQJP¡L&6'/YDSKDLWXQWKHRFDFTXLWFY QKGDQK
(5)EDQJSKDLFRWQNKDFQKDXYDSKDLWXQWKHRFDF TXL WF 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 )
E6GXQJFDFUDQJEXFWURQJEDQJGOL X
D D D
D5DQJEXF&+(&.
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 FXD 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+DP'$7(3$57
&XSKDS'$7(3$57GDWHSDUWGDWH
&KF QQJ +DP 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+DP'$<0217+<($5
&XSKDS'$<GDWH0217+GDWH<($5GDWH
&KF QQJ +DP WUD Y JLD WU QJD\ WQJ QJ WKDQJ QP FXD JLD WU NLX QJD\ÔFFKQK
(7)D+DP/()7
&XSKDS/()7VWULQJQ
&KFQQJ+DPWUFKUDWFKX¡LVWULQJQN\W£WQKWEQWUDL
E+DP5,*+7
&XSKDS/()7VWULQJQ
&KFQQJ+DPWUFKUDWFKX¡LVWULQJQN\W£WQKWEQSKDL
F+DP68%675,1*
&XSKDS68%675,1*VWULQJPQ
&KFQQJ+DPWUFKUDWFKX¡LVWULQJQN\W£WQKWN\W£WKP
G+DP/75,0575,0
&XSKDS/75,0VWULQJ575,0VWULQJ
&KFQQJ+DPFWERFDFNKRDQJWUQJWKDEQWUDLEQSKDLFKX¡LVWULQJ
H+DP/(1
&XSKDS/(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/,j8
758<;8i7'nv/,k8
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
%l681*&i31+i79$t;2$w'nv/,k8 % VXQJGOL£X &£SQK£WGOL£X ;RDGOL£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