1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161
| --------------
SET AUTOCOMMIT = 0
--------------
--------------
START TRANSACTION
--------------
--------------
DROP DATABASE IF EXISTS `base`
--------------
--------------
CREATE DATABASE `base`
DEFAULT CHARACTER SET `utf8`
DEFAULT COLLATE `utf8_unicode_ci`
--------------
--------------
DROP TABLE IF EXISTS `auteur`
--------------
--------------
CREATE TABLE IF NOT EXISTS `auteur`
( `id` integer unsigned NOT NULL auto_increment,
`nom` char(10) DEFAULT NULL,
`prenom` char(10) DEFAULT NULL,
Primary Key (`id`)
) ENGINE=InnoDB
DEFAULT CHARSET=`utf8` COLLATE=`utf8_unicode_ci`
ROW_FORMAT=COMPRESSED
--------------
--------------
INSERT INTO `auteur` (`nom`, `prenom`) VALUES
('écrivain 1', 'albert'), ('écrivain 2', 'marcel'), ('écrivain 3', 'henri')
--------------
--------------
select * from auteur
--------------
+----+------------+--------+
| id | nom | prenom |
+----+------------+--------+
| 1 | écrivain 1 | albert |
| 2 | écrivain 2 | marcel |
| 3 | écrivain 3 | henri |
+----+------------+--------+
--------------
DROP TABLE IF EXISTS `livre`
--------------
--------------
CREATE TABLE IF NOT EXISTS `livre`
( `id` integer unsigned NOT NULL auto_increment,
`titre` char(10) DEFAULT NULL,
Primary Key (`id`)
) ENGINE=InnoDB
DEFAULT CHARSET=`utf8` COLLATE=`utf8_unicode_ci`
ROW_FORMAT=COMPRESSED
--------------
--------------
INSERT INTO `livre` (`titre`) VALUES
('bouquin 1'),('bouquin 2'),('bouquin 3')
--------------
--------------
select * from livre
--------------
+----+-----------+
| id | titre |
+----+-----------+
| 1 | bouquin 1 |
| 2 | bouquin 2 |
| 3 | bouquin 3 |
+----+-----------+
--------------
DROP TABLE IF EXISTS `assoc`
--------------
--------------
CREATE TABLE IF NOT EXISTS `assoc`
( `auteur` integer unsigned not null,
`livre` integer unsigned not null,
Primary Key (`auteur`,`livre`)
) ENGINE=InnoDB
DEFAULT CHARSET=`utf8` COLLATE=`utf8_unicode_ci`
ROW_FORMAT=COMPRESSED
--------------
--------------
INSERT INTO `assoc` (`auteur`,`livre`) VALUES
(1, 1),(2, 1), (2, 2), (1, 3), (3, 3)
--------------
--------------
select * from assoc
--------------
+--------+-------+
| auteur | livre |
+--------+-------+
| 1 | 1 |
| 1 | 3 |
| 2 | 1 |
| 2 | 2 |
| 3 | 3 |
+--------+-------+
--------------
select t2.nom,
t2.prenom,
t3.titre
from assoc as t1
inner join auteur as t2
on (t2.id = t1.auteur)
inner join livre as t3
on (t3.id = t1.livre)
where auteur = 1
--------------
+------------+--------+-----------+
| nom | prenom | titre |
+------------+--------+-----------+
| écrivain 1 | albert | bouquin 1 |
| écrivain 1 | albert | bouquin 3 |
+------------+--------+-----------+
--------------
select t3.titre,
t2.nom,
t2.prenom
from assoc as t1
inner join auteur as t2
on (t2.id = t1.auteur)
inner join livre as t3
on (t3.id = t1.livre)
where livre = 3
--------------
+-----------+------------+--------+
| titre | nom | prenom |
+-----------+------------+--------+
| bouquin 3 | écrivain 1 | albert |
| bouquin 3 | écrivain 3 | henri |
+-----------+------------+--------+
--------------
COMMIT
--------------
--------------
SET AUTOCOMMIT = 1
--------------
Appuyez sur une touche pour continuer... |
Partager