Advertisement
LaCaraDeLaVerga

El repaso de la verga segundo parcial

May 21st, 2018
158
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. select mnombre, dia
  2. from marineros  inner join reservas on marineros.mid=reservas.mid
  3. where dia>'1999-01-01';
  4.  
  5. ----------------------------------
  6. select count ( mnombre)
  7. from marineros  inner join reservas on marineros.mid=reservas.mid
  8. where mnombre>'popeye';
  9.  
  10. obtener los numeros de botes alquilados
  11. por el marino n 71
  12.  
  13. select *
  14. from reservas  
  15. where mid = '71'
  16.  
  17. select bid
  18. from reservas  
  19. where mid = '71';
  20.  
  21. obtener los nombres de los marinos que alquilaron
  22. el bote titanic
  23.  
  24.  
  25. select mnombre
  26. from barcos b , marineros m, reservas r
  27. where b.bid=r.bid and m.mid=r.mid and b.bnombre='Titanic';
  28.  
  29. los barcos que alquilo popeye
  30. select bnombre
  31. from barcos b , marineros m, reservas r
  32. where b.bid=r.bid and m.mid=r.mid and m.mnombre='Popeye';
  33.  
  34.  
  35. agregas un marinero en la tabla
  36. insert into marineros values (15,'jack sparrow'. 0 , 36);
  37. eliminas un marinero de la tabla
  38. delete from marineros where mnombre='jack sparrow';
  39. cambiar valores de una fila
  40. update marineros set mnombre='Marco Poli' where mnombre= 'Marco Polo'
  41.  
  42. create table alumnos (
  43. id int not null,
  44. nombre varchar(20) not null,
  45. apellido varchar(20) not null,
  46. edad int not null);
  47.  
  48. create table materias(
  49. id int not null,
  50. nombre varchar(20) not null
  51. );
  52.  
  53.  
  54. create table cursa (
  55. aid int not null,
  56. mid int not null
  57. );
  58.  
  59. alter table cursa add column finsc date;
  60.  
  61. alter table alumnos add primary key (id);
  62.  
  63. alter table materias add primary key (id);
  64.  
  65. alter table cursa add foreign key (aid) references alumnos(id);
  66.  
  67. alter table cursa add foreign key (mid) references materias(id);
  68.  
  69. insert into alumnos values (1, 'jorge','carvalho', 23);
  70.  
  71. insert into alumnos values (2,'lucas', 'vargas',27);
  72.  
  73. insert into alumnos values (3, 'gonzalo','cardoso',30);
  74.  
  75. insert into alumnos values (4 , 'gonzalo','pesado',22);
  76.  
  77. insert into alumnos values (5, 'maxi','correa',23);
  78.  
  79. insert into alumnos values (6, 'maxi', 'contetas',22);
  80.  
  81. insert into materias values (1,'base de datos');
  82.  
  83. insert into materias values (2,'programacion ');
  84. update materias set nombre 'programacion' where id=2;
  85.  
  86.  
  87. insert into materias values (3,'organizacion');
  88.  
  89. insert into materias values (4 ,'matematica');
  90.  
  91. insert into materias values (5 ,'psec');
  92.  
  93. insert into cursa values (1,1,'2018-03-04');
  94.  
  95. insert into cursa values (2,5,'2017-03-04');
  96.  
  97. insert into cursa values (3,4,'2017-06-01');
  98.  
  99. insert into cursa values (1,5,'2016-03-05');   
  100.  
  101. insert into cursa values (1,2,'2018-03-10');
  102.  
  103. insert into cursa values (5,3,'2018-03-04');
  104.  
  105.  
  106.  
  107. alumnos que hacen programacion
  108.  
  109. select a.nombre , m.nombre
  110. from alumnos a , materias m , cursa c
  111. where c.aid=a.id and c.mid=m.id and m.nombre='programacion';
  112.  
  113. cantidad de alumnos que cursan psec
  114.  
  115. select count(a.nombre)
  116. from alumnos a , materias m , cursa c
  117. where c.aid=a.id and c.mid=m.id and m.nombre='psec';
  118.  
  119.  
  120. alumnos que se inscribieron en 2018
  121.  
  122. select dinstinct(nombre) ,apellido, finsc
  123. from alumnos a inner join  cursa c on a.id=c.aid
  124. where finsc >= '2018-01-01';
  125.  
  126. insert into alumnos values (7,'fernando','torres',50);
  127. insert into alumnos values (8,'sergio','esquivel',13);
  128.  
  129. alumnos que no cursan materias
  130.  
  131. select nombre ,  apellido
  132. from alumnos a
  133. where a.id  not in(select distinct(id)
  134. from alumnos a ,cursa c
  135. where a.id=c.aid  );
  136.  
  137. obtener alumno mas viejo edad
  138.  
  139. select max (alumnos.edad)
  140. from alumnos;
  141.  
  142.  
  143. obtener alumno mas viejo nombre
  144. select a.nombre
  145. from alumnos a
  146. where a.edad = (select max(edad) from alumnos);
  147.  
  148.  
  149. obtener al segundo mas viejo
  150.  
  151. select max (alumnos.edad)
  152. from alumnos
  153. where edad not in  (select max(edad)from alumnos);
  154.  
  155. select nombre
  156. from alumnos
  157. where edad=(select max (alumnos.edad)
  158. from alumnos
  159. where edad not in  (select max(edad)from alumnos)
  160. );
  161.  
  162.  
  163.  
  164.  
  165.  
  166.  
  167.  
  168. -----------------------------------------------------
  169. store procedures
  170.  
  171. * logica en la base de datos
  172. * funciones que s ejecutan en el server
  173. * PL/pgSQL
  174.  
  175.  
  176.     Estructura
  177.  
  178.  
  179. hello wolrd
  180. create function hello () returns void as$$
  181. begin
  182.     raise notice 'hello world!'
  183. end;
  184. $$ language plpgsql;
  185. -------------------------------
  186. suma (a,b)
  187. create or replace function suma(a int, b int) returns int as $$
  188. declare
  189.     c int
  190. begin
  191.     c:= a+b;
  192.     return c;
  193. end;
  194. $$ langiage plpgsql;    
  195. ----------------------------------------------
  196. 1. Escribir una función que reciba un número de legajo y que
  197. devuelva el nombre del alumno.
  198.  
  199.  create or replace function damenombre(legajo int) returns text as $$
  200.  declare
  201.     resultado text;
  202.  begin
  203.   select a.nombre into resultado from alumnos a where a.id = legajo;
  204.     return resultado;
  205.  end;
  206.  $$language plpgsql;   
  207.  
  208. ------------------------------------------------------------
  209. hacer un stored procedured que imprima un mensaje en
  210. "se ha agregado un nuevo registro".
  211.  
  212.  
  213. create or replace function mensaje() returns void as $$
  214.  
  215. begin
  216.  raise notice 'se ha agregado un nuevo registro'
  217. end;
  218. $$langiage plpgsql;
  219.  
  220.  
  221. ////////////////////////////////////////////////////////
  222.  
  223. TRIGGERS
  224.  
  225. trabaja con restricciones
  226.  
  227. *Eventos
  228.     insert on nombre de la tabla
  229.     delete on nombre de la tabla
  230.     update on nombre de la tabla
  231. *Activacion
  232.     before | after | instead of Evento
  233.  
  234.  *Row Level
  235.     for each Row
  236.  
  237.  *statement-level
  238.     for each statement
  239.  
  240. pasos  a seguir
  241.     1/tener una funcion que implemente el comportamiento.
  242.     2/definir un trigger que use la funcion anterior.
  243.  
  244. el trigger se asocia a una tabla para borrar poner
  245.  drop trigger on nombre de la tabla;
  246. las funciones que devuelen trigger no pueden recibir parametros.
  247.  
  248. hacer un trigger que implemente la funcion mensaje cuando
  249. se agregue una nueva materia en la base de datos escuela.
  250.  
  251.  
  252.  
  253.  
  254.     create trigger msjaddmateria
  255.     after insert on materias
  256.     for each Row
  257.     execute procedure addMateria();
  258.  
  259.     create function addMateria () returns trigger as $$
  260.     begin
  261.     raise notice 'Se ha agregado una nueva materia';
  262.     return new;
  263.     end;
  264.  
  265.     $$language plpgsql;
  266.  
  267.     create trigger msjdelmateria
  268.     after delete on materias
  269.     for each Row
  270.     execute procedure delMateria();
  271.  
  272.     create function delMateria () returns trigger as $$
  273.     begin
  274.     raise notice 'Se ha eliminado una materia';
  275.     return new;
  276.     end;
  277.  
  278.     $$language plpgsql;
  279. # trigger que no te notifique cuando se borre una batera
  280. #
  281. psql -U postgres -h localhost
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement