Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
2 views

4SQL

The document outlines SQL exercises for creating a user, database, and tables, as well as inserting, updating, and deleting records in those tables. It includes commands for backing up databases and loading data from a CSV file. Additionally, it provides notes on potential errors and solutions related to department and employee management.

Uploaded by

ezmandoye092
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

4SQL

The document outlines SQL exercises for creating a user, database, and tables, as well as inserting, updating, and deleting records in those tables. It includes commands for backing up databases and loading data from a CSV file. Additionally, it provides notes on potential errors and solutions related to department and employee management.

Uploaded by

ezmandoye092
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

4SQL

Exercice 1:
```sql
-- Création utilisateur et privilèges
CREATE USER 'tpUser'@'localhost' IDENTIFIED BY 'passer';
CREATE DATABASE tpDB;
GRANT ALL PRIVILEGES ON tpDB.* TO 'tpUser'@'localhost';
FLUSH PRIVILEGES;

-- Création des tables


CREATE TABLE LOCATIONS (
ID_LOCATION INT PRIMARY KEY,
VILLE VARCHAR(15)
);

CREATE TABLE DEPARTEMENTS (


ID_DEPARTEMENT INT(6) PRIMARY KEY,
NOM_DEPARTEMENT VARCHAR(15) NOT NULL,
ID_MANAGER INT(5) UNIQUE,
ID_LOCATION INT,
CONSTRAINT fk_dept_loc FOREIGN KEY (ID_LOCATION) REFERENCES
LOCATIONS(ID_LOCATION)
);

CREATE TABLE EMPLOYES (


ID_EMPLOYE INT(5) PRIMARY KEY,
PRENOM VARCHAR(20) NOT NULL,
NOM VARCHAR(15) NOT NULL,
DATE_EMBAUCHE DATETIME DEFAULT CURRENT_TIMESTAMP,
FONCTION VARCHAR(6),
SALAIRE DECIMAL(7,2),
COMMISSION INT DEFAULT 0,
ID_MANAGER INT(5),
ID_DEPARTEMENT INT(6),
CONSTRAINT fk_emp_manager FOREIGN KEY (ID_MANAGER)
REFERENCES EMPLOYES(ID_EMPLOYE),
CONSTRAINT fk_emp_dept FOREIGN KEY (ID_DEPARTEMENT)
REFERENCES DEPARTEMENTS(ID_DEPARTEMENT)
);

ALTER TABLE DEPARTEMENTS ADD CONSTRAINT fk_dept_manager


FOREIGN KEY (ID_MANAGER) REFERENCES EMPLOYES(ID_EMPLOYE);
```

Exercice 2:
```sql
-- 1. Insertion employés
INSERT INTO EMPLOYES VALUES
(100,'Steven','King','2007-06-17','AD_PRES',24000,NULL,NULL,NULL),
(101,'Neena','Kochhar','2009-09-21','AD_VP',17000,NULL,100,NULL),
(102,'Lex','De Haan','2009-01-13','AD_VP',17000,NULL,100,NULL),
(103,'Alexander','Hunold','2010-01-03','IT_PROG',9000,NULL,102,NULL);

-- 2. Insertion départements
INSERT INTO DEPARTEMENTS VALUES
(90,'Executive',100,NULL),
(60,'IT',103,1400);

-- 3-4. Affectation départements


UPDATE EMPLOYES SET ID_DEPARTEMENT = 90 WHERE ID_EMPLOYE IN
(100,101,102);
UPDATE EMPLOYES SET ID_DEPARTEMENT = 60 WHERE ID_EMPLOYE =
103;

-- 6. Doublement salaire programmeurs


UPDATE EMPLOYES SET SALAIRE = SALAIRE * 2 WHERE FONCTION =
'IT_PROG';

-- 7. Suppression employé 103


DELETE FROM EMPLOYES WHERE ID_EMPLOYE = 103;
```

Exercice 3:
```sql
-- Sauvegardes
mysqldump -u root -p --all-databases > alldatabases.sql
mysqldump -u root -p tpDB > tpDB.sql
mysqldump -u root -p tpDB EMPLOYES > employes.sql

-- Chargement fichier CSV


LOAD DATA INFILE 'd:/departements.csv'
INTO TABLE DEPARTEMENTS
FIELDS TERMINATED BY ';'
ENCLOSED BY '"'
LINES TERMINATED BY '\n';
```

Notes explicatives :
- 5.1: L'insertion de l'employé 124 échoue car le département 50 n'existe
pas
- 8.1: La suppression du département 90 échoue car des employés y sont
rattachés

Solutions :
- 5.2: Créer d'abord le département 50
- 8.2: Supprimer/réaffecter les employés du département 90 avant sa
suppression

You might also like