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
| --------------
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 `langage`
--------------
--------------
CREATE TABLE IF NOT EXISTS `langage`
( `langage_id` integer unsigned auto_increment not null primary key,
`langage_nom` varchar(255) not null
) engine=innoDB
default charset=latin1 collate=latin1_general_ci
row_format=compressed
--------------
--------------
insert into `langage` (`langage_nom`) value
('cobol'),('fortran'),('algol'),('basic'),('pl1')
--------------
--------------
select * from `langage`
--------------
+------------+-------------+
| langage_id | langage_nom |
+------------+-------------+
| 1 | cobol |
| 2 | fortran |
| 3 | algol |
| 4 | basic |
| 5 | pl1 |
+------------+-------------+
--------------
DROP TABLE IF EXISTS `personne`
--------------
--------------
CREATE TABLE IF NOT EXISTS `personne`
( `personne_id` integer unsigned auto_increment not null primary key,
`personne_nom` varchar(255) not null
) engine=innoDB
default charset=latin1 collate=latin1_general_ci
row_format=compressed
--------------
--------------
insert into `personne` (`personne_nom`) value
('bill gates'),('Steve Jobs'),('Steve Wozniak')
--------------
--------------
select * from `personne`
--------------
+-------------+---------------+
| personne_id | personne_nom |
+-------------+---------------+
| 1 | bill gates |
| 2 | Steve Jobs |
| 3 | Steve Wozniak |
+-------------+---------------+
--------------
DROP TABLE IF EXISTS `projet`
--------------
--------------
CREATE TABLE IF NOT EXISTS `projet`
( `projet_id` integer unsigned auto_increment not null primary key,
`projet_nom` varchar(255) not null
) engine=innoDB
default charset=latin1 collate=latin1_general_ci
row_format=compressed
--------------
--------------
insert into `projet` (`projet_nom`) value
('Apple inc'),('Microsoft')
--------------
--------------
select * from `projet`
--------------
+-----------+------------+
| projet_id | projet_nom |
+-----------+------------+
| 1 | Apple inc |
| 2 | Microsoft |
+-----------+------------+
--------------
DROP TABLE IF EXISTS `utilisation`
--------------
--------------
CREATE TABLE IF NOT EXISTS `utilisation`
( `id` integer unsigned auto_increment not null primary key,
`personne_id` integer unsigned null,
`projet_id` integer unsigned null,
`langage_id` integer unsigned null,
CONSTRAINT `FK_PERSONNE` FOREIGN KEY (`personne_id`) REFERENCES `personne` (`personne_id`) ON DELETE RESTRICT ON UPDATE CASCADE,
CONSTRAINT `FK_PROJET` FOREIGN KEY (`projet_id`) REFERENCES `projet` (`projet_id`) ON DELETE RESTRICT ON UPDATE CASCADE,
CONSTRAINT `FK_LANGAGE` FOREIGN KEY (`langage_id`) REFERENCES `langage` (`langage_id`) ON DELETE RESTRICT ON UPDATE CASCADE,
unique index `IDX` (`personne_id`,`projet_id`,`langage_id`)
) engine=innoDB
default charset=latin1 collate=latin1_general_ci
row_format=compressed
--------------
--------------
insert into `utilisation` (`personne_id`,`projet_id`,`langage_id`) values (null, null, null)
--------------
--------------
insert into `utilisation` (`personne_id`,`projet_id`,`langage_id`) values (null, null, 1)
--------------
--------------
insert into `utilisation` (`personne_id`,`projet_id`,`langage_id`) values (null, 1, 1)
--------------
--------------
insert into `utilisation` (`personne_id`,`projet_id`,`langage_id`) values ( 1, 1, 1)
--------------
--------------
insert into `utilisation` (`personne_id`,`projet_id`,`langage_id`) values ( 1, 1, 1)
--------------
ERROR 1062 (23000) at line 121: Duplicata du champ '1-1-1' pour la clef 'IDX'
--------------
select * from `utilisation`
--------------
+----+-------------+-----------+------------+
| id | personne_id | projet_id | langage_id |
+----+-------------+-----------+------------+
| 1 | NULL | NULL | NULL |
| 2 | NULL | NULL | 1 |
| 3 | NULL | 1 | 1 |
| 4 | 1 | 1 | 1 |
+----+-------------+-----------+------------+
--------------
select t1.id,
t2.personne_nom,
t3.projet_nom,
t4.langage_nom
from `utilisation` as t1
left outer join `personne` as t2
on t2.personne_id = t1.personne_id
left outer join `projet` as t3
on t3.projet_id = t1.projet_id
left outer join `langage` as t4
on t4.langage_id = t1.langage_id
--------------
+----+--------------+------------+-------------+
| id | personne_nom | projet_nom | langage_nom |
+----+--------------+------------+-------------+
| 1 | NULL | NULL | NULL |
| 2 | NULL | NULL | cobol |
| 3 | NULL | Apple inc | cobol |
| 4 | bill gates | Apple inc | cobol |
+----+--------------+------------+-------------+
--------------
COMMIT
--------------
--------------
SET AUTOCOMMIT = 1
--------------
Appuyez sur une touche pour continuer... |
Partager