Giáo trình này nhằm cung cấp cho bạn tài liệu tham khảo tương đối đầy đủ về các câu lệnh giao tác SQL sử dụng cho hệ quản trị cCSDL Microsoft SQL Server. Giáo trình bao gòm 4 chương:
Chương 1: Giới thiệu một số câu lệnh sử dụng trong việc định nghĩa các đối tượng dữ liệu như bảng dữ liệu, khung nhìn và chỉ mục
Chương 2: Trình bày bốn câu lệnh thao tác dữ liệu và SELECT, INSERT, UPDATE, và DELETE; trong đó tập trung chủ yếu ở câu lệnh SELECT
Chương 3: Đề cập tới hai câu lệnh điều khiển là GRANT và REVOKE sử dụng trong việc cấp phát và hủy bỏ quyền của người sử dụng CSDL
Chương 4: Giới thiệu về thủ tục lưu trữ là trigger. Đây là những đối tượng CSDL được sử dụng nhằm tăng hiệu năng khi sử dụng CSDL.
46 trang |
Chia sẻ: diunt88 | Lượt xem: 2995 | Lượt tải: 1
Bạn đang xem trước 20 trang tài liệu Giáo trình thực hành SQL (rất hay), để xem tài liệu hoàn chỉnh bạn click vào nút DOWNLOAD ở trên
Giaïo trçnh thæûc haình SQL
7UDQJ
*,lv,7+,j8
64/6WUXFWXUHG4XHU\/DQJXDJHODQJQQJ¤FVGX£QJFKRFDFK£TXDQ
WU F V G OL£X TXDQK£ 1JQ QJ 64/ FKX Q ¤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 VGX£QJFR WK G¡ GDQJQKQJKD¤FG OL£X WKDR WDF YLG OL£X0£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
&KQJJLL WKL£XP¤W V¢ FX O£QK V GX£QJ WURQJYL£FQKQJKDFDF ¢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&6'/
&KQJ JLL 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£FKQJ ED£QWL£QWUDFXYD¢LFKL¢XYLFDFYGX£DQX
1JRDLUDWURQJFKQJQD\FRQFRFDFKDPWKQJVGX£QJWURQJ64/6HUYHUFDF
ED£QWKDPNKDRWURQJWK£FKDQK
0£FGXDU¢WF¢JQJQKQJJLDRWUQKNKQJWK WUDQKX¤FFDFVDLVRW5¢W
PRQJQK£Q¤FV£JRS\FXDFDFED£Q JLDRWUQKQJD\FDQJKRDQWKL£QKQ
Tráön Nguyãn Phong
Chæång 1: NGÄN NGÆÎÎ ÂËNH NGHÉA DÆÎÎ LIÃÛÛU
1JQQJQKQJKDGOL£XEDRJPFDFFXO£QKFKRSKHSQJLVGX£QJQK
QJKD&6'/YDFDF¢LW¤QJWURQJ&6'/QKFDFEDQJFDFNKXQJQKQFKPX£F
7DREDQJGOL
X
'OL£XEQWURQJP¤W&6'/¤FW FKFOXWUWURQJFDFEDQJ%QWURQJFDF
EDQJG OL£X¤FW FKFGLGD£QJFDFGRQJYDFDFF¤W0¡LP¤WGRQJEL XGL¡Q
P¤WEDQJKLGX\QK¢WYDP¡LP¤WF¤WEL XGL¡QFKRP¤WWUQJ
&DFWKXFWQKOL
QTXDQ
QEDQJ
.KLWD£RYD ODPYL£FYLFDFEDQJG OL£X WDFQSKDL \¢QFDF WKX¤F WQK
NKDFWUQEDQJQKNL XG OL£XFDFUDQJEX¤FFDFNKRDFDFTXL WF&DF WKX¤F
WQKQD\¤FVGX£QJQKPWD£RUDFDFUDQJEX¤FWRDQYH£QWUQFDFF¤WWUQJWUQ
EDQJFXQJQKWD£RUDFDFWRDQYH£QWKDPFKL¢XJLDFDFEDQJGOL£XWURQJ&6'/
D.L
XGOL
X
0¡LP¤WF¤W WUQJFXDP¤WEDQJXSKDL WKX¤FYDRP¤WNL XG OL£XQK¢W
QKD¤FQKQJKD W WUF0¡LP¤WNL XG OL£XTXLQKFDFJLD WUG OL£X
¤FFKRSKHS¢LYLF¤WR&DFK£TXDQWU&6'/WKQJFXQJF¢SFDFNL XGOL£X
FKX QQJRDLUDFRQFRWK FKRSKHSQJLVGX£QJQKQJKDFDFNL XGOL£XNKDF
G£DWUQFDFNL XGOL£XDFR
'L\ODP¤WV¢NL XGOL£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
• 4XLQKFDFJLDWUGOL£XKD\NKXQGD£QJGOL£X¤FFKRSKHSFK¢SQK£QWUQ
FDFF¤WFXDEDQJUDQJEX¤F&+(&.
• 4XLQKJLDWUP£FQKFKRFDFF¤WUDQJEX¤F'()$8/7
Giaïo trçnh thæûc haình SQL
7UDQJ
• 7D£RQQWQKWRDQYH£QWK£FWK WURQJP¤WEDQJGOL£XYDWRDQYH£QWKDPFKL¢XJLD
FDF EDQJ G OL£X WURQJ &6'/ UDQJ EX¤F 35,0$5< .(< 81,48( YD
)25(,*1.(<
&KXQJWDVHWPKL XFKLWL¢WKQYFDFUDQJEX¤FQD\SKQWUQKED\YFXO£QK
&5($7(7$%/(
7DREDQJEQJWUX\YQ64/
7D£RFDFEDQJ ODP¤WNKXTXDQ WURQJ WUR¤QJ WURQJTXD WUQK WKL¢WN¢YD FDLG£W
FDF&6'/%QWURQJFDF&6'/P¡LP¤WEDQJWKQJ¤FVGX£QJQKPEL XGL¡Q
WKQJWKLYFDF¢LW¤QJWURQJWK¢JLLWK£FYDKR£FEL XGL¡QP¢LTXDQK£JLDFDF
¢L W¤QJRj FR WK W FKFW¢WP¤WEDQJG OL£XED£QWQK¢WFQSKDL[DFQK
¤FFDF\XFXVDX
• %DQJ¤FVGX£QJQKPPX£FFKJYDFRYDLWURQKWK¢QDREQWURQJ&6'/"
• %DQJVHEDRJPQKQJF¤WQDRYDNL XGOL£XFKRFDFF¤WRODJ"
• 1KQJF¤WQDRFKRSKHSFK¢SQK£QJLDWU18//
• &RVGX£QJFDFUDQJEX¤FFDFP£FQKKD\NKQJYDQ¢XFRWKVGX£QJXYD
QKXWK¢QDR"
• 1KQJF¤WQDRVHRQJYDLWURODNKRDFKQKNKRDQJRDLNKRDGX\QK¢W"1KQJ
GD£QJFKPX£FQDRODFQWKL¢WYDFQX
D7DREDQJGOL
X
j WD£RP¤WEDQJ WURQJ&6'/ED£Q V GX£QJFX 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
- colname_i: 7QFXDF¤W WK L WURQJEDQJ&DFF¤W WURQJP¡L
Tráön Nguyãn Phong
EDQJSKDLFRWQNKDFQKDXYDSKDLWXQWKHRFDF
TXL WF Y QK GDQK0¡L P¤W EDQJ SKDL FR W
QK¢WP¤WF¤W
- col_i_properties: &DFWKX¤FWQKFXDF¤WWKLTXLQKNL XGOL£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
D5DQJEXF&+(&.
5DQJEX¤F&+(&.¤FVGX£QJ FKQKFDFJLDWUKD\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
&+(&.j NKDLEDRP¤WUDQJEX¤F&+(&.¢LYLP¤WF¤WQDRRWDVGX£QJFX
SKDSQKVDX
[CONSTRAINT constraint_name]
CHECK (expression)
7URQJRH[SUHVVLRQ ODP¤WEL X WKF ORJLFTXLQKJLD WUKD\NKXQGD£QJFXDG
OL£X¤FFKRSKHS.KLRFKQKQJJLDWUGOL£XQDRODPFKRH[SUHVVLRQQK£QJLD
WUXQJPL¤FFK¢SQK£Q
9GXj TXLQKL£Q WKRD£LFXDQKQYLQSKDLFRGD£QJ
FKQJKD£Q
FXO£QKYGX£¤FYL¢WQKVDX
CREATE TABLE nhanvien
(
Giaïo trçnh thæûc haình SQL
7UDQJ
manv char(10) not null,
hoten char(30) not null,
ngaysinh datetime null,
diachi char(50) null,
dienthoai char(6) null
constraint check_dienthoai
check (dienthoai like '[0-9][0-9][0-9]
[0-9][0-9] [0-9]')
)
D5DQJEXF'()$8/7
5DQJEX¤F'()$8/7¤FVGX£QJ TXLQKJLDWUP£FQKFKRP¤WF¤W
*LDWUQD\VHW£¤QJ¤FJDQFKRF¤WQD\NKLQJLVGX£QJE VXQJP¤WEDQJKL
PDNKQJFKQKJLD WUFKRF¤W7UQP¡L F¤W FKFR WK FR QKLXQK¢WP¤W UDQJ
EX¤F'()$8/7WFODFKFRWK FRW¢LDP¤WJLDWUP£FQK
j NKDL EDR P¤W JLD WU P£F QK FKR P¤W F¤W WD FK QK P¤W UDQJ EX¤F
'()$8/7FKRF¤WEQJFDFKVGX£QJFXSKDSVDX
[CONSTRAINT constraint_name]
DEFAULT {const_expression
nonarguments_function
NULL}
9 GX &X O£QK GL \ FK QK JLD WU P£F QK OD
NKQJ EL¢W
FKR F¤W
',$&+,WURQJEDQJ1+$19,(1YGX£
CREATE TABLE nhanvien
(
manv char(10) not null,
hoten char(30) not null,
ngaysinh datetime null,
diachi char(50) default 'khäng biãút',
dienthoai char(6) null
)
D5DQJEXF35,0$5<.(<
muYGX£QD\FKXQJWLNKQJ£WWQFKRUDQJEX¤F'()$8/7.KLRK£TXDQWU&6'/VHW£¤QJ£WWQ
FKRUDQJEX¤FQD\7X\QKLQ G¡GDQJFKRYL£FTXDQWUED£QQQ£WWQFKRFDFUDQJEX¤F
Tráön Nguyãn Phong
5DQJEX¤F35,0$5<.(<¤FVGX£QJ QKQJKDNKRDFKQKFXDEDQJ
0¤W UDQJEX¤F35,0$5<.(<DPEDR UQJNKQJFR FDFJLD WU WUXQJ O£S¤F
DYDR WUQFDF F¤W+D\QRL FDFKNKDFJLD WU FXDNKRD FKQKVH JLXSFKR WD[DF
QK¤FGX\QK¢WP¤WGRQJEDQJKLWURQJEDQJGOL£X0¡LP¤WEDQJFKFRWK
FRGX\QK¢WP¤WNKRDFKQKYDEDQWKQNKRDFKQKNKQJFK¢SQK£QJLDWU18//
5DQJEX¤F35,0$5<.(<ODFVFKRYL£FDPEDRWQKWRDQYH£QWK£F WK FXQJ
QKWRDQYH£QWKDPFKL¢X
j NKDLEDRP¤WUDQJEX¤F35,0$5<.(<ED£QVGX£QJFXSKDSVDX
[CONSTRAINT constraint_name ]
PRIMARY KEY [CLUSTERED|NONCLUSTERED]
[( colname [,colname2 [...,colname16]])]
1¢XNKRDFKQKFXDP¤WEDQJFKODP¤WF¤WNKLRED£QNKQJFQWKL¢WSKDLFK
QKGDQKVDFKFDFF¤WVGX£QJUDQJEX¤FPFF¤W7URQJWUQJK¤SNKRDFKQK
ODP¤WW£SK¤SWKDLF¤WWUOQED£QSKDLFKQKGDQKVDFKFDFF¤WVGX£QJUDQJ
EX¤FPFEDQJ
9GXWD£REDQJ1+$19,(1YLNKRDFKQKOD0$19
CREATE TABLE nhanvien
(
manv char(10) primary key,
hoten char(30) not null,
ngaysinh datetime null,
diachi char(50) null,
dienthoai char(6) null
)
FXO£QKWUQFRWK YL¢WQKVDX
CREATE TABLE nhanvien
(
manv char(10) not null,
hoten char(30) not null,
ngaysinh datetime null,
diachi char(50) null,
dienthoai char(6) null
constraint pk_nv primary key(manv)
)
Giaïo trçnh thæûc haình SQL
7UDQJ
D 5DQJEXF81,48(
7KD\Y V GX£QJNKRD FKQKED£QFR WK V GX£QJ UDQJEX¤F81,48( DP
EDRWQKWRDQYH£QWK£FWK 6GX£QJUDQJEX¤F81,48(WUQP¤WKD\QKLXF¤WEW
EX¤FFDFJLDWUGOL£XWUQP¤WKD\QKLXF¤WQD\NKQJ¤FWUXQJO£SQKDXj
NKDLEDRP¤WUDQJEX¤F81,48(ED£QVGX£QJFXSKDSO£QKVDX\
[CONSTRAINT constraint_name]
UNIQUE [CLUSTERED | NONCLUSTERED]
[colname1 [,colname2 [...,colname16]])]
D 5DQJEXF)25(,*1.(<
&DFEDQJEQWURQJP¤W&6'/WKQJFRP¢LTXDQK£YLQKDX&DFP¢LTXDQ
K£QD\¤F[DFQKG£D WUQ WQKEQJQKDXJLDP¤WKD\QKLX WUQJFXDEDQJ
QD\YLP¤WKD\QKLXWUQJFXDEDQJNKDF1¢XP¤WKD\QKLXF¤WQDRRFXDP¤W
EDQJFRJLDWU¤F[DFQKWP¤WKD\QKLXWUQJNKRDFXDEDQJNKDFWKFDF
F¤WR¤FJR¤LODFRUDQJEX¤FNKRDQJRD£LIRUHLJQNH\&DFUDQJEX¤F)25(,*1
.(<¤FVGX£QJN¢WK¤SYLFDF UDQJEX¤F35,0$5<.(<YD81,48(QKP
DPEDRWQKWRDQYH£QWKDPFKL¢XJLDFDFEDQJ¤FFKQK
j NKDLEDRNKRDQJRD£LED£QVGX£QJFXSKDSO£QKQKVDX
[CONSTRAINT constraint_name ]
[FOREIGN KEY (colname [,colname2 [...,colname16]])]
REFERENCES reference_table [(ref_colname
[,ref_colname2
[...,ref_colname 16]])]
9 GX 7D£R KDL EDQJ 1+$19,(10$19 +27(1 1*$<6,1+ ',$&+,
',(17+2$,0$'9YD'219,0$'97(1'9WKHRKQKGL\
CREATE TABLE donvi
(
1+$19,(1
0$19
+27(1
1*$<6,1+
',$&+,
',(17+2$,
0$'9
'219,
0$'9
7(1'9
∞
+QK
Tráön Nguyãn Phong
madv char(2) primary key,
tendv char(20) not null
)
CREATE TABLE nhanvien
(
manv char(10) primary key,
hoten char(20) not null,
ngaysinh datetime null,
diachi char(50) default 'khong biet',
dienthoai char(6) check(dienthoai like '[0-9][0-9][0-9]
[0-9][0-9][0-9]'),
madv char(2) foreign key(madv)
references donvi(madv)
)
6DLEDQJ
6DXNKLD WD£REDQJED£QFR WK WL¢QKDQKVD LF¢X WUXFKD\ WKX¤F WQKFXD
EDQJ QKE VXQJ F¤W E VXQJ NKRD WKD\ L FDF UDQJ EX¤Fj FR WK VXD L
EDQJED£QVGX£QJFXO£QK$/7(5FRFXSKDSQKVDX
ALTER TABLE table_name
[ADD
{col_name column_properties [column_constraints]
[[,]table_constraint ] }
[,{next_col_name|next_table_constraint}]...]
[DROP
[CONSTRAINT] constraint_name1
[, constraint_name2]...]
9GX7D£RP¤WUDQJEX¤FFKREDQJ'219,WUQF¤W0$'9TXLQKPDQY
SKDLFRGD£QJKDLFKV¢YGX£
ALTER TABLE donvi
ADD CONSTRAINT check_madv
CHECK (madv LIKE ‘[0-9][0-9]’)
Giaïo trçnh thæûc haình SQL
7UDQJ
&KPXFLQGH[
&DFFKPX£F¤FVGX£QJQKPK¡WU¤YL£FWUX\F£S¢QFDFGRQJGOL£X¤F
QKDQKFKRQJG£DWUQFDFJLDWUFXDP¤WKD\QKLXF¤W&KPX£F¤FFKLDUDODPKDL
ORD£L FKPX£F WX£ QKRP FOXVWHUHG LQGH[ YD FKPX£F NKQJ WX£ QKRP QRQFOXVWHUHG
LQGH[
• 0¤WFKPX£FWX£QKRPODP¤WFKPX£FPDWURQJRWKW£ORJLFFXDFDFNKRDWQJ
W£QKWKW£Y£WO\FXDFDFGRQJWQJQJWQWD£L WURQJEDQJ0¤WEDQJFKFR
WK FRW¢LDP¤WFKPX£FWX£QKRP
• 0¤WFKPX£FNKQJWX£KRPODP¤WFKPX£FPDWURQJRWKW£ORJLFFXDFDFNKRD
NKQJQKWKW£Y£WO\FXDFDFGRQJWURQJEDQJ&DFFKPX£FWX£QKRPK¡WU¤YL£F
WUX\F£S¢QFDFGRQJGOL£XQKDQKKQQKLXVRYLFDFFKPX£FNKQJWX£QKRP
.KLWDNKDLEDRP¤WNKRDFKQKKD\NKRD81,48(WUQP¤WKD\QKLXF¤WQDRR
FXDEDQJK£TXDQWU&6'/VH W£¤QJWD£RFKPX£F WUQFDFF¤WR%D£QFR WK WD£R
WKPFDFFKPX£FNKDFEQJFDFKVGX£QJFXO£QKFRFXSKDSQKVDX
CREATE [CLUSTEREDNONCLUSTERED] INDEX index_name
ON table_name(column_name [, column_name]...)
9GX&XO£QKGL\VHWD£RP¤WFKPX£FNKQJWX£QKRPWUQF¤W0$'9FXD
EDQJ1+$19,(1
CREATE NONCLUSTERED INDEX idx_nhanvien_madv
ON nhanvien(madv)
.KXQJQKQ9LHZ
0¤WNKXQJQKQFRWK FRLQKODP¤WeEDQJDRfFRQ¤LGXQJ¤F[DFQKW
P¤W WUX\Y¢Q0¤W WUX\ Y¢Q TXHU\ ODP¤W W£S FDF FK G¡Q LQVWUXFWLRQ QKP WUX\
[X¢WYDKL QWKGOL£XWFDFEDQJWURQJ&6'/&DFWUX\Y¢Q¤FWK£FKL£QEQJ
FDFKVGX£QJFXO£QK6(/(&7¤FF£S¢QWURQJFKQJ
0¤WNKXQJQKQWURQJJL¢QJQKP¤WEDQJYLP¤WW£RFDFWQF¤WYDFDFGRQJ
GOL£X7X\QKLQNKXQJQKQNKQJWQWD£LQKODP¤WF¢XWUXFOXWUGOL£XWURQJ
&6'/'OL£XEQWURQJNKXQJQKQWK£FFK¢W ODG OL£X¤F[DFQKWP¤WKD\
QKLXEDQJFVYDGRRSKX£WK¤FYDRFDFEDQJFV
&DFNKXQJQKQWKQJ¤FVGX£QJEQWURQJ&6'/QKPFDFPX£FFKVDX
\
• 6GX£QJNKXQJQKQ W£SWUXQJWUQGOL£X¤F[DFQK
Tráön Nguyãn Phong
• 6GX£QJNKXQJQKQ QJLDQKRDWKDRWDFGOL£X
• 6GX£QJNKXQJQKQ WX\EL¢QGOL£X
• 6GX£QJNKXQJQKQ [X¢WNK XH[SRUWGOL£X
• 6GX£QJNKXQJQKQ EDRP£WGOL£X
j WD£RNKXQJQKQED£QVGX£QJFXO£QKFRFXSKDSQKVDX
CREATE VIEW view_name[(column_name [, column_name]...)]
AS select_statement
7URQJRVHOHFWBVWDWHPHQWODP¤WFXO£QK6(/(&7GXQJ WUX\[X¢WGOL£XW
P¤WKD\QKLXEDQJ
.KLWD£RNKXQJQKQFQOX\P¤WV¢L PVDX
• 7QNKXQJQKQSKDLWXQWKHRFDFTXLWFYQKGDQKYDSKDLGX\QK¢W¢LYL
P¡LQJLVGX£QJ
• .KQJWK UDQJEX¤FFDFP£FQKFDFTXLWFFKRNKXQJQKQ
• .KQJWK [\G£QJFKPX£FFKRNKXQJQKQ
• 7URQJFX O£QK&5($7(9,(:NKQJFQ WKL¢WSKDL FKQK WQF¤W7QFXD
FDFF¤WFXQJQKNL XGOL£XFXDFKXQJVHWQJQJYLFDFF¤WWURQJGDQKVDFK
FKR¤QFXDFXO£QK6(/(&7
• %D£QSKDL[DFQKWQF¤W WURQJFX O£QK&5($7(9,(:WURQJFDF WUQJK¤S
VDX
− 0¡LF¤WFXDNKXQJQKQ¤FSKDWVLQKWP¤WEL XWKFV¢KR¤FP¤WKDPFDL
VQKD\P¤WKQJ
− +DLKD\QKLXF¤WFXDNKXQJQKQFRWUXQJWQ
− %D£QPX¢QWKD\ LWQF¤WWURQJNKXQJQKQNKDFYLWQF¤WFXDEDQJFV
9GX&XO£QKGL\VHEO¡LGRWQFXDF¤WWKNKQJ[DFQK¤F
CREATE VIEW thongtin_nv
AS
SELECT manv,hoten,datediff(year,ngaysinh,getdate()),tendv
FROM nhanvien,donvi
WHERE nhanvien.madv=donvi.madv
j FXO£QKWUQFRWK WK£FKL£Q¤FED£QSKDL£W WQFKRFDFF¤WFXDNKXQJQKQ
QKVDX
CREATE VIEW thongtin_nv(manv,hoten,tuoi,donvi)
Giaïo trçnh thæûc haình SQL
7UDQJ
AS
SELECT manv,hoten,datediff(year,ngaysinh,getdate()),tendv
FROM nhanvien,donvi
WHERE nhanvien.madv=donvi.madv
Z Y
Tráön Nguyãn Phong
Chæång 2: NGÄN NGÆÎÎ THAO TAÏÏC DÆÎÎ LIÃÛÛU
1JQQJWKDRWDFGOL£XFXQJF¢SFKRQJLVGX£QJNKDQQJWL¢QKDQKFDF
WKDRWDFWUX\[X¢WE VXQJF£SQK£WYD[RDGOL£X1JQQJWKDRWDFGOL£XEDR
JPFDFFXO£QK6(/(&7,16(5783'$7(YD'(/(7(
7UX\[XWGOL
X
§Ó truy xuÊt d÷ liÖu tõ c¸c dßng vµ c¸c cét cña mét hay nhiÒu b¶ng, khung nh×n,
ta sö dông c©u lÖnh SELECT. C©u lÖnh nµy cã thÓ dïng ®Ó thùc hiÖn phÐp chän (tøc lµ
truy xuÊt mét tËp con c¸c dßng trong mét hay nhiÒu b¶ng), phÐp chiÕu (tøc lµ truy
xuÊt mét tËp con c¸c cét trong mét hay nhiÒu b¶ng) vµ phÐp nèi (tøc lµ liªn kÕt c¸c
dßng trong hai hay nhiÒu b¶ng ®Ó truy xuÊt d÷ liÖu).
Có ph¸p chung cña c©u lÖnh SELECT cã d¹ng nh sau:
SELECT [ ALL | DISTINCT ] select_list
[ INTO [ newtable_name ]]
FROM { table_name | view_name }
................
[,{table_name | view_name }]
[WHERE clause ]
[GROUP BY clause ]
[HAVING BY clause ]
[ORDER BY clause ]
[COMPUTE clause ]
Chó ý: C¸c thµnh phÇn trong mét c©u lÖnh SELECT ph¶i ®îc sö dông theo thø tù
®îc nªu trªn.
1.1 X¸c ®Þnh b¶ng b»ng mÖnh ®Ò FROM
MÖnh ®Ò FROM trong c©u lÖnh SELECT ®îc sö dông nh»m chØ ®Þnh c¸c b¶ng
vµ khung nh×n cÇn truy xuÊt d÷ liÖu. Sau mÖnh ®Ò FROM lµ danh s¸ch tªn c¸c b¶ng
vµ khung nh×n tham gia vµo truy vÊn (tªn cña c¸c b¶ng vµ khung nh×n ®îc ph©n c¸ch
nhau bëi dÊu phÈy).
SELECT select_list
FROM {table_nameview_name list}
Giaïo trçnh thæûc haình SQL
7UDQJ
§Ó ®¬n gi¶n ho¸ c©u hái, ta cã thÓ sö dông c¸c bÝ danh (alias) cho c¸c b¶ng hay
khung nh×n. BÝ danh ®îc g¸n trong mÖnh ®Ò FROM b»ng c¸ch chØ ®Þnh bÝ danh sau
tªn b¶ng. VÝ dô c©u lÖnh sau g¸n bÝ danh n1 cho b¶ng nhanvien.
SELECT ten, diachi FROM nhanvien n1
1.2 MÖnh ®Ò WHERE
MÖnh ®Ò WHERE trong c©u lÖnh SELECT x¸c ®Þnh c¸c ®iÒu kiÖn ®èi víi viÖc truy
xuÊt d÷ liÖu. Sau mÖnh ®Ò WHERE lµ mét biÓu thøc logic vµ chØ nh÷ng dßng d÷ liÖu
nµo tho¶ m∙n biÓu thøc sau WHERE míi ®îc hiÓn thÞ