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 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263
| --------------
SET AUTOCOMMIT = 0
--------------
--------------
START TRANSACTION
--------------
--------------
DROP DATABASE IF EXISTS `base`
--------------
--------------
CREATE DATABASE `base`
DEFAULT CHARACTER SET `utf8`
DEFAULT COLLATE `utf8_general_ci`
--------------
--------------
DROP TABLE IF EXISTS `table1`
--------------
--------------
CREATE TABLE `table1`
( `id` integer unsigned NOT NULL auto_increment primary key,
`ch_t1` varchar(255) NULL DEFAULT NULL
) ENGINE=InnoDB
DEFAULT CHARSET=`utf8` COLLATE=`utf8_general_ci`
ROW_FORMAT=COMPRESSED
--------------
--------------
insert into `table1` (`ch_t1`) values
('un'),('deux'),('trois')
--------------
--------------
select * from table1
--------------
+----+-------+
| id | ch_t1 |
+----+-------+
| 1 | un |
| 2 | deux |
| 3 | trois |
+----+-------+
--------------
DROP TABLE IF EXISTS `table2`
--------------
--------------
CREATE TABLE `table2`
( `id` integer unsigned NOT NULL auto_increment primary key,
`ch_t2` varchar(255) NULL DEFAULT NULL
) ENGINE=InnoDB
DEFAULT CHARSET=`utf8` COLLATE=`utf8_general_ci`
ROW_FORMAT=COMPRESSED
--------------
--------------
insert into `table2` (`ch_t2`) values
('one'),('two'),('three')
--------------
--------------
select * from table2
--------------
+----+-------+
| id | ch_t2 |
+----+-------+
| 1 | one |
| 2 | two |
| 3 | three |
+----+-------+
--------------
DROP TABLE IF EXISTS `table3`
--------------
--------------
CREATE TABLE `table3`
( `id` integer unsigned NOT NULL auto_increment primary key,
`ch_t3` varchar(255) NULL DEFAULT NULL
) ENGINE=InnoDB
DEFAULT CHARSET=`utf8` COLLATE=`utf8_general_ci`
ROW_FORMAT=COMPRESSED
--------------
--------------
insert into `table3` (`ch_t3`) values
('eins'),('zwei'),('drei')
--------------
--------------
select * from table3
--------------
+----+-------+
| id | ch_t3 |
+----+-------+
| 1 | eins |
| 2 | zwei |
| 3 | drei |
+----+-------+
--------------
DROP TABLE IF EXISTS `table4`
--------------
--------------
CREATE TABLE `table4`
( `id` integer unsigned NOT NULL auto_increment primary key,
`ch_t4` varchar(255) NULL DEFAULT NULL
) ENGINE=InnoDB
DEFAULT CHARSET=`utf8` COLLATE=`utf8_general_ci`
ROW_FORMAT=COMPRESSED
--------------
--------------
insert into `table4` (`ch_t4`) values
('uno'),('due'),('tre')
--------------
--------------
select * from table4
--------------
+----+-------+
| id | ch_t4 |
+----+-------+
| 1 | uno |
| 2 | due |
| 3 | tre |
+----+-------+
--------------
DROP TABLE IF EXISTS `product1`
--------------
--------------
CREATE TABLE `product1`
( `id` integer unsigned NOT NULL auto_increment primary key,
`id_table1` integer unsigned NULL DEFAULT NULL,
`id_table2` integer unsigned NULL DEFAULT NULL,
`id_table3` integer unsigned NULL DEFAULT NULL,
`id_table4` integer unsigned NULL DEFAULT NULL,
CONSTRAINT `FK_PRODUCT1_TABLE1` FOREIGN KEY (`id_table1`) REFERENCES `table1` (`id`) ON DELETE CASCADE ON UPDATE CASCADE,
CONSTRAINT `FK_PRODUCT1_TABLE2` FOREIGN KEY (`id_table2`) REFERENCES `table2` (`id`) ON DELETE CASCADE ON UPDATE CASCADE,
CONSTRAINT `FK_PRODUCT1_TABLE3` FOREIGN KEY (`id_table3`) REFERENCES `table3` (`id`) ON DELETE CASCADE ON UPDATE CASCADE,
CONSTRAINT `FK_PRODUCT1_TABLE4` FOREIGN KEY (`id_table4`) REFERENCES `table4` (`id`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB
DEFAULT CHARSET=`utf8` COLLATE=`utf8_general_ci`
ROW_FORMAT=COMPRESSED
--------------
--------------
insert into `product1` (`id_table1`,`id_table2`,`id_table3`,`id_table4`) values (1, 1, 1, 1)
--------------
--------------
insert into `product1` (`id_table1`,`id_table2`,`id_table3`,`id_table4`) values (2, 2, 2, 2)
--------------
--------------
insert into `product1` (`id_table1`,`id_table2`,`id_table3`,`id_table4`) values (3, 3, 3, 3)
--------------
--------------
insert into `product1` (`id_table1`,`id_table2`,`id_table3`,`id_table4`) values (DEFAULT,DEFAULT,DEFAULT,DEFAULT)
--------------
--------------
select * from product1
--------------
+----+-----------+-----------+-----------+-----------+
| id | id_table1 | id_table2 | id_table3 | id_table4 |
+----+-----------+-----------+-----------+-----------+
| 1 | 1 | 1 | 1 | 1 |
| 2 | 2 | 2 | 2 | 2 |
| 3 | 3 | 3 | 3 | 3 |
| 4 | NULL | NULL | NULL | NULL |
+----+-----------+-----------+-----------+-----------+
--------------
DROP TABLE IF EXISTS `product2`
--------------
--------------
CREATE TABLE `product2`
( `id_table1` integer unsigned NULL DEFAULT NULL,
`id_table2` integer unsigned NULL DEFAULT NULL,
`id_table3` integer unsigned NULL DEFAULT NULL,
`id_table4` integer unsigned NULL DEFAULT NULL,
primary key (`id_table1`,`id_table2`,`id_table3`,`id_table4`),
CONSTRAINT `FK_PRODUCT2_TABLE1` FOREIGN KEY (`id_table1`) REFERENCES `table1` (`id`) ON DELETE CASCADE ON UPDATE CASCADE,
CONSTRAINT `FK_PRODUCT2_TABLE2` FOREIGN KEY (`id_table2`) REFERENCES `table2` (`id`) ON DELETE CASCADE ON UPDATE CASCADE,
CONSTRAINT `FK_PRODUCT2_TABLE3` FOREIGN KEY (`id_table3`) REFERENCES `table3` (`id`) ON DELETE CASCADE ON UPDATE CASCADE,
CONSTRAINT `FK_PRODUCT2_TABLE4` FOREIGN KEY (`id_table4`) REFERENCES `table4` (`id`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB
DEFAULT CHARSET=`utf8` COLLATE=`utf8_general_ci`
ROW_FORMAT=COMPRESSED
--------------
--------------
insert into `product2` (`id_table1`,`id_table2`,`id_table3`,`id_table4`) values (1, 1, 1, 1)
--------------
--------------
insert into `product2` (`id_table1`,`id_table2`,`id_table3`,`id_table4`) values (2, 2, 2, 2)
--------------
--------------
insert into `product2` (`id_table1`,`id_table2`,`id_table3`,`id_table4`) values (3, 3, 3, 3)
--------------
--------------
insert into `product2` (`id_table1`,`id_table2`,`id_table3`,`id_table4`) values (DEFAULT,DEFAULT,DEFAULT,DEFAULT)
--------------
ERROR 1452 (23000) at line 182: Cannot add or update a child row: a foreign key constraint fails (`base`.`product2`, CONSTRAINT `FK_PRODUCT2_TABLE1` FOREIGN KEY (`id_table1`) REFERENCES `table1` (`id`) ON DELETE CASCADE ON UPDATE CASCADE)
--------------
select * from product2
--------------
+-----------+-----------+-----------+-----------+
| id_table1 | id_table2 | id_table3 | id_table4 |
+-----------+-----------+-----------+-----------+
| 1 | 1 | 1 | 1 |
| 2 | 2 | 2 | 2 |
| 3 | 3 | 3 | 3 |
+-----------+-----------+-----------+-----------+
--------------
select *
from product1 as pr
inner join table1 as t1
on t1.id = pr.id_table1
inner join table2 as t2
on t2.id = pr.id_table2
inner join table3 as t3
on t3.id = pr.id_table1
inner join table4 as t4
on t4.id = pr.id_table4
--------------
+----+-----------+-----------+-----------+-----------+----+-------+----+-------+----+-------+----+-------+
| id | id_table1 | id_table2 | id_table3 | id_table4 | id | ch_t1 | id | ch_t2 | id | ch_t3 | id | ch_t4 |
+----+-----------+-----------+-----------+-----------+----+-------+----+-------+----+-------+----+-------+
| 1 | 1 | 1 | 1 | 1 | 1 | un | 1 | one | 1 | eins | 1 | uno |
| 2 | 2 | 2 | 2 | 2 | 2 | deux | 2 | two | 2 | zwei | 2 | due |
| 3 | 3 | 3 | 3 | 3 | 3 | trois | 3 | three | 3 | drei | 3 | tre |
+----+-----------+-----------+-----------+-----------+----+-------+----+-------+----+-------+----+-------+
--------------
COMMIT
--------------
--------------
SET AUTOCOMMIT = 1
--------------
Appuyez sur une touche pour continuer... |
Partager