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
| --------------
START TRANSACTION
--------------
--------------
set session collation_connection = "latin1_general_ci"
--------------
--------------
DROP DATABASE IF EXISTS `base`
--------------
--------------
CREATE DATABASE IF NOT EXISTS `base`
DEFAULT CHARACTER SET `latin1`
DEFAULT COLLATE `latin1_general_ci`
--------------
--------------
DROP TABLE IF EXISTS `nombre`
--------------
--------------
CREATE TABLE `nombre`
( `id` smallint unsigned NOT NULL primary key,
`mess` varchar(25) NOT NULL
) ENGINE=InnoDB
DEFAULT CHARSET=`latin1` COLLATE=`latin1_general_ci`
ROW_FORMAT=COMPRESSED
--------------
--------------
insert into `nombre` (`id`,`mess`) values
(7, 'sept'),
(2, 'deux'),
(10, 'dix'),
(4, 'quatre'),
(1, 'un'),
(8, 'huit'),
(6, 'six'),
(3, 'trois'),
(5, 'cinq'),
(9, 'neuf')
--------------
--------------
select * from `nombre`
--------------
+----+--------+
| id | mess |
+----+--------+
| 1 | un |
| 2 | deux |
| 3 | trois |
| 4 | quatre |
| 5 | cinq |
| 6 | six |
| 7 | sept |
| 8 | huit |
| 9 | neuf |
| 10 | dix |
+----+--------+
--------------
DROP TABLE IF EXISTS `test`
--------------
--------------
CREATE TABLE `test`
( `clef` integer unsigned NOT NULL auto_increment primary key,
`val` smallint unsigned NOT NULL,
CONSTRAINT `FK_01` FOREIGN KEY (`val`) REFERENCES `nombre` (`id`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB
DEFAULT CHARSET=`latin1` COLLATE=`latin1_general_ci`
ROW_FORMAT=COMPRESSED
--------------
--------------
insert into `test` (`val`) values
(7),(5),(6),(4),(1),(10),(8),(3),(2),(9)
--------------
--------------
select t2.id,
t2.mess,
t1.clef,
t1.val
from `test` as t1
inner join `nombre` as t2
on t2.id = t1.val
order by t2.id
--------------
+----+--------+------+-----+
| id | mess | clef | val |
+----+--------+------+-----+
| 1 | un | 5 | 1 |
| 2 | deux | 9 | 2 |
| 3 | trois | 8 | 3 |
| 4 | quatre | 4 | 4 |
| 5 | cinq | 2 | 5 |
| 6 | six | 3 | 6 |
| 7 | sept | 1 | 7 |
| 8 | huit | 7 | 8 |
| 9 | neuf | 10 | 9 |
| 10 | dix | 6 | 10 |
+----+--------+------+-----+
--------------
COMMIT
--------------
Appuyez sur une touche pour continuer... |
Partager