Cours SQL (Oracle)
Cours SQL (Oracle)
Cours SQL (Oracle)
(ORACLE)
BDR.
Table
Vue
Squence
Index
Synonyme
1/21/15
Mr MOHAMED AIT HAMMOU
Table DUAL
Table DUAL
C'est une particularit d'Oracle. Cette pseudo-table ne contient qu'une
seule ligne et une seule colonne et ne peut tre utilise qu'avec une requte
select.
Elle permet de faire afficher une expression dont la valeur ne dpend
d'aucune table en particulier.
Exemple
Afficher la date d'aujourd'hui, le nom de l'utilisateur et le rsultat d'un
calcul, revient crire.
select sysdate, user, round(3676 / 7) from dual;
1/21/15
Mr MOHAMED AIT HAMMOU
Syntaxe :
Cration de Tables
Types de donnes
Description
VARCHAR2(taille)
Donnes caractres de longueur variable
(2000 caractres maximum)
CHAR(taille)
caractres maximum)
LONG
LONG RAW
Binaire(image)
1/21/15
Mr MOHAMED AIT HAMMOU
Cration de Tables
CREATE
CREATE TABLE
TABLE dept
dept
(( deptno
deptno NUMBER(2),
NUMBER(2),
dname
dname VARCHAR2(15),
VARCHAR2(15),
loc
loc VARCHAR2(12));
VARCHAR2(12));
CREATE
CREATE
AS
AS
TABLE
TABLE Empclerk
Empclerk
SELECT
SELECT empno
empno mat,
mat, ename,
ename, nom,
nom, sal
sal ++ comm
comm as
as salaire,
salaire,
hiredate,
hiredate, deptno
deptno
FROM
FROM emp
emp
WHERE
WHERE job
job like
like 'clerk');
'clerk');
1/21/15
Mr MOHAMED AIT HAMMOU
1/21/15
Mr MOHAMED AIT HAMMOU
NOT NULL
UNIQUE
PRIMARY KEY
FOREIGN KEY
CHECK
1/21/15
Mr MOHAMED AIT HAMMOU
[[CONSTRAINT
[[CONSTRAINT NomContrainte]
NomContrainte] TypeContrainte],
TypeContrainte],
,,
[[CONSTRAINT
[[CONSTRAINT NomContrainte]
NomContrainte] TypeContrainte(Champs,
TypeContrainte(Champs, ...),]
...),]
);
);
ALTER
NomTable
ALTER TABLE
TABLE
NomTable
ADD
ADD [CONSTRAINT
[CONSTRAINT NomContrainte]
NomContrainte] TypeContrainte(Champs);
TypeContrainte(Champs);
1/21/15
Mr MOHAMED AIT HAMMOU
CREATE
CREATE TABLE
TABLE Employes(
Employes(
empno
empno NUMBER(4)
NUMBER(4) PRIMARY
PRIMARY KEY,
KEY, ename
ename VARCHAR2(15)
VARCHAR2(15) NOT
NOT NULL,
NULL,
job
job VARCHAR2(10),
VARCHAR2(10), mgr
mgr NUMBER(4),
NUMBER(4), hiredate
hiredate DATE,
DATE, sal
sal NUMBER(8,2),
NUMBER(8,2),
comm
comm NUMBER(8,2),
NUMBER(8,2), deptno
deptno NUMBER(2)
NUMBER(2) NOT
NOT NULL
NULL ,,
CONSTRAINT
CONSTRAINT un_dept
un_dept UNIQUE
UNIQUE (ename)
(ename) ););
ALTER
ALTER TABLE
TABLE Employes
Employes
ADD
ADD CONSTRAINT
CONSTRAINT Emp_Dept_Fk
Emp_Dept_Fk FOREIGN
FOREIGN KEY
KEY (deptno)
(deptno)
REFERENCES
REFERENCES dept
dept (deptno)
(deptno) ON
ON DELETE
DELETE CASCADE;
CASCADE;
ALTER
ALTER TABLE
TABLE Employes
Employes
ADD
ADD CONSTRAINT
CONSTRAINT CONSTRAINT
CONSTRAINT CHECK
CHECK (Comm
(Comm >> 0);
0);
1/21/15
Mr MOHAMED AIT HAMMOU
1/21/15
Mr MOHAMED AIT HAMMOU
Notion
de vue
(Limitation d'accs )
1/21/15
Mr MOHAMED AIT HAMMOU
Syntaxe
CREATE
CREATE VIEW
VIEW NomVue
NomVue [(alias[,
[(alias[, alias]...)]
alias]...)]
AS
AS SousInterogation
SousInterogation
[WITH
[WITH CHECK
CHECK OPTION]
OPTION]
[WITH
[WITH READ
READ ONLY]
ONLY]
1/21/15
Mr MOHAMED AIT HAMMOU
CREATE
CREATE VIEW
VIEW manager
manager
(matricule,
(matricule, nom,
nom, fonction,
fonction, dateembouche,
dateembouche, salaire,
salaire, Departement
Departement ))
AS
AS SELECT
SELECT empno,
empno, ename,
ename, job,hiredate,
job,hiredate, sal
sal ++ nvl(comm,0),
nvl(comm,0), dname
dname
FROM
emp,
FROM
emp, dept
dept
WHERE
WHERE ((emp.deptno
((emp.deptno == dept.deptno)
dept.deptno) and
and
empno
empno in
in (select
(select mgr
mgr from
from emp
emp where
where mgr
mgr is
is not
not NULL))
NULL))
WITH
WITH READ
READ ONLY
ONLY
Select
Select **
from
from manager
manager ;;
1/21/15
Mr MOHAMED AIT HAMMOU
NomVue;
NomVue;
1/21/15
Mr MOHAMED AIT HAMMOU
Index
INDEX
CREATE INDEX
Un index se cre par la commande CREATE INDEX :
CREATE [UNIQUE] INDEX [nom-schma.]nom-index ON <nom-table> ( { nomcolonne [ASC | DESC ] } )
On peut spcifier par l'option UNIQUE que chaque valeur d'index doit tre
unique dans la table.
On peut spcifier par l'option ASC et DESC un tri en ordre croissant ou
dcroissant (respectivement).
Exemple : index sur les numros de pice, sans double, en ordre croissant
CREATE UNIQUE INDEX emp_idx ON EMP (EMPNO ASC) ;
1/21/15
Mr MOHAMED AIT HAMMOU
DROP INDEX
1/21/15
Mr MOHAMED AIT HAMMOU
SEQUENCE
Une squence est un compteur programmable stock en mmoire par Oracle
et utilisable de manire partag.
Il est, en gnral, utilis pour fournir les n des cls d'enregistrements.
CREATE SEQUENCE nom_squence [INCREMENT BY entier1]
[START WITH entier2]
Exemple
create sequence seqdept
increment by 10
start with 10
1/21/15
Mr MOHAMED AIT HAMMOU
Utilisation SEQUENCE
Exemple:
insert into dept(deptno, dname,loc)
values (seqdept.nextval, 'Finances', 'casablanca')
Pour voir la valeur d'une squence, on utilise currval avec la table dual
select seqdept.currval from dual.
1/21/15
Mr MOHAMED AIT HAMMOU
Lordre Select
Lutilisation la plus frquente de SQL seffectue dans les
requtes afin de rechercher les donnes dans une base de
donnes.
Syntaxe
select [distinct] * | <liste des champs >
From <Liste des tables>
[where <critre de slection >]
[group by <critres de regroupement>]
[having <conditions de filtrage sur les groupes>]
[order by <liste des champs > asc | desc ];
1/21/15
Mr MOHAMED AIT HAMMOU
Les
lignes.
1/21/15
Mr MOHAMED AIT HAMMOU
Exemple de SELECT
SELECT *
FROM
dept;
DEPTNO
--------10
20
30
40
DNAME
-------------ACCOUNTING
RESEARCH
SALES
OPERATIONS
LOC
------------NEW YORK
DALLAS
CHICAGO
BOSTON
LOC
------------NEW YORK
DALLAS
CHICAGO
BOSTON
1/21/15
Mr MOHAMED AIT HAMMOU
Exemple de SELECT
SELECT *
FROM
dept;
DEPTNO
--------10
20
30
40
SELECT
SELECT
FROM
FROM
DNAME
-------------ACCOUNTING
RESEARCH
SALES
OPERATIONS
deptno
deptno
emp;
emp;
DEPTNO
--------10
30
10
20
...
14 rows selected.
LOC
------------NEW YORK
DALLAS
CHICAGO
BOSTON
1/21/15
Mr MOHAMED AIT HAMMOU
LALIAS DE COLONNE
Doit
LE
LITTRAL
Un
JOB
DEPTNO
--------- --------CLERK
30
CLERK
20
CLERK
20
CLERK
10
Mr MOHAMED AIT HAMMOU
LES OPRATEURS
UTILISATION DES
OPRATEURS
SELECT
FROM
WHERE
ename, sal
emp
sal BETWEEN 1000 AND 1500;
ENAME
SAL
---------- --------MARTIN
1250
TURNER
1500
WARD
1250
ADAMS
1100
MILLER
1300
SELECT
FROM
WHERE
Limite
Limite
infrieure
suprieure
EMPNO
--------7902
7369
7788
7876
ENAME
SAL
MGR
---------- --------- --------FORD
3000
7566
SMITH
800
7902
SCOTT
3000
7566
ADAMS
1100
7788
Mr MOHAMED AIT HAMMOU
ename
emp
ename LIKE 'S%';
SELECT
FROM
WHERE
ename
emp
ename LIKE '_A%';
ENAME
---------JAMES
WARD
WHERE
WHERE
WHERE
WHERE
...
...
...
...
...
JOB
--------PRESIDENT
SALESMAN
SALESMAN
SALESMAN
SALESMAN
Mr MOHAMED AIT HAMMOU
emp;
Employee
Employee Details
Details
------------------------------------------------KING
KING is
is aa PRESIDENT
PRESIDENT
BLAKE
BLAKE is
is aa MANAGER
MANAGER
CLARK
CLARK is
is aa MANAGER
MANAGER
JONES
JONES is
is aa MANAGER
MANAGER
MARTIN
MARTIN is
is aa SALESMAN
SALESMAN
...
...
14
14 rows
rows selected.
selected.
Non-quijointure
Non-quijointure
Jointure
Jointure externe
externe
Autojointure
Autojointure
EXEMPLE
DQUIJOINTURE
SELECT
FROM
WHERE
EXEMPLE DE NON-QUIJOINTURE
SELECT
e.ename, e.sal, s.grade
FROM emp e, salgrade s
WHERE e.sal
BETWEEN
s.losal AND s.hisal;
ENAME
SAL
GRADE
---------- --------- --------JAMES
950
1
SMITH
800
1
ADAMS
1100
1
...
14 rows selected.
ENAME
DEPTNO DNAME
---------- --------- ------------KING
10 ACCOUNTING
CLARK
10 ACCOUNTING
...
40 OPERATIONS
15 rows selected.
EXEMPLE DE LAUTOJOINTURES
SELECT worker.ename||' works for '||manager.ename
FROM emp worker, emp manager
WHERE worker.mgr = manager.empno;
WORKER.ENAME||'WORKSFOR'||MANAG
WORKER.ENAME||'WORKSFOR'||MANAG
------------------------------------------------------------BLAKE
BLAKE works
works for
for KING
KING
CLARK
CLARK works
works for
for KING
KING
JONES
JONES works
works for
for KING
KING
MARTIN
MARTIN works
works for
for BLAKE
BLAKE
...
...
13
13 rows
rows selected.
selected.
AVG ([DISTINCT|ALL]n)
COUNT ({ *|[DISTINCT|ALL]expr})
MAX ([DISTINCT|ALL]expr)
MIN ([DISTINCT|ALL]expr)
SUM ([DISTINCT|ALL]n)
COUNT(*)
deptno = 30;
COUNT(*)
--------6
Mr MOHAMED AIT HAMMOU
SELECT AVG(NVL(comm,0))
FROM
emp;
AVG(NVL(COMM,0))
---------------157.14286
2916.6667
DEPTNO AVG(SAL)
"salaire
moyen pour ------- --------2175
chaque
10 2916.6667
dpartement
20
2175
de la table
30 1566.6667
EMP"
1566.6667
SELECT
deptno, AVG(sal)
FROM
emp
GROUP BY deptno;
Mr MOHAMED AIT HAMMOU
deptno, max(sal)
emp
deptno
max(sal)>2900;
DEPTNO MAX(SAL)
--------- --------10
5000
20
3000
JOB
PAYROLL
--------- --------ANALYST
6000
MANAGER
8275
INTERROGATIONS
Syntaxe
SELECT select_list
FROM tables
WHERE expr operator (SELECT select_list FROM
tables )
;
TYPES DE SOUS-INTERROGATIONS
Sous-interrogation
Sous-interrogation
mono-ligne
mono-ligne
Oprateurs mono-ligne
Sous-interrogation
Sous-interrogation
multi-ligne
multi-ligne
Oprateurs multi-ligne
AND
ename, job
emp
job =
(SELECT
FROM
WHERE
sal >
(SELECT
FROM
WHERE
ENAME
ENAME
------------------MILLER
MILLER
CLERK
job
emp
ename = 'ADAMS')
1100
sal
emp
ename = 'ADAMS');
JOB
JOB
----------------CLERK
CLERK
ENAME
ENAME
------------------SMITH
SMITH
SELECT
FROM
GROUP BY
HAVING
800
JOB
SAL
JOB
SAL
----------------- ----------------CLERK
800
CLERK
800
deptno, MIN(sal)
emp
deptno
800
MIN(sal) >
(SELECT MIN(sal)
FROM
emp
WHERE
deptno = 20);
Mr MOHAMED AIT HAMMOU
AND
1300
empno, ename, job
1100
emp
800
950
sal < ANY
(SELECT sal
FROM
emp
WHERE job = 'CLERK)
job <> ''CLERK';
EMPNO
EMPNO
----------------7654
7654
7521
7521
ENAME
ENAME
------------------MARTIN
MARTIN
WARD
WARD
JOB
JOB
----------------SALESMAN
SALESMAN
SALESMAN
SALESMAN
EMPNO
EMPNO
----------------7839
7839
7566
7566
7902
7902
7788
7788
ENAME
ENAME
------------------KING
KING
JONES
JONES
FORD
FORD
SCOTT
SCOTT
JOB
JOB
----------------PRESIDENT
PRESIDENT
MANAGER
MANAGER
ANALYST
ANALYST
ANALYST
ANALYST
EXEMPLE DE SOUS-INTERROGATIONS
SYNCHRONISES
Recherchez tous les employs dont le salaire est
suprieur au salaire moyen de leur dpartement
SELECT empno, sal, deptno
Chaque fois que la requte
externe est traite,
FROM
emp outer
la requte interne
WHERE sal > (SELECT AVG(sal)
est excute.
FROM emp inner
WHERE outer.deptno= inner.deptno);
EMPNO
SAL
DEPTNO
EMPNO
SAL
DEPTNO
--------------- ----------------- ----------------7839
5000
10
7839
5000
10
7698
2850
30
7698
2850
30
7566
2975
20
7566
2975
20
...
...
66 rows
rows selected.
selected.
Oprateur mono.ligne
ou muti-ligne
JOB
DEPTNO
--------- --------PRESIDENT
10
MANAGER
30
MANAGER
10
MANAGER
20
Intersect
Union
Minus
Table Emp
Table EMPHISTORY
NAME
EMPID
NAME
EMPID
--------------------------- --------BALFORD
6235
BALFORD
6235
BRIGGS
7225
BRIGGS
7225
JEWELL
7001
JEWELL
7001
SPENCER
6087
SPENCER
6087
...
...
66 rows
rows selected.
selected.
TITLE
TITLE
----------------CLERK
CLERK
PAY
PAY CLERK
CLERK
ANALYST
ANALYST
OPERATOR
OPERATOR
Indiquez
Placez
simples quotes.
Mr MOHAMED AIT HAMMOU
INSERT
INSERT INTO
INTO emp
emp (empno,
(empno, ename,
ename, job,
job,
mgr,
mgr, hiredate,
hiredate, sal,
sal, deptno)
deptno)
VALUES
VALUES
(8000,
(8000, SAAD',
SAAD', 'ANALYST',
'ANALYST',
7839,
7839, SYSDATE,
SYSDATE, 4000,
4000, 20);
20);
Nomtable
Nomtable
Champs
Champs == valeur
valeur [,
[, champs
champs == valeur]
valeur]
condition];
condition];
La
SET
com
SET
com =
=00
WHERE
WHERE com
com is
is NULL;
NULL;
UPDATE
UPDATE emp
emp
SET
SET mgr
mgr == (SELECT
(SELECT empno
empno
FROM
FROM emp
emp
WHERE
WHERE (deptno
(deptno == 30)
30) AND
AND (job
(job like
like 'MANAGER')
'MANAGER') )) ,,
job
job ='SALSESMEN'
='SALSESMEN'
WHERE
WHERE deptno
deptno == 30;
30;
des
Syntaxe :
DELETE
DELETE [FROM]
[FROM] Nomtable
Nomtable
[WHERE
[WHERE condition];
condition];
La
dept
dept
dname
dname == 'FINANCES';
'FINANCES';
DELETE
DELETE FROM
FROM emp
emp
WHERE
deptno
WHERE
deptno ==
(SELECT
(SELECT deptno
deptno
FROM
FROM dept
dept
WHERE
WHERE dname
dname ='SALES');
='SALES');