fork download
  1. -- TP N° 4 SQL
  2.  
  3. CREATE DATABASE HimalayaDB;
  4. USE HimalayaDB;
  5.  
  6. Create table Mountains(
  7. id int auto_increment primary key,
  8. name varchar(40),
  9. height int,
  10. country varchar(20));
  11.  
  12. create table Mountaineers(
  13. id int auto_increment primary key,
  14. name varchar(40),
  15. nationality varchar(20),
  16. experience int);
  17.  
  18. create table sherpas(
  19. id int auto_increment primary key,
  20. name varchar(40),
  21. nationality varchar(20));
  22.  
  23.  
  24. create table teams(
  25. id int auto_increment primary key,
  26. nationality varchar(20),
  27. country varchar(20));
  28.  
  29. create table expeditions(
  30. id int auto_increment primary key,
  31. mountain_id int,
  32. team_id int,
  33. year int,
  34. foreign key (mountain_id) references mountains(id),
  35. foreign key (team_id) references teams(id));
  36.  
  37. create table exploits(
  38. id int auto_increment primary key,
  39. mountaineer_id int,
  40. expedition_id int,
  41. success boolean,
  42. foreign key (mountaineer_id) references mountaineers(id),
  43. foreign key (expedition_id) references expeditions(id));
  44.  
  45. create table participation(
  46. mountaineer_id int,
  47. expedition_id int,
  48. role varchar(20),
  49. foreign key (mountaineer_id) references mountaineers(id),
  50. foreign key (expedition_id) references expeditions(id));
  51.  
  52.  
  53. INSERT INTO Mountains VALUES
  54. (1,'Everest',8848,'Nepal'),
  55. (2,'K2',8611,'Pakistan'),
  56. (3,'Kangchenjunga',8586,'Nepal/India'),
  57. (4,'Lhotse',8516,'Nepal'),
  58. (5,'Makalu',8485,'Nepal'),
  59. (6,'ChoOyu',8188,'Nepal'),
  60. (7,'Dhaulagiri',8167,'Nepal'),
  61. (8,'Manaslu',8163,'Nepal'),
  62. (9,'NangaParbat',8126,'Pakistan'),
  63. (10,'Annapurna',8091,'Nepal');
  64.  
  65. INSERT INTO Mountaineers VALUES
  66. (1,'Messner'
  67. ,
  68. 'Italy',50),
  69. (2,'Hillary'
  70. ,
  71. 'NZ',30),
  72. (3,'Tenzing'
  73. ,
  74. 'Nepal',25),
  75. (4,'Kukuczka'
  76. ,
  77. 'Poland',40),
  78. (5,'Moro'
  79. ,
  80. 'Italy',20),
  81. (6,'Steck'
  82. ,
  83. 'Swiss',25),
  84. (7,'Purja'
  85. ,
  86. 'Nepal',15),
  87. (8,'Bonington'
  88. ,
  89. 'UK',35),
  90. (9,'Urubko'
  91. ,
  92. 'Kazakh',30),
  93. (10,'Hinkes'
  94. ,
  95. 'UK',28);
  96.  
  97. INSERT INTO Sherpas VALUES
  98. (1,'Kami Rita','Nepal'),
  99. (2,'Apa','Nepal'),
  100. (3,'Phurba','Nepal'),
  101. (4,'Ang Rita','Nepal'),
  102. (5,'Pasang','Nepal'),
  103. (6,'Lakpa','Nepal'),
  104. (7,'Mingma','Nepal'),
  105. (8,'Dawa','Nepal'),
  106. (9,'Pemba','Nepal'),
  107. (10,'Dorje','Nepal');
  108.  
  109.  
  110.  
  111. INSERT INTO Teams VALUES
  112. (1,'Italian Team','Italy'),
  113. (2,'British Team','UK'),
  114. (3,'Nepal Team','Nepal'),
  115. (4,'Polish Team', 'Poland'),
  116. (5,'Swiss Team','Swiss'),
  117. (6,'French Team','France'),
  118. (7,'German Team','Germany'),
  119. (8,'Spanish Team','Spain'),
  120. (9,'US Team','USA'),
  121. (10,'Japan Team','Japan');
  122.  
  123. INSERT INTO Expeditions VALUES
  124. (1,1,3,1953),
  125. (2,2,4,1986),
  126. (3,3,4,1980),
  127. (4,4,1,1970),
  128. (5,5,2,1985),
  129. (6,6,5,2000),
  130. (7,7,6,1990),
  131. (8,8,7,1995),
  132. (9,9,8,2005),
  133. (10,10,9,2010);
  134.  
  135.  
  136. INSERT INTO Exploits VALUES
  137. (1,2,1,TRUE),
  138. (2,3,1,TRUE),
  139. (3,1,2,TRUE),
  140. (4,4,3,TRUE),
  141. (5,5,5, TRUE ), (6,6,6, TRUE ), (7,7,9, TRUE ), (8,8,4, TRUE ), (9,9,2, TRUE ), (10,10,10, TRUE );
  142.  
  143. INSERT INTO Participation VALUES
  144. (1,2,'Leader'),
  145. (2,1,'Leader'),
  146. (3,1,'Guide'),
  147. (4,3,'Leader'),
  148. (5,5,'Climber'),
  149. (6,6,'Climber'),
  150. (7,9,'Leader'),
  151. (8,4,'Climber'),
  152. (9,2,'Climber'),
  153. (10,10,'Leader');
  154.  
  155. -- question 1
  156. select * from mountains;
  157.  
  158. -- question 2
  159.  
  160. select * from mountaineers where experience >20;
  161.  
  162. -- question 3
  163.  
  164. select * from mountains order by height desc;
  165.  
  166. -- question 4
  167. alter table teams rename to ClimbingTeams;
  168. select * from Climbingteams order by country;
  169.  
  170. -- question 5
  171.  
  172. select * from expeditions where year >1990;
  173.  
  174. -- question 6
  175.  
  176. select * from mountaineers where nationality='Italy';
  177.  
  178. -- question 7
  179.  
  180. select count(*) from mountains;
  181.  
  182. -- question 8
  183.  
  184. select * from sherpas;
  185.  
  186. -- question 9
  187.  
  188. alter table ClimbingTeams rename column nationality to name;
  189. select * from ClimbingTeams where name like '%Team%';
  190.  
  191. -- question 10
  192. select * from expeditions where mountain_id=1;
  193.  
  194. -- question 11
  195.  
  196. select * from mountaineers;
  197. update mountaineers set experience = 51 where id=1;
  198.  
  199. -- question 12
  200.  
  201. delete from expeditions where id=1;
  202. select * from exploits;
  203. select * from participation;
  204. alter table exploits drop constraint exploits_ibfk_2;
  205. alter table participation drop constraint participation_ibfk_2;
  206. alter table exploits add constraint exploits_ibfk_2 foreign key (expedition_id) references expeditions(id) on delete set null;
  207. alter table participation add constraint participation_ibfk_2 foreign key (expedition_id) references expeditions(id) on delete cascade;
  208.  
  209.  
  210. -- question 13
  211.  
  212. insert into mountains (name, height, country) values ('Toubkal',4167, 'Morocco'),(' ighil Mgoun', 4073, 'Morocco'), ('Mont blanc', 5520, 'Swiss');
  213.  
  214. -- question 14
  215.  
  216. update ClimbingTeams set country ='Morocco' where id=1;
  217.  
  218. -- question 15
  219.  
  220. delete from mountaineers where id=1;
  221.  
  222. alter table exploits drop constraint exploits_ibfk_1;
  223. alter table participation drop constraint participation_ibfk_1;
  224. alter table exploits add constraint exploits_ibfk_1 foreign key (mountaineer_id) references mountaineers(id) on delete set null;
  225. alter table participation add constraint participation_ibfk_1 foreign key (mountaineer_id) references mountaineers(id) on delete set null;
  226.  
  227.  
  228.  
  229.  
