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
| --------------
SET AUTOCOMMIT = 0
--------------
--------------
START TRANSACTION
--------------
--------------
DROP DATABASE IF EXISTS `base`
--------------
--------------
CREATE DATABASE `base`
DEFAULT CHARACTER SET `latin1`
DEFAULT COLLATE `latin1_general_ci`
--------------
--------------
DROP TABLE IF EXISTS `Client`
--------------
--------------
CREATE TABLE Client (
`id_client` int(11) NOT NULL AUTO_INCREMENT,
`nom` varchar(12) NOT NULL,
PRIMARY KEY (`id_client`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1
--------------
--------------
INSERT INTO `Client` (`nom`) VALUES
('albert'), ('marcel'), ('léon'), ('alfred')
--------------
--------------
select * from client
--------------
+-----------+--------+
| id_client | nom |
+-----------+--------+
| 1 | albert |
| 2 | marcel |
| 3 | léon |
| 4 | alfred |
+-----------+--------+
--------------
DROP TABLE IF EXISTS `Commandes`
--------------
--------------
CREATE TABLE Commandes (
`id_commandes` int(11) NOT NULL AUTO_INCREMENT,
`id_client` int(11) NOT NULL,
`produit` varchar(20) NOT NULL,
PRIMARY KEY (`id_commandes`),
FOREIGN KEY(`id_client`) REFERENCES `Client` (`id_client`) ON UPDATE CASCADE ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1
--------------
--------------
INSERT INTO `Commandes` (`id_client`, `produit`) VALUES
(2, 'machin'), (4, 'truc'), (4, 'chose')
--------------
--------------
select * from commandes
--------------
+--------------+-----------+---------+
| id_commandes | id_client | produit |
+--------------+-----------+---------+
| 1 | 2 | machin |
| 2 | 4 | truc |
| 3 | 4 | chose |
+--------------+-----------+---------+
--------------
INSERT INTO `Commandes` (`id_client`, `produit`) VALUES
(5, 'bidule')
--------------
ERROR 1452 (23000) at line 72: Cannot add or update a child row: a foreign key constraint fails (`base`.`commandes`, CONSTRAINT `commandes_ibfk_1` FOREIGN KEY (`id_client`) REFERENCES `client` (`id_cl
ient`) ON DELETE CASCADE ON UPDATE CASCADE)
--------------
select * from commandes
--------------
+--------------+-----------+---------+
| id_commandes | id_client | produit |
+--------------+-----------+---------+
| 1 | 2 | machin |
| 2 | 4 | truc |
| 3 | 4 | chose |
+--------------+-----------+---------+
--------------
update `client` set id_client = 33 where id_client = 4
--------------
--------------
select * from client
--------------
+-----------+--------+
| id_client | nom |
+-----------+--------+
| 1 | albert |
| 2 | marcel |
| 3 | léon |
| 33 | alfred |
+-----------+--------+
--------------
select * from commandes
--------------
+--------------+-----------+---------+
| id_commandes | id_client | produit |
+--------------+-----------+---------+
| 1 | 2 | machin |
| 2 | 33 | truc |
| 3 | 33 | chose |
+--------------+-----------+---------+
--------------
delete from `client` where id_client = 33
--------------
--------------
select * from client
--------------
+-----------+--------+
| id_client | nom |
+-----------+--------+
| 1 | albert |
| 2 | marcel |
| 3 | léon |
+-----------+--------+
--------------
select * from commandes
--------------
+--------------+-----------+---------+
| id_commandes | id_client | produit |
+--------------+-----------+---------+
| 1 | 2 | machin |
+--------------+-----------+---------+
--------------
COMMIT
--------------
--------------
SET AUTOCOMMIT = 1
--------------
Appuyez sur une touche pour continuer... |
Partager