Database Reference
In-Depth Information
Schéma relationnel ( suite )
Script SQL ( suite )
ALTER TABLE effectue
ADD CONSTRAINT fk_effectu_forma
FOREIGN KEY (id_form)
REFERENCES formation (id_form);
ALTER TABLE effectue
ADD CONSTRAINT fk_effectue_
stagiair
FOREIGN KEY (id_stag)
REFERENCES stagiaire (id_stag);
ALTER TABLE voeux
ADD CONSTRAINT fk_voeux_form
FOREIGN
KEY (id_form)
REFERENCES formation (id_form);
ALTER TABLE voeux
ADD CONSTRAINT fk_voeux_stagiair
FOREIGN KEY (id_stag)
REFERENCES stagiaire (id_stag);
Le codage de cette contrainte se résume à la déclaration d'une nouvelle clé étrangère (com-
posée de deux colonnes) qui aura pour but de vériier si tout couple ( stagiaire , formation )
décrivant une inscription existe au préalable dans la table de référence des vœux.
Tableau 3-16 : Codage d'une contrainte d'inclusion
Contrainte d'inclusion
Jeu d'essai
INSERT INTO formation VALUES (1,'SQL',4,1750);
INSERT INTO formation VALUES (2,'C++',3,1050);
INSERT INTO formation VALUES (3,'Cobol',5,1900);
INSERT INTO stagiaire VALUES
(100,'Fred','Brouard','sqlspot@gmail.com');
INSERT INTO stagiaire VALUES
(101,'Rudi','Bruchez','rudi@gmail.com');
INSERT INTO voeux VALUES (100,2);
INSERT INTO voeux VALUES (101,1);
INSERT INTO voeux VALUES (101,2);
ALTER TABLE effectue
ADD CONSTRAINT fk_inclusion
FOREIGN KEY ( id_stag,id_form )
REFERENCES voeux ( id_stag,id_form );
SQL> INSERT INTO effectue VALUES (100,2);
1 row created.
SQL> INSERT INTO effectue VALUES (100,1);
*
ERROR at line 1:
ORA-02291: integrity constraint (SOUTOU.FK_
INCLUSION) violated - parent key not found
 
Search WWH ::




Custom Search