Success #stdin #stdout #stderr 0.01s 5292KB
stdin
Standard input is empty
stdout
1|Everest|8848|Nepal
2|K2|8611|Pakistan
3|Kangchenjunga|8586|Nepal/India
4|Lhotse|8516|Nepal
5|Makalu|8485|Nepal
6|ChoOyu|8188|Nepal
7|Dhaulagiri|8167|Nepal
8|Manaslu|8163|Nepal
9|NangaParbat|8126|Pakistan
10|Annapurna|8091|Nepal
1|Messner|Italy|50
2|Hillary|NZ|30
3|Tenzing|Nepal|25
4|Kukuczka|Poland|40
6|Steck|Swiss|25
8|Bonington|UK|35
9|Urubko|Kazakh|30
10|Hinkes|UK|28
1|Everest|8848|Nepal
2|K2|8611|Pakistan
3|Kangchenjunga|8586|Nepal/India
4|Lhotse|8516|Nepal
5|Makalu|8485|Nepal
6|ChoOyu|8188|Nepal
7|Dhaulagiri|8167|Nepal
8|Manaslu|8163|Nepal
9|NangaParbat|8126|Pakistan
10|Annapurna|8091|Nepal
6|French Team|France
7|German Team|Germany
1|Italian Team|Italy
10|Japan Team|Japan
3|Nepal Team|Nepal
4|Polish Team|Poland
8|Spanish Team|Spain
5|Swiss Team|Swiss
2|British Team|UK
9|US Team|USA
6|6|5|2000
8|8|7|1995
9|9|8|2005
10|10|9|2010
1|Messner|Italy|50
5|Moro|Italy|20
10
1|Kami Rita|Nepal
2|Apa|Nepal
3|Phurba|Nepal
4|Ang Rita|Nepal
5|Pasang|Nepal
6|Lakpa|Nepal
7|Mingma|Nepal
8|Dawa|Nepal
9|Pemba|Nepal
10|Dorje|Nepal
1|Italian Team|Italy
2|British Team|UK
3|Nepal Team|Nepal
4|Polish Team|Poland
5|Swiss Team|Swiss
6|French Team|France
7|German Team|Germany
8|Spanish Team|Spain
9|US Team|USA
10|Japan Team|Japan
1|1|3|1953
1|Messner|Italy|50
2|Hillary|NZ|30
3|Tenzing|Nepal|25
4|Kukuczka|Poland|40
5|Moro|Italy|20
6|Steck|Swiss|25
7|Purja|Nepal|15
8|Bonington|UK|35
9|Urubko|Kazakh|30
10|Hinkes|UK|28
1|2|1|1
2|3|1|1
3|1|2|1
4|4|3|1
5|5|5|1
6|6|6|1
7|7|9|1
8|8|4|1
9|9|2|1
10|10|10|1
1|2|Leader
2|1|Leader
3|1|Guide
4|3|Leader
5|5|Climber
6|6|Climber
7|9|Leader
8|4|Climber
9|2|Climber
10|10|Leader
stderr
Error: near line 3: near "DATABASE": syntax error
Error: near line 4: near "USE": syntax error
Error: near line 204: near "drop": syntax error
Error: near line 205: near "drop": syntax error
Error: near line 206: near "constraint": syntax error
Error: near line 207: near "constraint": syntax error
Error: near line 222: near "drop": syntax error
Error: near line 223: near "drop": syntax error
Error: near line 224: near "constraint": syntax error
Error: near line 225: near "constraint": syntax error