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
| --------------
START TRANSACTION
--------------
--------------
DROP DATABASE IF EXISTS `base`
--------------
--------------
CREATE DATABASE IF NOT EXISTS `base`
DEFAULT CHARACTER SET `latin1`
DEFAULT COLLATE `latin1_general_ci`
--------------
--------------
DROP TABLE IF EXISTS `type`
--------------
--------------
CREATE TABLE `type`
( `codety` integer unsigned NOT NULL primary key,
`nomty` varchar(255) NOT NULL,
`capacitety` decimal(15,2) NOT NULL DEFAULT 0
) ENGINE=Innodb
DEFAULT CHARSET=`latin1` COLLATE=`latin1_general_ci`
ROW_FORMAT=COMPRESSED
--------------
--------------
insert into `type` (`codety`,`nomty`,`capacitety`) values
(25,'exemple',17.45)
--------------
--------------
select * from `type`
--------------
+--------+---------+------------+
| codety | nomty | capacitety |
+--------+---------+------------+
| 25 | exemple | 17.45 |
+--------+---------+------------+
--------------
DROP TABLE IF EXISTS `vehicule`
--------------
--------------
CREATE TABLE `vehicule`
( `codeve` integer unsigned NOT NULL,
`immatve` varchar(255) NOT NULL,
`codety` integer unsigned NOT NULL,
CONSTRAINT `FK_01` FOREIGN KEY (`codety`) REFERENCES `type` (`codety`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=Innodb
DEFAULT CHARSET=`latin1` COLLATE=`latin1_general_ci`
ROW_FORMAT=COMPRESSED
--------------
--------------
insert into `vehicule` (`codeve`,`immatve`,`codety`) values (75,'123-abc-456',25)
--------------
--------------
insert into `vehicule` (`codeve`,`immatve`,`codety`) values (17,'987-xyz-321',49)
--------------
ERROR 1452 (23000) at line 63 in file: 'E:\base.sql': Cannot add or update a child row: a foreign key constraint fails (`base`.`vehicule`, CONSTRAINT `FK_01` FOREIGN KEY (`codety`) REFERENCES `type` (`codety`) ON DELETE CA
SCADE ON UPDATE CASCADE)
--------------
select * from `vehicule`
--------------
+--------+-------------+--------+
| codeve | immatve | codety |
+--------+-------------+--------+
| 75 | 123-abc-456 | 25 |
+--------+-------------+--------+
--------------
select a.codeve,
a.immatve,
b.codety,
b.nomty,
b.capacitety
from `vehicule` as a
inner join `type` as b
on b.codety = a.codety
--------------
+--------+-------------+--------+---------+------------+
| codeve | immatve | codety | nomty | capacitety |
+--------+-------------+--------+---------+------------+
| 75 | 123-abc-456 | 25 | exemple | 17.45 |
+--------+-------------+--------+---------+------------+
--------------
COMMIT
--------------
Appuyez sur une touche pour continuer... |
Partager