1 LDD et PL/SQL Patrice Saintenoy Université Paris X - Nanterre UFR SEGMI Licence MIAGE.
-
Upload
janie-sawin -
Category
Documents
-
view
218 -
download
3
Transcript of 1 LDD et PL/SQL Patrice Saintenoy Université Paris X - Nanterre UFR SEGMI Licence MIAGE.
![Page 1: 1 LDD et PL/SQL Patrice Saintenoy Université Paris X - Nanterre UFR SEGMI Licence MIAGE.](https://reader030.fdocuments.in/reader030/viewer/2022032516/56649c7b5503460f9492ea0b/html5/thumbnails/1.jpg)
1
LDD et PL/SQL
Patrice Saintenoy
Université Paris X - Nanterre UFR SEGMI Licence MIAGE
![Page 2: 1 LDD et PL/SQL Patrice Saintenoy Université Paris X - Nanterre UFR SEGMI Licence MIAGE.](https://reader030.fdocuments.in/reader030/viewer/2022032516/56649c7b5503460f9492ea0b/html5/thumbnails/2.jpg)
2
LDD
( Langage de description des Données)
![Page 3: 1 LDD et PL/SQL Patrice Saintenoy Université Paris X - Nanterre UFR SEGMI Licence MIAGE.](https://reader030.fdocuments.in/reader030/viewer/2022032516/56649c7b5503460f9492ea0b/html5/thumbnails/3.jpg)
3
SEQUENCESCREATE SEQUENCE [user.]nomSequence
[INCREMENT BY n] [START WITH n]
CREATE SEQUENCE eseq;INSERT INTO EMP VALUES(eseq.nextval,
'TOTO', 10000.00);
DROP SEQUENCE eseq;
![Page 4: 1 LDD et PL/SQL Patrice Saintenoy Université Paris X - Nanterre UFR SEGMI Licence MIAGE.](https://reader030.fdocuments.in/reader030/viewer/2022032516/56649c7b5503460f9492ea0b/html5/thumbnails/4.jpg)
4
SynonymesCREATE SYNONYM [user.]nomSym FOR
[user.]nomTable;
CREATE SYNONYM piece FOR TP.PIECE;
DROP SYNONYM piece;
![Page 5: 1 LDD et PL/SQL Patrice Saintenoy Université Paris X - Nanterre UFR SEGMI Licence MIAGE.](https://reader030.fdocuments.in/reader030/viewer/2022032516/56649c7b5503460f9492ea0b/html5/thumbnails/5.jpg)
5
INDEX
CREATE [UNIQUE] INDEX nomIndex ON table(attribut [ASC | DESC], ...);
CREATE INDEX indemp ON emp(numDept);
![Page 6: 1 LDD et PL/SQL Patrice Saintenoy Université Paris X - Nanterre UFR SEGMI Licence MIAGE.](https://reader030.fdocuments.in/reader030/viewer/2022032516/56649c7b5503460f9492ea0b/html5/thumbnails/6.jpg)
6
Langage de contrôle et de sécurité
![Page 7: 1 LDD et PL/SQL Patrice Saintenoy Université Paris X - Nanterre UFR SEGMI Licence MIAGE.](https://reader030.fdocuments.in/reader030/viewer/2022032516/56649c7b5503460f9492ea0b/html5/thumbnails/7.jpg)
7
Utilisateurs• Droits généraux GRANT privilèges TO utilisateurId IDENTIFIED BY motDePasse; CONNECT, RESOURCE, DBA
• Droits sur les objets GRANT droits ON table | vue TO utilisateur [WITH
GRANT OPTION];
SELECT , INSERT, UPDATE, DELETE, ALTER, INDEX, CLUSTER, ALL
![Page 8: 1 LDD et PL/SQL Patrice Saintenoy Université Paris X - Nanterre UFR SEGMI Licence MIAGE.](https://reader030.fdocuments.in/reader030/viewer/2022032516/56649c7b5503460f9492ea0b/html5/thumbnails/8.jpg)
8
Utilisateurs (suite)
REVOKE droits ON table TO nom;
![Page 9: 1 LDD et PL/SQL Patrice Saintenoy Université Paris X - Nanterre UFR SEGMI Licence MIAGE.](https://reader030.fdocuments.in/reader030/viewer/2022032516/56649c7b5503460f9492ea0b/html5/thumbnails/9.jpg)
9
Rôles et privilègesgrant create table to toto;
grant alter any table to toto;
create role lambda;
grant select, insert on tab to lambda;
grant create any view to lambda;
grant lambda to toto;
grant lambda to machin;
![Page 10: 1 LDD et PL/SQL Patrice Saintenoy Université Paris X - Nanterre UFR SEGMI Licence MIAGE.](https://reader030.fdocuments.in/reader030/viewer/2022032516/56649c7b5503460f9492ea0b/html5/thumbnails/10.jpg)
10
Programmation et BD
![Page 11: 1 LDD et PL/SQL Patrice Saintenoy Université Paris X - Nanterre UFR SEGMI Licence MIAGE.](https://reader030.fdocuments.in/reader030/viewer/2022032516/56649c7b5503460f9492ea0b/html5/thumbnails/11.jpg)
11
Différentes approches• Intégration dans un langage procédural par
utilisation de pré-compilateurs (embedded SQL):
ORACLE : PRO*C, PRO*ADA, PRO*COBOL
• Langages adaptés : Access Basic (Visual Basic), PL/SQL (Ada+SQL)
![Page 12: 1 LDD et PL/SQL Patrice Saintenoy Université Paris X - Nanterre UFR SEGMI Licence MIAGE.](https://reader030.fdocuments.in/reader030/viewer/2022032516/56649c7b5503460f9492ea0b/html5/thumbnails/12.jpg)
12
Différentes approches (suite)
• API : ODBC (tous langages), JDBC (Java), SERVLETS (Java)
• Langages de script - accès par internet (pages JSP (Java - SUN), ASP (MicroSoft), PHP, PERL, )
![Page 13: 1 LDD et PL/SQL Patrice Saintenoy Université Paris X - Nanterre UFR SEGMI Licence MIAGE.](https://reader030.fdocuments.in/reader030/viewer/2022032516/56649c7b5503460f9492ea0b/html5/thumbnails/13.jpg)
13
PL/SQL
![Page 14: 1 LDD et PL/SQL Patrice Saintenoy Université Paris X - Nanterre UFR SEGMI Licence MIAGE.](https://reader030.fdocuments.in/reader030/viewer/2022032516/56649c7b5503460f9492ea0b/html5/thumbnails/14.jpg)
14
PL/SQL (suite)Structure d’un bloc
DECLARE
-- déclarations de variables
BEGIN
-- instructions
EXCEPTION
-- traitements d'exception
END
![Page 15: 1 LDD et PL/SQL Patrice Saintenoy Université Paris X - Nanterre UFR SEGMI Licence MIAGE.](https://reader030.fdocuments.in/reader030/viewer/2022032516/56649c7b5503460f9492ea0b/html5/thumbnails/15.jpg)
15
PL/SQL (suite)Declare
numdeptv Dept.numdept%type = 10;
nbenr number(5);
minimum number;
valeur number;
![Page 16: 1 LDD et PL/SQL Patrice Saintenoy Université Paris X - Nanterre UFR SEGMI Licence MIAGE.](https://reader030.fdocuments.in/reader030/viewer/2022032516/56649c7b5503460f9492ea0b/html5/thumbnails/16.jpg)
16
PL/SQL (suite)begin
SELECT count(*) INTO nbenr FROM emp
WHERE dept=numdeptv;
if nbenr > 0 then
UPDATE emp SET dept = null WHERE dept=numdeptv;
end if;
DELETE FROM dept WHERE numdept=numdeptv;
![Page 17: 1 LDD et PL/SQL Patrice Saintenoy Université Paris X - Nanterre UFR SEGMI Licence MIAGE.](https://reader030.fdocuments.in/reader030/viewer/2022032516/56649c7b5503460f9492ea0b/html5/thumbnails/17.jpg)
17
PL/SQL (suite)SELECT min(salaire) INTO minimum
FROM emp WHERE dept=5;
if valeur < minimum
then augmentationSalaire;
else message(' valeur superieure a: '|| to_char(minimum)||'.');
end if;
COMMIT;
![Page 18: 1 LDD et PL/SQL Patrice Saintenoy Université Paris X - Nanterre UFR SEGMI Licence MIAGE.](https://reader030.fdocuments.in/reader030/viewer/2022032516/56649c7b5503460f9492ea0b/html5/thumbnails/18.jpg)
18
PL/SQL (suite)exception
when no_data_found then INSERT INTO
erreur_log VALUES(sysdate, numdeptv);
when others then
err_code:=sqlcod;
err_txt := sqlerrm;
INSERT INTO errlog2 VALUES(sysdate,
err_code,err_txt);
end;
![Page 19: 1 LDD et PL/SQL Patrice Saintenoy Université Paris X - Nanterre UFR SEGMI Licence MIAGE.](https://reader030.fdocuments.in/reader030/viewer/2022032516/56649c7b5503460f9492ea0b/html5/thumbnails/19.jpg)
19
Déclarations PL/SQL• Types SQL• Référence au type d'un attribut de table :
T.attribut%type
![Page 20: 1 LDD et PL/SQL Patrice Saintenoy Université Paris X - Nanterre UFR SEGMI Licence MIAGE.](https://reader030.fdocuments.in/reader030/viewer/2022032516/56649c7b5503460f9492ea0b/html5/thumbnails/20.jpg)
20
Déclarations PL/SQLdeclare x varchar2(50); OK boolean; v constant number(8) default 500; t number(5) not null :=0; cursor c1 is select numEmp from emp where nom like '%x%
order by nom; empRec emp%rowtype; buffer c1%rowtype; trop exception; pas_assez exception;
![Page 21: 1 LDD et PL/SQL Patrice Saintenoy Université Paris X - Nanterre UFR SEGMI Licence MIAGE.](https://reader030.fdocuments.in/reader030/viewer/2022032516/56649c7b5503460f9492ea0b/html5/thumbnails/21.jpg)
21
Déclarations PL/SQLCréation de types (limitée)
type txt_tab_type is table of varchar2(80) index by binary_integer;
txt_lines txt_tab_type; err_msg txt_tab_type;
type emp_rec_type is record (nom varchar2(30), datenaiss date, num number(8));
emp_rec emp_rec_type;
![Page 22: 1 LDD et PL/SQL Patrice Saintenoy Université Paris X - Nanterre UFR SEGMI Licence MIAGE.](https://reader030.fdocuments.in/reader030/viewer/2022032516/56649c7b5503460f9492ea0b/html5/thumbnails/22.jpg)
22
Instructions PL/SQLAffectation
Entier := 10;
sousChaine := substr(v5,1,10);
condition := salaire > 2000;
datenaiss := to_date('19-05-47', 'DD-MM-YYYY');
![Page 23: 1 LDD et PL/SQL Patrice Saintenoy Université Paris X - Nanterre UFR SEGMI Licence MIAGE.](https://reader030.fdocuments.in/reader030/viewer/2022032516/56649c7b5503460f9492ea0b/html5/thumbnails/23.jpg)
23
Instructions PL/SQLwhile salaire < 2000 loop ... end loop;
for i in reverse 1 .. 10 loop ... end loop;
if nom is null or numemp=1000
then fetch c1 into emp_rec;
else close c1;
end if;
![Page 24: 1 LDD et PL/SQL Patrice Saintenoy Université Paris X - Nanterre UFR SEGMI Licence MIAGE.](https://reader030.fdocuments.in/reader030/viewer/2022032516/56649c7b5503460f9492ea0b/html5/thumbnails/24.jpg)
24
Exceptions en PL/SQL• Nombreuses exceptions prédéfinies• Possibilité d'en définir de nouvelles
declare trop exception;begin ... raise trop; ...exception when trop then ...
![Page 25: 1 LDD et PL/SQL Patrice Saintenoy Université Paris X - Nanterre UFR SEGMI Licence MIAGE.](https://reader030.fdocuments.in/reader030/viewer/2022032516/56649c7b5503460f9492ea0b/html5/thumbnails/25.jpg)
25
Exceptions pré-définies• Program_error
• Storage_error
• No_data_founds
• Logon_denied
• Not_logged_on
• Too_many_rows
• Time_out_on_ressource
![Page 26: 1 LDD et PL/SQL Patrice Saintenoy Université Paris X - Nanterre UFR SEGMI Licence MIAGE.](https://reader030.fdocuments.in/reader030/viewer/2022032516/56649c7b5503460f9492ea0b/html5/thumbnails/26.jpg)
26
CurseurOutil destiné à conserver la mémoire de
l'exploration d'une table.
for(i in 1..10) loop select nom, prenom from employe;end loop;
Cette boucle retourne 10 fois les mêmes valeurs!
![Page 27: 1 LDD et PL/SQL Patrice Saintenoy Université Paris X - Nanterre UFR SEGMI Licence MIAGE.](https://reader030.fdocuments.in/reader030/viewer/2022032516/56649c7b5503460f9492ea0b/html5/thumbnails/27.jpg)
27
Curseur (utilisation)Cursor c1 is select * from emp order by nom;c1_rec c1%rowtype;begin open c1; loop fetch c1 into c1_rec; exit when c1%notfound end loop; nb := c1%rowcount; close c1;end;
![Page 28: 1 LDD et PL/SQL Patrice Saintenoy Université Paris X - Nanterre UFR SEGMI Licence MIAGE.](https://reader030.fdocuments.in/reader030/viewer/2022032516/56649c7b5503460f9492ea0b/html5/thumbnails/28.jpg)
28
Boucle « Pour_curseur »Cursor cur_for is select * from emp order by nom;
for c1_rec in cur_for loop
fetch cur_for into c1_rec;
total := cur_for%rowcount;
...
end loop;
![Page 29: 1 LDD et PL/SQL Patrice Saintenoy Université Paris X - Nanterre UFR SEGMI Licence MIAGE.](https://reader030.fdocuments.in/reader030/viewer/2022032516/56649c7b5503460f9492ea0b/html5/thumbnails/29.jpg)
29
Procédures et fonctions PL/SQL
create procedure p(...) is …
create or replace procedure p(...) is …
create or replace function f(...) return <type> is …
![Page 30: 1 LDD et PL/SQL Patrice Saintenoy Université Paris X - Nanterre UFR SEGMI Licence MIAGE.](https://reader030.fdocuments.in/reader030/viewer/2022032516/56649c7b5503460f9492ea0b/html5/thumbnails/30.jpg)
30
Procédures et fonctions PL/SQL
create or replace function f(num number,
txt varchar2(10)) return number is
total number;
begin
select count(*) into total from emp where
emp.num=num;
if total>20 then return total else return 0; end if;
end;
![Page 31: 1 LDD et PL/SQL Patrice Saintenoy Université Paris X - Nanterre UFR SEGMI Licence MIAGE.](https://reader030.fdocuments.in/reader030/viewer/2022032516/56649c7b5503460f9492ea0b/html5/thumbnails/31.jpg)
31
Paquetages• Spécificationcreate or replace package test as function fgetval(num number) return number;end test;
• Corps (implémentation)create or replace package body test as function fgetval(num number) return number is .... (code pl/sql) end; end test;
![Page 32: 1 LDD et PL/SQL Patrice Saintenoy Université Paris X - Nanterre UFR SEGMI Licence MIAGE.](https://reader030.fdocuments.in/reader030/viewer/2022032516/56649c7b5503460f9492ea0b/html5/thumbnails/32.jpg)
32
Utilisation d’un paquetageUtiliser le nom complet : [proprietaire].[paquetage].procedure[(parametres)]
Exemple :
create synonym ouv for tp.bibli.lister;
ouv('Moliere','seuil');
![Page 33: 1 LDD et PL/SQL Patrice Saintenoy Université Paris X - Nanterre UFR SEGMI Licence MIAGE.](https://reader030.fdocuments.in/reader030/viewer/2022032516/56649c7b5503460f9492ea0b/html5/thumbnails/33.jpg)
33
Triggers
Automatisation d'un traitement sur la prise en compte d'un évènement :
![Page 34: 1 LDD et PL/SQL Patrice Saintenoy Université Paris X - Nanterre UFR SEGMI Licence MIAGE.](https://reader030.fdocuments.in/reader030/viewer/2022032516/56649c7b5503460f9492ea0b/html5/thumbnails/34.jpg)
34
Create or replace trigger commande
after insert or update on commande_usine for each row
begin
if inserting then
insert into commande@magasin values(
:new.numcom, :new.numclient, :new.numarticle,
:new.montant);
elsif updating then
update commande@magasin set
numcom :=new numcom, numcli:=new.numcli, numarticle=new.numarticle, montant=new.montant
where numcom=:old.numcom;
end if;
end;