Vous êtes sur la page 1sur 5

TP initiation Base de données: initiation

I. A la découverte de MYSQL SERVER

1. Afficher la date et le jour du TP avec SELECT now(); ou avec SELECT now()


FROM dual;
2. Vérifier les caractères de votre base de données SHOW CHARACTER SET;
3. Créons notre premiere table avec mysql comme suit:
mysql> CREATE TABLE person
(
person_id SMALLINT UNSIGNED,
fname VARCHAR(20),
lname VARCHAR(20),
gender ENUM('M','F'),
birth_date DATE,
street VARCHAR(30),
city VARCHAR(20),
state VARCHAR(20),
country VARCHAR(20),
postal_code VARCHAR(20),
CONSTRAINT pk_person PRIMARY KEY (person_id)
);
4. Vérifier si la table person existe et afficher les différents attributs de la table avec un
DESC
5. Créer une deuxième table comme suit et expliquer la nouveauté sur cette table par
rapport à la table personne:

mysql> CREATE TABLE favorite_food


(
person_id SMALLINT UNSIGNED,

1
TP initiation Base de données: initiation

food VARCHAR(20),
CONSTRAINT pk_favorite_food PRIMARY KEY (person_id, food),
CONSTRAINT fk_fav_food_person_id FOREIGN KEY (person_id)
REFERENCES person (person_id)
);

6. Insérer des données dans la table personne comme suit:

mysql> INSERT INTO person


(person_id, fname, lname, gender, birth_date)
VALUES (null, 'William','Turner', 'M', '1972-05-27');

Expliquer chaque commande ci dessous après les avoirs essayés avec votre interpréteur de
commande mysql.

a) a) SELECT person_id, fname, lname, birth_date FROM person;


b) SELECT person_id, fname, lname, birth_date FROM person WHERE person_id = 1;
c) ELECT person_id, fname, lname, birth_date FROM person WHERE lname =
'Turner';
d) mysql> INSERT INTO favorite_food (person_id, food) VALUES (1, 'pizza');

mysql> INSERT INTO favorite_food (person_id, food) VALUES (1, 'cookies');

mysql> INSERT INTO favorite_food (person_id, food) VALUES (1, 'nachos');


e) SELECT food FROM favorite_food WHERE person_id = 1 ORDER BY food;
f) mysql> INSERT INTO person (person_id, fname, lname, gender, birth_date, street,
city, state, country, postal_code) VALUES (null, 'Susan','Smith', 'F', '1975-11-02', '23
Maple St.', 'Arlington', 'VA', 'USA', '20220') ;
g) SELECT person_id, fname, lname, birth_date FROM person;

2
TP initiation Base de données: initiation

h) UPDATE person SET street = '1225 Tremont St.', city = 'Boston', state = 'MA',
country = 'USA', postal_code = '02138' WHERE person_id = 1;
i) DELETE FROM person WHERE person_id = 2;
j) mysql> DROP TABLE favorite_food;
k) mysql> DROP TABLE person;

II. Les Scripts SQL

On veut mettre en place la base de données ci dessous pour ce faire copier le script ci
dessous dans un dossier et appelé le (source chemin_du_script) dans votre interpréteur de
commande Mysql.

1. Expliquer le résultat, afficher toutes les table créer ainsi que les données insérées
2. Expliquer chaque commande sql ci dessous après les avoirs tester avec votre
interpréteur de commande MYSQL Server:

 SELECT fname, lname FROM employee;


 SELECT emp_id, fname, lname FROM employee WHERE lname =
'Aminata';
 SELECT emp_id, 'ACTIVE', emp_id * 3.14159, UPPER(lname)FROM
employee;
 SELECT VERSION(), USER(), DATABASE() ;
 SELECT emp_id,
'ACTIVE' status,
emp_id * 3.14159 empid_x_pi,
UPPER(lname) last_name_upper
FROM employee;
 SELECT emp_id,
'ACTIVE' status,

3
TP initiation Base de données: initiation

emp_id * 3.14159 empid_x_pi,


UPPER(lname) last_name_upper FROM employee;

 SELECT cust_id FROM account;


 SELECT DISTINCT cust_id FROM account;
 SELECT e.emp_id, e.fname, e.lname FROM (SELECT emp_id, fname,
lname, start_date, title FROM employee) e;
 CREATE VIEW employee_vw AS SELECT emp_id, fname, lname,
YEAR(start_date) start_year FROM employee;
 SELECT emp_id, start_year FROM employee_vw;
 SELECT employee.emp_id, employee.fname, employee.lname,
department.name dept_name FROM employee INNER JOIN department ON
employee.dept_id = department.dept_id;
 SELECT e.emp_id, e.fname, e.lname, d.name dept_name FROM employee e
INNER JOIN department d ON e.dept_id = d.dept_id;
 SELECT emp_id, fname, lname, start_date, title FROM employee
WHERE (title = 'Head Teller' AND start_date > '2006-01-01')
OR (title = 'Teller' AND start_date > '2007-01-01');
 SELECT d.name, count(e.emp_id) num_employees FROM department d
INNER JOIN employee e ON d.dept_id = e.dept_id GROUP BY d.name
HAVING count(e.emp_id) > 2;
 SELECT open_emp_id, product_cd FROM account;
 SELECT open_emp_id, product_cd FROM account ORDER BY
open_emp_id;
 SELECT open_emp_id, product_cd FROM account ORDER BY
open_emp_id, product_cd;
 SELECT account_id, product_cd, open_date, avail_balance FROM account
ORDER BY avail_balance DESC;

4
TP initiation Base de données: initiation

III. Les jointures


 Produit Cartesian
 SELECT e.fname, e.lname, d.name FROM employee e JOIN department d;
 Jointure interne
 SELECT e.fname, e.lname, d.name FROM employee e JOIN department d
ON e.dept_id = d.dept_id;
 SELECT e.fname, e.lname, d.name FROM employee e
INNER JOIN department d
ON e.dept_id = d.dept_id;
 Jointure de plusieurs tables
SELECT a.account_id, c.fed_id
FROM account a INNER JOIN customer c
ON a.cust_id = c.cust_id
WHERE c.cust_type_cd = 'B';

SELECT a.account_id, c.fed_id, e.fname, e.lname


FROM account a INNER JOIN customer c
ON a.cust_id = c.cust_id
INNER JOIN employee e
ON a.open_emp_id = e.emp_id
WHERE c.cust_type_cd = 'B';

Vous aimerez peut-être aussi