Advertisement
Guest User

Untitled

a guest
Feb 20th, 2019
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.26 KB | None | 0 0
  1. create table pessoa(
  2. id_pessoa integer not null,
  3. nome varchar not null,
  4. endereco varchar not null,
  5. primary key (id_pessoa)
  6. )
  7.  
  8. create table cliente(
  9. id_cliente integer not null,
  10. cpf varchar(11) not null,
  11. data_nasc timestamp not null,
  12. nome varchar not null,
  13. primary key(id_cliente),
  14. foreign key(id_Cliente) references pessoa(id_pessoa)
  15. );
  16.  
  17. create table venda(
  18. id_venda integer not null,
  19. data date not null,
  20. id_cliente integer not null,
  21. primary key(id_venda),
  22. foreign key(id_cliente) references pessoa(id_pessoa)
  23. );
  24.  
  25. create table item_venda(
  26. id_venda integer not null,
  27. id_estoque integer not null,
  28. primary key(id_venda,id_estoque),
  29. foreign key(id_venda)references venda(id_venda),
  30. foreign key(id_estoque)references produto_estoque(id_estoque)
  31. );
  32.  
  33. create table fornecedor(
  34. id_fornecedor integer not null,
  35. cnpj varchar(14) not null,
  36. primary key(id_fornecedor)
  37. );
  38.  
  39. create table produto(
  40. id_produto integer not null,
  41. descricao varchar not null,
  42. preco double precision not null,
  43. id_fornecedor integer not null,
  44. primary key(id_produto),
  45. foreign key(id_fornecedor) references fornecedor(id_fornecedor)
  46. );
  47. create table produto_estoque(
  48. id_estoque integer not null,
  49. id_produto integer not null,
  50. primary key(id_estoque),
  51. foreign key(id_produto) references produto(id_produto)
  52. );
  53.  
  54. insert into pessoa
  55. (id_pessoa, nome, endereco)
  56. values
  57. (1,'Luiz Fernando', 'Rua das flores'),
  58. (2,'Bruce Wayne1', 'Mansão Wayne'),
  59. (3,'Darth Vader', 'Força Negra'),
  60. (4, 'Yoda', 'Não ter'),
  61. (5, 'Obi wan Kenobi', 'Terra Venus'),
  62. (6, 'Barry Allen','Central city'),
  63. (7, 'Felicity Smoal', 'Star City'),
  64. (8,'Solomon Grundy','Cyrus Gold'),
  65. (9,'Clark Kent','Metropolis'),
  66. (10,'Ted Mosby','New York');
  67.  
  68. insert into cliente (
  69. id_cliente, cpf, data_nasc)
  70. values
  71. (1, 66666666666, '25/03/1989'),
  72. (3, 99999999999, '12/05/1910'),
  73. (6, 8888888888, '12/05/1990'),
  74. (7, 8888888888, '12/10/1990'),
  75. (9, 33333333333, '05/05/1950');
  76.  
  77. insert into fornecedor (
  78. id_fornecedor, cnpj)
  79. values
  80. (2, 66666666666),
  81. (4, 99999999999),
  82. (5, 8888888888),
  83. (8, 8888888888),
  84. (10, 33333333333);
  85.  
  86. insert into produto (
  87. id_produto, descricao, preco, id_fornecedor)
  88. values
  89. (1, 'ESPADA', 39.60, 2),
  90. (2, 'Falconete', 12.60, 4),
  91. (3, 'TANQEE', 5000.03, 5),
  92. (4, 'KATANA', 12.56, 8),
  93. (5,'Alabarda', 200.30, 10),
  94. (6, 'Cotó', 156.98, 2),
  95. (7, 'Mangual', 58.90, 4),
  96. (8, 'Hauberk', 88.65, 2),
  97. (9, 'Estrela da Manhã', 66.58, 10);
  98.  
  99. insert into venda (
  100. id_venda, id_cliente, data)
  101. values
  102. (1, 1, '15/05/2015'),
  103. (2, 3, '15/05/2015'),
  104. (3, 6, '15/05/2015'),
  105. (4, 7, '15/05/2015'),
  106. (5, 9, '15/05/2015'),
  107. (6, 1, '16/05/2015'),
  108. (7, 3, '17/05/2015'),
  109. (8, 6, '18/05/2015'),
  110. (9, 7, '19/05/2015'),
  111. (10, 9, '20/05/2015'),
  112. (11, 1, '18/05/2015'),
  113. (12, 3, '19/05/2015'),
  114. (13, 6, '15/06/2015'),
  115. (14, 7, '17/05/2015'),
  116. (15, 9, '18/05/2017'),
  117. (16, 1, '15/05/2017'),
  118. (17, 3, '15/05/2017'),
  119. (18, 6, '15/05/2017'),
  120. (19, 7, '15/05/2017'),
  121. (20, 9, '15/05/2017'),
  122. (21, 1, '16/05/2017'),
  123. (22, 3, '17/05/2017'),
  124. (23, 6, '18/05/2017'),
  125. (24, 7, '19/05/2017'),
  126. (25, 9, '20/05/2017'),
  127. (26, 1, '18/05/2017'),
  128. (27, 3, '19/05/2017'),
  129. (28, 6, '15/06/2017'),
  130. (29, 7, '17/05/2017'),
  131. (30, 9, '18/05/2017');
  132.  
  133. insert into produto_estoque (
  134. id_produto, id_estoque)
  135. values
  136. (1, 1),
  137. (2, 2),
  138. (3, 3),
  139. (4, 4),
  140. (5, 5),
  141. (6, 6),
  142. (7, 7),
  143. (8, 8),
  144. (9, 9),
  145. (1, 10),
  146. (2, 11),
  147. (3, 12),
  148. (4, 13),
  149. (5, 14),
  150. (6, 15),
  151. (7, 16),
  152. (8, 17),
  153. (9, 18),
  154. (1, 19),
  155. (2, 20),
  156. (3, 21),
  157. (4, 22),
  158. (5, 23),
  159. (6, 24),
  160. (7, 25),
  161. (8, 26),
  162. (9, 27),
  163. (1, 28),
  164. (2, 29),
  165. (3, 30),
  166. (4, 31),
  167. (5, 32),
  168. (6, 33),
  169. (7, 34),
  170. (8, 35);
  171.  
  172. insert into item_venda
  173. (id_venda, id_estoque)
  174. values
  175. (1,1),
  176. (2,2),
  177. (3,3),
  178. (4,4),
  179. (5,5),
  180. (6,6),
  181. (7,7),
  182. (8,8),
  183. (9,9),
  184. (1,10),
  185. (11,11),
  186. (11,12),
  187. (12,13),
  188. (13,14),
  189. (13,15),
  190. (15,16),
  191. (1,17),
  192. (9,18),
  193. (12,19),
  194. (12,20),
  195. (11,21),
  196. (1,22),
  197. (5,23),
  198. (6,24),
  199. (7,25),
  200. (10,26),
  201. (9,27),
  202. (12,28),
  203. (12,29),
  204. (13,30),
  205. (14,31),
  206. (15,32),
  207. (16,33),
  208. (17,34),
  209. (20,35);
  210.  
  211.  
  212. update cliente set nome='João Paulo da Silva Santos', cpf='95980001856' where id_cliente=1;
  213. update cliente set data_nasc='30/12/1998' where data_nasc='12/05/1910';
  214.  
  215. update venda set id_cliente=3 where id_Cliente=6;
  216. update venda set id_Cliente=6 where id_cliente=9;
  217. update venda set id_Cliente=9 where id_cliente=3;
  218.  
  219. update produto set preco=preco+preco*0.20 where id_produto=1;
  220. update produto set preco=preco-preco*0.30 where id_produto=3;
  221. update produto set preco=preco-preco*0.50 where id_fornecedor=8;
  222. update produto set preco=preco+preco*0.50 where id_fornecedor=4;
  223.  
  224. delete from venda where id_venda in (1,4,6,9,11,14,16,19,21,24,26,29,5,10,15,20,25,30,2,7,12,17,22,27,3,8,13,18,23,28);
  225.  
  226. select * from item_venda
  227.  
  228. delete from item_venda where id_estoque in (1,3,4,5,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,27,28,29,30,31,32);
  229.  
  230. select * from cliente
  231.  
  232. delete from cliente where id_cliente in(6,7,9)
  233.  
  234. delete from fornecedor where id_fornecedor in(2,4,6,8,10)
  235.  
  236. select * from fornecedor
  237.  
  238. select * from produto
  239.  
  240. delete from produto where id_produto in(5,6,8,9,1,3,4,2,7)
  241.  
  242. select * from produto_estoque
  243.  
  244. delete from produto_estoque where id_estoque in (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)
  245.  
  246. select * from pessoa
  247.  
  248. delete from pessoa where id_pessoa in (1,2,3,4,5,6,7,8,9,10)
  249.  
  250.  
  251.  
  252.  
  253.  
  254.  
  255.  
  256.  
  257.  
  258.  
  259.  
  260.  
  261.  
  262.  
  263.  
  264.  
  265. 2 PARTE DESSA PORRA
  266.  
  267.  
  268. 4)
  269. select * from pessoa;
  270.  
  271. select * from pessoa where nome ilike '%wa%';
  272.  
  273. select * from venda where data between '2017-02-05' and '3000-02-05';
  274.  
  275. select * from produto where preco between >= 4000;
  276.  
  277. 5)
  278. a)select f.cnpj from fornecedor as f
  279. order by f.cnpj;
  280.  
  281. b)select f.cnpj,p.nome from fornecedor as f
  282. inner join pessoa as p on f.id_fornecedor = p.id_pessoa
  283. order by f.cnpj, p.nome;
  284.  
  285. c)select c.nome, p.endereco from cliente as c
  286. inner join pessoa as p on c.id_cliente = p.id_pessoa
  287. group by c.nome, p.endereco;
  288.  
  289. d)select id_venda from venda
  290.  
  291. e)select c.nome,v.id_venda from cliente as c
  292. inner join venda as v on id_cliente = id_venda
  293. group by c.nome, v.id_venda;
  294.  
  295. f)select p.descricao from produto as p
  296.  
  297. g)select
  298.  
  299. h)select p.descricao from produto as p
  300. inner join produto_estoque as pe
  301. on id_produto = id_estoque
  302. inner join venda as v
  303. on id_estoque = id_venda
  304. group by p.descricao
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement