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

Controle db2

This document contains SQL statements that create a student database with tables for students, classes, enrollments, subjects, exams, and grades. It defines the structure of the tables, relationships between them, inserts sample data, runs queries, and creates and grants privileges to database users.

Uploaded by

meddservice1
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)
13 views

Controle db2

This document contains SQL statements that create a student database with tables for students, classes, enrollments, subjects, exams, and grades. It defines the structure of the tables, relationships between them, inserts sample data, runs queries, and creates and grants privileges to database users.

Uploaded by

meddservice1
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/ 2

1>

CREATE DATABASE BaseDeDonnees;

USE BaseDeDonnees;

CREATE TABLE Eleve (


Num_eleve INT PRIMARY KEY AUTO_INCREMENT,
Nom_eleve VARCHAR(50),
Prenom_eleve VARCHAR(50)
);

CREATE TABLE Classe (


Num_classe INT PRIMARY KEY AUTO_INCREMENT,
Nom_classe VARCHAR(50)
);

CREATE TABLE Inscrit (


Num_inscription INT PRIMARY KEY AUTO_INCREMENT,
Num_eleve INT,
Num_classe INT,
Annee_scolaire VARCHAR(10),
FOREIGN KEY (Num_eleve) REFERENCES Eleve(Num_eleve),
FOREIGN KEY (Num_classe) REFERENCES Classe(Num_classe)
);

CREATE TABLE Matiere (


Num_matiere INT PRIMARY KEY AUTO_INCREMENT,
Nom_matiere VARCHAR(50)
);

CREATE TABLE Examen (


Num_examen INT PRIMARY KEY AUTO_INCREMENT,
date_examen DATE,
Num_matiere INT,
FOREIGN KEY (Num_matiere) REFERENCES Matiere(Num_matiere)
);

CREATE TABLE Note (


Num_inscription INT,
Num_examen INT,
note INT,
PRIMARY KEY (Num_inscription, Num_examen),
FOREIGN KEY (Num_inscription) REFERENCES Inscrit(Num_inscription),
FOREIGN KEY (Num_examen) REFERENCES Examen(Num_examen)
);

2>
ALTER TABLE Note
ADD CONSTRAINT check_note_range CHECK (note >= 0 AND note <= 20);

3>

INSERT INTO Eleve (Nom_eleve, Prenom_eleve) VALUES ('Dupont', 'Jean');


INSERT INTO Eleve (Nom_eleve, Prenom_eleve) VALUES ('Martin', 'Sophie');

INSERT INTO Classe (Nom_classe) VALUES ('Classe A');


INSERT INTO Classe (Nom_classe) VALUES ('Classe B');
INSERT INTO Inscrit (Num_eleve, Num_classe, Annee_scolaire) VALUES (1, 1, '2022-
2023');
INSERT INTO Inscrit (Num_eleve, Num_classe, Annee_scolaire) VALUES (2, 2, '2022-
2023');

INSERT INTO Matiere (Nom_matiere) VALUES ('Mathématiques');


INSERT INTO Matiere (Nom_matiere) VALUES ('Français');

INSERT INTO Examen (date_examen, Num_matiere) VALUES ('2022-05-01', 1);


INSERT INTO Examen (date_examen, Num_matiere) VALUES ('2022-05-02', 2);

INSERT INTO Note (Num_inscription, Num_examen, note) VALUES (1, 1, 18);


INSERT INTO Note (Num_inscription, Num_examen, note) VALUES (2, 2, 15);

4>

SELECT * FROM Eleve WHERE Nom_eleve LIKE 'A%' ORDER BY Nom_eleve ASC;

5>

SELECT COUNT(DISTINCT Num_eleve) FROM Inscrit


INNER JOIN Examen ON Inscrit.Num_inscription = Examen.Num_inscription
WHERE date_examen BETWEEN '2022-01-01' AND '2022-02-07'
GROUP BY Num_eleve
HAVING COUNT(*) > 3;

6>
SELECT Matiere.Nom_matiere, MAX(Note.note) AS meilleure_note
FROM Matiere
INNER JOIN Examen ON Matiere.Num_matiere = Examen.Num_matiere
INNER JOIN Note ON Examen.Num_examen = Note.Num_examen
GROUP BY Matiere.Num_matiere, Matiere.Nom_matiere
ORDER BY Matiere.Nom_matiere ASC;

7>

SELECT Eleve.Nom_eleve, Eleve.Prenom_eleve


FROM Eleve
INNER JOIN Inscrit ON Eleve.Num_eleve = Inscrit.Num_eleve
INNER JOIN Examen ON Inscrit.Num_inscription = Examen.Num_inscription
WHERE Examen.date_examen = CURDATE();

8>

CREATE USER 'Utilisateur1'@'localhost' IDENTIFIED BY 'mot_de_passe1';


CREATE USER 'Utilisateur2'@'localhost' IDENTIFIED BY 'mot_de_passe2';

9>
GRANT SELECT, UPDATE ON BaseDeDonnees.Examen TO 'Utilisateur1'@'localhost';

10>

GRANT CREATE TABLE, SELECT ON BaseDeDonnees.Eleve TO 'Utilisateur2'@'localhost';


GRANT SELECT ON BaseDeDonnees.Note TO 'Utilisateur2'@'localhost';

You might also like