1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
|
drop table inscription
/
drop table voyage
/
drop table client
/
CREATE TABLE CLIENT(
IDC INTEGER PRIMARY KEY ,
NOM VARCHAR2 (40));
CREATE TABLE VOYAGE(
IDV INTEGER PRIMARY KEY ,
DESTINATION VARCHAR2 (40),
MAXPLACE INTEGER , -- nombre total de places
PLACEDISPO INTEGER ) -- nombre de places disponibles
;
CREATE TABLE INSCRIPTION(
IDC INTEGER REFERENCES CLIENT(IDC),
IDV INTEGER REFERENCES VOYAGE(IDV),
DATERESERV DATE ,
CONSTRAINT INSCRIPTION_PK PRIMARY KEY (IDC, IDV));
INSERT INTO CLIENT(IDC, NOM) VALUES (1, 'DURAND');
INSERT INTO CLIENT(IDC, NOM) VALUES (2, 'DUBOIS');
INSERT INTO CLIENT(IDC, NOM) VALUES (3, 'DUGENOU');
COMMIT ;
INSERT INTO VOYAGE(IDV, DESTINATION, MAXPLACE, PLACEDISPO) VALUES (10, 'VENISE', 25, 1);
INSERT INTO VOYAGE(IDV, DESTINATION, MAXPLACE, PLACEDISPO) VALUES (11, 'PRAGUE', 20, 0);
COMMIT ;
CREATE OR REPLACE FUNCTION nb_places(p_IDV NUMBER) RETURN NUMBER IS
PRAGMA AUTONOMOUS_TRANSACTION;
NB_DISPO INTEGER ;
BEGIN
SELECT PLACEDISPO INTO NB_DISPO FROM VOYAGE
WHERE IDV=p_IDV;
RETURN NB_DISPO;
END;
/
CREATE OR REPLACE TRIGGER TRIG_INSCRIPTION BEFORE INSERT ON INSCRIPTION
FOR EACH ROW
DECLARE
NB_DISPO INTEGER ;
BEGIN
NB_DISPO := nb_places(:NEW.IDV);
IF NB_DISPO < 1 THEN
Raise_Application_Error(-20000,'Désolé, voyage complet');
ELSE
UPDATE VOYAGE SET PLACEDISPO=PLACEDISPO - 1
WHERE IDV=:NEW.IDV;
END IF ;
END ;
/
-- DUGENOU aimerait bien aller à Venise :
INSERT INTO INSCRIPTION(IDC, IDV, DATERESERV) SELECT 3, 10, TO_DATE(SYSDATE, 'DD/MM/YYYY') FROM DUAL ;
-- DUBOIS aimerait bien aller à Venise :
INSERT INTO INSCRIPTION(IDC, IDV, DATERESERV) SELECT 2, 10, TO_DATE(SYSDATE, 'DD/MM/YYYY') FROM DUAL ;
SQL> select * from voyage;
IDV DESTINATION MAXPLACE PLACEDISPO
---------- ---------------------------------------- ---------- ----------
10 VENISE 25 -1
11 PRAGUE 20 0
SQL> |
Partager