0% found this document useful (0 votes)
16 views2 pages

TP 8

The document outlines the creation of a database named 'GestionEmprunt' with tables for agencies, clients, accounts, and loans, including their respective fields. It also includes the establishment of primary keys and foreign key constraints to maintain data integrity, as well as a check constraint for client cities. Additionally, several SQL queries are provided to retrieve and analyze data from the created tables.

Uploaded by

amissanbennani
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)
16 views2 pages

TP 8

The document outlines the creation of a database named 'GestionEmprunt' with tables for agencies, clients, accounts, and loans, including their respective fields. It also includes the establishment of primary keys and foreign key constraints to maintain data integrity, as well as a check constraint for client cities. Additionally, several SQL queries are provided to retrieve and analyze data from the created tables.

Uploaded by

amissanbennani
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

CREATE DATABASE GestionEmprunt;

USE GestionEmprunt;
CREATE TABLE AGENCE (Num_Agence INT , Nom VARCHAR(12), Ville VARCHAR(12), Actif
FLOAT);

USE GestionEmprunt;
CREATE TABLE CLIENT (Num_Client INT , Nom VARCHAR(12), Ville VARCHAR(12));

USE GestionEmprunt;
CREATE TABLE COMPTE (Num_Compte INT, Num_Agence INT, Num_Client INT, Solde FLOAT);

USE GestionEmprunt;
CREATE TABLE EMPRUNT (Num_Emprunt INT, Num_Agence INT, Num_Client INT, Montant
FLOAT);

2.
ALTER TABLE AGENCE
ADD PRIMARY KEY (Num_Agence);

ALTER TABLE COMPTE


ADD PRIMARY KEY (Num_Compte);

ALTER TABLE CLIENT


ADD PRIMARY KEY (Num_Client);

ALTER TABLE COMPTE


ADD CONSTRAINT FK_Num_Agence FOREIGN KEY (Num_Agence) REFERENCES
AGENCE(Num_Agence);

ALTER TABLE COMPTE


ADD CONSTRAINT FK_Num_Client FOREIGN KEY (Num_Client) REFERENCES
CLIENT(Num_Client);

ALTER TABLE EMPRUNT


ADD PRIMARY KEY (Num_Emprunt);

ALTER TABLE EMPRUNT


ADD CONSTRAINT FK_Num_Agence_EMPRUNT FOREIGN KEY (Num_Agence) REFERENCES
AGENCE(Num_Agence);

ALTER TABLE EMPRUNT


ADD CONSTRAINT FK_Num_Client_EMPRUNT FOREIGN KEY (Num_Client) REFERENCES
CLIENT(Num_Client);

3.
ALTER TABLE CLIENT
ADD CONSTRAINT CK_Ville CHECK (Ville IN ('Marrakech', 'Tanger', 'Rabat'));
4.
a. SELECT * FROM AGENCE;

b. SELECT Nom FROM CLIENT WHERE Nom LIKE 'B%E' AND LENGTH(Ville) = 5;

c.SELECT COUNT(DISTINCT Ville) AS nombre_ville FROM CLIENT;

d.
SELECT*FROM agence,compte WHERE agence.Num_Agence=compte.Num_Agence;
e.
SELECT client.Nom FROM client, compte, agence WHERE client.Num_Client =
compte.Num_Client AND compte.Num_Agence = agence.Num_Agence AND agence.Ville =
'Marrakech';

f.
SELECT * FROM AGENCE
WHERE Ville = 'Marrakech'
AND Actif = (SELECT MIN(Actif) FROM AGENCE WHERE Ville = 'Marrakech');

g.
SELECT AVG(moyen) AS solde_moyen
FROM (SELECT Num_Agence, AVG(Solde) AS moyen FROM COMPTE
GROUP BY Num_Agence) AS agences_valides WHERE moyen >= 20000;

You might also like