Code SQL
Fahd Krachel
January 2025
1 Introduction
Ce document présente un exemple de code SQL divisé en trois grandes parties
: création de la base de données, insertion des enregistrements, et création de
requêtes.
2 1. En langage SQL, créez la BDD de gestion
demandée
1 CREATE DATABASE G e s t i o n P e r s o n ne l ;
2 USE G e s t i o n P e r s o n n e l ;
3
4 CREATE TABLE Service (
5 ServiceID INT PRIMARY KEY AUTO_INCREMENT ,
6 NomService VARCHAR (50) NOT NULL
7 );
8
9 CREATE TABLE Personnel (
10 PersonnelID INT PRIMARY KEY AUTO_INCREMENT ,
11 Nom VARCHAR (50) NOT NULL ,
12 Prenom VARCHAR (50) NOT NULL ,
13 Specialite VARCHAR (100) NOT NULL ,
14 ServiceID INT ,
15 FOREIGN KEY ( ServiceID ) REFERENCES Service ( ServiceID )
16 );
17
18 CREATE TABLE Permanence (
19 PermanenceID INT PRIMARY KEY AUTO_INCREMENT ,
20 PersonnelID INT ,
21 Date Permanen ce DATE NOT NULL ,
22 Assuree BOOLEAN NOT NULL ,
23 FOREIGN KEY ( PersonnelID ) REFERENCES Personnel ( PersonnelID )
24 );
3 2. Insérez 3 enregistrements de votre choix
1
1 INSERT INTO Service ( NomService ) VALUES ( ’ Informatique ’) , ( ’ RH ’) , (
’ Marketing ’) ;
2
3 INSERT INTO Personnel ( Nom , Prenom , Specialite , ServiceID )
4 VALUES
5 ( ’ El Idrissi ’ , ’ Mohamed ’ , ’ Admini strateur S y s t m e ’ , 1) ,
6 ( ’ Bennani ’ , ’ Fatima ’ , ’ Analyste de D o n n e s ’ , 1) ,
7 ( ’ Alaoui ’ , ’ Rachid ’ , ’ S p c i a l i s t e en R s e a u ’ , 1) ;
8
9 INSERT INTO Permanence ( PersonnelID , DatePermanence , Assuree )
10 VALUES
11 (1 , ’ 2025 -01 -01 ’ , TRUE ) ,
12 (2 , ’ 2025 -01 -02 ’ , FALSE ) ,
13 (3 , ’ 2025 -01 -03 ’ , TRUE ) ;
4 3. Créez 3 requêtes : 1 d’action, 1 scalaire et
1 sous requête
4.1 3.1 Requête d’action
1 UPDATE Permanence
2 SET Assuree = TRUE
3 WHERE PersonnelID = 2 AND DatePer manence = ’ 2025 -01 -02 ’;
4.2 3.2 Requête scalaire
1 SELECT COUNT (*) AS T o t a l P e r m a n e n c e s A s s u r e e s
2 FROM Permanence
3 WHERE Assuree = TRUE ;
4.3 3.3 Sous-requête
1 SELECT Nom , Prenom
2 FROM Personnel
3 WHERE PersonnelID IN (
4 SELECT PersonnelID
5 FROM Permanence
6 WHERE Assuree = FALSE
7 );