0% found this document useful (0 votes)
2 views

DB_GESTIONSTG

The document outlines the creation of several database tables related to internships, including 'stagiare' for interns, 'stage' for internship details, 'notation' for evaluations, and 'lignenotation' for evaluation criteria. It also includes SQL commands for inserting data into these tables, defining functions and procedures for calculating final grades, identifying the best intern, and managing intern records. Additionally, it features triggers to enforce data integrity rules for inserting records into the 'notation' and 'stagiare' tables.

Uploaded by

cosmeticskiza
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

DB_GESTIONSTG

The document outlines the creation of several database tables related to internships, including 'stagiare' for interns, 'stage' for internship details, 'notation' for evaluations, and 'lignenotation' for evaluation criteria. It also includes SQL commands for inserting data into these tables, defining functions and procedures for calculating final grades, identifying the best intern, and managing intern records. Additionally, it features triggers to enforce data integrity rules for inserting records into the 'notation' and 'stagiare' tables.

Uploaded by

cosmeticskiza
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 5

creattion des tables --------

--stagiare table
CREATE TABLE stagiare (
cne INT PRIMARY KEY,
nom VARCHAR(255),
prenom VARCHAR(255),
datenaissance DATE,
ville VARCHAR(255),
email VARCHAR(255)
);

--stage table
CREATE TABLE stage (
id INT PRIMARY KEY,
datedebut DATE,
datefin DATE,
societe VARCHAR(255)
);

--notation table
CREATE TABLE notation (
id INT PRIMARY KEY,
idstage INT,
cne INT,
note INT,
fait VARCHAR(255),
datenotation DATE,
FOREIGN KEY (idstage) REFERENCES stage(id),
FOREIGN KEY (cne) REFERENCES stagiare(cne)
);

--lignenotation table
CREATE TABLE lignenotation (
idligne INT PRIMARY KEY,
idnotation INT,
critiere VARCHAR(255),
note INT,
FOREIGN KEY (idnotation) REFERENCES notation(id)
);

--insertion stagiare table


INSERT INTO stagiare (cne, nom, prenom, datenaissance, ville, email)
VALUES
(1, 'Smith', 'John', '1990-05-15', 'New York', '[email protected]'),
(2, 'Johnson', 'Lisa', '1992-08-23', 'Los Angeles', '[email protected]'),
(3, 'Davis', 'Michael', '1988-03-10', 'Chicago', '[email protected]');

--insertions for stage table


INSERT INTO stage (id, datedebut, datefin, societe)
VALUES
(1, '2023-01-15', '2023-04-30', 'ABC Corporation'),
(2, '2023-02-20', '2023-05-15', 'XYZ Inc.'),
(3, '2023-03-10', '2023-06-25', '123 Industries');

--insertions for notation table


INSERT INTO notation (id, idstage, cne, note, fait, datenotation)
VALUES
(1, 1, 1, 90, 'Completed', '2023-04-30'),
(2, 2, 2, 85, 'In Progress', '2023-05-05'),
(3, 3, 3, 78, 'Completed', '2023-06-25');

--insertions for lignenotation table


INSERT INTO lignenotation (idligne, idnotation, critiere, note)
VALUES
(1, 1, 'Performance', 92),
(2, 1, 'Punctuality', 88),
(3, 2, 'Performance', 87),
(4, 2, 'Communication', 80),
(5, 3, 'Quality of Work', 76),
(6, 3, 'Attendance', 82);
1)
DELIMITER //
CREATE FUNCTION CalculerNoteFinale(cne_param INT) RETURNS FLOAT
BEGIN
DECLARE total_notes FLOAT;
DECLARE total_lignes INT;
SELECT SUM(note) INTO total_notes
FROM notation
WHERE cne = cne_param;
SELECT COUNT(*) INTO total_lignes
FROM notation
WHERE cne = cne_param;
IF total_lignes = 0 THEN
RETURN NULL;
ELSE
RETURN total_notes / total_lignes;
END IF;
END //
DELIMITER ;
SELECT cne, CalculerNoteFinale(cne) AS NoteFinale
FROM stagiare;
2)
DELIMITER //

CREATE FUNCTION MeilleurStagiairePourPeriode(datedebut_param DATE, datefin_param


DATE)
RETURNS VARCHAR(255)
BEGIN
DECLARE meilleur_stagiaire VARCHAR(255);
SELECT s.nom
INTO meilleur_stagiaire
FROM stagiare s
JOIN notation n ON s.cne = n.cne
WHERE n.datenotation BETWEEN datedebut_param AND datefin_param
ORDER BY n.note DESC
LIMIT 1;

