1. Trang chủ
  2. » Luận Văn - Báo Cáo

Phần mềm biến đổi lệnh select từ ngôn ngữ oracle sang ngôn ngữ microsoft sql server

91 0 0
Tài liệu đã được kiểm tra trùng lặp

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

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Định dạng
Số trang 91
Dung lượng 40,6 MB

Nội dung

BO GIAo DVC vA DAo T~O TRUONG DHDL NGO~I NGU- TIN HQC TP.HCM KHOA CONG NGH~ THONG TIN KHoA LUAN TOT NGHIEP • • pHAN MEM BIEN DOl LENH SELECT TU NGON NGU ORACLE SANG NGON NGU MICROSOFT SQL SERVER GVHD:ThS NGUYEN TRUNG TRVC SVTH: VO NGQC KIM TRANG TP.HO CHi MINH - 2006 Lai dun on LmcAMON Em xin chan dun on thAy NguySn Trung Tl1!c da:t~n tinh huang dfut Em su6t qua trinh lam khoa lu~n t6t nghi~p Em cling xin dun on cac ThAy, Co Khoa Cong Ngh~ Thong Tin truang Daj hQc dan l~p Ngo~i Ngu - Tin HQc Tp Hi) Chi Minh da: luon t~o diSu ki~n cho cac em hoc t~p t6t su6t nien hQc qua sv.V6 NgQc Kim Trang MI}.C ll}.c MUCLUC • • , , , ChU'O'I1g 1: Tong quan ve de tai ChU'O'ng2: Van ph~m diu SELECT clla Oracle va Microsoft Sql Server 2.1 Ngon ngfr SQL 2.1.1 Cac d~c diSm cua SQL 2.1.2 T~p l~nh SQL 2.2 L~nh SELECT 2.2.1 M~nh d~ SELECT 2.2.2 M~nh d~ FROM 2.2.3 Tir khoa DISTINCT 2.2.4 M~nh d~.INTO 2.2.5 M~nh d~ WHERE 2.2.6 M~nh d~ GROUP By 2.2.7 M~nh d~ HAVING 2.2.8 M~nh d~ ORDER By 2.3 Van ph~m cau troy v~n cua Oracle 2.4 Van ph~m cau troy v~n cua Microsoft Sql Server 2.5 S\f khac cua cau troy v~n gifra Oracle va Microsoft SQL 2.5.1 BiSm khac bi~t v~ cac m~nh d~ 2.5.2 BiSm khac bi~t v~ cac toan tu va tu khoa 2.5.3 BiSm khac bi~t v~ kiSu dfr li~u 2.5.4 BiSm khac bi~t v~ ham 2.5.4.1 Ham s6 hQc 2.5.4.2 Ham ki tv v6'i gia tri tra v~ la ki tv 2.5.4.3 Ham ki tv v6'i gia tri tra v~ la s6 2.5~4.-4-=-Harrnlgay thang 2.5.4.5 Ham chuySn d6i 2.5.4.6 Ham xu ly gia tri r6ng 2.5.4.7 Ham nhom 2.6 Cac phep toan d~i s6 quan h~ duQ'csu d\lng dS phan tich cau troy v~n cay toan til 2 2 3 4 4 4 10 15 16 19 21 22 22 23 24 25 26 26 27 27 My.c Iy.c A ?? ChU'O'ng 3: Qua trinh chuyen doi diu trur van 3.1 T6ng quan qua trinh chuyen d6i 3.2 Chi tiSt tUng buac chuySn d6i 3.2.1 KiSm tra 16i 3.2.2 Phan tich cau troy vfu1 3.2.2.1 ChuySn sang cay Cll phap 3.2.2.2 ChuySn sang cay toan tu 3.2.3 ChuySn d6i cac diSm khac v~ ham 3.2.4 ChuySn d6i cac diSm khac v~ toan tu 3.2.5 ChuySn d6i cac diSm khac v~ tiT khoa 3.2.6 ChuySn d6i cac diSm khac bi~t l~i 3.2.7 Phat sinh rna Sql 32 32 33 33 35 40 42 44 47 47 48 48 ChU'O'ng 4: Ph An m~m Chuy~n Dai Cau Truy V~n Oracle 49 4.1 T6ng quan 49 4.2 Cac ki thu~t dung thvc hi~n chuang trinh 50 4.2.1 KiSn truc thvc hi~n 51 4.2.2 KiSn truc v~t ly 52 4.2.2.1 BQ kiSm tra 16i 52 4.2.2.2 BQ rna h6a chu6i, tir kh6a, toan tU, ham 52 4.2.2.3 BQ phan tich Cll phap cua cau troy vfin 53 4.2.2.4 BQ rna h6a chu6i, tir kh6a, toan tU, ham 55 4.2.2.5 BQ chuySn sang cay toan tu 55 4.2.2.6 BQ chuySn d6i cac diSm khac lo~i 1, lo~i 2, lo~i 58 4.2.2.7 BQ phat sinh rna SQL 60 4.3 Giao di~n va cach su d\lng phfin m~m 61 4.3.1 Giao di~n chinh 61 -4.3.2-.-Tab-Gay Cll phap • •• -=-=-.-644.3.3 Tab Cay toan tu 65 4.3.4 Tab Minh hQa 66 4.3.5 Tham khao cO' dfr li~u 68 4.3.6 Thu vi~n toan tU 69 4.3.7 Thu vi~n tir kh6a 70 4.3.8 Thu vi~n ham 71 4.3.9 Dinh nghia ham mai 72 sa ? , Ch traDg : To ng kef 5.1 T6ng ket 5.2 Nhfrng phftn da:thvc hi~n duQ'c 5.3 Nhfrng phftn chua duQ'c ly dS tai 5.4 Ung d\lng va huang phcit triSn cua dS tai xu PhD IDe: MQt so, vi - dl} mao Daub m \Ie titi li~u tham khao • 73 73 70 75 76 77 86 Chu'O'Og1: Tang quan v~ d~ tai ChU'ong 1: TONG QUAN VE DE TAl I>~ tai: Phc1n m@m bien d6i l~nh SELECT tiT ngon ngfr Oracle sang ngon ngfr Microsoft SQL Server 1.1 Yeu eiu ella d~ tai • • Tim hiSu van ph~m l~nh SELECT cua Oracle va Microsoft SQL Server Phan bi~t Sl! khac v@ van ph~m cua cau troy vl1n gifra Oracle va Microsoft SQL Server • • Tim hiSu cac phfrn m@m cfrn thiet lien quan den vi~c thl!C hi~n d@tai: ngon ngfr l~p trinh C# tren moi truCmg NET, Viet phfrn m@m minh havi~c chuySn d6i cau l~nh SELECT cua Oracle sang Microsoft SQL Server 1.2 Phan tieh yeu eiu • Tim hiSu v@ngfr nghi'a van ph~m cua Oracle va Microsoft SQL Server dS phan bi~t Sl! khac cua cau troy vl1n gifra hai h~ qmin tri cO' so dfr li~u Tuy nhien, vi van ph~m cua chung qua phuc t~p, d@tai khong thS t6ng quat het, nen chung ta chi nghien cuu cac vl1n d@khac cO'ban • Sau nh~n biet diSm gi6ng va xac dinh Sl! khac gifra hai cau troy vl1n cua hai h~ qUID tri cO' so dfr li~u, ta cfrn tien hanh phan lo~i cac diSm khac dS thl1y duQ'c Sl! t6ng quat v@kien truc ngon ngfr SQL • Xay d\fllg qua trinh chuySn d6i troy vl1n gifra hai h~ qufm tri cO'so dfr li~u Oracle va Microsoft SQL Server • Yiet chuang trinh thl!c hi~n vi~c chuySn d6i cac Sl! khac gifra hai cau _~tru~v~n Chrrong 2: Van ph~m diu SELECT clla Oracle va Microsoft SQL ¥ A ? ChU'ong 2: VAN PH~M CAV SELECT CVA ORACLE vA MICROSOFT SQL SERVER 2.1 Ngon ngu' SQL H~ qm\n tri CSDL (DBMS - Database Management System) c~n phai co mQt ngon ngfr cho phep nguai su d\lng truy xuat dfr li~u Ngon ngl'l truy van co cau truc (SQL - Structured Query Language) la mQt ngon ngfr su d\lng h~u hSt cac h~ CSDL quan h~ Ngon ngfr SQL dugc phat triSn mQt h~ quan tri CSDL mfiu - System R - boi IBM van gifra nhfrng nam 1970 Nam 1979, Oracle Corporation dff gi6i thi~u vS vi~c th\1'c hi~n SQL thuang m~i hoa d~u tien 2.1.1 Cac d~c di~m clla SQL • SQL la ngon ngfr tva tiSng Anh (English - like language), su d\lng cac tu nhu select, insert, delete t~p l~nh • SQL la mQt ngon ngfr phi thu !\Ic (non-procedural language): chi cac thong tin nao c~n thiSt (what), khong c~n chi cach th\1'c hi~n nhu thS nao (how) dS co dugc thong tin • Tat ca cac l~nh SQL dSu su d\lng bQ t6i uu hoa truy van (query optimizer) - la mQt ph~n cua RDBMS - dS xac dinh cac cach truy van nhanh nhat lay dfr li~u c~n thiSt • SQL xu ly cac t~p hgp mau tin han la mlii l~n mQt mau tin D~ng chung nhat cua t~p hgp cac mau tin la mQt bang (table) • SQL co thS dugc dung cho nhiSu nguai su d\lng, bao g6m DBA, nguai l~p trinh (mg d\lng, nguai quan ly va nguai su d\lllg cu6i cung (end user) , SQL cung cap nhiSu l~nh dS th\1'c hi~n nhiSu cong vi~c khac nhau, bao gom: + Truy van dfr-li~u.- + Them vao, c~p nh~t va huy b6 cac hang cua bang + T~o l~p, thay d6i va huy b6 cac d6i tugng CSDL + DiSu khiSn truy xuat dSn CSDL va cac d6i tugng CSDL + Bao dam tinh nhat quan cua dfr li~u ChU'O'ng2: Van ph~m • CaD SELECT clla Oracle va Microsoft SQL 2.1.2 T~p l~nh SQL Cac l~nh SQL duQ'cchia lO{li:ngon ngfr dinh nghia dfr li~u, ngon ngfr thao tac dfr li~u, ngon ngfr diSu khiSn dfr li~u • Ngon ngii iljnh nghia dii lifu (DDL - Data Definition Language): bao g6m cac l~nh dung dS t{lOl~p, tahy d6i va huy b6 cac bang Vi d\l: CREATE, ALTER, DROP • Ngon ngii thao ttic dU'lifu (DML - Data Manipulation Language): bao g6m cac l~nh dung dS truy vfin, them vao, c~p nh~t va xoa b6 dfr li~u CSDL Cac l~nh co thS duQ'cdua vao tnJc tiSp (chS dQ tU011gtac) va kSt qua tra vS sau thvc hi~n l~nh nay, ho~c co thS dua vao cac chu011g trinh Vi d\l: SELECT, INSERT, DELETE, UPDATE • Ngon ngii iliJu khiJn dU'lifu (DCL - Data Control Language): bao g6m cac l~nh dung dS h6 trQ'cho DBA dS diSu khiSn CSDL Cac l~nh dung dS gan va huy b6 cac quySn (privilege) xufit CSDL va cac d6i tuqng rieng bi~t CSDL va luu tm ho~c huy b6 cac giao dich (transaction) anh hUOngdSn CSDL Vi d\l: GRANT, REVOKE 2.2 Lenh SELECT • L~nh SELECT dung dS lfiy dfr li~u CSDL, th\fc hi~n cac phep toan cua d{lis6 quan h~: phep dch, phep chQn, phep chiSu, phep kSt • Ngfr phap cua l~nh SELECT nhu sau: SELECT [DISTINCT] [INTO ] FROM [WHERE ] [GROUP BY [HAVING ]] [ORDER BY ]; 2.2.1 Menh d~ SELECT M~nh dS SELECT th\fc hi~n phep chiSu cua d{li s6 quan h~ list of expressions la danh sach cac biSu thuc duQ'cchQn, cac biSu thuc duQ'cphiin cach boi dfiu phfiy BiSu thuc co thS la mQt h~ng, mQt biSn (ho~c cQt), ho~c S\fkSt hqp gifra cac h~ng, cac biSn v6i phep toano M6i biSu thuc co thS co mQt bi danh (alias) dung phia sau, duQ'cgQi la bi danh cQt (column alias); bi danh cQt chi duQ'c Slr d\lng m~nh dS SELECT NSu list of expressions la dfiu * ChuO'ng2: Van ph~m diu SELECT clla Oracle va Microsoft SQL • (asterisk) thi t~t ca cac table - name column - name cQt duQ'c chQn CQt co thS co di;lng 2.2.2 Menh d~ FROM M~OOds"PROM thvc hi~n phep tich cua di;lis6 quan h~, dung dS chi cac bang chua dfr Ii~u cfin I~y list of tables hI danh sach cac bang duQ'cphan cach boi d~u phAy.M6i bang co thS co bi daOOdUng phia sau 2.2.3 Til' khoa DISTINCT Tu khoa DISTINCT IOi;lib6 cac gia tri trung kSt qua cua truy v~n NSu co OOiSucQt duQ'c chQn thi DISTINCT aOOhuang dSn toan bQ cac cQt Til khoa DISTINCT phai duQ'cd~t sau tu khoa SELECT 2.2.4 Menh d~ INTO M~nh d{ INTO chi duQ'c su d\lng PL/SQL, dung dS gan gia tri cua cac biSu thuc cho cac biSn theo thu tv tuang ung list of variables Ia daOOsach cac biSn duQ'cphan cach OOauboi d~u phAy 2.2.5 Menh d~ WHERE M~OOd{ WHERE thvc hi~n cac phep chQn, phep kSt cua di;lis6 quan h~ row conditions Ia cac diSu ki~n duQ'cxet tren m6i hang, cac hang nao th6a man cac diSu ki~n thi duQ'cdua vao kSt qua cua truy v~n 2.2.6 Menh d~ GROUP BY M~OOdS.GROUP BY duQ'c dung dS phan chia cac hang cua mQt bang thaOOcac OOom006 han Cac ham nhom co thS duQ'csir d\lng dS tra vS thong tin chung cho m6i nhom M6i OOomchi xu~t hi~n mQt hang kSt qua cua truy v~n Trong truemg hQ'p I~OOSELECT co ca hai m~nh dS WHERE va GROUP BY-thi-eac-hang se duQ'c chQn boi diSu ki~n cua-m~nh-dS-WHERE, r6i sau phan chia cac hang duQ'c chQn cac OOom.Cac cQt m~OOdS GROUP BY khong b~t bUQcphai co SELECT Tuy OOien,dS cho kSt qua cua truy v~n Ia co ngfr nghia thi cac cQt cua m~nh dS GROUP BY nen co m~nh dS SELECT Khi co m~OO dS GROUP BY, t~t ca cac cQt co m~OO dS SELECT nSu chung khong n~m ham OOomthi chung phai co m~nh dS GROUP BY Trong truemg hQ'PI~OOSELECT khong co m~nh dS GROUP BY thi toan bQ bang duQ'c xem Ia mQt OOom.Do do, nSu m~OOdS SELECT co chua ham OOomthi chung ta khong thS I~y duQ'cchi tiSt cua m6i hang ChlfO'Dg 2: Van ph~m Call SELECT ella Oracle va Microsoft SQL • • 2.2.7 Menh d~ HAVING M~OOdS.HAVING duQ'c dung d~ xac dinh OOomduQ'c dua vao kSt qua cua truy v~n group of rows conditions la cac diSu ki~n duQ'cxet cho m6i nhom M~OOdS HAVING co th~ dUng truac m~OOdS GROUP BY, OOung m~OOdS GROUP BY nen dung truac d~ cho d~ hi~u Cac OOomduQ'ct~o va cac ham nhom duQ'c tiOOtoan truac thvc hi~n m~OOdS HAVING d~ chQn cac nhom NSu I~OOSELECT co cac m~l1h dS WHERE, GROUP BY va HAVING thi thu tv thvc hi~n cua cac m~nh dS la WHERE (d~ chQn cac hang), kS tiSp GROUP BY (d~ phiin chia nhom) va sau cung la HAVING (d~ chQn cac OOom) 2.2.8 Menh d~ ORDER BY Thong th.ucmg,thu tv cua cac hang duQ'ctra vS kSt qua cua truy v~n la khong xac diOO.M~OOdS ORDER BY co th~ duQ'c dung d~ s~p thu tv cac hang M~OOdS ORDER BY luon luon la m~OOdS cu&i cung cua I~OO SELECT Thu tv ngftm diOO la tang dftn (ASC - ascending), tiT khoa DESC (descending) dUng sau ten cQt dung d~ chi diOOthu tv giam dftn Cac cQt m~nh dS ORDER BY khong b~t bUQcphai co m~nh dS SELECT • Chu'O'ng4: Phdn m~m chuy~n d&i diu truy v~n Oracle 72 4.3.9 Bjob ogbia bam mOl DlNH NGHIA HAl\1 Ten ham : ITO_SINGLE _BYTE M6bi: IChuy~n sang ki~u byte Ki~u tra v@ : INumber S6 lUQ'11g tham s6 t6i thi~u: 11 C~u SIT Thams6 Char true ham: I a !TO_SINGLE_BYTE (Char) : Number Them I Tho at I ChUO'Dg5: TAng k~t 73 ChlfO'Dg 5: TONG KET 5.1 TAng k~t Bao cao dfftrinh bay mqt phuong phap tmg dVng cho vi~c chuySn d6i diu truy vin tu Oracle sang Microsoft Sql Server Tuy nhien, van ph~m cau SELECT cua Oracle tuong d6i rqng, kiSn thuc phan tich va thai gian thlJc hi~n h~n chS nen bao cao chi giai quySt duqc mqt s6 vin de co bfm vi~c chuySn d6i Cv thS: • Bao cao xu ly dlJa tren van ph~m cua Oracle9i • Tim hiSu va phan lo~i duqc SlJkhac bi~t ve cau truy vin gifra Oracle va Microsoft Sql Server • Xu ly cau truy vin gam co m~nh de: Select, From, Where, Group By, Having, Order By 5.2 Nhfrng ph~n dii thuc hien dtr()'c 5.2.1 Ki~m tra lai diu truy v~n KSt n6i co So'dfr li~u v6i Oracle nen Oracle co thS bAt cac l6i sai ciu truc van ph~m cau SELECT 5.2.2 Ch uy~n dAicac di~m khac bift • Toan tu (xem chuong 2_trang 15) • Ham chi khac ve ten ham Oracle Microsoft Sql Server ATAN2(n, m) ATN2(n,m) CEIL(n) CEILING(n) LN(n) LOG(n) LENGTH( char) LEN(char) TO CHAR(d [,fmt [, 'nlsparam']]) CONVERT(varchar,d) TO CHAR(n [,fmt [, 'nlsparam']]) CONYER T(varchar,n) TO DATE (char [,fmt [, 'nlsparam']]) CONVER T(datetime,char) TO_NUMBER( char [,fmt [, CONVERT(int,char) 'nlsparam']]) NVL( expr, non-null value) ISNULL( expr, non-null value) VARIANCE(n) VAR(n) Chlfong 5: Tang k~t 74 • Ham duQ'cthay thS b~ng toan tu Oracle LOG(m, n) MOD(m, n) CONCAT(charl, char2) Microsoft Sql Server LOG(n) / LOG(m) m%n charl + char2 • Ham duQ'cchuySn d6i b~g cac ham khac Oracle LPAD(charl, n (,char2]) Microsoft Sql Server REPLICATE( char2(n - LEN( charl )) RPAD(charI, n (,char2]) ADD MONTHS( d,n) Charl + REPLICATE( ; char2; n LEN( charI) ) / LEN( char2 ) DATEADD(M, n , d) • Ham co sf>luqng tham sf>khac & tr~t t\1'tham sf>khac Oracle ADD MONTHS( d,n) INSTR(str, substr) Microsoft Sql Server DATEADD(M, n , d) CHARINDEX(substr, str, pos) • ChuySn d6i ham nhom 16ng Vi d\l5.1: Ham nhom lAng 0' mtfc dq don ghin Oracle Select max( count( dept_id ) ) From s_emp Group by dept id~- Microsoft Sql Server Select MAX(Col) from ( select count( dept_id ) as Col from s_emp group by dept id) as Tmp Vi d\l 5.2: Ham nhom lAng 0' mtfc dq phfrc t~p Oracle Microsoft Sql Server select dept_id, count( dept_id) select sum(salary) as "Sum" into #T from s_emp from s_emp group by dept_ id group by salary having sum(salary) > (select max( sum( salary) ) select dept id, count( dept id) ChUO'Dg5: TAng k~t 75 from s_emp group by salary ); from s_emp group by dept_ id having sum( salary ) > (select max( Sum) from #T) 5.3 Nhfrng ph~n chua duO'c xif Iy d~ Uli • Cac ill khoa ngon ngfr Microsoft Sql Server khong h6 trq STT I ~ I Til Khoa _-~=INU=T_E_R_S_S_E=C=T=============== • Cac ham khac ngon ngfr Microsoft SqI Server khong co ham tuang ung & khong thS biSn d6i ( STT Oracle BITAND(argl, arg2) COSH(n) i' STT 10 i I J SINH(n) TANH(n) 11 12 TRUNC(n [,m]) 13 INITCAP( char) 14 NLS _ INITCAP( char[, -'nlsparam ']) NLS _LOWER( char[, 'nlsparam' ]) 15 - Oracle NLSSORT(char[, 'nlsparam']) NLS _ UPPER( char[, 'nlsparam']) SUBSTRB(char,m [,n]) TRANSLATE('char', 'from', 'to ') TRIM(trim_ch FROM trim sour) INSTRB(str, substr [, pos [, occur]]) LENGTHB(char) 16 LAST_DAY(d) • T6i uu hoa truy vfuI Vi dl} 5.3: select a.name, phone from s_customer a, s_region b where ( a.sales_rep_id = b.id) and (b.name = 'Asia' ) ChUO'Dg5: TAng k~t 76 select name, phone from s custome where sales_rep_id in ( select id from s_region name = 'Asia' ) 5.4 U ng dung va hU'6'ngphat tri~n ella d~ tai 5.4.1 Ung d\lng D~ tai giup cho chung ta hiSu va phan bi~t duQ'c S\f khac giua cau truy vfin Oracle va Microsoft Sql Server Chuong trinh co thS chuySn d6i duQ'c nhung cau truy vfin don gian mQt cach nhanh chong va dS dang 5.4.2 Hu:6'n~phat tri~~ , Giai quyet tiep nhUng van de sau - TiSp t\lC tim hiSu dS chuySn d6i diSm khac bi~t v~ cac phep toan, ill khoa, ham duQ'cli~t ke phAn nhfmg ph6n chua au(Yc xu If; trang a~tili - BAng cach su d\lng cac phep biSn d6i d:;tisa quan h~ dS tai uu hoa cau truy vfin truac th\fc hi~n thao tac biSn d6i Phu luc: Mot sA vi du m~u 77 _ x.' -::t' ~Ph= u;;;"",;l~uc~: MQT SO VI Dl) MAD Ki~m tra lai 1.1 Ngii' phap Dun Sai SELECT id; SELECTid FROM s_emp; => L6i: FROM keyword not found WHERE ex ected SELECT name FROM s_dept WHERE id = 'abc' , SELECT name FROM s_dept WHEREid=3 , -> L6i: invalid number SELECT id, last_name FROMemp; SELECT id, last_name FROM s_emp; => L6i: table or view does not exist SELECT last name FROM s_dept; SELECT last name FROM s_emp; => L6i: invalid column name 13.T~ncOtnhAm~n SELECT id, e.last_ name FROM s_emp e, s_dept d WHERE e.id = d.id; => L6i: column ambiguously defined Dun SELECT d.id, e.last_ name FROM s_emp e, s_dept d WHERE e.id = d.id; Phu luc: Mot sa vi du miu 78 1.4 Nhim lin khong d~t bi danh cho bang , ,> Sai DUI1'2 SELECT id FROM s_emp a, s_emp b WHERE a.id = b.id; SELECTid FROM s_emp, s_emp; => L6i: column ambiguously defined Cac vi du chuy~n dAi 2.1 Xac djnh bi~u th rc Oracle select 3+54+8 as "Cong" from dual Select 27*(3+54+8) as "Bieu thuc" from dual 2.2 Tir khoa 0tacle SELECT round( (2 * * 3.14),2) "Dien Tich" FROM dual; SELECT credit_rating as "Loai", count(name) as "So luong" FROM s customer GROUP BY credit ratin ; SELECT dept_id as "Don vi", avg(salary) as "Trung binh" FROM s_emp HAVING avg( salary) > 1000 GROUP BY de tid; 2.3 Toan til va phep toan Ie,!> SELECT 18 * 64 * ( 27 + 36 ) FROM dual; > as Microsoft S I Server, select round( 12.56 , 2) as "Dien tich" SELECT credit_rating as "Loai", count(name) as "So luong" FROM s customer GROUP BY credit ratin SELECT last_name as "Don vi", avg(salary) as "Trung binh" FROM s_emp GROUP BY last name HAVING avg( salary) > 1000 i Phu luc: Mot •• sA vi du miu SELECT 'Trang' II 'Tu' FROM dual; SELECT filename II filename FROM sima e; SELECT chr(54) II chr(36) FROM dual; SELECT * FROM s_emp WHERE (dept_id = 44 or dept_id 42) and (sal >1000); 79 SELECT 'Trang'+' Tu' SELECT filename + filename FROM sima e; SELECT char(54) + char(36) = SELECT * FROMs_emp WHERE (dept_id = 44 or dept_id 42) and (salary> 1000) = 2.4 Ham chi khac 0' ten ham SELECT sysdate FROM dual; SELECT id + length(name) FROM s customer; SELECT id + ( length (name) - * (length (name) + )) FROM s customer; SELECT id + len(name) FROM s customer SELECT id + ( len(name)2*(len(name) + )) FROM s customer 2.5 Ham gAm nhi~u ham Select RP AD( 13, '*.d' ) as "Rpad" From dual select add_months( sysdate, ) from dual 2.6 Ham dlfQ'Cchuy~n d8i bing mqt bi~u thtfc , Phy Iyc: MQt sf, vi dy miu 80 2.7 Hai ham cQt lAng Microsoft S I Server Oracle SELECT lower( substr('TRANG' , , SELECT lower( substring('TRANG' , , 4) ) 4) ) FROM dual; SELECT CEILING( ABS(12.42)) SELECT CEIL( ABS(12.42) ) FROM dual; SELECT POWER( 26%6 , ) SELECT POWER( MOD(26,6) , ) FROM dual; 2.8 Hai ham nhom lAng Select max( count( dept_id ) ) From s_emp Group by dept_id select sum( mine salary) ) from s_emp group by salary; T 2.9 Bi danh bang din xuit select s em * from salary from from 2.10 Phep join cua m~nh d~ Where select a.id as "Ma so" , a.name as "Khach hang" from s_customer a, s_ord b where a.id = b.customer _ide+) as 81 Phu lue: Mot sa vi du miu select a.last name as "Nhan vien" , b.last name as "Phu trach" from s_emp a, s_emp b where a.mana er id (+)= b.id select a.last name as "Nhan vien" , b.last name as "Phu trach" from s_emp a, s_emp b where a.manager_id =* b.id 2.11 Tir khoa Rownum Microsoft S select top salary from s_emp 2.12 Til' khoa Cube Phu luc: Mot sA vi du miu 82 Vi du minh hoa cac buO'c thuc hien chuang trinh Cau troy v~n Oracle: SELECT id + (length(name) - 2*( length( substr('Trang' , 3, ) ) + » AS ViDu FROM s customer 3.1 Ki~m tra lai Cau troy v~n nh~p van dung cu phap nen Oracle khong bao 16i va chuang trinh se thgc hi~n nhiing bUGCxu ly thiSp theo 3.2 Phan tich cu phap 3.2.1 Phan tich ciu truc Clla cau truy vin 'EokenO • Lo;;ti Ham Substr('Trang' ,3,2) Length(Substr('Trang' ,3,2) ) Len th name C\lm Substr('Trang' ,3,2) length( substr('Trang' , 3, ) ) + ) -2*( length( substr('Trang' , 3, ) ) + ) id + ( length(name) - 2*( length( substr('Trang' , 3, ) + AS ViDu 3.2.2 Xli' Ten Ham SUBSTR LENGTH LENGTH Token s customer Iy ham Tliam So 'Tran ' Substr('Trang' , 3, name Phn Inc: Mot , sB vi dn 83 3.2.3 Cay Cll phap S lect id + ( length(name) - 2*( length( substr('Trang' , 3, ) ) + )) AS ViDu From s customer S lect id + (length(name) - 2*(length(substr('Trang',3,2)) + 1)) AS ViDu length(name) I~ Length CName length( substr('Trang' , 3, ) I- length Csubstr('Trang' , 3, ) substr('Trang' , 3, ) , substr 'Trang' FrOm s_c~stomer s customer 3.3 Phan tich cay toan t •.• 3.3.1 Token [0] _ lo~i 1: chuang trinh se xu Node[l] key left + length( substr('Trang' , 3, 2)) * Node[2] length(name) Node[O] - ly ri!!ht length( sub str('Trang' , 3, )) + 2*( length( substr('Trang' , Phn Inc: Mot sAvi dn 84 + id Node[3] 3.3.2 Cay tmlD tti' Select id + ( length(name) - 2*( length( substr('Trang' , 3, ) ) + )) AS ViDu From s customer :~~u: + ( length(name) - 2*( length( substr('Trang' ,3,2) ) + » Pt',p id L: Id lr~~name) - 2*(length( substr('Trang' ,3,2» t: I Length( name) 2i( l:n*gth( substr(Trang' ,3,2) L: " + 1) ) + 1) 2ength( substr('Trang' ,3,2» +1 + ength(substr('Trang', 3, ) ) Phep tich: s_customer 3.3.3 Thao tac Ten ham Tr~t t\1'tham so ChUY~D substr 'Tran ' dai cac di~m khac bi~t Microsoft S I Server Substrin 'Tran Phu luc: Mot sa vi du miu 85 I~ • I~ • Ten ham Tr~t t\l' tham so Microsoft S I Server len Name Oracle Ten ham Tr~t t\l' tham so len th name • ChuySn d6i toan til Oracle Node(Ol Node(l] Node(2] Node(3] Microsoft Sql Server + + * * + - + •• • ChuySn d6i til kh6a Microsoft Sql Server Oracle Til kh6a AS AS 3.4 Cau truy vAn Microsoft Sql Server ttr01lg ung Cau troy vfuI la S\l'kSt hQ'Pcua token va token -+ Cau troy van Microsoft Sql Server tuang ling: SELECT id + ( len(name) - 2*( len( substring('Trang' , 3, ) ) + )) AS ViDu FROM s customer r 86 Daub ml}e titi li~u tbam khao • Danb Muc Titi Lieu • • Tbam Kbao sa [1] Nguyen Trung Trvc, Co dfi li?u phan bc5, NXB D~i HQc Qu6c Gia Tp.HCM, 2004 [2] Phuong Lan - Ph~m Hfru Khang - Hoang Duc Hcii, KI Thu~t L~p Trinh Ung D\IDg C#.NET tooo t~p, NXB Lao DQng - Xa HQi, 2005 [3] Books online, Phfin mSm Microsoft SQL Server 2000 [4] Nancy Greenberg va Priya Nathan, Introduction to Oracle9i: SQL, Oracle Corporation 2000-2001 [5] Diana Lorentz, Oracle8i SQL Reference, Release 3, Oracle Corporation, September 2000 •

Ngày đăng: 01/09/2023, 22:05

w