3.giao trinh sql_va_pl_sql

423 views

Published on

Published in: Education, Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
423
On SlideShare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
21
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

3.giao trinh sql_va_pl_sql

  1. 1. www.nhipsongcongnghe.net
  2. 2. C«ng ty cæ phÇn ®Çu t− ph¸t triÓn c«ng nghÖ - FPT Gi¸o tr×nh SQL vµ PL/SQLC«ng ty cæ phÇn ®Çu t− ph¸t triÓn c«ng nghÖ - FptHµ Néi, th¸ng 11 n¨m 2002. §µo t¹o Oracle c¬ b¶n Gi¸o tr×nh SQL vµ PL/SQL§µo t¹o c¬ b¶n: SQL vµ PL/SQL Trang 2
  3. 3. C«ng ty cæ phÇn ®Çu t− ph¸t triÓn c«ng nghÖ - FPT Gi¸o tr×nh SQL vµ PL/SQLMôc lôcMôc lôc .................................................................................................................................................31 Giíi thiÖu.......................................................................................................................................6 1.1 Môc tiªu kho¸ häc ..............................................................................................................................6 1.2 Khëi ®éng vµ tho¸t khái Oracle.........................................................................................................6 1.2.1 T¹i Server (Window NT) .........................................................................................6 1.2.2 T¹i Client (Window 9x) ............................................................................................6 1.3 Giíi thiÖu ng«n ng÷ SQL ...................................................................................................................7 1.3.1 LÞch sö ph¸t triÓn cña ng«n ng÷ SQL .....................................................................7 1.3.2 ChuÈn SQL .............................................................................................................7 1.4 C¸c kh¸i niÖm trong CSDL.................................................................................................................7 1.5 Danh s¸ch rót gän c¸c ®èi t−îng CSDL ...........................................................................................8 1.6 C¸c lÖnh SQL ......................................................................................................................................8 1.7 Giíi thiÖu vÒ vÝ dô thùc hµnh.............................................................................................................9 1.7.1 M« h×nh quan hÖ d÷ liÖu..........................................................................................9 1.7.2 M« t¶ d÷ liÖu............................................................................................................92 LÖnh truy vÊn c¬ b¶n ...........................................................................................................10 2.1 LÖnh truy vÊn c¬ b¶n........................................................................................................................10 2.2 C¸c thµnh phÇn kh¸c cña mÖnh ®Ò SELECT..................................................................................10 2.3 Gi¸ trÞ Null..........................................................................................................................................11 2.4 Läc d÷ liÖu tõ c¸c row cã cïng gi¸ trÞ.............................................................................................11 2.5 HiÓn thÞ cÊu tróc b¶ng ......................................................................................................................12 2.6 C¸c lÖnh cña c«ng cô SQL*Plus......................................................................................................12 2.6.1 C¸c lÖnh so¹n th¶o ...............................................................................................12 2.6.2 C¸c lÖnh vÒ file......................................................................................................13 2.6.3 C¸c lÖnh vÒ column...............................................................................................13 2.7 Bµi tËp................................................................................................................................................143 Truy vÊn d÷ liÖu cã ®iÒu kiÖn ............................................................................................16 3.1 MÖnh ®Ò ORDER BY .........................................................................................................................16 3.2 MÖnh ®Ò WHERE...............................................................................................................................16 3.3 C¸c to¸n tö ........................................................................................................................................17 3.4 Bµi tËp................................................................................................................................................194 C¸c hµm ¸p dông cho 1 dßng d÷ liÖu..............................................................................20 4.1 C¸c hµm sè........................................................................................................................................20 4.2 C¸c hµm ký tù ...................................................................................................................................22 4.3 C¸c hµm ngµy ...................................................................................................................................26 4.4 C¸c hµm chuyÓn ®æi kiÓu.................................................................................................................28 4.5 Bµi tËp................................................................................................................................................295 BiÕn runtime..............................................................................................................................31 5.1 Bµi tËp................................................................................................................................................326 C¸c hµm nhãm ¸p dông cho lín h¬n hoÆc b»ng 1 dßng d÷ liÖu ........................32 6.1 C¸c hµm t¸c ®éng trªn nhãm ..........................................................................................................32 6.2 MÖnh ®Ò GROUP BY .........................................................................................................................34 6.3 Bµi tËp................................................................................................................................................357 HiÓn thÞ néi dung d÷ liÖu tõ nhiÒu b¶ng ........................................................................35 7.1 Mèi liªn kÕt t−¬ng ®−¬ng .................................................................................................................35§µo t¹o c¬ b¶n: SQL vµ PL/SQL Trang 3
  4. 4. C«ng ty cæ phÇn ®Çu t− ph¸t triÓn c«ng nghÖ - FPT Gi¸o tr×nh SQL vµ PL/SQL 7.2 Mèi liªn kÕt kh«ng t−¬ng ®−¬ng......................................................................................................35 7.3 Mèi liªn kÕt céng...............................................................................................................................36 7.4 Liªn kÕt cña b¶ng víi chÝnh nã .......................................................................................................36 7.5 C¸c to¸n tö tËp hîp ..........................................................................................................................36 7.6 Bµi tËp................................................................................................................................................378 C¸c lÖnh truy vÊn lång nhau...........................................................................................39 8.1 C©u lÖnh SELECT lång nhau. ..........................................................................................................39 8.2 Bµi tËp................................................................................................................................................409 CÊu tróc h×nh c©y...................................................................................................................40 9.1 CÊu tróc h×nh c©y trong 1 table .......................................................................................................40 9.2 Kü thuËt thùc hiÖn ............................................................................................................................41 9.3 Bµi tËp................................................................................................................................................4210 Tæng kÕt vÒ lÖnh select .....................................................................................................4411 T¹o table ...................................................................................................................................44 11.1 LÖnh t¹o b¶ng ...................................................................................................................................44 11.2 C¸c quy t¾c ®Æt tªn object ...............................................................................................................46 11.3 C¸c quy t¾c khi tham chiÕu ®Õn object...........................................................................................47 11.4 KiÓu d÷ liÖu vµ ®iÒu kiÖn ..................................................................................................................47 11.4.1 CHAR ....................................................................................................................47 11.4.2 VARCHAR2...........................................................................................................48 11.4.3 VARCHAR.............................................................................................................48 11.4.4 NUMBER...............................................................................................................48 11.4.5 FLOAT...................................................................................................................48 11.4.6 LONG ....................................................................................................................49 11.4.7 DATE.....................................................................................................................49 11.4.8 RAW vµ LONG RAW ............................................................................................50 11.4.9 ROWID ..................................................................................................................50 11.4.10 MLSLABEL............................................................................................................50 11.4.11 ChuyÓn ®æi kiÓu ....................................................................................................50 11.5 Constraint..........................................................................................................................................51 11.6 Bµi tËp................................................................................................................................................5212 c¸c lÖnh DDL kh¸c vµ d÷ liÖu trong tõ ®iÓn d÷ liÖu...............................................52 12.1 ChØnh söa cÊu tróc table ..................................................................................................................52 12.2 C¸c lÖnh DDL kh¸c ...........................................................................................................................53 12.2.1 Xãa table ...............................................................................................................53 12.2.2 Gi¶i thÝch b¶ng ......................................................................................................53 12.2.3 Thay ®æi tªn object................................................................................................53 12.2.4 Xãa d÷ liÖu cña table.............................................................................................53 12.3 D÷ liÖu trong tõ ®iÓn d÷ liÖu ............................................................................................................54 12.4 Bµi tËp................................................................................................................................................5413 C¸c lÖnh Thao t¸c d÷ liÖu kh¸c ......................................................................................55 13.1 ChÌn mét row vµo table ...................................................................................................................55 13.2 ChØnh söa d÷ liÖu..............................................................................................................................55 13.3 Xãa dßng ...........................................................................................................................................55 13.4 Lçi rµng buéc d÷ liÖu .......................................................................................................................56 13.5 LÖnh ®iÒu khiÓn giao dÞch................................................................................................................56 13.6 Bµi tËp................................................................................................................................................5714 Sequence vµ index..................................................................................................................57§µo t¹o c¬ b¶n: SQL vµ PL/SQL Trang 4
  5. 5. C«ng ty cæ phÇn ®Çu t− ph¸t triÓn c«ng nghÖ - FPT Gi¸o tr×nh SQL vµ PL/SQL 14.1 Sequence...........................................................................................................................................57 14.1.1 T¹o Sequence.......................................................................................................57 14.1.2 Xo¸ vµ söa sequence ...........................................................................................58 14.2 Index ..................................................................................................................................................58 14.3 Bµi tËp................................................................................................................................................5915 T¹o view ......................................................................................................................................59 15.1 View....................................................................................................................................................59 15.2 Bµi tËp................................................................................................................................................6116 QuyÒn vµ b¶o mËt ...................................................................................................................61 16.1 QuyÒn - PRIVILEGE ..........................................................................................................................61 16.2 ROLE..................................................................................................................................................62 16.3 Synonym............................................................................................................................................6317 tæng quan vÒ pl/sql vµ procedure builder...............................................................63 17.1 Có ph¸p lÖnh PL/SQL .......................................................................................................................63 17.2 PL/SQL block ....................................................................................................................................63 17.3 Giíi thiÖu Procedure builder............................................................................................................6418 có ph¸p lËp tr×nh ...................................................................................................................66 18.1 IF ........................................................................................................................................................66 18.2 LOOP vµ EXIT....................................................................................................................................66 18.3 FOR ....................................................................................................................................................67 18.4 WHILE ................................................................................................................................................67 18.5 GOTO .................................................................................................................................................6719 cursor ........................................................................................................................................68 19.1 §Þnh nghÜa .........................................................................................................................................68 19.2 KiÓu d÷ liÖu Table vµ Record...........................................................................................................69 19.3 Sao kiÓu d÷ liÖu ................................................................................................................................70 19.4 C©u lÖnh SELECT... INTO... trong PL/SQL......................................................................................70 19.5 Bµi tËp................................................................................................................................................7020 procedure vµ funtion..........................................................................................................71 20.1 Procedure ..........................................................................................................................................71 20.2 Function.............................................................................................................................................72 20.3 Bµi tËp................................................................................................................................................7321 pakage.........................................................................................................................................73 21.1 Package .............................................................................................................................................7322 database trigger ..................................................................................................................74 22.1 Database Trigger...............................................................................................................................74 22.2 Bµi tËp................................................................................................................................................7523 error handing .........................................................................................................................76 23.1 Bµi tËp................................................................................................................................................78§µo t¹o c¬ b¶n: SQL vµ PL/SQL Trang 5
  6. 6. C«ng ty cæ phÇn ®Çu t− ph¸t triÓn c«ng nghÖ - FPT Gi¸o tr×nh SQL vµ PL/SQL1 Giíi thiÖu1.1 Môc tiªu kho¸ häcKÕt thóc kho¸ häc häc viªn ph¶i n¾m ®−îc• HiÓu ®−îc ph−¬ng ph¸p, c¸c thµnh phÇn, thuËt ng÷ vµ thao t¸c trong CSDL quan hÖ• T¹o ®−îc c¸c cÊu tróc d÷ liÖu nh− table, view dïng SQL• Ghi, ®äc, vµ cËp nhËt d÷ liÖu trong CSDL• X©y dùng c¸c PL/SQL block dïng Procedure Builder1.2 Khëi ®éng vµ tho¸t khái Oracle1.2.1 T¹i Server (Window NT)SQLDBA cung cÊp c¸c dÞch vô qu¶n trÞ hÖ thèng, nh−: t¹o lËp CSDL, më - ®ãng CSDL, t¹o vµ qu¶n lý c¸cUSER ... C¸c b−íc ®Ó khëi ®éng t¹i Server nh− sau:• Khëi ®éng m¸y chñ• BËt dÞch vô OracleServiceXXX (trong ®ã XXX lµ tªn cña CSDL) b»ng c¸ch nhÊn vµo Start -> Program - > Service -> OracleServiceXXX -> NhÊn chuét ph¶i -> NhÊn Start. Chó ý chØ bËt dÞch vô nµy khi ng−êi cµi ®Æt kh«ng ®Ó chÕ ®é tù ®éng hay khi dÞch vô nµy ch−a ®−îc bËt.• BËt dÞch vô OracleXXXTNSLÝstener (trong ®ã XXX lµ tªn cña Database Home) b»ng c¸ch nhÊn vµo Start -> Program -> Service -> OracleXXXTNSLÝstener -> NhÊn chuét ph¶i -> NhÊn Start. Chó ý chØ bËt dÞch vô nµy khi ng−êi cµi ®Æt kh«ng ®Ó chÕ ®é tù ®éng hay khi dÞch vô nµy ch−a ®−îc bËt.• Khi bËt xong CSDL ®· s½n sµng ®Ó lµm viÖc§Ó ®ãng CSDL cÇn lµm theo c¸c b−íc ng−îc l¹i:• T¾t dÞch vô OracleXXXTNSLÝstener (trong ®ã XXX lµ tªn cña Database Home) b»ng c¸ch nhÊn vµo Start -> Program -> Service -> OracleXXXTNSLÝstener -> NhÊn chuét ph¶i -> NhÊn Stop.• T¾t dÞch vô OracleServiceXXX (trong ®ã XXX lµ tªn cña CSDL) b»ng c¸ch nhÊn vµo Start -> Program - > Service -> OracleServiceXXX -> NhÊn chuét ph¶i -> NhÊn Stop.• Shutdown m¸y chñ.1.2.2 T¹i Client (Window 9x)C¸c øng dông cña oracle ch¹y trong m«i tr−êng Windows víi giao diÖn graphic, c¸c øng dông th−êng dïngcã SQL*Plus, Oracle Form, Oracle Report, Oracle Designer ... ViÖc ch¹y c¸c øng dông nµy hoµn toµn gièngnh− viÖc ch¹y c¸c øng dông th«ng th−êng trong m«i tr−êng windows.§Ó lµm viÖc víi c¸c øng dông truy cËp CSDL Oracle, ng−êi sö dông (NSD) ph¶i connect vµo CSDL. Cã haic¸ch ®Ó connect.Connect NSD/password, vÝ dô NSD tªn Scott cã password lµ tiger th×Connect Scott/tigerPh¸t lÖnh connect víi tªn NSD, khi ®ã Oracle sÏ hái passwordConnect ScottEnter password: *****§µo t¹o c¬ b¶n: SQL vµ PL/SQL Trang 6
  7. 7. C«ng ty cæ phÇn ®Çu t− ph¸t triÓn c«ng nghÖ - FPT Gi¸o tr×nh SQL vµ PL/SQLNSD cã thÓ lµm viÖc trong ph¹m vi cho phÐp cña m×nh mµ Oracle gäi lµ "khung c¶nh" (Schema) cña NSD.Mçi khung c¶nh chøa nhiÒu ®èi t−îng c¸c lo¹i, NSD chØ cã thÓ t¸c ®éng lªn c¸c ®èi t−îng trong khung c¶nhcña m×nh.Trong c¸c øng dông ®Òu cã chøc n¨ng tho¸t vµ tù ®éng disconnect.§Ó thùc hµnh phÇn SQL vµ PL/SQL gäi øng dông SQL* Plus.1.3 Giíi thiÖu ng«n ng÷ SQL1.3.1 LÞch sö ph¸t triÓn cña ng«n ng÷ SQLM« h×nh c¬ së d÷ liÖu (CSDL) quan hÖ do E.F Codd ®−a ra vµo ®Çu thËp kû 70, tõ ®ã ®Õn nay nã liªn tôcph¸t triÓn trë thµnh m« h×nh CSDL phæ biÕn bËc nhÊt (RDBMS). M« h×nh quan hÖ gåm c¸c thµnh phÇn sau:• TËp hîp c¸c ®èi t−îng vµ/hoÆc c¸c mèi quan hÖ• TËp hîp c¸c xö lý t¸c ®éng tíi c¸c quan hÖ• Rµng buéc d÷ liÖu ®¶m b¶o tÝnh chÝnh x¸c vµ nhÊt qu¸n.SQL (Structured Query Language, ®äc lµ "sequel") lµ tËp lÖnh truy xuÊt CSDL quan hÖ. Ng«n ng÷ SQL ®−îcIBM sö dông ®Çu tiªn trong hÖ qu¶n trÞ CSDL System R vµo gi÷a nh÷ng n¨m 70, hÖ ng«n ng÷ SQL ®Çu tiªn(SEQUEL2) ®−îc IBM c«ng bè vµo th¸ng 11 n¨m 1976. N¨m 1979, tËp ®oµn ORACLE giíi thiÖu th−¬ngphÈm ®Çu tiªn cña SQL, SQL còng ®−îc cµi ®Æt trong c¸c hÖ qu¶n trÞ CSDL nh− DB2 cña IBM vµ SQL/DS.Ngµy nay, SQL ®−îc sö dông réng r·i vµ ®uîc xem lµ ng«n ng÷ chuÈn ®Ó truy cËp CSDL quan hÖ.1.3.2 ChuÈn SQLN¨m 1989, viÖn tiªu chuÈn quèc gia Hoa kú (ANSI) c«ng nhËn SQL lµ ng«n ng÷ chuÈn ®Ó truy cËp CSDLquan hÖ trong v¨n b¶n ANSI SQL89.N¨m 1989, tæ chøc tiªu chuÈn quèc tÕ (ISO) c«ng nhËn SQL ng«n ng÷ chuÈn ®Ó truy cËp CSDL quan hÖtrong v¨n b¶n ISO 9075-1989.TÊt c¶ c¸c hÖ qu¶n trÞ CSDL lín trªn thÕ giíi cho phÐp truy cËp b»ng SQL vµ hÇu hÕt theo chuÈn ANSI.1.4 C¸c kh¸i niÖm trong CSDLTable lµ cÊu tróc l−u tr÷ c¬ b¶n nhÊt trong CSDL quan hÖ (RDBMS), nã bao gåm 1 hoÆc nhiÒu column vµ 0 hoÆc nhiÒu row.Row lµ tæ hîp nh÷ng gi¸ trÞ cña Column trong b¶ng. Mét row cßn cã thÓ ®−îc gäi lµ 1 record.Column hiÓn thÞ mét lo¹i d÷ liÖu trong b¶ng, vÝ dô tªn phßng ban trong b¶ng phßng ban. Ng−êi ta thÓ hiÖn nã th«ng qua tªn column vµ gi÷ sè liÖu d−íi c¸c kiÓu vµ kÝch cì nhÊt ®Þnh.Field lµ giao cña column vµ row. Field chÝnh lµ n¬i chøa d÷ liÖu. NÕu kh«ng cã d÷ liÖu trong field ng−êi ta nãi field cã gia trÞ lµ null.Primary Key lµ mét column hoÆc mét tËp c¸c column x¸c ®Þnh tÝnh duy nhÊt cña c¸c row ë trong b¶ng. VÝ dô m· phßng ban. Primary Key nhÊt thiÕt ph¶i cã sè liÖu.Foreign Key lµ mét column hoÆc mét tËp c¸c column tham chiÕu tíi chÝnh b¶ng ®ã hoÆc mét b¶ng kh¸c. Foreign Key x¸c ®Þnh mèi quan hÖ gi÷a c¸c b¶ng.Constraint lµ c¸c rµng buéc d÷ liÖu, vÝ dô Foreign Key, Primary Key...§µo t¹o c¬ b¶n: SQL vµ PL/SQL Trang 7
  8. 8. C«ng ty cæ phÇn ®Çu t− ph¸t triÓn c«ng nghÖ - FPT Gi¸o tr×nh SQL vµ PL/SQLVÝ dô: EMP DEPT EMPNO ENAME DEPTNO EMP DEPT DEPTNO DNAME 7369 SMITH 20 10 ACCOUNTING Row 7499 ALLEN 30 20 RESEARCH 7521 WARD 30 30 SALES 7566 JONES 20 40 OPERATIONS 7654 MARTIN 30 7698 BLAKE 30 Foreign key 7782 CLARK 10 Primary key Column1.5 Danh s¸ch rót gän c¸c ®èi t−îng CSDLTable lµ cÊu tróc l−u tr÷ c¬ b¶n nhÊt trong CSDL quan hÖ (RDBMS), gåm row vµ columnView lµ cÊu tróc logic hiÓn thÞ d÷ liÖu tõ 1 hoÆc nhiÒu b¶ngSequence kÕt sinh gi¸ trÞ cho c¸c primary keyIndex t¨ng tÝnh thùc thi cña c¸u truy vÊnSynonym tªn t−¬ng ®−¬ng cña ®èi t−îngProgram unit gåm Procedure, function, package...1.6 C¸c lÖnh SQLLÖnh M« t¶SELECT Lµ lÖnh th«ng dông nhÊt, dïng ®Ó lÊy, xem d÷ liÖu trong CSDL.INSERT Lµ 3 lÖnh dïng ®Ó nhËp thªm nh÷ng row míi, thay ®æi néi dung d÷ liÖu trªn c¸c rowUPDATE hay xo¸ c¸c row trong table. Nh÷ng lÖnh nµy ®−îc gäi lµ c¸c lÖnh thao t¸c d÷ liÖuDELETE DML (Data Manipulation Language)CREATE Lµ 3 lÖnh dïng ®Ó thiÕt lËp, thay ®æi hay xo¸ bá cÊu tróc d÷ liÖu nh− lµ table, view,ALTER index. Nh÷ng lÖnh nµy ®−îc gäi lµ c¸c lÖnh ®Þnh nghÜa d÷ liÖu DDL (Data DefinitionDROP Language)RENAMETRUNCATECOMMIT Qu¶n lý viÖc thay ®æi d÷ liÖu b»ng c¸c lÖnh DML. ViÖc thay ®æi d÷ liÖu cã thÓ ®−îcROLLBACK nhãm l¹i thµnh c¸c transaction.SAVE POINTGRANT 2 lÖnh nµy dïng ®Ó g¸n hoÆc huû c¸c quyÒn truy nhËp vµo CSDL Oracle vµ c¸c cÊuREVOKE tróc bªn trong nã. Nh÷ng lÖnh nµy ®−îc gäi lµ c¸c lÖnh ®iÒu khiÓn d÷ liÖu DCL (Data§µo t¹o c¬ b¶n: SQL vµ PL/SQL Trang 8
  9. 9. C«ng ty cæ phÇn ®Çu t− ph¸t triÓn c«ng nghÖ - FPT Gi¸o tr×nh SQL vµ PL/SQL Control Language)1.7 Giíi thiÖu vÒ vÝ dô thùc hµnh1.7.1 M« h×nh quan hÖ d÷ liÖu DEPT EMP SALGRADE DUMMY BONUS1.7.2 M« t¶ d÷ liÖu Tªn KiÓu Kho¸ Gi¶i thÝch DEPT DEPTNO NUMBER(2) NOT NULL PK M· phßng ban DNAME CHAR(14) Tªn phßng ban LOC CHAR(13) §Þa chØ SALGRADE GRADE NUMBER PK Møc l¬ng LOSAL NUMBER Gi¸ trÞ thÊp HISAL NUMBER Gi¸ trÞ cao EMP EMPNO NUMBER(4) NOT NULL, PK M· nh©n viªn ENAME CHAR(10), Tªn nh©n viªn JOB CHAR(9), NghÒ nghiÖp MGR NUMBER(4) FK (EMP.EMPNO) M· ng−êi qu¶n lý HIREDATE DATE Ngµy gia nhËp c«ng ty SAL NUMBER(7,2) L−¬ng COMM NUMBER(7,2) Th−ëng DEPTNO NUMBER(2) NOT NULL, FK (DEPT.DEPTNO) M· phßng ban§µo t¹o c¬ b¶n: SQL vµ PL/SQL Trang 9
  10. 10. C«ng ty cæ phÇn ®Çu t− ph¸t triÓn c«ng nghÖ - FPT Gi¸o tr×nh SQL vµ PL/SQL2 LÖnh truy vÊn c¬ b¶n2.1 LÖnh truy vÊn c¬ b¶n SELECT [DISTINCT ] {*, column [alias],...} FROM table;• SELECT tr¶ lêi c©u hái lÊy d÷ liÖu nµo? (column, biÓu thøc...), trong mÖnh ®Ò SELECT cÇn cã Ýt nhÊt 1 column.• FROM tr¶ lêi c©u hái lÊy d÷ liÖu ë ®Çu? (table, view...)• DISTINCT chØ ®Þnh hiÓn thÞ 1 lÇn c¸c d÷ liÖu trïng nhau.• * thay cho viÖc chØ tªn tÊt c¶ c¸c column• alias ®−a ra nh·n cña column hiÓn thÞ d÷ liÖu.Vd: SELECT * FROM emp; SELECT empno, ename, deptno, mgr FROM emp; SELECT DISTINCT job nghenghiep FROM emp;2.2 C¸c thµnh phÇn kh¸c cña mÖnh ®Ò SELECTTrong mÖnh ®Ò SELECT cßn cã thÓ ®−a vµo c¸c thµnh phÇn kh¸c:• BiÓu thøc to¸n häc• Column alias• C¸c column ®−îc ghÐp chuçi• LiteralBiÓu thøc to¸n häcTrong mÖnh ®Ò SELECT biÓu thøc to¸n häc cã thÓ c¸c gi¸ trÞ (column hoÆc hµng sè), c¸c to¸n tö, c¸c hµm.C¸c to¸n tö ®−îc dïng lµ (+), (-), (*), (/). §é −u tiªn cña c¸c to¸n tö gièng trong phÇn sè häc. Vd: SELECT ename, sal *12, comm FROM emp; SELECT ename, (sal+250)*12 FROM emp;Column aliasTrong mÖnh ®Ò SELECT, column alias lµ phÇn nh·n hiÓn thÞ cña column khi lÊy sè liÖu ra. Trong columnalias kh«ng ®−îc cã dÊu c¸ch vµ viÕt c¸ch sau tªn column mét dÊu c¸ch. Column alias ®−îc chÊp nhËn cãdÊu c¸ch khi ®−îc ®Æt trong dÊu nh¸y kÐp (“ “).Vd: (ANUAL chÝnh lµ column alias) SELECT ename, SAL*12 ANUAL, comm FROM emp;C¸c column ®−îc ghÐp chuçiTo¸n tö ghÐp chuçi (||) cho phÐp c¸c column ®−îc nèi víi nhau thµnh d¹ng chuçi. Cã thÓ cã nhiÒu to¸n töghÐp chuçi trong cïng mét column alias. Vd: SELECT empno||ename EMPLOYEE FROM emp; SELECT ename || co luong la || (sal+250)*12 as "mieu ta" FROM emp; Chuçi ®Æt trong nh¸y ®¬n, bÝ danh ®Æt trong nh¸y kÐp§µo t¹o c¬ b¶n: SQL vµ PL/SQL Trang 10
  11. 11. C«ng ty cæ phÇn ®Çu t− ph¸t triÓn c«ng nghÖ - FPT Gi¸o tr×nh SQL vµ PL/SQLLiteralTrong mÖnh ®Ò SELECT, literal lµ bÊt kú ký tù nµo, biÓu thøc, hay sè nµp mµ kh«ng ph¶i lµ column hoÆccolumn alias. Vd: SELECT empno||ename EMPLOYEE, ‘WORK IN DEPARTMENT’, deptno FROM emp;2.3 Gi¸ trÞ NullCét cã gi¸ trÞ rçng (NULL) lµ cét ch−a ®−îc g¸n gi¸ trÞ, nãi c¸ch kh¸c nã ch−a ®−îc khëi t¹o gi¸ trÞ. C¸c cétvíi bÊt cø kiÓu d÷ liÖu nµo còng cã thÓ cã trÞ NULL, trõ khi ®−îc nã lµ khãa hay cã rµng buéc toµn vÑn NOTNULL. Trong biÓu thøc cã bÊt kú gi¸ trÞ NULL nµo kÕt qu¶ còng lµ NULL. Vd: SELECT ename, sal*12 + comm ANUAL_SAL FROM emp;NULL trong c¸c hµm cña SQL- Trong c¸c hµm lµm viÖc víi tõng cét hay hµm v« h−íng (scalar function)C¸c hµm lo¹i nµy tr¶ vÒ trÞ null khi cã tham sè null, trõ hµm NVL vµ TRANSLATE cã thÓ tr¶ vÒ gi¸ trÞ thùc.Có ph¸p cña hµm NVL NVL (DATECOLUMN,’01-01-2001’) NVL(NUMBERCOLUMN, 9) NVL(CHARCOLUMN,’STRING’)VÝ dô: NVL(comm,0) tr¶ vÒ trÞ 0 khi comm lµ null SELECT ename, sal*12 + NVL(comm,0) ANUAL_SAL FROM emp;- Trong c¸c hµm lµm viÖc víi nhãm c¸c cét (group function)HÇu hÕt c¸c hµm lµm viÖc trªn nhãm bá qua trÞ null, vÝ dô nh− khi sö dông hµm AVG ®Ó tÝnh trung b×nh chomét cét cã c¸c gi¸ trÞ 1000, null, null, null, 2000 khi ®ã trung b×nh ®−îc tÝnh lµ (1000+2000)/2=1500, nh− vËytrÞ null bÞ bá qua chø kh«ng ph¶i xem lµ trÞ 0.NULL trong c¸c biÓu thøc so s¸nh, ®iÒu kiÖn§Ó kiÓm tra cã ph¶i null hay kh«ng dïng c¸c to¸n tö IS NULL hoÆc IS NOT NULL. NÕu trong biÓu thøc sos¸nh cã trÞ null tham gia vµ kÕt qu¶ cña biÓu thøc phô thuéc vµo trÞ null th× kÕt qu¶ lµ kh«ng x¸c ®Þnh, tuynhiªn trong biÓu thøc DECODE, hai gi¸ trÞ null ®−îc xem lµ b»ng nhau trong phÐp so s¸nh.ORACLE xem c¸c biÓu thøc víi kÕt qu¶ kh«ng x¸c ®Þnh t−¬ng ®−¬ng víi FALSE, vÝ dô comm = NULL cã kÕtqu¶ kh«ng x¸c ®Þnh vµ do ®ã biÓu thøc so s¸nh xem nh− cho kÕt qu¶ FALSE. Trong c©u lÖnh sau kh«ng cãmÉu tin nµo ®−îc chänSELECT * FROM emp WHERE comm=NULL;NÕu muèn chän c¸c nh©n viªn cã comm lµ NULL th× ph¶i dïng to¸n tö IS NULLSELECT * FROM emp WHERE comm IS NULL;2.4 Läc d÷ liÖu tõ c¸c row cã cïng gi¸ trÞMét c©u lÖnh truy vÊn cã thÓ tr¶ vÒ c¸c row cã cïng gÝa trÞ. Vd: SELECT JOB FROM EMP; SELECT DEPTNO FROM EMP; SELECT JOB, DEPTNO FROM EMP;§µo t¹o c¬ b¶n: SQL vµ PL/SQL Trang 11
  12. 12. C«ng ty cæ phÇn ®Çu t− ph¸t triÓn c«ng nghÖ - FPT Gi¸o tr×nh SQL vµ PL/SQL§Ó läc lÊy mét gi¸ trÞ duy nhÊt ng−êi ta dïng mÖnh ®Ò DISTINCT. MÖnh ®Ò nµy ph¶i ®−îc ®Æt tr−íc tÊt c¶c¸c column, sau mÖnh ®Ò SELECT. Vd: SELECT DISTINCT JOB FROM EMP; SELECT DISTINCT DEPTNO FROM EMP; SELECT DISTINCT JOB, DEPTNO FROM EMP;2.5 HiÓn thÞ cÊu tróc b¶ngCó ph¸p DESC[RIBE] table_name(lÖnh nµy chØ ch¹y ®−îc trªn sqlplus, kh«ng ch¹y ®−îc trªn PL/SQL Develop)VÝ dô: DESC emp; Name Null? Type ------------------------------- -------- ---- EMPNO NOT NULL NUMBER(4) ENAME VARCHAR2(10) JOB VARCHAR2(9) MGR NUMBER(4) HIREDATE DATE SAL NUMBER(7,2) COMM NUMBER(7,2) DEPTNO NUMBER(2)KiÓu d÷ liÖu NOT NULL nghÜa lµ column nhÊt ®Þnh ph¶i cã gi¸ trÞ.2.6 C¸c lÖnh cña c«ng cô SQL*Plus2.6.1 C¸c lÖnh so¹n th¶o LÖnh M« t¶ A[PPEND] text §−a thªm ®o¹n text vµo dßng hiÖn t¹i C[HANGE] /old/new ChuyÓn ®o¹n text cò thµnh ®o¹n text míi trong dßng hiÖn t¹i C[HANGE] /text/ Xo¸ ®o¹n text trong dßng hiÖn t¹i CL[EAR] BUFF[ER] Xo¸ tÊt c¶ c¸c dßng trong SQL buffer DEL Xo¸ dßng hiÖn t¹i DEL n Xo¸ dßng n DEL m n Xo¸ dßng tõ m ®Õn n I[NPUT] Thªm mét sè dßng nhÊt ®Þnh I[NPUT] text Thªm dßng cã chøa text L[IST] LiÖt kª toµn bé c¸c dßng trong SQL buffer L[IST] n LiÖt kª dßng n L[IST] m n LiÖt kª dßng m ®Õn n§µo t¹o c¬ b¶n: SQL vµ PL/SQL Trang 12
  13. 13. C«ng ty cæ phÇn ®Çu t− ph¸t triÓn c«ng nghÖ - FPT Gi¸o tr×nh SQL vµ PL/SQL R[UN] HiÓn thÞ vµ ch¹y lÖnh trong buffer N Nhµy ®Õn dßng n N text Thay dßng n bëi ®o¹n text 0 text ChÌn 1 dßng tr−íc dßng 12.6.2 C¸c lÖnh vÒ file LÖnh M« t¶ SAVE filename [.ext] [REP[LACE]|APP[END]] Ghi néi dung bufer thµnh file. APPEND ®Ó ghi thªm vµo file. REPLACE ®Ó chÌn lªn néi dung file cò. GET filename [.ext] Ghi néi dung file vµo buffer. MÆc ®Þnh phÇn ®u«i lµ .sql STA[RT] filename [.ext] Ch¹y c¸c lÖnh trong file @ filename [.ext] Gièng lÖnh Start ED[IT] So¹n th¶o néi dung bufffer cã tªn lµ afiedt.buf §Ó ch¹y néi dung buffer dïng lÖnh / ED[IT] filename [.ext ] So¹n th¶o néi dung file SPO[OL] filename [.ext ] [OFF|OUT] CÊt kÕt qu¶ hiÓn thÞ trªn mµn h×nh ra file. Vd: SPOOL result.sql .... SPOOL OFF EXIT Tho¸t khái SQL*Plus2.6.3 C¸c lÖnh vÒ columnCó ph¸p COLUMN [{column | alias} [option]]LÖnh M« t¶CLE[AR] Xo¸ ®Þnh d¹ng cña columnFOR[MAT] format ChuyÓn ®Þnh d¹ng cña cét d÷ liÖuHEA[DING] text §Æt nh·n co columnJUS[TIFY] align C¸n tr¸i “ left , ph¶i - right, gi÷a - center cho nh·nNOPRI[NT] Èn columnNUL[L] text HiÓn thÞ text nÕu gi¸ trÞ cña column lµ NULLPRI[NT} HiÓn thÞ column§µo t¹o c¬ b¶n: SQL vµ PL/SQL Trang 13
  14. 14. C«ng ty cæ phÇn ®Çu t− ph¸t triÓn c«ng nghÖ - FPT Gi¸o tr×nh SQL vµ PL/SQLTRU[NCATED] Xo¸ chuçi t¹i cuèi dßng ®Çu tiªn khi hiÓn thÞWRA[PPED] Phñ cuèi chuçi cña dßng tiÕp theoWOR[D_WAPPED] Gièng WAP nh−ng tõ kh«ng bÞ c¾tL−u ý: c¸c ®Þnh d¹ng hiÓn thÞ nµy ®−îc l−u vµo bé nhí vµ ¸p dông cho mäi cét cã tªn nh− vËy, dï chóng ëtrong b¶ng nµoVd: ChØnh ®Þnh d¹ng vµ nh·n cña column COLUMN ename HEADING ‘Employee|Name’ FORMAT A15 COLUMN sal JUSTIFY LEFT FORMAT $ 99,990.00 COLUMN hiredate FORMAT A9 NULL ‘ Not hired’Vd: HiÓn thÞ ®Þnh d¹ng hiÖn t¹i cña column COLUMN COLUMN enameVd: Xo¸ ®Þnh d¹ng hiÖn t¹i cña column COLUMN ename CLEAR CLEAR COLUMNC¸c lo¹i ®Þnh d¹ng §Þnh M« t¶ VÝ dô KÕt qu¶ d¹ng An HiÓn thÞ dµi nhÊt n ký tù dïng cho c¸c column d¹ng ký tù hoÆc d¹ng ngµy 9 HiÓn thÞ sè, kh«ng bao gåm sè 0 999999 1234 0 HiÓn thÞ c¶ sè 0 099999 01234 $ HiÓn thi $ $9999 $1234 L HiÓn thÞ ký tù L L9999 L1234 . HiÓn thÞ dÊu thËp ph©n 9999.99 1234.00 , HiÓn thÞ dÊu ph©n chia hµng ngh×n 9,999 1,2342.7 Bµi tËp1. Chän toµn bé th«ng tin trong b¶ng SALGRADE GRADE LOSAL HISAL ---------- ---------- ---------- 1 700 1200 2 1201 1400 3 1401 2000 4 2001 3000 5 3001 99992. Chän toµn bé th«ng tin trong b¶ng EMP EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO ------ ---------- --------- ----- --------- ----- ------ ---------- 7839 KING PRESIDENT 17-11-1981 5000 10 7698 BLAKE MANAGER 7839 01-05-1981 2850 30 7782 CLARK MANAGER 7839 09-06-1981 2450 10 7566 JONES MANAGER 7839 02-04-1981 2975 20§µo t¹o c¬ b¶n: SQL vµ PL/SQL Trang 14
  15. 15. C«ng ty cæ phÇn ®Çu t− ph¸t triÓn c«ng nghÖ - FPT Gi¸o tr×nh SQL vµ PL/SQL 7654 MARTIN SALESMAN 7698 28-09-1981 1250 1400 30 7499 ALLEN SALESMAN 7698 20-02-1981 1600 300 30 7844 TURNER SALESMAN 7698 08-09-1981 1500 0 30 7900 JAMES CLERK 7698 03-12-1981 950 30 7521 WARD SALESMAN 7698 22-02-1981 1250 500 30 7902 FORD ANALYST 7566 03-12-1981 3000 20 7369 SMITH CLERK 7902 17-12-1980 800 20 7788 SCOTT ANALYST 7566 09-12-1982 3000 20 7876 ADAMS CLERK 7788 12-01-1983 1100 20 7934 MILLER CLERK 7782 23-01-1982 1300 103. HiÓn thÞ mäi lo¹i nghÒ nghiÖp JOB --------- ANALYST CLERK MANAGER PRESIDENT SALESMAN4. HiÓn thÞ tªn nh©n viªn vµ thu nhËp trong mét n¨m (REMUNERATION) ENAME REMUNERATION ---------- ------------ KING 60000 BLAKE 34200 CLARK 29400 JONES 35700 MARTIN 16400 ALLEN 19500 TURNER 18000 JAMES 11400 WARD 15500 FORD 36000 SMITH 9600 SCOTT 36000 ADAMS 13200 MILLER 15600 14 rows selected.5. HiÓn thÞ theo néi dung d−íi ®©y Who, what and when ---------------------------------------------------------------------- KING HAS HELP THE POSITION OF PRESIDENT IN DEPT 10 SINCE 17-11-1981 BLAKE HAS HELP THE POSITION OF MANAGER IN DEPT 30 SINCE 01-05-1981 CLARK HAS HELP THE POSITION OF MANAGER IN DEPT 10 SINCE 09-06-1981 JONES HAS HELP THE POSITION OF MANAGER IN DEPT 20 SINCE 02-04-1981 MARTIN HAS HELP THE POSITION OF SALESMAN IN DEPT 30 SINCE 28-09-1981 ALLEN HAS HELP THE POSITION OF SALESMAN IN DEPT 30 SINCE 20-02-1981 TURNER HAS HELP THE POSITION OF SALESMAN IN DEPT 30 SINCE 08-09-1981 JAMES HAS HELP THE POSITION OF CLERK IN DEPT 30 SINCE 03-12-1981 WARD HAS HELP THE POSITION OF SALESMAN IN DEPT 30 SINCE 22-02-1981 FORD HAS HELP THE POSITION OF ANALYST IN DEPT 20 SINCE 03-12-1981 SMITH HAS HELP THE POSITION OF CLERK IN DEPT 20 SINCE 17-12-1980 SCOTT HAS HELP THE POSITION OF ANALYST IN DEPT 20 SINCE 09-12-1982 ADAMS HAS HELP THE POSITION OF CLERK IN DEPT 20 SINCE 12-01-1983 MILLER HAS HELP THE POSITION OF CLERK IN DEPT 10 SINCE 23-01-1982 14 rows selected.6. HiÓn thÞ cÊu tróc b¶ng emp;7. Thay ®æi nh·n vµ ®Þnh d¹ng hiÓn thÞ cña cét sal vµ hiredate trong b¶ng emp;§µo t¹o c¬ b¶n: SQL vµ PL/SQL Trang 15
  16. 16. C«ng ty cæ phÇn ®Çu t− ph¸t triÓn c«ng nghÖ - FPT Gi¸o tr×nh SQL vµ PL/SQL3 Truy vÊn d÷ liÖu cã ®iÒu kiÖn3.1 MÖnh ®Ò ORDER BYCó ph¸p SELECT [DISTINCT ] {*, column [alias],...} FROM table; [WHERE condition] [ORDER BY expr/position [DESC/ASC]]MÖnh ®Ò ORDER BY dïng ®Ó s¾p xÕp sè liÖu ®−îc hiÓn thÞ vµ ph¶i ®Æt ë vÞ trÝ sau cïng cña c©u lÖnh truyvÊn, VÝ dô: SELECT ENAME, JOB, SAL*12, DEPTNO FROM EMP ORDER BY ENAME;MÖnh ®Ó ORDER BY mÆc ®Þnh s¾p xÕp theo thø tù t¨ng dÇn ASC[ENDING]• Sè thÊp tr−íc• Ngµy nhá tr−íc• Ký tù theo b¶ng ch÷ c¸i§Ó s¾p xÕp theo thø tù ng−îc l¹i (gi¶m dÇn) ®Æt tõ kho¸ DESC[ENDING] sau column cÇn s¾p thø tù. Vi dô: SELECT ENAME, JOB, HIREDATE FROM EMP ORDER BY HIREDATE DESC ;Order nhiÒu columnMÖnh ®Ò Order cßn cã thÓ s¾p xÕp nhiÒu column. C¸c column cÇn s¾p xÕp ®−îc viÕt thø tù sau mÖnh ®ÒORDER BY vµ c¸ch bëi dÊu phÈy (,). Column nµo gÇn mÖnh ®Ó ORDER BY h¬n cã møc ®é −u tiªn khi s¾pxÕp cao h¬n. ChØ ®Þnh c¸ch thøc s¾p xÕp ASC/DESC ®−îc viÕt sau column c¸ch bëi mét dÊu c¸ch. VÝ dô: SELECT DEPTNO, JOB, ENAME, SAL FROM EMP ORDER BY DEPTNO, SAL DESC ;Order gi¸ trÞ NULLRiªng ®èi víi gi¸ trÞ NULL, nÕu s¾p xÕp theo thø tù ASCENDING sÏ n»m ë c¸c vÞ trÝ cuèi cïng.Chó ýCã thÓ chØ ®Þnh s¾p xÕp theo thø tù c¸c column trong mÖnh ®Ò SELECT. VÝ dô: SELECT DEPTNO, JOB, ENAME, SAL FROM EMP ORDER BY 2;3.2 MÖnh ®Ò WHERECó ph¸p SELECT [DISTINCT ] {*, column [alias],...} FROM table; [WHERE condition (s)] [ORDER BY expr/position [DESC/ASC]]§µo t¹o c¬ b¶n: SQL vµ PL/SQL Trang 16
  17. 17. C«ng ty cæ phÇn ®Çu t− ph¸t triÓn c«ng nghÖ - FPT Gi¸o tr×nh SQL vµ PL/SQLMÖnh ®Ò WHERE dïng ®Ó ®Æt ®iÒu kiÖn cho toµn bé c©u lÖnh truy vÊn. Trong mÖnh ®Ò WHERE cã thÓ cãc¸c thµnh phÇn:• Tªn column• To¸n tö so s¸nh• Tªn column, h»ng sè hoÆc danh s¸ch c¸c gi¸ trÞVÝ dô: SELECT DEPTNO, JOB, ENAME, SAL FROM EMP WHERE SAL BETWEEN 1000 AND 2000 ;Truy vÊn d÷ liÖu víi nhiÒu ®iÒu kiÖnMÖnh ®Ò WHERE cho phÐp ghÐp ®−îc nhiÒu ®iÒu kiÖn th«ng qua c¸c to¸n tö logic AND/OR. To¸n tö ANDyªu cÇu d÷ liÖu ph¶i tho¶ m·n c¶ 2 ®iÒu kiÖn. To¸n tö OR cho phÐp d÷ liÖu tho¶ m·n 1 trong 2 ®iÒu kiÖn. VÝdô: SELECT DEPTNO, JOB, ENAME, SAL FROM EMP WHERE SAL BETWEEN 1000 AND 2000 AND JOB = ‘MANAGER’; SELECT DEPTNO, JOB, ENAME, SAL FROM EMP WHERE SAL BETWEEN 1000 AND 2000 OR JOB = ‘MANAGER’; SELECT DEPTNO, JOB, EMPNO, ENAME, SAL FROM EMP WHERE SAL > 1500 AND JOB = ‘MANAGER’ OR JOB =’SALESMAN’; SELECT DEPTNO, JOB, EMPNO, ENAME, SAL FROM EMP WHERE SAL > 1500 AND (JOB = ‘MANAGER’ OR JOB =’SALESMAN’);3.3 C¸c to¸n töTo¸n tö so s¸nh• = : To¸n tö b»ng hay t−¬ng ®−¬ng• !=, ^=, +, <> : To¸n tö kh¸c hay kh«ng t−¬ng ®−¬ng• > : To¸n tö lín h¬n• < : To¸n tö nhá h¬n• >= : To¸n tö lín h¬n hoÆc b»ng• <= : To¸n tö nhá h¬n hoÆc b»ngC¸c to¸n tö logic• NOT : Phñ ®Þnh mÖnh ®Ò• AND : yªu cÇu d÷ liÖu ph¶i tho¶ m·n c¶ 2 ®iÒu kiÖn• OR : cho phÐp d÷ liÖu tho¶ m·n 1 trong 2 ®iÒu kiÖn§µo t¹o c¬ b¶n: SQL vµ PL/SQL Trang 17
  18. 18. C«ng ty cæ phÇn ®Çu t− ph¸t triÓn c«ng nghÖ - FPT Gi¸o tr×nh SQL vµ PL/SQLC¸c to¸n tö cña SQL• [NOT] BETWEEN x AND y : [Kh«ng] lín h¬n hoÆc b»ng x vµ nhá h¬n hoÆc b»ng y.• IN (danh s¸ch) : thuéc bÊt kú gi¸ trÞ nµo trong danh s¸ch• x [NOT] LIKE y : §óng nÕu x [kh«ng] gièng khung mÉu y. C¸c ký tù dïng trong khu«n mÉu: DÊu g¹ch d−íi ( _ ) : ChØ mét ký tù bÊt kú DÊu phÇn tr¨m ( % ) : ChØ mét nhãm ký tù bÊt kú• IS [NOT] NULL : kiÓm tra gi¸ trÞ rçng• EXISTS : Tr¶ vÒ TRUE nÕu cã tån t¹i.[NOT] BETWEEN x AND yVÝ dô chän nh©n viªn cã l−¬ng n»m trong kho¶ng 2000 vµ 3000 SELECT * FROM emp WHERE sal BETEEN 2000 AND 3000;IN (danh s¸ch)Chän nh©n viªn cã l−¬ng b»ng mét trong 2 gi¸ trÞ 1400 hoÆc 3000 SELECT * FROM emp WHERE sal IN (1400, 3000);T×m tªn phßng ban nÕu phßng ®ã cã nh©n viªn lµm viÖc. SELECT dname FROM dept WHERE EXISTS (SELECT * FROM emp WHERE dept.deptno = emp.deptno);x [NOT] LIKE yT×m nh©n viªn cã tªn b¾t ®Çu b»ng chuçi SMITH SELECT * FROM emp WHERE ename LIKE SMITH_;§Ó chän nh÷ng nh©n viªn cã tªn b¾t ®Çu b»ng SM SELECT * FROM emp WHERE ename LIKE SM%;§Ó t×m nh÷ng nh©n viªn cã tªn cã chuçi A_B SELECT ename FROM emp WHERE ename LIKE %A_B%; ESCAPE V× ký hiÖu "_" dïng ®Ó ®¹i diÖn cho mét ký tù bÊt kú nªn nÕu kh«ng cã mÖnh ®Ò ESCAPE, c©u lÖnh trªn sÏt×m tÊt c¶ c¸c nh©n viªn tªn AAB, ABB, ACB, v.v... (nÕu kh«ng cã mÖnh ®Ò ESCAPE )NÕu muèn ký hiÖu "_" mang ý nghÜa nguyªn thñy, tøc lµ kh«ng cßn ®¹i diÖn cho ký tù bÊt kú n÷a, ta ®Æt dÊu"" tr−íc ký hiÖu. §ång thêi khai b¸o thªm mÖnh ®Ò ESCAPE ""Ta còng cã thÓ dïng mét ký tù bÊt kú thay cho "". Ch¼ng h¹n mÖnh ®Ò sau cã cïng kÕt qu¶ víi mÖnh ®Òtrªn SELECT ename FROM emp WHERE ename LIKE %A^_B%; ESCAPE ^Ta gäi c¸c ký tù nh− "" hay "^" nãi trªn lµ c¸c ký tù ESCAPE.IS [NOT] NULLVÝ duSELECT * FROM emp WHERE comm IS NULL ;§µo t¹o c¬ b¶n: SQL vµ PL/SQL Trang 18
  19. 19. C«ng ty cæ phÇn ®Çu t− ph¸t triÓn c«ng nghÖ - FPT Gi¸o tr×nh SQL vµ PL/SQL3.4 Bµi tËp1. Chän nh©n viªn trong b¶ng EMP cã møc l−¬ng tõ 1000 ®Õn 2000 (chän c¸c tr−êng ENAME, DEPTNO,SAL). ENAME DEPTNO SAL ---------- ---------- ---------- ALLEN 30 1600 WARD 30 1250 MARTIN 30 1250 TURNER 30 1500 ADAMS 20 1100 MILLER 10 1300Sal Between 1000 to 20002.HiÓn thÞ m· phßng ban, tªn phßng ban, s¾p xÕp theo thø tù tªn phßng ban. DEPTNO DNAME ------ ----------- 10 ACCOUNTING 40 OPERATIONS 20 RESEARCH 30 SALESOrder by dname3. HiÓn thÞ danh s¸ch nh÷ng nh©n viªn lµm t¹i phßng 10 vµ 20 theo thø tù A,B,C EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO ---------- ----------------- ----- ---------- ---------- ----- ----- 7876 ADAMS CLERK 7788 12-01-1983 1100 20 7782 CLARK MANAGER 7839 09-06-1981 2450 10 7902 FORD ANALYST 7566 03-12-1981 3000 20 7566 JONES MANAGER 7839 02-04-1981 2975 20 7839 KING PRESIDENT 17-11-1981 5000 10 7934 MILLER CLERK 7782 23-01-1982 1300 10 7788 SCOTT ANALYST 7566 09-12-1982 3000 20 7369 SMITH CLERK 7902 17-12-1980 800 20Where deptno in (10,20) order by ename asc4. HiÓn thÞ tªn vµ nghÒ nghiÖp nh÷ng nh©n viªn lµm nghÒ th− ký (clerk) t¹i phßng 20. ENAME JOB ---------- --------- SMITH CLERK ADAMS CLERKWhere upper(job)= upper(clerk) and deptno = 20; (l−u ý vÊn ®Ò ch÷ Hoa-th−êng)5. HiÓn thÞ tÊt c¶ nh÷ng nh©n viªn mµ tªn cã c¸c ký tù TH vµ LL. ENAME ---------- SMITH ALLEN MILLERWhere ename like “%TH%“ or ename like “%LL%“6. HiÓn thÞ tªn nh©n viªn, nghÒ nghiÖp, l−¬ng cña nh÷ng nh©n viªn cã gi¸m ®èc qu¶n lý. ENAME JOB SAL ---------- --------- ---------- SMITH CLERK 800 ALLEN SALESMAN 1600 WARD SALESMAN 1250 JONES MANAGER 2975§µo t¹o c¬ b¶n: SQL vµ PL/SQL Trang 19
  20. 20. C«ng ty cæ phÇn ®Çu t− ph¸t triÓn c«ng nghÖ - FPT Gi¸o tr×nh SQL vµ PL/SQL MARTIN SALESMAN 1250 BLAKE MANAGER 2850 CLARK MANAGER 2450 SCOTT ANALYST 3000 TURNER SALESMAN 1500 ADAMS CLERK 1100 JAMES CLERK 950 FORD ANALYST 3000 MILLER CLERK 1300 13 rows selected.7. HiÓn thÞ tªn nh©n viªn, m· phßng ban, ngµy gia nhËp c«ng ty sao cho gia nhËp c«ng ty trong n¨m 1983. ENAME DEPTNO HIREDATE ---------- ---------- --------- ADAMS 20 12-JAN-83Where to_char(hiredate) like “%83“Where hiredate like “%83“8. HiÓn thÞ tªn nh©n viªn, l−¬ng mét n¨m (ANUAL_SAL ), th−ëng sao cho l−¬ng lín h¬n th−ëng vµ nghÒnghiÖp lµ SALEMAN, s¾p theo thø tù l−¬ng gi¶m dÇn vµ tªn t¨ng dÇn. ANUAL_SAL COMM ---------- ---------- 19200 300 18000 0 15000 5004 C¸c hµm ¸p dông cho 1 dßng d÷ liÖu4.1 C¸c hµm sè§Çu vµo vµ ®Çu ra lµ c¸c gi¸ trÞ kiÓu sèROUND(n[,m]) cho gi¸ trÞ lµm trßn cña n (®Õn cÊp m, mÆc nhiªn m=0)TRUNC(n[,m]) cho gi¸ trÞ n lÊy m ch÷ sè tÝnh tõ chÊm thËp ph©n = Format(dl, “99999,00“)CEIL(n) cho sè nguyªn nhá nhÊt lín h¬n hoÆc b»ng n =FLOOR(n) cho sè nguyªn lín nhÊt b»ng hoÆc nhá h¬n n =POWER(m,n) cho lòy thõa bËc n cña m =EXP(n) cho gi¸ trÞ cña en =SQRT(n) cho c¨n bËc 2 cña n, n>=0 =SIGN(n) cho dÊu cña n. = n<0 cã SIGN(n)= -1 n=0 cã SIGN(n)= 0 n>0 cã SIGN(n)= 1ABS(n) cho gi¸ trÞ tuyÖt ®èi =MOD(m,n) cho phÇn d− cña phÐp chia m cho n =Mét sè hµm kiÓu sè tham kh¶o kh¸c:LOG(m,n) cho logarit c¬ sè m cña n =§µo t¹o c¬ b¶n: SQL vµ PL/SQL Trang 20
  21. 21. C«ng ty cæ phÇn ®Çu t− ph¸t triÓn c«ng nghÖ - FPT Gi¸o tr×nh SQL vµ PL/SQLSIN(n) cosin cña n (n tÝnh b»ng radian) =COS(n) cho cosin cña n (n tÝnh b»ng radian) =TAN(n) cotang cña n (n tÝnh b»ng radian) =VÝ dô hµm ROUND(n[,m]) SELECT ROUND(4.923,1), ROUND(4.923), ROUND(4.923,-1), ROUND(4.923,2) FROM DUMMY; ROUND(4.923,1) ROUND(4.923) ROUND(4.923,-1) ROUND(4.923,2) -------------- ------------ --------------- -------------- 4.9 5 0 4.92VÝ dô hµm TRUNC(n[,m]) SELECT TRUNC (4.923,1), TRUNC (4.923), TRUNC (4.923,-1), TRUNC (4.923,2) FROM DUMMY; TRUNC(4.923,1) TRUNC(4.923) TRUNC(4.923,-1) TRUNC(4.923,2) -------------- ------------ --------------- -------------- 4.9 4 0 4.92VÝ dô hµm CEIL(n) SELECT CEIL (SAL), CEIL(99.9),CEIL(101.76), CEIL(-11.1) FROM EMP WHERE SAL BETWEEN 3000 AND 5000; CEIL(SAL) CEIL(99.9) CEIL(101.76) CEIL(-11.1) ---------- ---------- ------------ ----------- 5000 100 102 -11 3000 100 102 -11 3000 100 102 -11VÝ dô hµm FLOOR(n) SELECT FLOOR (SAL), FLOOR (99.9), FLOOR (101.76), FLOOR (-11.1) FROM EMP WHERE SAL BETWEEN 3000 AND 5000; FLOOR(SAL) FLOOR(99.9) FLOOR(101.76) FLOOR(-11.1) ---------- ----------- ------------- ------------ 5000 99 101 -12 3000 99 101 -12 3000 99 101 -12VÝ dô hµm POWER(m,n) SELECT SAL, POWER(SAL,2), POWER(SAL,3), POWER(50,5) FROM EMP WHERE DEPTNO =10; SAL POWER(SAL,2) POWER(SAL,3) POWER(50,5) ---------- ------------ ------------ ----------- 5000 25000000 1.2500E+11 312500000 2450 6002500 1.4706E+10 312500000 1300 1690000 2197000000 312500000VÝ dô hµm EXP(n) SELECT EXP(4) FROM DUMMY; EXP(4)§µo t¹o c¬ b¶n: SQL vµ PL/SQL Trang 21
  22. 22. C«ng ty cæ phÇn ®Çu t− ph¸t triÓn c«ng nghÖ - FPT Gi¸o tr×nh SQL vµ PL/SQL ---------- 54.59815VÝ dô hµm SQRT(n) SELECT SAL, SQRT(SAL), SQRT(40), SQRT (COMM) FROM EMP WHERE DEPTNO =10; SAL SQRT(SAL) SQRT(40) SQRT(COMM) ---------- ---------- ---------- ---------- 5000 70.7106781 6.32455532 2450 49.4974747 6.32455532 1300 36.0555128 6.32455532VÝ dô hµm SIGN(n) SELECT SAL-NVL(COMM,0), SIGN(SAL-NVL(COMM,0)), NVL(COMM,0)-SAL, SIGN(NVL(COMM,0)-SAL) FROM EMP WHERE DEPTNO =30 SAL-NVL(COMM,0)SIGN(SAL-NVL(COMM,0))NVL(COMM,0)-SAL SIGN(NVL(COMM,0)-SAL) --------------- ----------- --------------- --------------------- 2850 1 -2850 -1 -150 -1 150 1 1300 1 -1300 -1 1500 1 -1500 -1 950 1 -950 -1 750 1 -750 -14.2 C¸c hµm ký tùCONCAT(char1, char2) cho kÕt hîp cña 2 chuçi ký tù, t−¬ng tù nh− sö dông to¸n tö ||INITCAP(char) cho chuçi víi ký tù ®Çu c¸c tõ lµ ký tù hoaLOWER(char) cho chuçi ký tù viÕt th−êng (kh«ng viÕt hoa)LPAD(char1, n [,char2]) cho chuçi ký tù cã chiÒu dµi b»ng n. NÕu chuçi char1 ng¾n h¬n n th× thªm vµo bªn tr¸i chuçi char2 cho ®ñ n ký tù. NÕu chuçi char1 dµi h¬n n th× gi÷ l¹i n ký tõ tÝnh tõ tr¸i sangLTRIM(char1, n [,char2]) bá c¸c ký tù trèng bªn tr¸iNLS_INITCAP(char) cho chuçi víi ký tù ®Çu c¸c tõ lµ ch÷ hoa, c¸c ch÷ cßn l¹i lµ ch÷ th−êngREPLACE(char,search_string[,replacement_string]) : thay tÊt c¶ c¸c chuçi search_string cã trong chuçi char b»ng chuçi replacement_string.RPAD(char1, n [,char2]) Gièng LPAD(char1, n [,char2]) nh−ng c¨n ph¶iRTRIM(char1, n [,char2]) bá c¸c ký tù trèng bªn ph¶iSOUNDEX(char) cho chuçi ®ång ©m cña char.SUBSTR(char, m [,n]) cho chuçi con cña chuçi char lÊy tõ vÞ trÝ m vÕ ph¶i n ký tù, nÕu kh«ng chØ n th× lÊy cho ®Õn cuèi chuçiTRANSLATE(char, from, to) cho chuçi trong ®ã mçi ký tù trong chuçi from thay b»ng ký tù t−¬ng øng trong chuçi to, nh÷ng ký tù trong chuçi from kh«ng cã t−¬ng øng trong chuçi to sÏ bÞ lo¹i bá.UPPER(char) cho chuçi ch÷ hoa cña chuçi char§µo t¹o c¬ b¶n: SQL vµ PL/SQL Trang 22
  23. 23. C«ng ty cæ phÇn ®Çu t− ph¸t triÓn c«ng nghÖ - FPT Gi¸o tr×nh SQL vµ PL/SQLASCII(char) cho ký tù ASCII cña byte ®Çu tiªn cña chuçi charINSTR(char1, char2 [,n[,m]]) t×m vÞ trÝ chuçi char2 trong chuçi char1 b¾t ®Çu tõ vÞ trÝ n, lÇn xuÊt hiÖn thø m.LENGTH(char) cho chiÒu dµi cña chuçi charVÝ dô hµm LOWER(char) SELECT LOWER(DNAME), LOWER(‘SQL COURSE’) FROM DEPT; LOWER(DNAME) LOWER(SQL -------------- ---------- accounting sql course research sql course sales sql course operations sql courseVÝ dô hµm UPPER(char) SELECT ENAME FROM EMP WHERE ENAME = UPPER(‘Smith’); ENAME ---------- SMITHVÝ dô hµm INITCAP(char) SELECT INITCAP(DNAME), INITCAP(LOC) FROM DEPT; INITCAP(DNAME) INITCAP(LOC) -------------- ------------- Accounting New York Research Dallas Sales Chicago Operations BostonVÝ dô hµm CONCAT(char1, char2) SELECT CONCAT(ENAME, JOB) JOB FROM EMP WHERE EMPNO = 7900; JOB ------------------- JAMES CLERKVÝ dô hµm LPAD(char1, n [,char2]) SELECT LPAD(DNAME,20,’*’), LPAD(DNAME,20), LPAD(DEptno,20,’ ’) FROM DEPT; LPAD(DNAME,20,*) LPAD(DNAME,20) LPAD(DEPTNO,20,) -------------------- -------------------- -------------------- ******ACCOUNTING ACCOUNTING 10 ******RESEARCH RESEARCH 20 ******SALES SALES 30 ******OPERATIONS OPERATIONS 40VÝ dô hµm RPAD(char1, n [,char2]) SELECT RPAD(DNAME,20,’*’), RPAD(DNAME,20), RPAD(DEptno,20,’ ’) FROM DEPT;§µo t¹o c¬ b¶n: SQL vµ PL/SQL Trang 23
  24. 24. C«ng ty cæ phÇn ®Çu t− ph¸t triÓn c«ng nghÖ - FPT Gi¸o tr×nh SQL vµ PL/SQL RPAD(DNAME,20,*) RPAD(DNAME,20) RPAD(DEPTNO,20,) -------------------- -------------------- -------------------- ACCOUNTING ****** ACCOUNTING 10 RESEARCH ****** RESEARCH 20 SALES ****** SALES 30 OPERATIONS ****** OPERATIONS 40VÝ dô hµm SUBSTR(char, m [,n]) SELECT SUBSTR(‘ORACLE’,2,4), SUBSTR(DNAME,2), SUBSTR(DNAME,3,5) FROM DEPT; SUBS SUBSTR(DNAME, SUBST ---- ------------- ----- RACL CCOUNTING COUNT RACL ESEARCH SEARC RACL ALES LES RACL PERATIONS ERATIVÝ dô hµm INSTR(char1, char2 [,n[,m]]) SELECT DNAME, INSTR(DNAME, ‘A’), INSTR(DNAME,’ES’), INSTR(DNAME,’C’,1,2) FROM DEPT; DNAME INSTR(DNAME,A) INSTR(DNAME,ES) INSTR(DNAME,C,1,2) -------------- ---------------- ----------------- -------------------- ACCOUNTING 1 0 3 RESEARCH 5 2 0 SALES 2 4 0 OPERATIONS 5 0 0VÝ dô hµm LTRIM(char1, n [,char2]) SELECT DNAME, LTRIM(DNAME,’A’), LTRIM(DNAME,’AS’), LTRIM(DNAME,’ASOP’) FROM DEPT; DNAME LTRIM(DNAME,A LTRIM(DNAME,A LTRIM(DNAME,A -------------- -------------- -------------- -------------- ACCOUNTING CCOUNTING CCOUNTING CCOUNTING RESEARCH RESEARCH RESEARCH RESEARCH SALES SALES LES LES OPERATIONS OPERATIONS OPERATIONS ERATIONSVÝ dô hµm RTRIM(char1, n [,char2]) SELECT DNAME, RTRIM(DNAME,’A’), RTRIM(DNAME,’AS’), RTRIM(DNAME,’ASOP’) FROM DEPT; DNAME RTRIM(DNAME,A RTRIM(DNAME,A RTRIM(DNAME,A -------------- -------------- -------------- -------------- ACCOUNTING ACCOUNTING ACCOUNTING ACCOUNTING RESEARCH RESEARCH RESEARCH RESEARCH SALES SALES SALES SALES OPERATIONS OPERATIONS OPERATIONS OPERATIONSVÝ dô hµm SOUNDEX(char) SELECT ENAME, SOUNDEX(ENAME) FROM EMP WHERE SOUNDEX(ENAME)= SOUNDEX(‘FRED’); ENAME SOUN ---------- ---- FORD F630VÝ dô hµm LENGTH(char) SELECT LENGTH(‘SQL COURSE’), LENGTH(DEPTNO), LENGTH(DNAME) FROM DEPT;§µo t¹o c¬ b¶n: SQL vµ PL/SQL Trang 24
  25. 25. C«ng ty cæ phÇn ®Çu t− ph¸t triÓn c«ng nghÖ - FPT Gi¸o tr×nh SQL vµ PL/SQL§µo t¹o c¬ b¶n: SQL vµ PL/SQL Trang 25
  26. 26. C«ng ty cæ phÇn ®Çu t− ph¸t triÓn c«ng nghÖ - FPT Gi¸o tr×nh SQL vµ PL/SQL LENGTH(SQLCOURSE) LENGTH(DEPTNO) LENGTH(DNAME) ------------------- -------------- ------------- 10 2 14 10 2 14 10 2 14 10 2 14VÝ dô hµm TRANSLATE(char, from, to) SELECT ENAME, TRANSLATE(ENAME,C,F), JOB, TRANSLATE(JOB,AR,IT) FROM EMP WHERE DEPTNO = 10; ENAME TRANSLATE( JOB TRANSLATE ---------- ---------- --------- --------- KING KING PRESIDENT PTESIDENT CLARK FLARK MANAGER MINIGET MILLER MILLER CLERK CLETKVÝ dô hµm REPLACE(char,search_string[,replacement_string]) SELECT JOB, REPLACE(JOB, ‘SALESMAN’, ‘SALESPERSON’), ENAME, REPLACE(ENAME, ‘CO’,’PR’) FROM EMP WHERE DEPTNO =30 OR DEPTNO =20; JOB REPLACE(JOB,SALESMAN, ENAME REPLACE(ENAME,CO, --------- ----------------------- --------- --------------- MANAGER MANAGER BLAKE BLAKE MANAGER MANAGER JONES JONES SALESMAN SALESPERSON MARTIN MARTIN SALESMAN SALESPERSON ALLEN ALLEN SALESMAN SALESPERSON TURNER TURNER CLERK CLERK JAMES JAMES SALESMAN SALESPERSON WARD WARD ANALYST ANALYST FORD FORD CLERK CLERK SMITH SMITH ANALYST ANALYST SCOTT SPRTT CLERK CLERK ADAMS ADAMSVÝ dô c¸c hµm lång nhau: SELECT DNAME, LENGHT(DNAME), LENGHT(TRANSLATE,DNAME, ‘AS’,’A’)) FROM DEPT; DNAME LENGTH(DNAME) LENGTH(TRANSLATE(DNAME,AS,A)) -------------- ------------- --------------------------------- ACCOUNTING 14 14 RESEARCH 14 13 SALES 14 12 OPERATIONS 14 134.3 C¸c hµm ngµyMONTH_BETWEEN(d1, d2) cho biÕt sã th¸ng gi÷a ngµy d1 vµ d2.ADD_MONTHS(d,n) cho ngµy d thªm n th¸ng.NEXT_DAY(d, char ) cho ngµy tiÕp theo ngµy d cã thø chØ bëi char.LAST_DAY(d) cho ngµy cuèi cïng trong th¸ng chØ bëi d.VÝ dô hµm MONTH_BETWEEN(d1, d2) SELECT MONTHS_BETWEEN( SYSDATE, HIREDATE), MONTHS_BETWEEN(01-01-2000,05-10-2000)§µo t¹o c¬ b¶n: SQL vµ PL/SQL Trang 26
  27. 27. C«ng ty cæ phÇn ®Çu t− ph¸t triÓn c«ng nghÖ - FPT Gi¸o tr×nh SQL vµ PL/SQL FROM EMP WHERE MONTHS_BETWEEN( SYSDATE,HIREDATE)>240; MONTHS_BETWEEN(SYSDATE,HIREDATE) TWEEN(01-01-2000,05-10-2000) -------------------------------- -------------------------------- 241.271055 -9.1290323 241.206539 -9.1290323 243.367829 -9.1290323VÝ dô hµm ADD_MONTHS(d,n) SELECT HIREDATE, ADD_MONTHS(HIRE,3), ADD_MONTHS(HIREDATE,-3) FROM EMP WHERE DEPTNO=20; HIREDATE ADD_MONTHS ADD_MONTHS ---------- ---------- ---------- 02-04-1981 02-07-1981 02-01-1981 03-12-1981 03-03-1982 03-09-1981 17-12-1980 17-03-1981 17-09-1980 09-12-1982 09-03-1983 09-09-1982 12-01-1983 12-04-1983 12-10-1982VÝ dô hµm NEXT_DAY(d, char ) SELECT HIREDATE, NEXT_DAY(HIREDATE,’FRIDAY’), NEXT_DAY(HIREDATE,6) FROM EMP WHERE DEPTNO = 10; HIREDATE NEXT_DAY(H NEXT_DAY(H ---------- ---------- ---------- 17-11-1981 20-11-1981 20-11-1981 09-06-1981 12-06-1981 12-06-1981 23-01-1982 29-01-1982 29-01-1982VÝ dô hµm LAST_DAY(d) SELECT SYSDATE, LAST_DAY(SYSDATE), HIREDATE, LAST_DAY(HIREDATE), LAST_DAY(’15-01-2001’) FROM EMP WHERE DEPTNO =20; SYSDATE LAST_DAY(S HIREDATE LAST_DAY(H LAST_DAY( ---------- ---------- ---------- ---------- ---------- 28-03-2001 31-03-2001 02-04-1981 30-04-1981 31-01-2001 28-03-2001 31-03-2001 03-12-1981 31-12-1981 31-01-2001 28-03-2001 31-03-2001 17-12-1980 31-12-1980 31-01-2001 28-03-2001 31-03-2001 09-12-1982 31-12-1982 31-01-2001 28-03-2001 31-03-2001 12-01-1983 31-01-1983 31-01-2001Mét sè hµm kh¸c cã thÓ ¸p dông cho kiÓu ngµy:ROUND(date1) tr¶ vÒ ngµy date 1 t¹i thêi ®iÓm gi÷a tr−a 12:00 AMROUND(date1,’MONTH’) NÕu date 1 n»m trong nöa th¸ng ®Çu tr¶ vÒ ngµy ®Çu tiªn cña thµng, ng−îc l¹i sÏ tr¶ vÒ ngµy ®Çu tiªn cña th¸ng sau.ROUND(date1,’YEAR’) NÕu date 1 n»m trong nöa n¨m ®Çu tr¶ vÒ ngµy ®Çu tiªn cña thµng, ng−îc l¹i sÏ tr¶ vÒ ngµy ®Çu tiªn cña n¨m sau.TRUNC(date1, ’MONTH’) Tr¶ vÒ ngµy ®Çu tiªn cña th¸ng chøa date1TRUNC(date1, ’YEAR’) Tr¶ vÒ ngµy ®Çu tiªn cña n¨m chøa date1§µo t¹o c¬ b¶n: SQL vµ PL/SQL Trang 27
  28. 28. C«ng ty cæ phÇn ®Çu t− ph¸t triÓn c«ng nghÖ - FPT Gi¸o tr×nh SQL vµ PL/SQL4.4 C¸c hµm chuyÓn ®æi kiÓuTO_CHAR(number|date, ‘fmt’) ChuyÓn kiÓu sè vµ ngµy vÒ kiÓu ký tù.TO_NUMBER(char) ChuyÓn ký tù cã néi dung sè sang sèTO_DATE(‘chsr’,’fmt’) ChuyÓn ký tù sang kiÓu ngµy víi ®Þnh d¹ng ®Æt trong fmt.DECODE(EXPR, SEARCH1, RESULT1, SEARCH2, RESULT2, DEFAULT): So s¸nh biÓu thøc expr víi gi¸ trÞ search nÕu ®óng tr¶ vÒ gi¸ trÞ result nÕu kh«ng tr¶ vÒ gi¸ trÞ default.NVL(COL|VALUE, VAL) ChuyÓn gi¸ trÞ COL|VALUE thµnh val nÕu null.Greatest(col|value1, col|value2) Tr¶ gi¸ trÞ lín nhÊt trong d·y gi¸ trÞ.Vd: SELECT To_char (sysdate, ‘day, ddth month yyyy’) from dummy; SELECT EMPNO, ENAME, HIREDATE FROM EMP WHERE HIREDATE = TO_DATE (‘June 4, 1984’, ‘month dd, yyyy’); INSERT INTO EMP (EMPNO, DEPTNO, HIREDATE VALUES (777, 20, TO_DATE(’19-08-2000’, ‘DD-MM-YYYY’); SELECT ENAME, JOB, DECODE (JOB, ‘CLERK’,’WWORKER’,’MANAGER’,’BOSS’,’UNDEFINED’) DECODÑD_JOB FROM EMP; SELECT GREATEST(1000,2000), GREATEST(SAL,COMM) FROM EMP WHERE DEPTNO = 10;Mét sè khu«n d¹ng ngµy SCC hoÆc CC thÕ kû; S chØ ngµy BC YYYY hoÆc SYYYY n¨m; S chØ ngµy BC YYY, YY, Y ChØ n¨m víi 3,2,1 ký tù sè IYYY, IYY, IY, I ChØ n¨m theo chuÈn ISO SYEAR, YEAR ChØ n¨m theo c¸ch ph¸t ©m cña ng−êi anh; Q Quý trong n¨m MM Gi¸ trÞ th¸ng víi 2 sè (01-12) MONTH Tªn ®Çy ®ñ cña th¸ng theo tiÕng anh, ®ä dµi 9 MON Th¸ng víi 3 ký tù viÕn t¾t (JAN, FEB...) WW, W TuÇn trong n¨m hoÆc trong th¸ng DDD, DD, D Ngµy trong n¨m, th¸ng hoÆc tuÇn DAY ChØ thø trong tuÇn DY ChØ thø trong tuÇn víi 3 ký tù viÕt t¾t J Ngµy Julian; b¾t ®Çu tõ ngµy 31/12/4713 tr−íc c«ng nguyªn AM, PM ChØ ®Þnh s¸ng, chiÒu HH, HH12 HH24 ChØ giê trong ngµy (1-12) hoÆc (0-23) MI Phót (0-59) SS Gi©y (0-59) SSSSS Sè gi©y ®Õn nöa ®ªm (0-86399) / . , - ®−îc tù ®éng thªm khi ®Æt trong khu«n d¹ng “char” §o¹n ký tù ®Æt trong nh¸y ®óp ®−îc tù ®éng thªm khi ®Æt trong khu«n d¹ng TH Thªm phÇn thø tù (1st, 2nd, 4th ) SP Ph¸t ©m sè ( FOUR víi DDSP)§µo t¹o c¬ b¶n: SQL vµ PL/SQL Trang 28
  29. 29. C«ng ty cæ phÇn ®Çu t− ph¸t triÓn c«ng nghÖ - FPT Gi¸o tr×nh SQL vµ PL/SQL SPTH, THSP Ph¸t ©m vµ chuyÓn sang d¹ng thø tù ( First, second, ...) RR Ngµy chuyÓn giao thiªn niªn kû víi c¸c n¨m <1999. N¨m 0-49 50-99 N¨m hiÖn 0-49 thÕ kû hiÖn ThÕ kû t¹i t¹i sau 50-99 ThÕ kû tr−íc ThÓ kû hiÖn t¹iMét sè khu«n d¹ng sè Ký M« t¶ VÝ dô KÕt qu¶ tù 9 X¸c ®Þnh hiÓn thÞ 1 sè 999999 1234 0 HiÓn thÞ c¶ sè 0 ë ®Çu nÕu ®é 099999 001234 dµi khu«n d¹ng lín h¬n sè hiÖn cã $ Thªm ký tù tiÒn tÖ $999999 $1234 L Thªm ký tù tiÒn tÖ b¶n ®Þa L999999 FF1234 . DÊu thËp ph©n 999999.99 1234.00 , DÊu ph©n c¸ch phÇn ngh×n 999,999 1,234 MI DÊu ©m ë bªn ph¶i ( víi c¸c 999999MI 1234- gi¸ trÞ ©m) PR Thªm ngoÆc nhän vµo c¸c gi¸ 999999PR <1234> trÞ ©m EEE ChuyÓn sang hiÓn thÞ sè E 99.9999RRRR 1.234E+03 V Nh©n víi 10 n, n lµ sè c¸c sè 9999V99 123400 9 ®Æt sau V B HiÓn thÞ c¶ gi¸ trÞ 0 nÕu = B9999.99 1234.00 0.4.5 Bµi tËp1. LiÖt kª tªn nh©n viªn, m· phßng ban vµ l−¬ng nh©n viªn ®−îc t¨ng 15% (PCTSAL). DEPTNO ENAME PCTSAL ---------- ---------- ---------- 10 KING 5000 30 BLAKE 2850 10 CLARK 2450 20 JONES 2975 30 MARTIN 1250 30 ALLEN 1600 30 TURNER 1500 30 JAMES 950 30 WARD 1250 20 FORD 3000 20 SMITH 800 20 SCOTT 3000 20 ADAMS 1100 10 MILLER 13002. ViÕt c©u lÖnh hiÓn thÞ nh− sau: EMPLOYEE_AND_JOB -------------------- KING*******PRESIDENT BLAKE********MANAGER CLARK********MANAGER JONES********MANAGER MARTIN******SALESMAN ALLEN*******SALESMAN TURNER******SALESMAN JAMES**********CLERK WARD********SALESMAN FORD*********ANALYST§µo t¹o c¬ b¶n: SQL vµ PL/SQL Trang 29
  30. 30. C«ng ty cæ phÇn ®Çu t− ph¸t triÓn c«ng nghÖ - FPT Gi¸o tr×nh SQL vµ PL/SQL SMITH**********CLERK SCOTT********ANALYST ADAMS**********CLERK MILLER*********CLERK3. ViÕt c©u lÖnh hiÓn thÞ nh− sau: EMPLOYEE ----------------- KING (President) BLAKE (Manager) CLARK (Manager) JONES (Manager) MARTIN (Salesman) ALLEN (Salesman) TURNER (Salesman) JAMES (Clerk) WARD (Salesman) FORD (Analyst) SMITH (Clerk) SCOTT (Analyst) ADAMS (Clerk) MILLER (Clerk)4. ViÕt c©u lÖnh hiÓn thÞ nh− sau: ENAME DEPTNO JOB ---------- ---------- --------------- BLAKE 30 Manager MARTIN 30 Salesperson ALLEN 30 Salesperson TURNER 30 Salesperson JAMES 30 Clerk WARD 30 Salesperson5. T×m ngµy thø 6 ®Çu tiªn c¸ch 2 th¸ng so víi ngµy hiÖn t¹i hiÓn thÞ ngµy d−íi d¹ng 09 February 1990.6. T×m th«ng itn vÒ tªn nh©n viªn, ngµy gia nhËp c«ng ty cña nh©n viªn phßng sè 20, sao cho hiÓn thÞ nh−sau: ENAME DATE_HIRED ---------- -------------------------- JONES april,SECOND 1981 FORD december,THIRD 1981 SMITH december,SEVENTEENTH 1980 SCOTT december,NINTH 1982 ADAMS january,TWELFTH 19837. HiÓn thÞ tªn nh©n viªn, ngµy gia nhËp c«ng ty, ngµy xÐt n©ng l−¬ng (sau ngµy gia nhËp c«ng ty 1 n¨m),s¾p xÕp theo thø tù ngµy xÐt n©ng l−¬ng. ENAME HIREDATE REVIEW ---------- ---------- ---------- SMITH 17-12-1980 17-12-1981 ALLEN 20-02-1981 20-02-1982 WARD 22-02-1981 22-02-1982 JONES 02-04-1981 02-04-1982 BLAKE 01-05-1981 01-05-1982 CLARK 09-06-1981 09-06-1982 TURNER 08-09-1981 08-09-1982 MARTIN 28-09-1981 28-09-1982 KING 17-11-1981 17-11-1982 JAMES 03-12-1981 03-12-1982 FORD 03-12-1981 03-12-1982 MILLER 23-01-1982 23-01-1983 SCOTT 09-12-1982 09-12-1983 ADAMS 12-01-1983 12-01-19848.HiÓn thÞ tªn nh©n viªn vµ l−¬ng d−íi d¹ng§µo t¹o c¬ b¶n: SQL vµ PL/SQL Trang 30
  31. 31. C«ng ty cæ phÇn ®Çu t− ph¸t triÓn c«ng nghÖ - FPT Gi¸o tr×nh SQL vµ PL/SQL ENAME SALARY ---------- ------------ ADAMS BELOW 1500 ALLEN 1600 BLAKE 2850 CLARK 2450 FORD 3000 JAMES BELOW 1500 JONES 2975 KING 5000 MARTIN BELOW 1500 MILLER BELOW 1500 SCOTT 3000 SMITH BELOW 1500 TURNER On Target WARD BELOW 15009. Cho biÕt thø cña ngµy hiÖn t¹i10. §−a chuçi d−íi d¹ng nn/nn, kiÓm tra nÕu khóng khu«n d¹ng tr¶ lêi lµ YES, ng−îc l¹i lµ no. KiÓm tra víic¸c chuçi 12/34, 01/1a, 9988 VALUE VALID? ----- ------- 12/34 YES11. HiÓn thÞ tªn nh©n viªn, ngµy gia nhËp c«ng ty, ngµy lÜnh l−¬ng sao cho ngµy lÜnh l−¬ng ph¶i vµo thø 6,nh©n viªn chØ ®−îc nhËn l−¬ng sau Ýt nhÊt 15 ngµy lµm viÖc t¹i c«ng ty, s¾p xÕp theo thø tù ngµy gia nhËpc«ng ty.5 BiÕn runtimeD÷ liÖu thay thÕ trong c©u lÖnhDïng (&) ®Ó chØ phÇn thay thÕ trong c©u lÖnh.NÕu dïng (&&) chØ biÕn thay thÕ th× sau c©u lÖnh biÕn thay thÕ vÉn cßn tån t¹iVÝ dô SELECT * FROM emp WHERE &Condition Enter value for condition: sal > 1000 Khi Êy c©u lÖnh trªn t−¬ng ®−¬ngSELECT * FROM empWHERE sal > 1000VÝ du 2: Select ename, deptno, job From emp Where deptno = &&depno_please;LÖnh DefineKhai b¸o vµ g¸n trÞ cho c¸c biÕn, vÝ dô khai b¸o biÕn condition cã tri sal > 1000 DEFINE condition = sal > 1000Khi ®ã c©u lÖnh sau kh«ng yªu cÇu nhËp vµo gi¸ trÞ cho codition SELECT * FROM emp WHERE &Condition§µo t¹o c¬ b¶n: SQL vµ PL/SQL Trang 31
  32. 32. C«ng ty cæ phÇn ®Çu t− ph¸t triÓn c«ng nghÖ - FPT Gi¸o tr×nh SQL vµ PL/SQL§Ó lo¹i bá biÕn ra khái bé nhí dïng lÖnh UNDEFINE, vÝ dô UNDEFINE condition§Ó liÖt kª c¸c biÕn ®· khai b¸o dïng lÖnh DEFINE mµ kh«ng chØ biÕn, vÝ dô DEFINE DEFINE CONDITION = SAL > 1000VÝ dô: DEFINE REM=’SAL*12+NVL(COMM,0)’ SELECT ENAME, JOB, &REM FROM EKP ORDER BY & REM;LÖnh AcceptKhai b¸o vµ g¸n trÞ cho biÕn víi dßng hiÓn thÞ ACCEPT variable [NUMBER/CHAR] [PROMPT/NOPROMPT text] HIDEVÝ dô ACCEPT Salary NUMBER PROMPT Salary figure: Salary figure : 3000Tõ kho¸ hide cho phÐp che chuçi nhËp liÖu, hay dïng khi nhËp password. ACCEPT password CHAR PROMPT Enter password: HIDE Password : ******5.1 Bµi tËp1. HiÓn thÞ tªn nh©n viªn, ngµy gia nhËp c«ng ty víi ®iÒu kiÖn ngµy gia nhËp c«ng ty n»m trong kho¶ng haibiÕn runtime ®−îc nhËp vµo tõ bµn phÝm (&first_date, &last_date).2. HiÓn thÞ tªn nh©n viªn, nghÒ nghiÖp, l−¬ng, m· gi¸m ®èc, m· phßng ban víi ®iÒu kiÖn nghÒ nghiÖp b»ngmét biÕn ®−îc nhËp vµo tõ bµn phÝm. (&job)3. §Þnh nghÜa mét biÕn tÝnh thu nhËp mét n¨m cña nh©n viªn. Dïng biÕn nµy ®Ó t×m nh÷ng nh©n viªn cã thunhËp lín h¬n hoÆc b»ng $30000.4. §Þnh nghÜa mét biÕn lµ kho¶ng thêi gian nh©n viªn lµm trong c«ng ty. HiÓn thÞ tªn nh©n viªn vµ qu·ng thêigian nh©n viªn ®ã lµm viÖc víi ®iÒu kiÖn nh©n viªn lµ mét biÕn ®−îc nhËp vµo tõ bµn phÝm. ENAME LENGTH OF SERVICE ---------- --------------------- KING 19 YEAR 4 MONTHS6 C¸c hµm nhãm ¸p dông cho lín h¬n hoÆc b»ng 1 dßng d÷ liÖu6.1 C¸c hµm t¸c ®éng trªn nhãmC¸c hµm t¸c ®éng trªn nhãm c¸c dßng d÷ liÖu t¸c ®éng lªn mét tËp hîp c¸c c¸c dßng d÷ liÖu. Gåm c¸chµm:AVG([DISTINCT/ALL] n) Gi¸ trÞ trung b×nh cña n,kh«ng kÓ trÞ nullCOUNT([DISTINCT/ALL] expr) Sè row cã expr kh¸c nullMAX([DISTINCT/ALL] expr) Gi¸ trÞ lín nhÊt cña exprMIN([DISTINCT/ALL] expr) Gi¸ trÞ nhá nhÊt cña exprSTDDVE([DISTINCT/ALL] n) Ph−¬ng sai cña n kh«ng kÓ trÞ null§µo t¹o c¬ b¶n: SQL vµ PL/SQL Trang 32
  33. 33. C«ng ty cæ phÇn ®Çu t− ph¸t triÓn c«ng nghÖ - FPT Gi¸o tr×nh SQL vµ PL/SQLSUM([DISTINCT/ALL] n) Tæng cña cña n kh«ng kÓ trÞ nullVARIANCE([DISTINCT/ALL] n) Variance cña n kh«ng kÓ trÞ nullChó ý tÊt c¶ c¸c hµm trªn nhãm mÉu tin ®Òu bá qua gi¸ trÞ NULL trõ hµm COUNT. Dïng hµm NVL ®ÓchuyÓn ®æi vµ tÝnh gi¸ trÞ NULL.Cã 2 c¸ch ®Ó dïng c¸c c¸c hµm nµy• T¸c ®éng trªn toµn bé c¸c dßng d÷ liÖu cña c©u lÖnh truy vÊn• T¸c ®éng trªn mét nhãm d÷ liÖu cïng tÝnh chÊt cña c©u lÖnh truy vÊn. Cïng tÝnh chÊt ®−îc chØ bëi mÖnh ®Ò [GROUP BY expr] [HAVING condition]§µo t¹o c¬ b¶n: SQL vµ PL/SQL Trang 33

×