RETURN meilleur_stagiaire;
END //

DELIMITER ;
SELECT MeilleurStagiairePourPeriode('2023-01-01', '2023-12-31') AS
MeilleurStagiaire;
3)
DELIMITER //

CREATE FUNCTION NombreStagiairesParSociete(societe_param VARCHAR(255))


RETURNS INT
BEGIN
DECLARE nombre_stagiaires INT;
SELECT COUNT(*)
INTO nombre_stagiaires
FROM stagiare s
JOIN stage st ON s.cne = st.cne
WHERE st.societe = societe_param;

RETURN nombre_stagiaires;
END //

DELIMITER ;
SELECT NombreStagiairesParSociete('ABC Corporation') AS NombreStagiaires;
4)
DELIMITER //

CREATE PROCEDURE AfficherInfosStagiairesPourPeriode(datedebut_param DATE,


datefin_param DATE)
BEGIN
SELECT
s.cne AS CIN,
CONCAT(s.nom, ' ', s.prenom) AS 'Nom Complet',
YEAR(NOW()) - YEAR(s.datenaissance) AS Age,
st.societe AS Société,
n.note AS Note
FROM
stagiare s
JOIN
stage st ON s.cne = st.cne
JOIN
notation n ON s.cne = n.cne
WHERE
n.datenotation BETWEEN datedebut_param AND datefin_param;
END //

DELIMITER ;

5)
DELIMITER //

CREATE PROCEDURE AfficherInfosStagiairesPourPeriode(datedebut_param DATE,


datefin_param DATE)
BEGIN
SELECT
s.cne AS CIN,
CONCAT(s.nom, ' ', s.prenom) AS 'Nom Complet',
YEAR(CURDATE()) - YEAR(s.datenaissance) AS Age,
st.societe AS Société,
n.note AS Note
FROM
stagiare s
JOIN
stage st ON s.cne = st.cne
JOIN
notation n ON s.cne = n.cne
WHERE
n.datenotation BETWEEN datedebut_param AND datefin_param;
END //
DELIMITER ;
6)
DELIMITER //

CREATE PROCEDURE SupprimerStagiairesSansStage()


BEGIN
DELETE s
FROM stagiare s
LEFT JOIN stage st ON s.cne = st.cne
WHERE st.id IS NULL;
END //

DELIMITER ;
CALL SupprimerStagiairesSansStage();
7)
8)
DELIMITER //

CREATE TRIGGER EmpecherInsertionNotation


BEFORE INSERT ON notation
FOR EACH ROW
BEGIN
IF NEW.note IS NULL OR NEW.fait IS NULL OR NEW.datenotation < (
SELECT datedebut
FROM stage
WHERE id = NEW.idstage
) THEN
SIGNAL SQLSTATE '45000'
SET MESSAGE_TEXT = 'L''insertion de notation est invalide.';
END IF;
END //

DELIMITER ;

9)
DELIMITER //

CREATE TRIGGER EmpecherInsertionStagiaire


BEFORE INSERT ON stagiare
FOR EACH ROW
BEGIN
DECLARE is_duplicate INT;

-- Vérifier si le CNE existe déjà


SELECT COUNT(*) INTO is_duplicate
FROM stagiare
WHERE cne = NEW.cne;

-- Vérifier l'âge
IF YEAR(CURDATE()) - YEAR(NEW.datenaissance) < 18 THEN
SIGNAL SQLSTATE '45000'
SET MESSAGE_TEXT = 'L''âge du stagiaire doit être d'au moins 18 ans.';
END IF;

-- Vérifier le format de l'adresse e-mail


IF NEW.email IS NOT NULL AND NEW.email NOT REGEXP '^[A-Za-z0-9._%+-]+@[A-Za-z0-
9.-]+\\.[A-Za-z]{2,4}$' THEN
SIGNAL SQLSTATE '45000'
SET MESSAGE_TEXT = 'L''adresse e-mail n'a pas un format valide.';
END IF;

-- Vérifier si le CNE existe déjà


IF is_duplicate > 0 THEN
SIGNAL SQLSTATE '45000'
SET MESSAGE_TEXT = 'Le CNE du stagiaire existe déjà.';
END IF;
END //

DELIMITER ;

You might also like