sql valodas paplašinājums – valoda pl/sql web viewthe pl/sql engine is the tool used...
TRANSCRIPT
Programmēšana datu bāzes serverī (server-side programming)
In a nonprocedural language such as SQL, the set of data to be operated on is specified, but not the operations to be performed or the manner in which they are to be carried out. In a procedural language program, most statement execution depends on previous or subsequent statements and on control structures, such as loops or conditional branches, that are not available in SQL.The problem is how procedural database applications requiring conditional logic and program flow control can use SQL. The basic development approaches are as follows:
1) use client-side programming to embed SQL statements in applications written in procedural languages such as C, C++, or Java. You can place SQL statements in source code and submit it to a precompiler or Java translator before compilation. Alternatively, you can eliminate the precompilation step and use an API such as Java Database Connectivity (JDBC) or Oracle Call Interface (OCI) to enable the application to interact with the database.
2) use server-side programming to develop data logic that resides in the database. An application can explicitly invoke stored subprograms (procedures and functions), written in PL/SQL or Java. The principal benefit of server-side programming is that functionality built into the database can be deployed anywhere. The database and not the application determine the best way to perform tasks on a given operating system. Also, subprograms increase scalability by centralizing application processing on the server, enabling clients to reuse code. Because subprogram calls are quick and efficient, a single call can start a compute-intensive stored subprogram, reducing network traffic.
1
Calling a stored procedure
2
Calling subprograms in a package
3
SQL valodas paplašināšanājumi ar procedūru valodu
1. Vaicājumu valoda SQL ir ceturtās paaudzes valoda (4GL – fourth-
generation language). Tā definē kādi dati jāiegūst, ievērojot kādus meklēšanas
noteikumus. Kā šos datus iegūt netiek noteikts. Pamatojoties uz relāciju
algebru, datu bāzes sistēma to "izdomā" pati.
2. Ja nepieciešama loģiski sarežģīta datu izguve, ne vienmēr to var vai ir grūti
realizēt tikai ar SQL valodas palīdzību. Nepieciešams datu bāzes serverī
papildus izmantot procedurālās programmēšanas valodas (3GL). Tās veido
4
Languages in Oracle Database
1. PL/SQL. PL/SQL is the Oracle Database procedural extension to SQL. PL/SQL is integrated with the database, supporting all Oracle SQL statements, functions, and data types. Applications written in database APIs can invoke PL/SQL stored subprograms and send PL/SQL code blocks to the database for execution.
2. Java. Oracle Database also provides support for developing, storing, and deploying Java applications. Java stored subprograms run in the database and are independent of programs that run in the middle tier. Java stored subprograms interface with SQL using a similar execution model to PL/SQL.
5
PL/SQL, the Oracle procedural extension of SQL, is a portable, high-performance transaction-processing language.
PL/SQL is tightly integrated with SQL, the most widely used database manipulation language.
PL/SQL combines the data-manipulating power of SQL with the processing power of procedural languages.
PL/SQL bloku apstrāde
6
The PL/SQL Engine and Oracle Database
PL/SQL supports both native execution and interpreted execution. In interpreted execution, PL/SQL source code is compiled into a so-called byte code representation, which is run by a portable virtual computer implemented as part of Oracle Database. In native execution, which offers the best performance on computationally intensive program units, the source code of PL/SQL program units is compiled directly to object code for the given platform. This object code is linked into Oracle Database.The PL/SQL engine is the tool used to define, compile, and run PL/SQL program units. This engine is a special component of many Oracle products, including Oracle Database. The program unit is stored in a database. When an application calls a stored procedure, the database loads the compiled program unit into the shared pool in the system global area (SGA). The PL/SQL and SQL statement executors work together to process the statements in the procedure.You can call a stored procedure from another PL/SQL block, which can be either an anonymous block or another stored procedure. A PL/SQL procedure executing on Oracle Database can call an external procedure or function written in the C programming language and stored in a shared library. The C routine runs in a separate address space from that of Oracle Database.
7
Tīkla noslodzes samazināšana
a) b)
PL/SQL bloks
Datu bāzes serveris
SQL
SQL
Lietojums
SQL
Datu bāzes serveris
Lietojums
SQL…SQL…SQL
8
Java and PL/SQL IntegrationYou can call existing PL/SQL programs from Java and Java programs from PL/SQL. This solution protects and leverages your PL/SQL and Java code.Oracle Database offers two different approaches for accessing SQL data from Java:
1) JDBC;2) SQLJ.
Both approaches are available on the client and server. As a result, you can deploy applications on the client and server without modifying the code.
JDBC is a database access protocol that enables you to connect to a database and run SQL statements and queries to the database. The core Java class libraries provide only one JDBC API, java.sql. However, JDBC is designed to enable vendors to supply drivers that offer the necessary specialization for a particular database. Oracle provides the distinct JDBC drivers shown in the following table.
SQLJ is an ANSI standard for embedding SQL statements in Java programs. You can use SQLJ in stored procedures, triggers, and methods within the Oracle Database environment. In addition, you can combine SQLJ programs with JDBC.SQLJ provides a simple, but powerful, way to develop client-side and middle-tier applications that access databases from Java. A developer writes a program using SQLJ and then uses the SQLJ translator to translate embedded SQL to pure JDBC-based Java code. At run time, the program can communicate with multi-vendor databases using standard JDBC drivers.The following example shows a simple SQLJ executable statement:
String name;#sql { SELECT first_name INTO :name FROM employees WHERE employee_id=112 };System.out.println("Name is " + name + ", employee number = " + employee_id);
Because Oracle Database provides a complete Java environment, you cannot compile SQLJ programs on a client that will run on the database. Instead, you can compile them directly on the server.
9
PL/SQL (Oracle DBVS) bloks
Valodas PL/SQL pamatvienība ir bloks.
No blokiem sastāv visas PL/SQL programmas.
Bloki var atrasties viens aiz otra.
Viens bloks var tikt iekļauts citā.
Valodas PL/SQL bloka struktūra ir sekojoša:
<<label >> (optional)
[ DECLARE ]
mainīgo un kursoru definēšana
BEGIN
izpildāmās darbības (operatori, procedūras, funkcijas un SQL
operatori)
[ EXCEPTION ]
īpašo situāciju apstrāde
END;
10
PL/SQL valodas bloku tipi1. Anonīmais bloks (anonymous block). Tas tiek veidots dināmiski un izpildīts
parasti vienu reizi.
2. Apakšprogrammas (subprograms). Tās ir:
- procedūra;
- funkcija;
- programmu pakete (package) jeb modulis (Oracle DBVS).
Programmu pakete ļauj glabāt saistītas programmas un datus vienā vietā.
Tā sastāv no apraksta un ķermeņa.
3. Trigeris (trigger). Trigeri ir programmas, kuras izpildas vienmēr kad notiek
noteikts notikums (piemēram, pirms raksta dzēšanas vai pēc tam).
4. Objektu metode (MAP, ORDER, MEMBER, STATIC,
CONSTRUCTOR). Tiek izmantota relāciju – objektu datu bāzes
struktūrām.
11
PL/SQL valodas pamatvienības
1. Leksiskās vienības
Valodā PL/SQL tiek izmantotas sekojošas leksiskās vienības:- lielie un mazie burti: A - Z un a - z;- cipari: 0 – 9;- atdalītāji: tabulācijas simboli, tukšumzīme (atstarpe, starpa), simbols – rindas beigas;- matemātiskie simboli: + - * / < > = ;- punktuācijas simboli: ( ) [ ] ? ! ~ ; : . ‘ “ @ # % $ ^ & _ | .PL/SQL valodā nav atšķirības starp lieliem un maziem burtiem, tikai gadījumā, ja simboli veido rindu, kas ieslēgta pēdiņās vai apostrofos.
2. Identifikatori
Identifikatori tiek lietoti, lai veidotu PL/SQL valodas objektu nosaukumus. Tiem jāsākas ar burta simbolu. Piemēri: AAA, AA_BB, A1111, A###34. Identifikatora maksimālais garums ir 30 simboli. PL/SQL valodā nav atšķirību starp latīņu lielajiem un mazajiem burtiem. Ja nepieciešams atpazīt lielos un mazos burtus, kā arī atstarpes (tukšumus), identifikators tiek iekļauts pēdiņās (piemēram, identifikators "EXCEPTION”). Datu bāzes vārdnīcā visi metadatu nosaukumi glabājas lielo burtu formā.
3. Komentāri
Tiek lietoti divu veidu komentāri:1. vienas rindas komentārs – komentāra rinda iesākas ar divām domu zīmēm: --Komentārs2. vairāku rindu komentārs – tie iesākas ar /* un beidzas ar */ zīmēm. Šā tipa komentārus
var lietot arī rindā pēc PL/SQL valodas komandām (rindām).
4. Mainīgie
Mainīgie (variables) ir atmiņas vienības, kurās var tikt glabātas noteiktas vērtības.
Mainīgie tiek definēti bloka deklarāciju daļā. To apraksta sintakse ir sekojoša:
mainīgā_nosaukums mainīgā_tips [CONSTANT]
[NOT NULL] [:= vērtība] ;
mainīgā_nosaukums mainīgā_tips [CONSTANT]
[DEFAULT vērtība] ;
12
Mainīgo tipi PL/SQL valodā
PL/SQL valodā tiek lietotas trīs maiīgo tipu grupas: 1. skalārie tipi;2. saliktie tipi;3. atsauksmju tipi;4. lielo objektu tipi.
Skalārie datu tipiSkaitliskie tipi Simbolu tipiBINARY_INTEGER CHARDEC CHARACTERDECIMAL LONGDOUBLE PRECISION NCHARFLOT NVARCHAR2INT STRING INTEGER VARCHAR NATURAL VARCHAR2 NATURALN1 Loģiskais tipsNUMBER BOOLENNUMERIC Datuma tipsPLS_INTEGER DATE POSITIVEPOSITIVENREALSIGNTYPESMALLINT
PL/SQL valodas datu tipi
Saliktie tipiRECORDTABLEVARRAY
Atsauces tipiREF CURSORREF objekta tips
Lielo objektu tipiBFILELOBCLOBNLOB
13
PL/SQL valodā biežāk izmantotie datu tipi
1) skaitlis - NUMBER (P,S) P-ciparu skaits, S – decimālzīmju skaits;
1) definēta garuma simbolu rinda - CHAR (garums), garums –
maksimālais simbolu skaits;
2) mainīga garuma simbolu rinda - VARCHAR2 (garums);
3) simbolu rinda (parasti raksts) – RAW(garums);
4) datums un laiks – DATE;
5) loģiskie mainīgie - BOOLEAN {TRUE, FALSE};
6) rindu (rakstu) identifikatoru saglabāšana – ROWID;
7) atsauce uz tabulas lauka tipu - Tabula.LAUKS%TYPE;
8) jauna tipa veidošana - SUBTYPE Jaunais_tips IS Standarta_tips.
Piešķires darbība (assignment): mainīgais : = izteiksme;
Datu tipu pārveidojuma funkcijas:TO_CHAR, TO_DATE, TO_NUMBER, CHARTOROWID, ROWIDTOCHAR.
14
PL/SQL valodas vadības struktūras15
PL/SQL valodas vadības struktūru definēšana
1. Lpģiskais operators IF – THEN - ELSE
IF loģikas_izteiksme THEN
operatoru_virkne;
ELSIF loģikas_izteiksme THEN
operatoru_virkne;
ELSIF loģikas_izteiksme THEN
operatoru_virkne;
ELSE
operatoru_virkne;
END IF;
2. Loģiskais operators CASE
CASE izteiksme
WHEN izteiksmes vērtība THEN izpildāmais kods;
WHEN izteiksmes vērtība THEN izpildāmais kods;
...
ELSE izpildāmais kods;
END CASE;
3. Vienkāršs cikls LOOP – END LOOP
LOOP
operatoru_virkne;
END LOOP;
Izeju no cikla realizē operators EXIT vai EXIT WHEN noteikums.
PiemērsDECLARE …BEGIN…IF pensija < 50 THEN vērtējums := ‘ļoti maza’;ELSIF pensija < 80 THEN vērtējums := ‘maza’;ELSIF pensija < 100 THEN vērtējums := ‘standarta’;ELSE vērtējums := ‘ko Dievs devis’;END IF;…END;
16
PL/SQL valodas vadības struktūru definēšana (turpinājums)
4. Cikls ar noteikumu WHILE – LOOP – END LOOP
WHILE noteikums LOPP
operatoru_virkne;
END LOOP;
5. Cikls ar iterāciju skaitītāju FOR – LOOP – END LOOP
FOR iterāciju_skaitītājs IN REVERSE iterācijas_sākum_numurs ..
iterācijas_beigu_numurs LOOP
operatoru_virkne;
END LOOP;
BEGIN
FOR skaitītājs IN 1 .. 30 LOOP
INSERT INTO Tabula_1 VALUES ( skaitītājs, ‘Raksta numurs’);
END LOOP;
END;
5. Operators GOTO un iezīmes
GOTO iezīmeDECLARE done BOOLEAN;BEGIN FOR i IN 1..50 LOOP IF done THEN GOTO end_loop; END IF; <<end_loop>> END LOOP;END;
17
6. Operators NULL
Iekļaujot attiecīgā programmas vietā operatoru NULL, nekādas darbības netiek
izpildītas.
18