אאא ã¹]<gè…‚jÖ]æ<ËÖ]<Üé×Ãj×Ö<íÚ^ÃÖ]<퉉ö¹] אאאא
אא
١٦٢
אא
א
١٦٢
W،،אא،א
אאאאאאאאאא א א ،א א א א א א
אא אאאא
،אאאאאא
K אאא
אאאאאאא
א ،א אא א א א א א א א ،
א،אאאא אאאאאאאאאאאאא
א א ،א Kאא،אא
א???אא?אא Kאאאאאא
אאאאאאא ،א،אאאא Kאאאאא
،אאאא
Kא
אאאא
אאא ã¹]<gè…‚jÖ]æ<ËÖ]<Üé×Ãj×Ö<íÚ^ÃÖ]<퉉ö¹] אאאא
אא
אא
אא
١
אא אא
אא
א
١٦٢
Wאא אאא Wאא אא K١ אאא K٢
Wאאא K٪١٠٠אאא Wאא Wאא H Wאא Kאאאאא
-١-
אא אא
אא
א
١٦٢
א אאאאאWא
אאאKK
אKאאKKKאאאאאא אא אאאאאא
אאאKאאא
א אאאאאאא
Kאא
אא ؟EDatabase Management Information SystemFDBMSאא ، אאא אא
،אאאא
א ،א אא
Kאאאאאאא
-٢-
אא אא
אא
א
١٦٢
א Wא WEHardwareFא אJ ١ א، א א، אאאא KאKKKאאא WESoftwareF אJ ٢ W،אאאא אאאאא אאWא J KLinux, Unix, WindowsK…א
Oracle, אאאאWא J KKKKSybase, DB2
אאא אאWאאאאא אJ KKKKאאא WאאאW אJ ٣
א אאאא אWא J Wא
Kאאאאא K١ Kאאאא K٢ Kא K٣
WאאאאWא J Kאאא K١
Kאאא K٢ Kאא K٣
אאאאאאא K٤ Kא
-٣-
אא אא
אא
א
١٦٢
א אE אF א א J Wא
אאאא K١ Kאאא K٢ Kאא K٣
אאאא K٤ Kאאאאא
Kאא K٥ אאאאWא אJ Wא
אאE F אא K١ Kא
Kאאא K٢
Kאאאא K٣
אאא אWאאJ هـ KאKKKא،א،א،אאאא
אאא אאWאאא אJ ٤ Kא
KאאאאW אJ ٥
אאאא Kאאא
-٤-
אא אא
אא
א
١٦٢
Wאא ،א
אאאאא אאא K E אF אא א
אאאאא
אאא אאא K Wאא Wאאא אJ ١
Kאאאאאאאאא J אאאאאאאאא א J Kאאאאא
Kאאאאאאאאא J א אאאWא J ٢
אאאא א
Wא א F אא א J WEאא
אאאאאא K١
אא K٢ Kא
אא K٣ K אאא، אאאאא K٤ KKKK אא אא،א، אאא
K
KEDBMSFאא אJ -٥-
אא אא
אא
א
١٦٢
KEDBMSFאאאא J אאאאא אJ KEDBMSFאאאא
אאא אאאW א J ٣ KKKKאאאאא
Wאא J ٤ Kאא J Kאאאא J ،KKאאאאאWאא J ٥ Kאאאאאאא
אאW אJ ٦ אאאאאאאאא
Kא
-٦-
אא אא
אא
א
١٦٢
؟אאא J ١
אאאא J KאאאאאאJ Kאא J
W J ٢
א
אא ؟א J ٣
Kא אJ ٤ Kאא،אא אJ ٥
-٧-
אאא ã¹]<gè…‚jÖ]æ<ËÖ]<Üé×Ãj×Ö<íÚ^ÃÖ]<퉉ö¹] אאאא
אא
אאא
אאא
٢
אא
אא
א
אאא
١٦٢
Wאא Kאאאא Wאא Kאאא KEאFאאאא
KEאFאאאאא Wאאא K٪١٠٠אאאא Wאא K Wאא KH Wאא Kאאאאא
-٨-
אא
אא
א
אאא
١٦٢
Wאא אאאאאCoddא١٩٧٠אאא
Kאאא אא אא،אאאאא
אאאKאאאאאאא אERelationsFETablesFאא
אKEאFא
א،EFא
אאERecords or TupleF א אKא
א א EDomainFEAttributesFאא KKאאאאKאא
א אאאאEPrimary KeyF
אא אאKאאEאF KERelational DatabaseFא
אEStudentFאאא Studentא אJ
Kא J KאSt _Noא אJ KאאDept_Code אJ
J ٢٠٠١אאאאא אEGpaFא J K١٠J ٠١
GpaאWא J
אאאא Dept_CodeאK٥KK١
Kא
-٩-
אא
אא
א
אאא
١٦٢
אאKאא J Wאא
Student St _No J ٠١J ٢٠٠١
St _Name Sami
Gpa ٣{٧٥
Birth_Date ١٩٨١J ٠١J ٠١
Dept_Code Comp
J ٠٢J ٢٠٠١
Khalid
٣{٥
١٩٨٢J ١٠J ١٠
Math
Ali
٤{٢
١٩٨٠J ٠٨J ١٢
Comp
١٠
٩٩ J ٠١J ٢٠٠٠
١٠١
- ١٠ -
אא
אא
א
אאא
١٦٢
WEאFאא א אאאא אאKאאאאאא Wאא
אא אWESuper KeyFא אJ אאKאאא
K
St_No St_No, St _Name St_No ,dept_code אאEאFאWECandidate KeyFא אJ
אא KENullFא St_No, St _Name
St_No
KSt_No،،א
אאאאאWEPrimary KeyFא אJ K St_NoKאא
،אW א אJ Customer _idאא E>؟ אFא
אאא،אאאא
K Customer _id Customer name
tel
Address
WEForeign KeyFאאJ
EStudentFא EDept_CodeFאEUniqueF EDepartmentFאEForeign KeyF - ١١ -
אא
אא
א
אאא
١٦٢
WEאFאאא אאאאאאא WאאKאאא אאאאאWE١W١Fאא K١ אא
אאאאא Kא Person
1
1
- ١٢ -
Passport
אא
אא
א
אאא
١٦٢
אא אE١WNNW١Fא א K٢
Kאאאאאאא
אאא
Kאא
n
Student
1
Department
אאאאWEN:NF K٣
Kאאאאאא
א
K
Student
n
n
- ١٣ -
Class
אא
אא
א
אאא
١٦٢
W J ١
א א
א א א א
Kאאאאאא J ٢ אאאאאא J ٣ K
אא J ٤ EN :NFאא E NW١Fא
EN :NF
- ١٤ -
אאא ã¹]<gè…‚jÖ]æ<ËÖ]<Üé×Ãj×Ö<íÚ^ÃÖ]<퉉ö¹] אאאא
אא
אא
אא
٣
אא
אא
א
אא
١٦٢
Wאא אאאא Kאא
Kאאא
Wאא
Kאא Wאאא K٪١٠٠א Wאא K٤ Wאא H Wאא Kאאאאאא
- ١٥ -
אא
אא
א
אא
١٦٢
W א ،אא אא
אKאאא
K אאאאאאא אאאא Kאאאאאאא אא Entity
אאאא אאא
KRelationship (ER)Diagram Wא ؟א
KאאEFא אKא
אאאאאאא אאאאKאא אאאK אא
אאKאאא
E אFאאאאאאא
،אאE אFאEאFא
אאEאFאאאאא Kאאא
ﻣﺨﻄﻂ ﻣﻨﺰل
- ١٦ -
אא
אא
א
אא
١٦٢
Wאא אאא WאאאKKא
EObjectsF אא אEEntity SetF א EEntityF אKאאא ، א،אK אאאאא
אאKאאא
א אK א Kא
Patient ﻣﺮﻳﺾ
Student ﻃﺎﻟﺐ
א،א א WEAttributesFאא KK אאאאא
،א،אא،א،א،א،א
אאאאא Kא،א
K
Stu_address
Stu_name
Stu_major
Student
א אא WEDomainFא ٣٠אא، א
אKE٣{٥F ٥KK٠אא א، אK ٢٢אא Kאא אאאאאא
Eprimary keyFאאEאFא - ١٧ -
Kא
אא
אא
א
אא
١٦٢
Car_model
Car_year
car Car_Code
א
WSimple and Composite AttributesאאאJ K אא، א ،א،א،אFאא،Eאא،א،אאF
אKKEא
Kאאאאאאאא Fname
Init
Lname
Name
Student
WSingle-Valued or Multiple-Valued AttributesאJ אEא،אFאאא
Eאא،א، F א אK
Kאאאאא Car_Col or
car
- ١٨ -
אא
אא
א
אא
١٦٢
WEDerived AttributesFאא א אא א א Kאאאא
Kאאאא Kא–אאZא
ِAge
Student
K א אא
אאא אא Kאאא
WERelationshipsFאאא אאאאאאאא
KEKKKK،،،Fאא
אאאK Kאאאא
KEN:NFEN :1FאE١W١Fאא Kאא N
Student
N Enroll
- ١٩ -
Class
אא
אא
א
אא
١٦٢
אא א Wאא
א
אאKאאא،אא
EאF אא א אא
אאK א
אאFא אאאא KEאאא No
Employee
Name
Relationship D_Name
Dependent
B_date
: ESupertype and SubtypeFאאאאאא SupertypeE F א אא אאא
Account אאאא אא،א Kאא
אאאאאW
EOverdraft AmountFאECurrent AccountFאאW א
אאK א KEInterest RateFאא
KEISAFאאאאאאאא - ٢٠ -
אא
אא
א
אא
١٦٢
:ERecursiveFא אאא،אאא E אאF EאFא
- ٢١ -
אא
אא
א
אא
١٦٢
F אאWא אאאKEER Diagramאא
Kאאאאאא
Fאא א J ١ אאאא،Eא א
Kא،אאאא
Kא אJ ٢ אא א J ٣ א، אאFאא
Kאאאא،א،Eאאאא،אאF
אא אאF אJ ٤ KאאאEא
،א،א،، אאא J ٥ Kאא Kאאא J ٦
- ٢٢ -
אא
אא
א
אא
١٦٢
- ٢٣ -
אא
אא
א
אא
١٦٢
WJ ١
KEntity א KAttribute א
KRelationship א
KאאאאאאאאJ ٢ ؟אא؟אאאאאJ ٣ Kאאא אJ ٤ WאאאJ ٥
אאJ Kאא،
אאJ Kא
אאאJ Kא
אאאJ ٦
אאאא
אאאאא
Kאאאא
WאאאJ ٧
- ٢٤ -
אא
אא
א
אא
١٦٢
- ٢٥ -
אאא ã¹]<gè…‚jÖ]æ<ËÖ]<Üé×Ãj×Ö<íÚ^ÃÖ]<퉉ö¹] אאאא
אא
אא
אא
٤
אאא
אא
א
אא
١٦٢
Wאא K3NFאאאאאא : EData AnomaliesFאאא
Wאא
אאא
אאאא אאאא אאאא Wאאא K٪١٠٠3NFאאאאאא Wאא
Wאא H Wאא Kאאאאאא
- ٢٦ -
אאא
אא
א
אא
١٦٢
W אאאא
אאאKאא
אKK אאאאאאא
אאאאאאאא
.EThird Normal Form 3NFFא
: EData AnomaliesFאא Employee_department Empno Ename Job Salary Deptno Dname Loc 101 Sami clerk 3000 10 Accounting Riyadh 205 Khalid manager 2500 10 Accounting Riyadh 303 Ali salesman 1200 20 Sales Jeddah 502 Saeed salesman 2100 20 Sales Jeddah 601 Salem clerk 1000 30 Operation Dmmam אאאאאא Wאאאאא
،אאא Wא J ١ Kאאא، ٢٠אELocFאאא אW א J ٢
אאאאאRiyadh Jeddah K אאא א Kא٣٠٣אאא
٣٠٣אJeddah אאא Kא ،א٣٠אWא J ٣ Kא٣٠א٦٠٦א
- ٢٧ -
אאא
אא
א
אא
١٦٢
WEFunctional Dependency FD Fאא
EF EFאא A
B
KBAAאא B אאאאא
אאאאאאא KEAnomaliesF
Wאא אאW FD1 : Empno Ename FD2 : Empno Deptno אאא FD1 : Empno Ename,Deptno FD :Functional Dependency אא Functional Fאאאא WאאאאאEDependency FD EXאYFFXYאWReflexive אJ ١
YX 1- X ⊇ Y : X ÆY XZ YXאWAugmentationא א J ٢ YZ 2- { XÆ Y} ╞ XZÆYZ ZY YXאWTransitiveא J ٣ KZX 3- {XÆ Y ,YÆZ} ╞ XÆZ ZX Y XאWUnionא J ٤ KYZX
- ٢٨ -
אאא
אא
א
אא
١٦٢
4- {XÆ Y ,XÆZ} ╞ XÆYZ א Decompositionא J ٥ 5- {XÆ YZ } ╞ XÆY , X Æ Z א Pseudotransitive אאא J ٦ 6- {XÆ Y , WYÆZ } ╞ WXÆY Kאאאאאא╞ :EFirst Normal Form 1NF Fאאא אאאאאא KאEF
WEmployeeאא١ No
Name Adresse Fname Mid Lname city Street House no 100 Ali Salem musa Riyadh Immam saud 210 120 Saeed Eisa Ali Riyadh King Fahad 202 אאאאא
א אאאאא אאאאKא
אאאאאא1NF, אא No Fname Mid Lname city 100 Ali Salem musa Riyadh
Street House no Immam 210 saud 120 Saeed Eisa Ali Riyadh King Fahad 202 אאאא
K1NF אאא
אHOURS אאאW ٢ אאאPROJECTS - ٢٩ -
אאא
אא
א
אא
١٦٢
NO Name Project_Code Hours Deptno 210 Ali P1,p2,p3 12,20,40 10,20,20 Research, 201 Salem 305 Ali
P1,p3 P2,p3
30,15 40,20
10,20 20,20
Dname Operation, Operation Research Operation Operation, Operation
אא אא
אDeptnoאHoursאProject_Codeא
،אאאאאKא Kאאאא NO Name Project_Code Hours Deptno Dname 210 Ali P1 12 10 Research 210 Ali p2 20 20 Operation 210 Ali p3 40 20 Operation 201 salem P1 30 10 Research 201 salem p3 15 20 Operation 305 Ali P2 40 20 Operation 305 Ali p3 20 20 Operation א
א אEPrimary KeyF אאאאאאK FD 1 :No Æ Name Kאא FD 2 : Project_CodeÆ Deptno Kא FD 3 : Deptno ÆDname Kאא
א אא א
KאAli - ٣٠ -
אאא
אא
א
אא
١٦٢
P1א אא K
٢٠אאא
Kא
אאאאא
אEאאF Wא
אא FD 4 :No, Project_CodeÆ name FD 5 :No, Project_CodeÆ Deptno FD 6 :No, Project_CodeÆ Hours FD 7 : Deptno ÆDname FD 8 :No, Project_CodeÆ Name ,Hours, Deptno, Dname אאאFD1,FD2FD4,FD5 אאאאFD5 ،א
אא، א KE1NFFאאא :ESecond Normal Form 2NF Fאאא Wאאאא Kאאאאא J ١ Kאאא J ٢
אאאאEאFאWאא
A C B C C B A,B אאCCA,B KאאאאKBאא - ٣١ -
אאא
אא
א
אא
١٦٢
NO 210 210 210 201 201 305 305
Name Ali Ali Ali Salem Salem Ali Ali
Project_Code P1 p2 p3 P1 p3 P2 p3
Hours 12 20 40 30 15 40 20
Deptno 10 20 20 10 20 20 20
Dname Research Operation Operation Research Operation Operation Operation
؟אאאאאא
Wאאא ؟אאאאJ ١
Kא،،
؟אJ ٢ אא FD 1 :No Æ Name FD 2 : Project_CodeÆ Deptno,Dname FD 3 :No, Project_CodeÆ name ,deptno, hours אאNameNo No, Project_Codeאא
Kא DnamedeptnoProject_Code אאאא אא
Wאאאאא
אאא אא J ١ KEאאF
אא אא J ٢ KEאאFא
KEאFא J ٣
Wאאאאא J ٤ - ٣٢ -
אאא
אא
א
אא
١٦٢
NO 210 210 210 201 201 305 305
Project_Code P1 p2 p3 P1 p3 P2 p3
Project_Code P1 p2 p3
Hours 12 20 40 30 15 40 20
NO 210 210 210 201 201 305 305
Deptno 10 20 20
Name Ali Ali Ali salem salem Ali Ali
Dname Research Operation Operation
WEThird Normal Form 3NF Fאאא Wאאאא Kאאאא J ١ Kאאא J ٢
KאאאאEאFאWאא
B A C
C B A אאC,BC,B A
אאאKBאא C
K
؟אאאאאאא Wאאא - ٣٣ -
אאא
אא
א
אא
١٦٢
؟אאאאאJ ١ Kאאאאאא
؟אJ ٢
אא
אאJ FD 1 :No Æ Name Kא
אאJ FD 1 :No, Project_CodeÆ hours Kא
אאJ FD 1 : Project_CodeÆ Deptno,Dname FD 2 : Deptno Æ Dname Deptno אDeptnoDnameProject_Code אא
אאK אDname א א א אאא EDeptnoFאא
Kאאאאא J ١ Wאאאאא J ٢
NO 210 210 210 201 201 305 305
Project_Code P1 p2 p3 P1 p3 P2 p3
Project_Code
Hours 12 20 40 30 15 40 20
NO 210 210 210 201 201 305 305
Deptno
Deptno - ٣٤ -
Name Ali Ali Ali Salem Salem Ali Ali Dname
אאא
אא
א
אא
١٦٢
P1 p2 p3
10 20
10 20 20
Research Operation
א3NF אאאאא א Kאא
- ٣٥ -
אאא
אא
א
אא
١٦٢
Kא EData AnomaliesFאאא K١ ؟EFunctional Dependency FD Fאא K٢ Kאאא אK٣ א K٤ Kאא אJ
Kאא אJ Kאא אJ
W K٥ Kאאא J
Kאאא J Kאאא J
אא؟אאאאא K٦ Kאאאא
Course _No
Sec_ No
Dept
Credit _Hours
Course Level
Ins_id
א א א א
א
Semeste Year r
Date
Room _No
No_of _ stu
א א א
א אא אאא
אא א
- ٣٦ -
אאא ã¹]<gè…‚jÖ]æ<ËÖ]<Üé×Ãj×Ö<íÚ^ÃÖ]<퉉ö¹] אאאא
אא
אא
٥
אא
אא אא
אא
א
١٦٢
Wאא Kאא א
Wאא Kאאא Wאאא K٪١٠٠אאא Wאא Wאא H Wאא Kאאאאאא
- ٣٧ -
אא אא
אא
א
١٦٢
Eאא אא א
אאKEאאFאא
Eא א Fא א . אא
WEאאFאאא
- ٣٨ -
אא
אא
א
١٦٢
אא
W א Relation (R)א Entity (E) J ١ אאאאא
K א א ،
Employee Eno
Fname
Mid
Wאא
Lname
sex Birthdate
Salary
KEFא
Department Deptno Dname Location אEFא KEQueryFאא
- ٣٩ -
אא
אא
א
١٦٢
אא
Project Pnumber
Pname
Plocation
W Weak Entityא Relation (R)א EWeak EntityF א א א א א א א א א א א ، א א ، א א א KEאאאFאא
Dependent Eno
Name
Sex Birthdate Relationship
- ٤٠ -
אא
אא
א
١٦٢
אא
E ١W١F א א א א Wא
אE NWNFE NW١Fא
Wאא E١W١FאאJ ١ א E ١W١F א א Kאא
1
1
Fאאאא EMgrF א KאאאאאEא Department Deptno
Dname Location Mgr Startdate
EN: 1Fא J ٢ EF EN:1F א KE١FאאאאENFא
1
Employee Eno
N
Fname
Mid
Lname - ٤١ -
sex
Birthdate Salary
Mgr
אא
אא
א
١٦٢
אא
אא א א EMgrF א EאFא N
1
Employee Eno
Fname
Mid
Lname
sex
Birthdate Salary
Mgr Deptno
FאאאEDeptnoF א Eא
N
1
Project Pnumber
Pname Plocation Deptno
א א א א EDeptnoF א
KEאF
- ٤٢ -
אא
אא
א
١٦٢
אא
EN:NFJ ٣ א א EN:NF
אKאאא N
N
Works_for Eno Pnumber Hours (א،א،א )א אאא אEא،אF Kא א،א ISAESuper Type FאאאESubtypeFאאאא אאאאאאא Wא
א אא J ١
א J ٢ Kאאא
- ٤٣ -
אא אא
אא
א
١٦٢
Wאאא
S_Employee
H_Employee Eno Hour_Rate
Eno
- ٤٤ -
Salary
אא אא
אא
א
١٦٢
؟אאא K١ KN:Nאא K٢
Super FאאאESubtypeFאאא אאא K٣ KISAEType
אא K٤
Kאא٦אאאא K٥
- ٤٥ -
אאא ã¹]<gè…‚jÖ]æ<ËÖ]<Üé×Ãj×Ö<íÚ^ÃÖ]<퉉ö¹] אאאא
אא
אא
אא
٦
אא
אא
א
אא
١٦٢
Wאא KPLSQLאאאא Wאא PLSQL אא PLSQL אאא
Kאאאאאא KאDatatypesאאא Wאאא K٪١٠٠אאאאא Wאא Wאא H Wאא Kאא אאאSQLאאא
- ٤٧ -
אא
אא
א
אא
١٦٢
W Programming Language FSQLא؟PL/SQL
אאOracleEStructured Query Language
א אאאאאאא
אאאאאא KאאSQL
KKKKא
،Modulesאאאאאאאאא
אאKאאKPL/SQL ،Modulesאאאא
אאא،אאאא Database Serverא א אאאאא
Source אאאאאאKא
אKP-Code אCompileאCode Wאאאאא
Wאאאא אא אאאWAnonymous Block K١
אאאK Kאאאאאא Kא - ٤٨ -
אא
אא
א
אא
١٦٢
אא،SubprogramsWNamed Block K٢ Function, Procedure, Trigger, Wאאא
KPackage
אאא א
KNested Blocksאא
W (Blocks)אא Wא(Block) א
אאאאאDeclarative EאFא K١
אאאאECursors Fאא،(Block)א
אאאאאאאאאא
Kא(Block)
אאא SQLExecutable:א אK٢ Eא،א،אאFא ،אא،א(Block) אאאPLSQL KאאאאאKKKKKאא
אא אאWEExceptionFאאא אK٣ Kאאאאא DECLARE
Æ
BEGIN
Æ
SQL
Æ
PLSQL
Æ
EXCEPTION END;
Æ
א אאאאא،אא אאא SQL
PLSQL
א
EאFאאא אא
- ٤٩ -
אא
אא
א
אא
١٦٢
Wאאאא Fאאאאאאאא WPLSQL(Block)
Wאאאאא
Wאא K١
אאאאאאאאא Kאאאאא
Wאא K٢
אאאאאאא
Kאאא Wא K٣
EאאF%ROWTYPE %Typeאא
א
אאאאאא א Kאאא
Wאא א אאאPLSQLאאאא
א، אאאאא PLSQL (Block) א
Wאאאאא KE;F identifier [CONSTANT] datatype [NOT NULL] [:= DEFAULT | expression ] ;
- ٥٠ -
אא
אא
א
אא
١٦٢
אא אאאא
Identifier
CONSTANT
K
KאאCONSTANTאאא אאא
datatype
NOT NULLאאא،
NOT NULL
، PLSQL
expression
Kאא
EFא
Declare v_name VARCHAR2(10) ; v_date
W א
אא
DATE ;
v_id NUMBER(2) NOT NULL :=10 ; ١٠אא NULL V_comm CONSTANT NUMBER :=120; אאא ١٢٠אא
BEGIN …… END;
- ٥١ -
אא
אא
א
אא
١٦٢
Wאאאאא KSQLאאא אK١
(Block) אא J K
K(Block) אאאאאא J K٣٠א J KSQLאאאאא J A-Z, a-z, 0-9, _, #,$ J CONSTANTNOT NULLאאאאא K٢ Kאאא
KDEFUALT אאא:=אאא K٣
Kא K٤
Wאא אא Kא:=אאאא J Identifier := expression ; W v_name :=’Ali’ ; v _name Ali אא net_sal := v_sal –v_sal*.08; net_sal א SELECT אאא J J
SELECT ename INTO v_name FROM emp WHERE empno = 7788; א ename אאאא v_name ename אאא K
- ٥٢ -
אא
אא
א
אא
١٦٢
W Datatypes אאא WScalar Datatype אאאאJ ١ Wאא אאKאאאא א
א sizeאאא
א VARCHAR2(size)
אKאא Byte ٣٢{٦٧٦Kא
א א sizeאאא
CHAR[(SIZE)]
אאאKא Byte٣٢{٦٧٦١אא אאPrecisionאאא
NUMBER (precision,scale)
EFאא
DATE
אאscale
٩٩٩٩א٤٧١٢אא Kא
א sizeאאא
LONG
אKאא Byte٣٢{٦٧٠Kא Byte٢{١٤٧{٤٨٣{٦٤٧LONGא Kא EBinaryFאא TRUE,FLASEאא
LONG RAW BOOLEAN
א BINARY_INTEGER
٢{١٤٧{٤٨٣{٦٤٧J ٢{١٤٧{٤٨٣{٦٤٧ ٢{١٤٧{٤٨٣{٦٤٧J ٢{١٤٧{٤٨٣{٦٤٧א BINARY_INTEGERNUMBER
- ٥٣ -
PLS_INTEGR
אא
אא
א
אא
١٦٢
W
v_job VARCHAR2(9); v_count BINARY_INTEGER := 0; v_total_sal NUMBER(9,2) := 0; v_orderdate DATE := SYSDATE + 7; c_tax_rate CONSTANT NUMBER(3,2) := 8.25; v_valid BOOLEAN NOT NULL := TRUE; א%TYPEאאא Kא
v_enameemp.ename%TYPE;
empא enameאא v_enameא
v_balanceNUMBER(7,2); v_bqlqnceא v_min_balancev_balance%TYPE := 10; K١٠אא v_balanceאא v_min_balanceא WBOOLEAN אאא KTRUE , FLASE, NULL אאאJ
AND,OR ,NOTאאאאאJ אאאאאאאא
KFALSE TRUE(A<B)
W
V_Sal1 NUMBER: =1000 ; V_Sal2 NUMBER: =2500 ; Valid BOOLEAN:= (V_Sal1 > V_Sal2); אV_SAL1 > V_SAL2 Validא KTRUE ValidאאאTRUE א - ٥٤ -
אא
אא
א
אא
١٦٢
WComposite Datatype אאאJ ٢
א RECORDS אJ Kאא
אאאEMP_REC א W EMP_REC ID NAME SALARY ALI 3000 ١٠١
אTABLES אאJ F אאא
KאאKEא
WLOB Datatype ( Large Objectas) אאאאJ ٣ אאאאאא
KאאאאKא
א אאאאאWBind Variables אאJ ٤ (Block) אאאאאESQL* PlusFא(Block)א
K(Block) אאא
SQL > VARIABLE NUMBER SQL > VARIABLE varchar2(20)
Wאא
Emp_Sal emp_name
Emp_Sal
EHostFאNUMBER Emp_ Name
٢٠VARCHAR2(2) K EHostFא
אאאא(Block) אאאא E:Fאאאא(Block) א : Emp_Name:= ‘AHMED’; - ٥٥ -
אא
אא
א
אא
١٦٢
PRINT אאSQL* Plusא SQL> PRINT Emp_ Name :אא PUT_LINEאאאא(Block) א WאאDBMS_OUTPUTאא DBMS_OUTPUT.PUT_LINE(‘Well Come to PLSQL Programming’); אאאאא
S QL >SET SERVEROUTPUT ON
K(Block) אאאא
SET SERVEROUTPUT ON ACCEPT p_annual_sal PROMPT ' Please enter the annual salary ' Declare V_sal NUMBER(9,2) :=&p_annual_sal ; BEGIN V_sal :=V_sal /12; DBMS_OUTPUT.PUT_LINE('Monthly Salary is '||TO_CHAR(V_sal)); END;
- ٥٦ -
אא
אא
א
אא
١٦٢
؟אPLSQLאאאא K١ 1-
DECLARE VI_ID NUMBER (4);
2-
DECLARE v_x ,v_y ,v_z
VARACHAR2(10);
3-
DECLARE V_Date DATE NOT NULL;
4-
DECLARE V_valid BOOLEAN :=1;
אאאא K٢
1-
V_days := v_date-SYSDATE;
2-
V_sender := USER||’ :’|| TO_CHAR(V_DEPTNO);
3-
v_sum := $100 + $3000;
4-
V_days:=v_date-SYSDATE;
5-
v_flage := TRUE;
6-
v_n1 := v_n2 > ( 2 * v_n3);
7-
v_value := NULL;
KאMY PLSQL WORKS PLSQL(Block) אK٣
G_MESSAGE -------------------------------My PL/SQL Block Works
- ٥٧ -
אא
אא
א
אא
١٦٢
V-CHRPLSQL (Block) K٤ ’42 is the answer’ אV-CHRV_NUM Kp6q4.sqlBlockאSQL * Plus אא
SQL> PRINT g_char G_CHAR ---------------------42 is the answer
SQL> PRINT g_num G_NUM ---------42
- ٥٨ -
אאא ã¹]<gè…‚jÖ]æ<ËÖ]<Üé×Ãj×Ö<íÚ^ÃÖ]<퉉ö¹] אאאא
אא
אא
אא
٧
אא אא
אא
א
١٦٢
Wאא Kאאאאא KEBlockFאאאאא
Wאא
Kאאא
Kאאאאאאאא Kאאא
Kאאאא
Wאאא K٪١٠٠אאא Wאא Wאא K H Wאא Kאאאאאאאא
- ٥٩ -
אא אא
אא
א
١٦٢
،PLSQL EBlockF א א א א א
(Block),אאאאאא אא Kאאאאאא
PLSQL ESyntax Fא אא E ،אF אא PLSQL א
אKKKKא،אא،אאאא אKאאאאאאאא
K WאJ WEאFא אK١ Kא٣٠אJ
אאJ KJ
K(Block) א אJ W
v_name varchr2(20); xyx number; birth_date date;
W
1no number ; Dept%id number(2); Select varchar2(10);
W Literal Values א אK٢ אDateאCharacterאאאJ
KE‘ ‘F
v_name:=’ali’; KאאאאNumbersאאJ v_id :=201; - ٦٠ -
אא אא
אא
א
١٦٢
WOperations אK٣ K ENOT,**FאאJ EJ ،HFא אJ
EL،*FאאJ
KE =,>,<,<=, <=, IS NULL, LIKE, BETWEEN, IN FאJ EAND OR FאאJ J WComments אאJ אא אאא
Kאאא
אאאE--FאאאJ
K
אE*/ FאאE/* Fאא J
Kא
….. V_name varchar2(20) ;-- this variable used to hold the employee name Begin /* this code is used to read The employee salary and calculate the annual salary And print the annual salary */ ……. End ; K Using Functions אאאא WEBlockFאאאאאאא W SQLאאאא K١
(ROUND ,TRUNC, SQRT ….FNumber Functionsאא אJ v_sal:= ROUND(v_sal,2) ;
ECONCAT, INITCAP, LOWER,…FCharacter Functionsאא אJ SELECT INITCAP (enam) INTO v_name - ٦١ -
אא אא
אא
א
١٦٢
FROM emp WHERE empno =7788; (ADD_MONTHS, MONTHS_BETWEEN,… ) Date א א J Functions Num_months := MONTHS_BETWEEN(SYSDATE,v_date); אא SQL א J Decode J EMIN, MAX, AVG, ….. FGroup Functions J TO_CHAR
Wאאאאא K٢ ◄ CHARACTER אJ
KTO_NUMBER◄ NUMBER אJ K TO_DATE◄ DATE אJ
ENested Blocks Fאאאא א،אEBlockF אאEBlockF،ENested BlockFאא
KאאאאאאK ... x BINARY_INTEGER; BEGIN ... DECLARE y NUMBER; BEGIN ... END; ... END;
ﻣﺠﺎلx
ﻣﺠﺎلy
- ٦٢ -
אא אא
אא
א
١٦٢
x אEBlockFא x א א אא אאאאא (Block) א
א(Blocks) אאאאאא y K(Block) א Kא(Block) אאא
אאאאאא J
Kאא(Block) א ... x BINARY_INTEGER; BEGIN ... DECLARE X NUMBER; BEGIN ... END; ... END;
داﺧﻞ هﺬﻩX أي اﺳﺘﺨﺪام ( ﻳﻜﻮن لBlock) اﻟﻮﺣﺪة
DECLARE v_sal v_comm v_message BEGIN
NUMBER(7,2) := 60000; NUMBER(7,2) := v_sal * .20; VARCHAR2(255) := ' eligible for commission'; DECLARE v_sal
NUMBER(7,2) := 50000; NUMBER(7,2) := 0;
v_comm
NUMBER(7,2) := v_sal + v_comm;
v_total_comp BEGIN
v_message := 'CLERK not' || v_message; END;
- ٦٣ -
אא אא
אא
א
١٦٢
v_message := 'SALESMAN'||v_message; END; Wאאאאא ESub BlockFאאא v_messageJ
אEBlockFאv_total_compJ ESubBlockFאאאv_comm J
אEBlockFא v_comm J
אEBlockFא v_messageJ J
EProgramming GuidelinesFא א ، א א א א א א א א א אא א א א א א אא א K
Wא אאאא K١ Kאאאאא
E Case conventionF אאאא אK٢ Wאאאאאאאא
SELECT, INSERT DECLARE ,BEGIN, END v_sal, id, g_sal emp, dept, ename
א
א
SQL
אא
אאא
אאא
- ٦٤ -
אא אא
אא
א
١٦٢
Wאאאאא אא v_empno, v_sal
א v_name
c_sal , c_tax
c_name
emp_cursor,
Name_cursor
e_too_many
e_name
p_empno
p_name
g_sal
g_name
א variablesאא אא cursors אא exceptionאא אאא substitute variables globalאאא
Wאאאאאאאאא K٣ BEGIN IF x=0 THEN y:=1; ELSE y:=2; END IF; END;
Kאאאאאא BEGIN IF x=0 THEN y:=1; ELSE y:=2; END IF; END;
- ٦٥ -
אא אא
אא
א
١٦٢
J ١
DECLARE v_weight number(3):=600; v_message VARCHAR2(255):='Product 10012'; BEGIN /* SUB BLOCK אא* א/ DECLARE v_weight number(3):=1; v_message VARCHAR2(255):='Product 11001'; v_new_loc VARCHAR2(50):= 'Europe '; BEGIN v_weight := v_weight +1; v_new_loc:= 'Western '|| v_new_loc; END; v_weight := v_weight +1; v_message := v_message || 'is in Stock '; v_new_loc:= 'Western '|| v_new_loc; END ;
Wאאאאאאאא KESub Block Fאאאv_weight
J
KESub Block Fאאאv_new_loc J
KEmain Block Fאאv_weight J KEmain Block Fאאv_message J KEmain Block Fאאv_new_locJ
א EBlockF J ٢ א ،א א א א א
Kאא - ٦٦ -
אא אא
אא
א
١٦٢
Please enter the first number : 2 Please Enter The Second Number :4 G_RESULT ----------------------4.5 א אא א EBlockF J ٣ א אא אא K א
KאKאאא W
KE 0.15١٥אאFא J
KENVLאFאאא J Please enter the salary amount: 50000 Please enter the bonus percentage: 10 PL/SQL procedure successfully completed. G_TOTAL ---------55000
- ٦٧ -
אאא ã¹]<gè…‚jÖ]æ<ËÖ]<Üé×Ãj×Ö<íÚ^ÃÖ]<퉉ö¹] אאאא
אא
Oracleא
Oracle א
٨
אא Oracleא
אא
א
١٦٢
Wאא KEBlockFאאOracle אא EBlockFאאאא
Wאא
EBlockFאאאא
Transactionsאא SQL CursorSQLאאא
Wאאא K٪١٠٠Oracleאא Wאא Wאא H Wאא Kאאאאאאא
- ٦٨ -
אא Oracleא
אא
א
١٦٢
PL/SQL SQLאא אאא
ETransactionF PL/SQL א
،Block א א COMMIT, SAVEPOINT, ROLLBACK אא CREATE TABLE, Blockאאאא DDL
KGRANT, REVOKE DCLALTER TABLE
Select Statement א SELECT select_list {variable_name[, variable_name]... INTO | record_name} table FROM condition; WHERE
אאאא
select_list
record_nameVariablesSelect_ListK
INTO
אאאא
אאאאEאאFא select_listאא אא אאאאאא אא אאFא
variable_name record_name table condition
Exception א
- ٦٩ -
אא
אא
א
١٦٢
Oracleא
W
DECLARE v_deptno NUMBER(2); v_loc VARCHAR2(15); BEGIN SELECT INTO FROM WHERE
deptno, loc v_deptno, v_loc dept dname = 'SALES';
END;
א'SALES'א אאאאא Kאv_locv_deptnoאא
Wאאאא KE;Fא J
KINTOא J
אאאאאאא J Kאאא
K%TYPE אאאאאא J v_deptno dept.deptno%TPYPE; v_loc dept.loc%TPYPE; KאאWHERE J אא Group Function אאא J KPLSQLאאGroup Functionא
- ٧٠ -
אא Oracleא
DECLARE v_sum_sal v_deptno BEGIN SELECT INTO FROM WHERE END;
אא
א
١٦٢
emp.sal%TYPE; NUMBER NOT NULL := 10; SUM(sal) -- group function v_sum_sal emp deptno = v_deptno;
Control TransactionאDMLאא Data ManipulationDMLאאאא W
KאWINSERTא E١
BEGIN INSERT INTO emp(empno, ename, job, deptno) VALUES (empno_sequence.NEXTVAL, 'HARDING', 'CLERK', 10); END; KאאאWUPDATEא E٢ אאאWHERE K א
:=אאZאאא J DECLARE v_sal_increase emp.sal%TYPE := 2000; BEGIN UPDATE emp SET sal = sal + v_sal_increase WHERE job = 'ANALYST'; END;
- ٧١ -
אא Oracleא
אא
א
١٦٢
KאWDELETEא E٣ K א
DECLARE v_deptno emp.deptno%TYPE := 10; BEGIN DELETE FROM emp WHERE deptno = v_deptno; END; אאאא אאW א אא K א א WHERE א אאordאordidאאא ordid
אא،אאאא WHERE ordid = ordid ; אא 601א ordidא
אאאאא ordid Kאאא
DECLARE orderdate ord.orderdate%TYPE; shipdate ord.shipdate%TYPE; ordid ord.ordid%TYPE := 601; BEGIN SELECT orderdate, shipdate INTO orderdate, shipdate FROM ord WHERE ordid = ordid; END; SQL> / DECLARE * ERROR at line 1: ORA-01422: exact fetch returns more than requested number of rows ORA-06512: at line 6 - ٧٢ -
אא Oracleא
אא
א
١٦٢
WControl Transactionא אE،،FאWTransaction
אאKא Transactionא
Wא
KאאאאאCOMMITJ
KאאאאאאאROLLBACKJ אאאאאאSAVEPOINT J
Kאא
COMMIT [WORK]; SAVEPOINT savepoint_name; ROLLBACK [WORK]; ROLLBACK [WORK] TO [SAVEPOINT] savepoint_name;
SQL Cursor SQLאא א Cursor א אאאאאאKאאא
Kאא א א ESQL CURSORS Fאא א א
Kא
WאJ
אאאא Wא
KSQLאא
SQL%ROWCOUNTJ ١
KSQLאETRUEF
SQL%NOTFOUNDJ ٣
KSQLאETRUEF
- ٧٣ -
SQL%FOUNDJ ٢
אא Oracleא
אא
א
١٦٢
SQL CURSORS K א א ETRUEF SQL%ISOPEN J ٤ אאאא FALSEאאאא Kא
1
SQL> VARIABLE rows_deleted VARCHAR2(30)
2 3 4 5 6 7 8 9 10
DECLARE v_ordid NUMBER := 605; BEGIN DELETE FROM item WHERE ordid = v_ordid; :rows_deleted := (SQL%ROWCOUNT ||' rows deleted.'); END; / SQL >PRINT rows_deleted
SQL*Plus rows_deleted א א א 605orderitemאאEBlockF א אא SQL%ROWCOUNTאא ٧א
Krows_deletedא ' rows deleted.' ||אא، KSQL*Plusא
- ٧٤ -
אא Oracleא
אא
א
١٦٢
WEBlockFאJ ١
KDEPT אJ KSQL*PlusאJ
KאאJ
Kp8q1.sql אJ
G_MAX_DEPTNO -----------40 א DEPT אאא א J ٢ Wא . p8q2.sql
Kאאא١٠אאJ KאאSQL*PlusSubstitution Variable אJ
K location NULLJ
KEBlockFאJ
Please enter the department name: EDUCATION PL/SQL procedure successfully completed. KאאאJ DEPTNO DNAME LOC ---------------------------------50 EDUCATION DEPTK location אEBlockFאJ ٣ WאK . p8q3.sql א
KאSQL*PlusSubstitution Variable אJ
Klocation אSQL*PlusSubstitution Variable אJ Please enter the department number: 50 Please enter the department location: HOUSTON PL/SQL procedure successfully completed. - ٧٥ -
אא Oracleא
DEPTNO DNAME ----------------------50 EDUCATION
אא
א
١٦٢
KאאJ LOC ------------HOUSTON
א Kאאאאא EBlockFא J ٤ WאK . p8q4.sql
KאSQL*PlusSubstitution Variable אJ KאאJ KPlease enter the department number: 50 PL/SQL procedure successfully completed. G_RESULT -------------------------------------------------------------------------------1 row(s) deleted. ؟אJ Please enter the department number: 99 PL/SQL procedure successfully completed. G_RESULT -------------------------------------------------------------------------------0 row(s) deleted.
. אאJ
- ٧٦ -
אאא ã¹]<gè…‚jÖ]æ<ËÖ]<Üé×Ãj×Ö<íÚ^ÃÖ]<퉉ö¹] אאאא
אא
א
א
٩
אא
אא
א
א
١٦٢
Wאא Kאאאאאאא Wאא Kאאאא Kאאא
Kאאאא Kאאא
Kאאאאא Wאאא K٪١٠٠אאא
Wאא ٣
Wאא H -
Wאא Kאאאאאא
- ٧٧ -
אא
אא
א
א
١٦٢
W א א א א EBlockF א אאאא ٪٨אא ٣٠٠٠ א ٪١٠אא Kאאא K٣٠٠٠אא Kאאאא
KIF Statement אא
א אא א א א IF Kא
IF condition THEN statements; END IF;
Ali v_enameאאא v_sal٣٥٠٠אא
. v_sal := א א EאFא True א
FalseאAliv_enameאא3500
W END IFאאאv_sal := 3500;א
…. IF v_ename = ‘Ali’ THEN v_sal := 3500; END IF; …….
: IF THEN ELSE א אאאאא IF THEN ELSE Kאאאאאא
- ٧٨ -
אא
אא
א
א
١٦٢
IF CONDITION1 THEN Statement1; ELSE Statement2. END IF; IF v_deptno = 10 THEN UPDATE emp SET sal = sal * 1.10 WHERE deptno = v_deptno; ELSE UPDATE emp SET sal = sal * 1.08 WHERE deptno = v_deptno; END IF; אא ١٠אאאאא א
אאEאFאא،٪١٠א
K٪٨א
IF CONDITION1 THEN Statement1; ELSE - ٧٩ -
אא
אא
א
א
١٦٢
IF CONDITION2 THEN Statement2; END IF; END IF; IF v_deptno = 10 THEN UPDATE emp SET sal = sal * 1.10 WHERE deptno = v_deptno; ELSE IF v_job = ‘SALESMAN’ THEN UPDATE emp SET sal = sal * 1.11 WHERE job = v_job; END IF; אא١٠אאאאאא אאאאEאFאא،٪١٠א K٪٨אאאאSALESMAN
WIFTHEN ELSIF א אאאאא IF THEN ELSIF (ELSIF) א א IF א א אאאאאא
IF IF IF KESLEא
- ٨٠ -
אא
אא
א
א
١٦٢
IF CONDITION1 THEN Statement1; ELSIF CONDITION2 THEN Statement2; ELSIF CONDITION3 THEN Statement3; . . . ELSE StatementN; END IF; IF v_grade >100 OR v_grade < 0 THEN DBMS_OUTPUT.PUT_LINE(‘Invalid Grade ’); ELSEF v_grade >= 90 THEN DBMS_OUTPUT.PUT_LINE(‘A’); ELSIF v_grade >= 80 THEN DBMS_OUTPUT.PUT_LINE(‘B’); ELSIF v_grade >= 70 THEN DBMS_OUTPUT.PUT_LINE(‘C’); ELSIF v_grade >= 60 THEN - ٨١ -
אא
אא
א
א
١٦٢
DBMS_OUTPUT.PUT_LINE(‘D’); ELSE DBMS_OUTPUT.PUT_LINE(‘F’); END IF; ٠אאv_gradeאאאאא אK١٠٠٠אא١٠٠ Wאאאאא ١٠٠٠א א A B C D F
א ١٠٠٩٠ ٨٩٨٠ ٧٩٧٠ ٦٩٦٠ ٥٩J ٠
Wאאאא אאא IF אאIF Wא
IS NULL אNULL אאאאאJ IF v_name IS NULL THEN … …. END IF KNULL אNULL J v_sal := 3000; v_comm ;v_ annual_salary := 12 * v_sal + v_ אNULL אא v_commא
KNULLannual_salary
KאNULL NULL אJ
… v_job := NULL;
- ٨٢ -
אא
אא
א
א
١٦٢
v_name:=’AHMED’; v_info:= v_name || ‘ is ‘ || v_job ; …. v_jobAHMED is v_infoאאא K
AND ,OR , NOT אאאJ
IF v_sal > 3000 AND v_job = ‘SALESMAN’ OR v_deptno = 10 THEN ….. END IF ; Wאאא TRUE FLASE NULL AND TRUE TRUE FLASE NULL FLASE FLASE FLASE FLASE NULL NULL FLASE NULL OR TRUE FLASE NULL
TRUE TRUE TRUE TRUE
NOT TRUE FLASE NULL
FLASE TRUE TRUE
FLASE TRUE FLASE NULL
- ٨٣ -
NULL TRUE NULL NULL
אא
אא
א
א
١٦٢
; v_reorder_flag ANDv_reorder_flag
v_flag :=
Wאאv_flag
v_reorder_flag TRUE TRUE NULL NULL
v_reorder_flag TRUE FALSE TRUE FALSE
v_flag TRUE FALSE NULL FALSE
KLoops אא א אא אא PL /SQL
א א אאא،א WאPL /SQLKאא KBasic Loopאאא J KFORאא J
KWHILE אא J KNested LOOPS אאאא J :Basic Loop אאא ، End Loopאא Loop אאאא EאאFאאאאאא א
אא א Eא אא F אא
אאאא אאאא Exit K End Loopאא
- ٨٤ -
אא
אא
א
א
١٦٢
LOOP
Æ
statement1; ...
Æ
EXIT [WHEN condition];
Æ
אאא
אא
Fא Eאא אא
Æ END LOOP;
DECLARE item.ordid%TYPE := 601; v_ordid NUMBER(2) := 1; v_counter BEGIN LOOP INSERT INTO item(ordid, itemid) VALUES(v_ordid, v_counter); v_counter := v_counter + 1; EXIT WHEN v_counter > 10; END LOOP; END;
- ٨٥ -
אא
אא
א
א
١٦٢
אא item items ١٠אא K١٠v_counterאא
WאאאאW DECLARE v_counter NUMBER :=0; BEGIN LOOP DBMS_OUTPUT.PUT_LINE('v_counter = '||v_counter); EXIT WHEN v_counter > 5; v_counter:=v_counter+1; END LOOP; END ;
DECLARE v_counter NUMBER :=0; BEGIN LOOP DBMS_OUTPUT.PUT_LINE('v_counter = '||v_counter); EXIT WHEN v_counter > 5; v_counter:=v_counter-1; END LOOP; END ;
DECLARE v_counter NUMBER :=10; BEGIN LOOP DBMS_OUTPUT.PUT_LINE('v_counter = '||v_counter); EXIT WHEN v_counter > 5; v_counter:=v_counter+1; - ٨٦ -
J
J
J
אא
אא
א
א
١٦٢
For counter in [REVERSE] Lower_bound .. upper_pound LOOP statement1; statement2; ... END LOOP; END LOOP; END ;
Æ
אאא אא
Æ אא Æ
א٧
J
אא
J
א١
J
אאEFאאאאWא ؟Loop J J
: FOR אא
אאאאא א FORאא
KKKK،١٥،א١٠א
- ٨٧ -
J
אא
אא
א
א
١٦٢
אLower_bound אאJ
Counter
אאאאא١א KEאאreverse אאFupper_pound KאאJ
אאאאאJ
Kאא
KאאJ
אאאאאJ א K אאאאא אJ
REVERSE Lower_cound
F א J KE
א אאאאJ F אJ
KE
BEGIN FOR i IN 1..5 LOOP DBMS_OUTPUT.PUT_LINE('i= '||i); END LOOP; END; SQL> / i= 1 i= 2 i= 3 i= 4 i= 5
- ٨٨ -
upper_cound
אא
אא
א
א
١٦٢
WאאאBlock א
DECLARE v_lower number:=1; v_upper number:=5; BEGIN FOR i IN v_lower.. v_upper LOOP DBMS_OUTPUT.PUT_LINE('i= '||i); END LOOP; END; SQL> / i= 1 i= 2 i= 3 i= 4 i= 5
: WHILE אא אא א א א א WHILE אא
Kאאאאא، WHILE (condition) Æ אאא אא
statement1; Æ ... END LOOP; Æ
אא
DECLARE v_counter NUMBER :=0; BEGIN WHILE (v_counter <= 5) LOOP DBMS_OUTPUT.PUT_LINE('v_counter = '||v_counter); v_counter:=v_counter+1; END LOOP; END ;
- ٨٩ -
J
אא
אא
א
א
١٦٢
אאאאאאאwhile אא
KאאKאאא DECLARE v_counter NUMBER :=0; BEGIN LOOP DBMS_OUTPUT.PUT_LINE('v_counter = '||v_counter); EXIT WHEN v_counter > 5; v_counter:=v_counter+1; END LOOP; END ;
WHILEאאאאאWא אאאאאאW ACCEPT p_new_order PROMPT 'Enter the order number: ' ACCEPT p_items PROMPT 'Enter the number of items in this order: ' DECLARE NUMBER(2) := 1; v_count BEGIN WHILE v_count <= &p_items LOOP INSERT INTO item (ordid, itemid) VALUES (&p_new_order, v_count); v_count := v_count + 1; END LOOP; COMMIT; END;
item
- ٩٠ -
אא
אא
א
א
١٦٢
: Nested Loops אאאא א אא אKEWHILE FOR،Basic LoopsאFאא אאאKאELabelFאא
אא א (Exit)א א אא ELabelFא Kאאאאאאא
BEGIN אאאא <<Outer_loop>> LOOP v_counter := v_counter+1; EXIT WHEN v_counter>10; אאאאא <<Inner_loop>> LOOP ... EXIT Outer_loop WHEN total_done = 'YES'; אאא -- Leave both loops EXIT WHEN inner_done = 'YES'; אאאאא -- Leave inner loop only ... END LOOP Inner_loop; ... END LOOP Outer_loop; END;
- ٩١ -
אא
אא
א
א
١٦٢
CREATE TABLE messages VARCHAR2(60)) (results
WאאאאJ ١
KEFORאאאFK٨،٦אא١٠KKK١אJ
KEBlockFאאאאJ
RESULTS ---------1 2 3 4 5 7 9 10
KmessagesאJ
WNULLsalary אא،empאאJ ٢
insert into emp values (8000, 'DOE', 'CLERK', 7698, SYSDATE, NULL, NULL, 10); אאE PL/SQL BlockF J Wאא Ksubstitution variable אאא J
Wאאא(commission)א J - ٩٢ -
אא
אא
א
א
١٦٢
א
אא
٪١٠
١٠٠٠
٪١٥
١٥٠٠J ١٠٠٠
٪٢٠
١٥٠٠א NULL
Kאempא J אmessage E‘The number is odd’Fp6q4.sqlאJ ٣ KאאאE‘The number is even’F א KempSTARS K١
אא١٠٠STARSא* E PL/SQL BlockF K٢ אKE**********STARS١٠٠٠אאאF
Kp9q3.sql
Ksubstitution variable אאאJ Kא J
- ٩٣ -
אאא ã¹]<gè…‚jÖ]æ<ËÖ]<Üé×Ãj×Ö<íÚ^ÃÖ]<퉉ö¹] אאאא
אא
אא
אא
١٠
אא
אא
א
אא
١٦٢
Wאא Kאאאאאא Kאא
Wאא
Kאאאא Kאאא
Wאאא K٪١٠٠אאאאאא Wאא 3 Wאא K KH -
Wאא Kאאאאאאא
- ٩٤ -
אא
אא
א
אא
١٦٢
W א Block אאא
אאא א אKאא א،אאאא
אאK א אאKEErrorOracleF אא א،אא
K EBlockFא Wאאא Kאאא Wאא : Predefined Oracle Server errors א אJ ١ אKאאאא ٢٠ Oracle FOracle ERAISEF KEServer
: Non-Predefined Oracle server errors א אJ ٢ Kאoracle Oracle ,EBlockFאא KEOracle Server F WUser Defined Exceptionsאא אJ ٣ EBlockF אא
KאאאאKאא
אאא אאא KEBlockFאאא אאאWHEN
K אאאאאאאא K אאאWHEN OTHERS THEN א אאאאWHEN - ٩٥ -
OTHERS
אא
אא
א
אא
١٦٢
אWHENאK Kאאאאא EXCEPTION WHEN exception1 [OR exception2 . . .] THEN statement1; statement2; ... [WHEN exception3 [OR exception4 . . .] THEN statement1; statement2; . . .] [WHEN OTHERS THEN statement1; statement2; . . .] Kאאא אאאאאא אאאאאK א
KEBlockF
oracle אאא א
אK אאKEOracle Server F אאאאEBlockFאאא
KאאאאאK
- ٩٦ -
אא
אא
א
אא
١٦٢
א initialization nested collection method
Kinitialization varraytables
א אאאא .Select א אא א אאא PL/SQL אאאא
א ACCESS_INTO_NULL COLLECTION_IS_NULL CURSOR_ALREADY_OPEN DUP_VAL_ON_INDEX INVALID_CURSOR LOGIN_DENIED NO_DATA_FOUND NOT_LOGGED_ON PROGRAM_ERROR ROWTYPE_MISMATCH STORAGE_ERROR
collection method א SUBSCRIPT_BEYOND_COUNT אvarraytables nested Kא אאאא Select א א א
- ٩٧ -
TIMEOUR_RESOURCE TOO_MANY_ROWS VALUE_ERROR ZERO_DEVIDE
אא
אא
א
אא
١٦٢
DECLARE V_ename emp.ename%Type; V_empno emp.empno%Type := &p_eno; Begin SELECT ename INTO v_ename From emp WHERE empno = v_empno; DBMS_OUTPUT.PUT_LINE ('Employee Name is ' || v_ename ); EXCEPTION WHEN NO_DATA_FOUND THEN DBMS_OUTPUT.PUT_LINE (' Invalid Employee Number ' || v_empno); END; אא٧٧٧٧،٧٧٨٨אאא Enter value for p_eno: 7788
K٧٧٧٧א
Employee Name is SCOTT PL/SQL procedure successfully completed. SQL> / Enter value for p_eno: 7777 Invalid Employee Number 7777 PL/SQL procedure successfully completed.
- ٩٨ -
אא
אא
א
אא
١٦٢
DECLARE V_ename emp.ename%Type; V_job emp.job%Type := upper( '&p_ejob'); Begin SELECT ename INTO v_ename From emp WHERE job= v_job; DBMS_OUTPUT.PUT_LINE ('Employee Name is ' || v_ename ); EXCEPTION
WHEN NO_DATA_FOUND THEN DBMS_OUTPUT.PUT_LINE ('There is no Job Employee has this '|| V_job ); WHEN TOO_MANY_ROWS THEN DBMS_OUTPUT.PUT_LINE ('The Job '|| V_job || ' has more than one Employee ' ); END; DRIVER ،MANAGER،PRESIDENTאאא
MANAGERאאאא KDRIVER Enter value for p_ejob: PRESIDENT old 3: V_job new 3: V_job
emp.job%Type := upper( '&p_ejob'); emp.job%Type := upper( 'PRESIDENT');
Employee Name is KING PL/SQL procedure successfully completed. SQL> / - ٩٩ -
אא
אא
א
אא
١٦٢
Enter value for p_ejob: MANAGER old 3: V_job new 3: V_job
emp.job%Type := upper( '&p_ejob'); emp.job%Type := upper( 'MANAGER');
The Job MANAGER has more than one Employee PL/SQL procedure successfully completed. SQL> / Enter value for p_ejob: DRIVER There is no Job Employee has this DRIVER PL/SQL procedure successfully completed. :אאא Kאoracle oracle,EBlockFאא KEOracle Server F
Wאאאא :EBlockFאאא K١ DECLARE ….. Exception_name EXCEPTION; - ١٠٠ -
אא
אא
א
אא
١٦٢
אPRAGMA_EXCEPTION_INITאאא K٢ אא אא
Kאא PRAGMA_EXCEPTION_INIT(Exception_name ,error_number); WEBlockFאאאא K٣
אWHENאאאJ
Kא
KאאאJ W DECLARE EXCEPTION;
Æ
e_emps_remaining
PRAGMA EXCEPTION_INIT ( e_emps_remaining , -2292);
Æ
dept.deptno%TYPE := &p_deptno; v_deptno BEGIN DELETE FROM dept deptno = v_deptno; WHERE COMMIT; EXCEPTION WHEN e_emps_remaining THEN DBMS_OUTPUT.PUT_LINE ('Cannot remove dept ' || TO_CHAR(v_deptno) || '. Employees exist. '); END;
א אא
-٢٢٩٢
Æ
א
אJ ٢٢٩٢אe_emps_remainingאאא אאאא
Kא، - ١٠١ -
אא
אא
א
אא
١٦٢
Wאאאא אאאאאאא אEBlockFאא
KאאאאKא
Wאאאא :EBlockFאאא J ١
DECLARE ….. Exception_name EXCEPTION; EBlockFאאאא J ٢ KאRAISE אא
Begin …. RAISE Exception_name ….
; WKEBlockFאאאא J ٣
WHENאאאא
Kאא
،אאאאא W א e_invalid_productאEאFא
KאאאאK
- ١٠٢ -
אא
אא
א
אא
١٦٢
DECLARE EXCEPTION;e_invalid_product
א
Æ
BEGIN product UPDATE descrip = '&product_description' prodid = &product_number; WHERE
SET
IF SQL%NOTFOUND THEN RAISE e_invalid_product;
Æ
END IF; COMMIT; EXCEPTION WHEN e_invalid_product THEN DBMS_OUTPUT.PUT_LINE('Invalid product number.');
א א
Æ
א END;
: SQLERRM وSQLCODEא אאאאאאSQLERRMSQLCODE J אאאא
Kאא אא KאאאWSQLCODE J
KאאאSQLERRM J SQLCODE א א
٠
אא NO_DATA_FOUND
١ H١٠٠
ORACLE
- ١٠٣ -
אא
אא
א
אא
١٦٢
؟אא אאאא אאא אאאאאאKאא
KאKKKKאא
DECLARE ... EXCEPTION; e_no_rows EXCEPTION; e_integrity PRAGMA EXCEPTION_INIT (e_integrity, -2292); BEGIN FOR c_record IN emp_cursor LOOP BEGIN SELECT ... UPDATE ... IF SQL%NOTFOUND THEN RAISE e_no_rows; END IF; EXCEPTION WHEN e_integrity THEN ... WHEN e_no_rows THEN ... END;
אאאא אSub Block
אאאא א א Main
Blockאא
א
אא Main Block
END LOOP; אא EXCEPTION אMain Block א WHEN NO_DATA_FOUND THEN . . . WHEN TOO_MANY_ROWS THEN . . . א א END; SQL א אא *Plus
- ١٠٤ -
אא
אא
א
אא
١٦٢
WRAISE_APPLICATION_ERROR אאאא אאאאא Procedures, FאאאאאאאKא
KEFunctions
RAISE_APPLICATION_ERROR(error_number,message,[TRUE,FLASE]); אKאא
error_number
J ٢٠٩٩٩J ٢٠٠٠٠א
א
Stack.אאאאTRUEKא
message
[TRUE,FLASE]
KאאאFLASE EDefualtFאא
אאRAISE_APPLICATION_ERRORאא Kאא
… BEGIN …. IF (v_grade > 100 OR v_grade < 0) THEN RAISE_APPLICATION_ERROR(-20210,’invlid grade ’); END IF; ….. END;
… EXCEPTION WHEN NO_DATA_FOUND THEN RAISE_APPLICATION_ERROR(-20010,’invlid employee number’); ….. END; - ١٠٥ -
אא
אא
א
אא
١٦٢
KEאאFאאאאE PL/SQL BlockF J ١
אאאאאא J
Kmessage More than one employee with a salary<salary>
אאאאאא J Kmessage No employee with a salary<salary>
אאאאא א אא J Kmessage
Kאא J
RESULTS -----------------------------------------------------------SMITH – 800 More than one employee with a salary of 3000 No employee with a salary of 6000 אאאאp8q3.sqlא J ٢ Kdept
Kא Please enter the department number: 50 Please enter the department location: HOUSTON PL/SQL procedure successfully completed. G_MESSAGE -------------------------------------------------------------------------------Department 50 is an invalid department אאאE PL/SQL BlockF J ٣ K١٠٠אאאא Kאאאאאא J Kאאאאא J
אאאאא J
K(Some other error occurred ) - ١٠٦ -
אא
אא
א
אא
١٦٢
Please enter the salary: 800 PL/SQL procedure successfully completed. G_MESSAGE -------------------------------------------------------------------------------There is/are 1 employee(s) with a salary between 700 and 900 Please enter the salary: 3000 PL/SQL procedure successfully completed. G_MESSAGE -------------------------------------------------------------------------------There is/are 3 employee(s) with a salary between 2900 and 3100 Please enter the salary: 6000 PL/SQL procedure successfully completed. G_MESSAGE -------------------------------------------------------------------------------There is no employee salary between 5900 and 6100
- ١٠٧ -
אא
א
١٦٢
SQL> DESCRIBE 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 NOT NULL NUMBER(2)
SQL> SELECT * FROM emp; EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO ------ ---------- --------- ---------- -------- ---------- ---------- ---------7839 KING PRESIDENT 17/11/81 5000 10 7698 BLAKE MANAGER 7839 01/05/81 2850 30 7782 CLARK MANAGER 7839 09/06/81 2450 10 7566 JONES MANAGER 7839 02/04/81 2975 20 7654 MARTIN SALESMAN 7698 28/09/81 1250 1400 30 7499 ALLEN SALESMAN 7698 20/02/81 1600 300 30 7844 TURNER SALESMAN 7698 08/09/81 1500 0 30 7900 JAMES CLERK 7698 03/12/81 950 30 7521 WARD SALESMAN 7698 22/02/81 1250 500 30 7902 FORD ANALYST 7566 03/12/81 3000 20 7369 SMITH CLERK 7902 17/12/80 800 20 7788 SCOTT ANALYST 7566 09/12/82 3000 20 7876 ADAMS CLERK 7788 12/01/83 1100 20 7934 MILLER CLERK 7782 23/01/82 1300 10
SQL>
DESCRIBE dept
Name Null? Type ----------------------------------------------------- -------- ----------------DEPTNO NOT NULL NUMBER(2) DNAME VARCHAR2(14) LOC VARCHAR2(13)
SQL> SELECT * FROM DEPTNO ---------10 20 30 40
dept;
DNAME -------------ACCOUNTING RESEARCH SALES OPERATIONS
LOC ------------NEW YORK DALLAS CHICAGO BOSTON
- ١٠٨ -
SQL>
אא
א
١٦٢
DESCRIBE SALGRADE
Name Null? Type ----------------------------------------------------- -------- ------------------GRADE NUMBER LOSAL NUMBER HISAL NUMBER
SQL> SELECT *
FROM SALGRADE;
GRADE ---------- 1 2 3 4 5
LOSAL HISAL --------700 1201 1401 2001 3001
SQL>
---------1200 1400 2000 3000 9999
DESCRIBE ord
Name Null? Type ----------------------------------------------------ORDID NOT NULL NUMBER(4) ORDERDATE COMMPLAN VARCHAR2(1) CUSTID NOT NULL SHIPDATE DATE TOTAL NUMBER(8,2)
SQL>
SELECT *
ORDID -------610 611 612 601 602 604 605 606 609 607 608 603 620 613
ORDERDAT -------07/01/87 11/01/87 15/01/87 01/05/86 05/06/86 15/06/86 14/07/86 14/07/86 01/08/86 18/07/86 25/07/86 05/06/86 12/03/87 01/02/87
FROM
ord;
C CUSTID SHIPDATE TOTAL - ---------- -------- ---------A 101 08/01/87 101.4 B 102 11/01/87 45 C 104 20/01/87 5860 A 106 30/05/86 2.4 B 102 20/06/86 56 A 106 30/06/86 698 A 106 30/07/86 8324 A 100 30/07/86 3.4 B 100 15/08/86 97.5 C 104 18/07/86 5.6 C 104 25/07/86 35.2 102 05/06/86 224 100 12/03/87 4450 108 01/02/87 6400
- ١٠٩ -
--------
DATE
NUMBER(6)
614 616 619 617 615 618 621
01/02/87 03/02/87 22/02/87 05/02/87 01/02/87 15/02/87 A 15/03/87 A
102 103 104 105 107 102 100
אא
א
١٦٢
05/02/87 10/02/87 04/02/87 03/03/87 06/02/87 06/03/87 01/01/87
23940 764 1260 46370 710 3510.5 730
SQL> DESCRIBE PRODUCT Name Null? Type ----------------------------------------------------- -------- ---------------------PRODID NOT NULL NUMBER(6) DESCRIP VARCHAR2(30)
SQL>
SELECT *
PRODID ---------100860 100861 100870 100871 100890 101860 101863 102130 200376 200380
FROM
PRODUCT ;
DESCRIP -----------------------------ACE TENNIS RACKET I ACE TENNIS RACKET II ACE TENNIS BALLS-3 PACK ACE TENNIS BALLS-6 PACK ACE TENNIS NET SP TENNIS RACKET SP JUNIOR RACKET RH: "GUIDE TO TENNIS" SB ENERGY BAR-6 PACK SB VITA SNACK-6 PACK
SQL> DESCRIBE ITEM Name -------------------------ORDID ITEMID PRODID ACTUALPRICE QTY ITEMTOT SQL>
SELECT *
FROM
Null? -------NOT NULL NOT NULL
Type -----------NUMBER(4) NUMBER(4) NUMBER(6) NUMBER(8,2) NUMBER(8) NUMBER(8,2)
ITEM;
ORDID ITEMID PRODID ACTUALPRICE QTY ITEMTOT ------- ---------- ---------- ----------- ---------- ---------610 3 100890 58 1 58 611 1 100861 45 1 45 612 1 100860 30 100 3000 601 1 200376 2.4 1 2.4 602 1 100870 2.8 20 56
- ١١٠ -
604 604 604 603 610 610 613 614 614 612 612 620 620 620 613 613 613 619 617 617 614 616 616 616 616 616 619 619 615 607 615 617 617 617 617 617 617 617 617 609 609 618 618 618 621 621 615 608 608 609 606 605 605 605 605
1 2 3 2 1 2 4 1 2 2 3 1 2 3 1 2 3 3 1 2 3 1 2 3 4 5 1 2 1 1 2 3 4 5 6 7 8 9 10 2 3 1 2 3 1 2 3 1 2 1 1 1 2 3 4
100890 100861 100860 100860 100860 100870 200376 100860 100870 100861 101863 100860 200376 102130 100871 101860 200380 102130 100860 100861 100871 100861 100870 100890 102130 200376 200380 200376 100861 100871 100870 100870 100871 100890 101860 101863 102130 200376 200380 100870 100890 100860 100861 100870 100861 100870 100871 101860 100871 100861 102130 100861 100870 100890 101860
אא
א
١٦٢
58 42 44 56 35 2.8 2.2 35 2.8 40.5 10 35 2.4 3.4 5.6 24 4 3.4 35 45 5.6 45 2.8 58 3.4 2.4 4 2.4 45 5.6 2.8 2.8 5.6 58 24 12.5 3.4 2.4 4 2.5 50 35 45.11 45 45 2.8 5 24 5.6 35 3.4 45 2.8 58 24
- ١١١ -
3 2 10 4 1 3 200 444 1000 20 150 10 1000 500 100 200 150 100 50 100 1000 10 50 2 10 10 100 100 4 1 100 500 500 500 100 200 100 200 300 5 1 23 50 10 10 100 50 1 2 1 1 100 500 5 50
174 84 440 224 35 8.4 440 15540 2800 810 1500 350 2400 1700 560 4800 600 340 1750 4500 5600 450 140 116 34 24 400 240 180 5.6 280 1400 2800 29000 2400 2500 340 480 1200 12.5 50 805 2255.5 450 450 280 250 24 11.2 35 3.4 4500 1400 290 1200
605 605 612 619
5 6 4 4
101863 102130 100871 100871
אא
א
١٦٢
9 3.4 5.5 5.6
100 10 100 50
900 34 550 280
SQL> DESCRIBE PRICE Name Null? Type ----------------------------------------------------- -------- -----------------PRODID NOT NULL NUMBER(6) STDPRICE NUMBER(8,2) MINPRICE NUMBER(8,2) STARTDATE DATE ENDDATE DATE
SQL> SELECT
* FROM PRICE;
PRODID STDPRICE MINPRICE STARTDAT ENDDATE ---------- ---------- ---------- -------- -------100871 4.8 3.2 01/01/85 01/12/85 100890 58 46.4 01/01/85 100860 35 28 01/06/86 100860 30 24 01/01/85 31/12/85 100861 45 36 01/06/86 100861 39 31.2 01/01/85 31/12/85 100870 2.8 2.4 01/01/86 100870 2.4 1.9 01/01/85 01/12/85 100871 5.6 4.8 01/01/86 101860 24 18 15/02/85 101863 12.5 9.4 15/02/85 102130 3.4 2.8 18/08/85 200376 2.4 1.75 15/11/86 200380 4 3.2 15/11/86
SQL> DESCRIBE CUSTOMER Name Null? Type ----------------------------------------------------- -------- -------------CUSTID NOT NULL NUMBER(6) NAME VARCHAR2(45) ADDRESS VARCHAR2(40) CITY VARCHAR2(30) STATE VARCHAR2(2) ZIP VARCHAR2(9) AREA NUMBER(3) PHONE VARCHAR2(9) REPID NOT NULL NUMBER(4) CREDITLIMIT NUMBER(9,2) COMMENTS LONG
- ١١٢ -
CUSTID -------100 101 102 103 104 105 106 107 108
א
١٦٢
NAME ADDRESS --------------------------------------------- ---------------------JOCKSPORTS TKB SPORT SHOP VOLLYRITE JUST TENNIS EVERY MOUNTAIN K + T SPORTS SHAPE UP WOMENS SPORTS NORTH WOODS HEALTH AND FITNESS SUPPLY CENTER
CITY CREDITLIMIT -----------------BELMONT 5000 REDWOOD CITY 10000 BURLINGAME 7000 BURLINGAME 3000 CUPERTINO 10000 SANTA CLARA 5000 PALO ALTO 6000 SUNNYVALE 10000 HIBBING 8000 CUSTID -------100 101 102 103 104 105 106 107
אא
ST ZIP
AREA PHONE
345 VIEWRIDGE 490 BOLI RD. 9722 HAMILTON HILLVIEW MALL 574 SUYYYYY RD. 3476 EL PASEO 908 SEQUOIA VALCO VILLAGE 98 LONE PINE WAY REPID
-- --------- ---------- --------- ---------- ---------CA 96711
415 598-6609
7844
CA 94061
415 368-1223
7521
CA 95133
415 644-3341
7654
CA 97544
415 677-9312
7521
CA 93301
408 996-2323
7499
CA 91003
408 376-9966
7844
CA 94301
415 364-9777
7521
CA 93301
408 967-4398
7499
MN 55649
612 566-9123
7844
COMMENTS ---------------------------------------------------------------------------Very friendly people to work with -- sales rep likes to be called Mike. Rep called 5/8 about change in order - contact shipping. Company doing heavy promotion beginning 10/89. Prepare for large orders during Contact rep about new line of tennis rackets. Customer with high market share (23%) due to aggressive advertising. Tends to order large amounts of merchandise at once. Accounting is considering Support intensive. Orders small amounts (< 800) of merchandise at a time. First sporting goods store geared exclusively towards women. Unusual Promotional style and very willing to take chances towards new products!
108
- ١١٣ -
א
אא
א
١٦٢
א א ١ ٨ ١٥
א Wאא Kאא Wאא Kאאא Wאא Kאא
Wאאא Kא א
٢٦ ٣٧
Wאא Kאא
Wאא Kאא
٤٦ ٥٩ ٦٨ ٧٧
Wאא Kאא
Wאא KOracle א Wאא Kא
٩٤
Wאא Kאא
١٠٨
א
אאאאא א אEאFאא GOTEVOT appreciates the financial support provided by BAE SYSTEMS