Advertisement
LeoMonte

b

Feb 5th, 2018
745
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MySQL 64.73 KB | None | 0 0
  1. CREATE SCHEMA IchibaSuperMarket;
  2. USE IchibaSuperMarket;
  3.  
  4. SET foreign_key_checks = 0;
  5.  
  6.  
  7. -- TABELA DE CURSOS
  8.  
  9. CREATE TABLE curso (
  10.  
  11.     id char(4),
  12.     cargahoraria integer(4) not null CHECK ( cargahoraria > 0 ),
  13.     descricao varchar(40),
  14.     primary key(id)
  15. );
  16.  
  17. -- inserindo valores para curso
  18.  
  19. INSERT INTO curso VALUES
  20.  
  21. ( '7070', 12 , 'entregas'),
  22. ( '1010' , 20, 'libras'),
  23. ( '2276' , 5 , 'marketing'),
  24. ( '9085' , 13 , 'informatica'),
  25. ( '7324' , 7 , 'financas'),
  26. ( '8012'  , 60 , 'ingles'),
  27. ( '3390', 30 , 'relacoes humanas'),
  28. ( '8344' , 30 , 'estatistica'),
  29. ( '5793' , 40 , 'circuitos'),
  30. ( '3333' , 20 , 'ginastica laboral'),
  31. ( '5449', 40 , 'mecanica'),
  32. ( '7573' , 20, 'administracao'),
  33. ( '9090' , 40 , 'programacao'),
  34. ( '2101' , 40 , 'espanhol');
  35.  
  36. -- TABELA DE JORNADA DE TRABALHO
  37.  
  38. CREATE TABLE jornadatrabalho (
  39.    
  40.     id char(4) ,
  41.     descricao varchar(60),
  42.     trabalha_sabado boolean not null default true,
  43.     primary key(id)
  44.  
  45. );
  46.  
  47. -- inserindo valores na tabela jornadatrabalho
  48.  
  49. INSERT INTO jornadatrabalho ( id , descricao , trabalha_sabado) VALUES
  50.  
  51. ('0001' , 'segunda a domingo' , true),
  52. ('0002' , NULL , false),
  53. ('0003' , 'trabalha feriados' , true),
  54. ('0004' , ' ' , true ),
  55. ('0005' , ' ' , false),
  56. ('0006' , ' ', false),
  57. ('0007' , 'trabalha feriados' , true),
  58. ('0008' , ' ' , true),
  59. ('0009' , ' ' , true),
  60. ('0010' , ' ', true ),
  61. ('0011' , ' ', true),
  62. ('0012', ' ', true),
  63. ('0013' , ' ' , true),
  64. ('0014', ' ' , true),
  65. ('0015' , ' ', true ),
  66. ('0016'  , ' ' , false);
  67.  
  68.  
  69. -- TABELA DE TURNO
  70.  
  71. CREATE TABLE turno (
  72.  
  73.     cod char(4),
  74.     descricao varchar(60),
  75.     hora_inicio time not null,
  76.     hora_fim time not null,
  77.     primary key (cod)
  78.  
  79. );
  80.  
  81. -- inserindo valores na tabela de turno
  82.  
  83. INSERT INTO turno ( cod, descricao , hora_inicio, hora_fim ) VALUES
  84.  
  85. ('0001' , ' ' , '07:30:00' , '16:30:00'),
  86. ('0002' , ' ', '08:40:00' , '17:40:00'),
  87. ('0003' , ' ' , '07:00:00' , '14:00:00'),
  88. ('0004' , ' ' , '11:00:00' , '17:00:00'),
  89. ('0005' , ' ' , '14:00:00' , '18:30:00'),
  90. ('0006' , ' ' , '13:35:00' , '19:45:00'),
  91. ('0007' , ' ' , '07:30:00' , '12:30:00'),
  92. ('0008' , ' ' , '07:10:00' , '13:30:00'),
  93. ('0009' , ' ' , '18:00:00' , '23:55:00'),
  94. ('0010' , ' ' , '00:00:01' , '08:00:01');
  95.  
  96. -- TABELA DE DIA
  97.  
  98. CREATE TABLE dia (
  99.  
  100.     sequencial char(4),
  101.     descricao varchar(60),
  102.     primary key (sequencial)
  103.    
  104. );
  105.  
  106. -- inserindo valores na tabela de dia
  107.  
  108. INSERT INTO dia ( sequencial , descricao ) VALUES
  109.  
  110. ('0001' , ' segunda-feira' ),
  111. ('0002' , ' terça-feira' ),
  112. ('0003' , ' quarta-feira' ),
  113. ('0004' , ' quinta-feira' ),
  114. ('0005' , ' sexta-feira' ),
  115. ('0006' , ' sabado' ),
  116. ('0007' , ' domingo' );
  117.  
  118. -- TABELA DA MATRIZ
  119.  
  120. CREATE TABLE matriz (
  121.    
  122.     CNPJ char(14),
  123.     nomefantasia varchar(10) not null,
  124.     primary key (CNPJ)
  125.  
  126. );
  127.  
  128. -- inserindo valores na tabela Matriz
  129.  
  130. INSERT INTO matriz ( CNPJ , nomefantasia ) VALUES
  131.  
  132. ( '23416393000114' , 'ICBSP'),
  133. ( '23416393000140' , 'ICBPE'),
  134. ( '23416393000169' , 'ICBRJ');
  135.  
  136.  
  137. -- TABELA DE TELEFONE DA MATRIZ
  138.  
  139.  
  140. CREATE TABLE telefone_matriz (
  141.      
  142.     CNPJ char(14),
  143.     telefone char(10),
  144.     CONSTRAINT telefone_matriz_pk  primary key (CNPJ, telefone),
  145.     CONSTRAINT fk_cnpjmatriz foreign key (CNPJ) references matriz (CNPJ) ON DELETE CASCADE ON UPDATE CASCADE
  146.  
  147. );
  148.  
  149. -- inserindo valores na tabela telefone_matriz
  150.  
  151. INSERT INTO telefone_matriz (CNPJ , telefone ) VALUES
  152.  
  153. ('23416393000114' , '8133002432'),
  154. ('23416393000140' , '8133002149'),
  155. ('23416393000140' , '1133002932'),
  156. ('23416393000140' , '1140443949'),
  157. ('23416393000169' , '1433004732'),
  158. ('23416393000169' , '1440002449');
  159.  
  160. -- TABELA DE FILIAL
  161.  
  162. CREATE TABLE filial (
  163.  
  164.     seq char(4),
  165.     CNPJ_Matriz char(14) not null,
  166.     CPF_gerente char(11) ,
  167.     endereco varchar(50),
  168.     qtd_func int(4) CHECK (qtd_func >= 0),
  169.     CONSTRAINT filial_pk primary key ( seq , CNPJ_Matriz ),
  170.     CONSTRAINT fk_cnpjmatrizfilial foreign key ( CNPJ_MATRIZ ) references matriz (CNPJ) ON DELETE CASCADE ON UPDATE CASCADE
  171.    
  172. );
  173.  
  174.  
  175. -- inserindo valores na tabela filial
  176.  
  177. INSERT INTO filial ( seq , CNPJ_Matriz , CPF_gerente , endereco , qtd_func ) VALUES
  178.  
  179. ( '0001' ,  '23416393000114' , NULL , 'Rua valtavares ' , 4 ),
  180. ( '0002' ,  '23416393000114' , NULL , 'Rua alivetania ' , 4 ),
  181. ( '0003' ,  '23416393000140' , NULL, 'Rua maranguape ' , 4 ),
  182. ( '0004' ,  '23416393000169' , NULL , 'Rua fernigan ' , 4 );
  183.  
  184. -- TABELA DE TELEFONE DA FILIAL
  185.  
  186. CREATE TABLE telefone_filial (
  187.  
  188.     seq_filial char(4),
  189.     CNPJ_Matriz char(14),
  190.     telefone char(10),
  191.     CONSTRAINT telefone_filial_pk primary key (seq_filial , CNPJ_Matriz , telefone ),
  192.     CONSTRAINT fk_cnpj foreign key (seq_filial, CNPJ_Matriz) references filial (seq , CNPJ_Matriz) ON DELETE CASCADE ON UPDATE CASCADE
  193.  
  194. );
  195.  
  196. -- inserindo valores na tabela telefone_filial
  197.  
  198. INSERT INTO telefone_filial( seq_filial , CNPJ_Matriz , telefone ) VALUES
  199.  
  200. ( '0001' ,  '23416393000114' , '1133115021' ),
  201. ( '0002' ,  '23416393000114' , '1133505231' ),
  202. ( '0003' ,  '23416393000140' , '8143022151' ),
  203. ( '0004' ,  '23416393000169' , '1431205412' );
  204.  
  205.  
  206. -- TABELA DE FUNCIONARIOS
  207.  
  208.  
  209. CREATE TABLE funcionario (
  210.  
  211.     CPF char(11) ,
  212.     id_jornada char(4) ,
  213.     seq_filial char(4),
  214.     cnpj_matriz char(14),
  215.     data_admissao date not null,
  216.     sex enum ('M', 'F'),
  217.     estado_civil varchar(10),
  218.     login varchar(60) default 'func' ,
  219.     senha varchar(15) default 'func',
  220.     RG char(7) not null UNIQUE,
  221.     nome varchar(45),
  222.     situacao varchar(10),
  223.     endereco varchar(45),
  224.     primary key(CPF),
  225.     foreign key (id_jornada) references jornadatrabalho (id) ,
  226.     foreign key ( seq_filial ) references filial (seq),
  227.     foreign key (cnpj_matriz) references matriz (CNPJ)
  228.  
  229. );
  230.  
  231.  
  232. -- inserindo valores para funcionario
  233.  
  234. INSERT INTO funcionario (CPF, id_jornada, seq_filial, cnpj_matriz, data_admissao, sex, estado_civil, login, senha, RG, nome, situacao, endereco) VALUES
  235.  
  236. ('77491222226' , '0001', '0001' ,  '23416393000114' , '2005-04-12' , 'F', 'solteiro' , 'helo.12' , '123' ,  '1259312' , 'Heloisa Macedo de Souza' , 'ativo' , 'Rua Eloy Monteiro Nunes'),
  237. ('98243208909' , '0002' , '0001' ,'23416393000114' , '2007-07-25' , 'M' , 'casado' , 'Garza2019' , 'E3221' ,'3654296' , 'Elias ChateauBriand Gomes' , 'inativo' , 'Avenida Paraíba'),
  238. ('57325297050' , '0003' , '0003' , '23416393000140', '2003-09-02', 'F', 'viuva' , 'Riggs642' , 'YM' ,       '4563573' , 'Maria Helena Rosendo' , 'ativo', 'Rua Pedro Viana Neto'),
  239. ('16565525749' , '0004' , '0002' , '23416393000114' , '2000-01-07', 'F', 'solteiro' , 'Eugene759' , 'XX133','2144770', 'Afrodite Bezerra das Flores' , 'ativo', 'Rua Amelia'),
  240. ('57859332507', '0005' , '0002' ,  '23416393000114' , '2006-02-02' , 'M' , 'solteiro' , 'TM1' , '98UJ' ,    '1555582' , 'Mauricio de Souza Carvalho' , 'ativo' , ' Rua da Concordia'),
  241. ('96202875763' , '0006' , '0003' , '23416393000140' , '2001-09-11' , 'F' , 'solteiro' ,'Juli.Alves', '8900','3494135' , 'Juliana Macedo Pinheiro' , 'inativo' , 'Rua Tucano'),
  242. ('02123011878' , '0007' , '0004' , '23416393000169' , '2000-08-10' , 'M' , 'solteiro' , 'ana.mari' , 'bb34','1783833' , 'Mariana Siqueira Jardim' , 'ativo' , 'Rua Lealberto Leal'),
  243. ('15141182894' , '0008' , '0001' , '23416393000114' , '2005-06-13' , 'F' , 'casado' , 'dudu.arda' , '1999' ,'2267700', 'Bernadete Maria da Silva' , 'ativo' , 'Rua Guajuvira'),
  244. ('33666472214' , '0009' , '0004' , '23416393000169' , '2011-03-01' , 'F' , 'solteiro', 'ana.belle' , 'AX6', '4137799','Anabelle Cristina Leal de Figueiredo' , 'ativo', 'Rua Manuel de Medeiros'),
  245. ('32568071001', '0010' , '0001' ,  '23416393000114' , '2010-09-26' , 'F' , 'casado' , 'bee.a' , '134N' ,    '4163131', 'Ana Beatriz Castanho Guedes' , 'ativo', 'Rua Jornalista Benedito Cunha'),
  246. ('45321186898' , '0011', '0004',   '23416393000169' , '2007-09-20', 'M' , 'solteiro' , 'alan.marq' , '0j7e','1557345','Allan Jose Malta de Souza' , 'inativo', ' Rua Projetada'),
  247. ('86147207504' , '0012' , '0004' , '23416393000169' , '2001-10-27' , 'F' , 'casado' , 'lelezinha' , '223d' ,'3217467', 'Leticia Santana Rodrigues' , 'ativo' , 'Rua Felipe Guerra'),
  248. ('85902755239' , '0013' , '0003' , '23416393000140' , '2005-11-14' , 'F' , 'solteiro' , 'mwd2', '1332',     '2437550', 'Gabriela Amado Batista', 'ativo', 'Rua da Palma'),
  249. ('14073416260' , '0014' , '0002' , '23416393000114', '2000-02-14', 'M' , 'casado', 'mumu321', '9901',       '1190089','Jadiane Matoso dos Santos', 'ativo', 'Rua Real da Torre'),
  250. ('32970753502', '0015' , '0002' ,  '23416393000114' , '2013-01-10', 'F' , 'solteiro' , 'JH10' , '1132' ,    '2422874', 'Viviane Mendonca do Nascimento', 'ativo', 'Rua da Harmonia'),
  251. ('88356795591' , '0016' , '0003' , '23416393000140' , '2012-06-10', 'M' , 'casado', '991jj', '1233',        '1316411', 'Ronaldo Fagundes da Silva' , 'ativo', 'Rua da Praia');
  252.  
  253.  
  254. -- TABELA DE ESTOQUISTA HERDA FUNCIONARIO
  255.  
  256.  
  257. CREATE TABLE estoquista (
  258.    
  259.     CPF char(11),
  260.     primary key (CPF),
  261.     CONSTRAINT fk_estoq foreign key (CPF) references funcionario (CPF) ON DELETE CASCADE ON UPDATE CASCADE
  262.  
  263. );
  264.  
  265.  -- inserindo valores para estoquista
  266.  
  267. INSERT INTO estoquista (CPF) VALUES
  268.  
  269. ('77491222226'),
  270. ('98243208909'),
  271. ('57859332507'),
  272. ('16565525749');
  273.  
  274. -- TABELA DE DBA HERDA FUNCIONARIO
  275.  
  276. CREATE TABLE DBA (
  277.    
  278.     CPF char(11),
  279.     primary key (CPF),
  280.     CONSTRAINT fk_dba foreign key(CPF) references funcionario(CPF) ON DELETE CASCADE ON UPDATE CASCADE
  281.  
  282. );
  283.  -- inserindo valores para dba
  284.  
  285. INSERT INTO DBA (CPF) VALUES
  286.  
  287. ('45321186898');
  288.  
  289. -- TABELA DE GERENTE HERDA FUNCIONARIO
  290.  
  291. CREATE TABLE gerente(
  292.    
  293.     CPF char(11),
  294.     primary key (CPF),
  295.     CONSTRAINT fk_gerente foreign key(CPF) references funcionario(CPF) ON DELETE CASCADE ON UPDATE CASCADE
  296.  
  297. );
  298.  
  299. -- inserindo valores para gerente
  300.  
  301. INSERT INTO gerente (CPF) VALUES
  302.  
  303. ('96202875763'),
  304. ('02123011878'),
  305. ('15141182894'),
  306. ('57325297050');
  307.  
  308. -- TABELA DE ENTREGADOR HERDA FUNCIONARIO
  309.  
  310. CREATE TABLE entregador(
  311.    
  312.     CPF char(11),
  313.     primary key (CPF),
  314.     CONSTRAINT fk_entregador foreign key(CPF) references funcionario(CPF) ON DELETE CASCADE ON UPDATE CASCADE
  315.  
  316. );
  317.  
  318. -- inserindo valores na tabela entregador
  319.  
  320. INSERT INTO entregador (CPF) VALUES
  321.  
  322. ( '33666472214'),
  323. ('86147207504'),
  324. ('32970753502'),
  325. ('32568071001');
  326.  
  327. -- TABELA DE SUPERVISOR ESTOQUE HERDA FUNCIONARIO
  328.  
  329. CREATE TABLE supervisorestoque (
  330.    
  331.     CPF char(11),
  332.     primary key(CPF),
  333.     CONSTRAINT fk_super foreign key(CPF) references funcionario(CPF) ON DELETE CASCADE ON UPDATE CASCADE
  334.  
  335. );
  336.  
  337. -- inserindo valotes na tabela supervisorestoque
  338.  
  339. INSERT INTO supervisorestoque (CPF) VALUES
  340.  
  341. ('14073416260'),
  342. ('85902755239' ),
  343. ('88356795591');
  344.  
  345.  
  346. -- TABELA MULTVALORADA DE FUNCIONARIO
  347.  
  348.  
  349. CREATE TABLE telefone_funcionario(
  350.    
  351.     CPF char(11),
  352.     telefone char(11),
  353.     primary key (CPF, telefone),
  354.     CONSTRAINT fk_telefonefuncinario foreign key(CPF) references funcionario(CPF) ON DELETE CASCADE ON UPDATE CASCADE
  355.  
  356. );
  357.  
  358. -- inserindo valores na tabela de telefone_funcionario
  359.  
  360. INSERT INTO telefone_funcionario ( CPF, telefone ) VALUES
  361.  
  362. ('14073416260' , '08133552321'),
  363. ('85902755239' , '08132324567'),
  364. ('88356795591', '08131311111'),
  365. ('32568071001', '08134587831'),
  366. ('32970753502', '08132324502'),
  367. ('32970753502' , '08191912343'),
  368. ('33666472214' , '08199096532'),
  369. ('86147207504', '08132732100'),
  370. ('96202875763' , '08199690359');
  371.  
  372.  
  373. -- alterando a tabela filial
  374.  
  375. ALTER TABLE filial add constraint foreign key ( CPF_gerente ) references gerente (CPF);
  376.  
  377.  
  378. -- TABELA DE NOTIFICAÇÃO DADA A FUNCIONARIO
  379.  
  380. CREATE TABLE notificacao (
  381.    
  382.     id char(4),
  383.     cpf_fun char(11),
  384.     dia date not null,
  385.     descricao varchar(80),
  386.     primary key (id),
  387.     foreign key (Cpf_fun) references funcionario (CPF)
  388.    
  389. );
  390.  
  391. -- inserindo valores na tabela de notificacao
  392.  
  393. INSERT INTO notificacao (id, cpf_fun, dia, descricao) VALUES
  394.  
  395. ('2940' , '33666472214' , '2014-02-26' , NULL),
  396. ('2941' , '86147207504' , '2015-03-06' , NULL),
  397. ('2942' , '32970753502' , '2015-06-02' , NULL),
  398. ('2943' , '33666472214' , '2014-07-20' , NULL),  
  399. ('2944' , '33666472214' , '2014-07-20' , NULL),
  400. ('2945' , '16565525749' , '2014-02-12' , NULL),
  401. ('2946' , '33666472214' , '2015-03-06' , NULL),
  402. ('2947' , '33666472214' , '2015-06-02' , NULL),
  403. ('2948' , '77491222226' , '2014-07-20' , NULL),
  404. ('2949' , '88356795591' , '2014-07-20' , NULL),
  405. ('2950' , '45321186898' , '2014-07-20' , NULL),
  406. ('2951' , '86147207504' , '2014-02-10' , NULL);
  407.  
  408. -- TABELA DE NOTIFICAÇÃO DE MULTA DADA A FUNCIONARIO HERDA DE NOTIFICAÇÃO
  409.  
  410. CREATE TABLE notif_multa (
  411.  
  412.     id char(4),
  413.     pontos_cnh int,
  414.     valor float not null,
  415.     cep char(8),
  416.     complemento varchar(30),
  417.     primary key(id),
  418.     CONSTRAINT fk_notmulta foreign key (id) references notificacao(id) ON DELETE CASCADE ON UPDATE CASCADE
  419.    
  420. );
  421.  
  422. -- inserindo dados para notif_multa
  423.  
  424. INSERT INTO notif_multa (id , pontos_cnh, valor, cep, complemento) VALUES
  425.  
  426. ('2940',  3 , 58.50 , 50789123 , 'Rua da Aurora'),
  427. ('2941' ,4 , 198.50 , 54330315 , 'Rua Itacuruba'),
  428. ('2942' , 5 , 398.50 , 54315330 , 'Rua verdejantes');
  429.  
  430. -- TABELA DE NOTIFICAÇÃO ADVERTENCIA DADA A FUNCIONARIO HERDA DE NOTIFICAÇÃO
  431.  
  432. CREATE TABLE notif_advertencia (
  433.  
  434.     id char(4),
  435.     descricao varchar(20),
  436.     primary key(id),
  437.     CONSTRAINT fk_notadvert foreign key (id) references notificacao (id) ON DELETE CASCADE ON UPDATE CASCADE
  438. );
  439.  
  440. -- inserindo dados para notif_advertencia
  441.  
  442. INSERT INTO notif_advertencia (id, descricao ) VALUES
  443.  
  444. ('2943' , NULL),
  445. ('2944' , NULL),
  446. ('2945' , NULL);
  447.  
  448. -- TABELA DE NOTIFICAÇÃO DE SUSPENSAO DADA A FUNCIONARIO HERDA DE NOTIFICAÇÃO
  449.  
  450. CREATE TABLE notif_suspensao (
  451.    
  452.     id char(4),
  453.     data_inicio date not null,
  454.     data_termino date not null,
  455.     primary key(id),
  456.     CONSTRAINT not_suspen foreign key (id) references notificacao(id) ON DELETE CASCADE ON UPDATE CASCADE
  457. );
  458.  
  459. -- inseindo dados para notif_suspensao
  460.  
  461. INSERT INTO notif_suspensao ( id , data_inicio , data_termino ) VALUES
  462.  
  463. ('2946' , '2015-06-02' , '2015-06-04' ),
  464. ('2947' , '2015-08-20' , '2015-08-23'  ),
  465. ('2948' , '2015-09-10' , '2015-09-13'  );
  466.  
  467.  
  468. -- TABELA DE NOTIFICAÇÃO DE FALTA DADA A FUNCIONARIO HERDA DE NOTIFICAÇÃO
  469.  
  470.  
  471. CREATE TABLE notif_falta (
  472.    
  473.     id char(4),
  474.     dia date,
  475.     duracao int CHECK (duracao > 0 ),
  476.     primary key (id),
  477.     CONSTRAINT fk_notfalta foreign key (id) references notificacao (id) ON DELETE CASCADE ON UPDATE CASCADE
  478. );
  479.  
  480.  -- inserindo valores para notif_falta
  481.  
  482. INSERT INTO notif_falta ( id , dia , duracao ) VALUES
  483.  
  484. ('2949' , '2015-06-01' , 3),
  485. ('2950' , '2015-08-19' , 2 ),
  486. ('2951' , '2015-09-09' , 2);
  487.  
  488. -- TABELA DE FERIAS DE FUNCIONARIO
  489.  
  490. CREATE TABLE ferias (
  491.    
  492.     id char(4),
  493.     cpf_fun char(11),
  494.     numero_dias int CHECK ( numero_dias > 0 ),
  495.     seq char(4) not null,
  496.     data_inicio date not null,
  497.     data_fim date not null,
  498.     primary key (id),
  499.     foreign key (cpf_fun) references funcionario (CPF)
  500.    
  501. );
  502.  
  503. -- insenrindo valores na tabela ferias
  504.  
  505. INSERT INTO ferias (id , cpf_fun , numero_dias , seq , data_inicio , data_fim) VALUES
  506.  
  507. ( '2340' , '96202875763' , 20 , '0001' , '2016-06-02' , '2016-06-22' ),
  508. ( '2341' , '32970753502' , 20 , '0002' , '2016-06-02' , '2016-06-22' ),
  509. ( '2342' , '45321186898' , 20 , '0003' , '2016-01-02' , '2016-01-22' ),
  510. ( '2343' , '57859332507' , 20 , '0004' , '2016-01-02' , '2016-01-22' ),
  511. ( '2344' , '14073416260' , 20 , '0005' , '2016-04-02' , '2016-04-22' );
  512.  
  513.  
  514. -- TABELA DE CONTRA CHEQUE
  515.  
  516. CREATE TABLE contracheque (
  517.    
  518.     mes_referencia char(2) CHECK( mes_referencia > 0 AND mes_referencia < 13 ),
  519.     CPF char(11),
  520.     data_pagamento date,
  521.     valor_bruto float(5) not null CHECK( valor_bruto >= 0 ),
  522.     valor_liqd float(5) not null CHECK( valor_liqd >= 0),
  523.     valor_desconto float(5) CHECK ( valor_desconto >= 0 ),
  524.     primary key (mes_referencia , CPF),
  525.     foreign key(CPF) references funcionario(CPF) ON DELETE CASCADE ON UPDATE CASCADE
  526.  
  527. );
  528.  
  529.  
  530. -- inserindo valores na tabela contracheque
  531.  
  532. INSERT INTO contracheque (mes_referencia , CPF, data_pagamento , valor_bruto, valor_liqd, valor_desconto) VALUES
  533.  
  534. ('02', '96202875763' , '2017-02-15', 3250.30, 3237.68 , 12.32),
  535. ('03' , '96202875763', '2017-03-15' , 3250.30,  3237.68, 12.32),
  536. ('05' , '45321186898' , '2017-05-15' , 8720.55,8708.8, 11.75),
  537. ('01' , '57325297050' , '2017-01-15' , 5956.00, 5942.00 , 14.00),
  538. ('03' , '85902755239' ,'2017-03-15', 4567.32 , 4553.32 , 14.00),
  539. ('08' , '85902755239' , '2017-08-15',4567.32 , 4553.32 , 14.00),
  540. ('01', '96202875763' , '2017-01-15', 3250.30, 3237.68 , 12.32),
  541. ('11' , '45321186898' , '2017-11-15' , 8720.55,8708.8, 11.75),
  542. ('07' , '45321186898' , '2017-07-15' , 8720.55,8708.8, 11.75),
  543. ('04' , '45321186898' , '2017-04-15' , 8720.55,8708.8, 11.75),
  544. ('04' , '02123011878' , '2017-04-15' ,4567.32 , 4553.32, 14.00),
  545. ('05' ,'86147207504', '2016-05-16' , 3250.30, 3237.68 , 12.32);
  546.  
  547.  
  548. -- TABELA DE ENTREGA
  549.  
  550. CREATE TABLE entrega (
  551.    
  552.     seq char(4) ,
  553.     CPF_entregador char(11),
  554.     data_entrega date ,
  555.     hora_estimada time,
  556.     primary key (seq),
  557.     foreign key(CPF_entregador) references entregador(CPF)
  558.  
  559. );
  560.  
  561.  -- inserindo valores na tabela de entrega
  562.  
  563. INSERT INTO entrega ( seq , CPF_entregador , data_entrega , hora_estimada ) VALUES
  564.  
  565. ('0001' , '33666472214' , '2016-04-09 ' , '07:30:00'),
  566. ('0002' , '33666472214' , '2016-04-09 ' , '19:45:00'),
  567. ('0003' , '33666472214' , '2016-04-09 ' , '18:00:00'),
  568. ('0004' , '33666472214' , '2016-04-09 ' , '09:30:00'),
  569. ('0005' , '33666472214' , '2016-04-09 ' , '13:30:00'),
  570. ('0006' , '86147207504' , '2016-04-02 ' , '06:30:00'),
  571. ('0007' , '86147207504' , '2016-04-02 ' , '11:45:00'),
  572. ('0008' , '86147207504' , '2016-04-02 ' , '14:00:00'),
  573. ('0009' , '86147207504' , '2016-04-02 ' , '015:30:00'),
  574. ('0010' , '86147207504' , '2016-04-02 ' , '14:30:00');
  575.  
  576.  
  577.  
  578.  
  579.  
  580.  
  581. -- TABELA DE COMPRA
  582.  
  583. CREATE TABLE compra (
  584.  
  585.     codd char(4),
  586.     seq_entrega char(4),
  587.     cpfcliente char(11),
  588.     valor_total float not null CHECK( valor_total > 0 ),
  589.     dia date not null,
  590.     valor_total_desconto float CHECK (valor_total_desconto >= 0 ),
  591.     statos boolean default true,
  592.     primary key (codd),
  593.     foreign key (cpfcliente) references cliente (CPF),
  594.     foreign key (seq_entrega) references entrega ( seq )
  595. );
  596.  
  597. -- inserindo valores na tabela de compra
  598.  
  599. INSERT INTO compra ( codd , seq_entrega , cpfcliente , valor_total , dia , valor_total_desconto , statos ) VALUES
  600.  
  601. ('0001' , '0001 ' , '54501233290' , 693 , '2016-04-09 ' , 0 , true ),
  602. ('0002' , '0002 ' , '25251145314' ,789 , '2016-04-09 ' , 23.4 , true ),
  603. ('0003' , '0003 ' , '54501212341' ,403 , '2016-04-09 ' , 4.2 , true ),
  604. ('0004' , '0004 ' , '25251444444' ,233 , '2016-04-09 ' , 0 , true ),
  605. ('0005' , '0005 ' , '52125199997' ,70  , '2016-04-09 ' , 0 , true ),
  606. ('0006' , '0006 ' , '96468754345' ,2020 , '2016-04-02  ' , 20.2 , true ),
  607. ('0007' , '0007 ' , '13345509287' ,400 , '2016-04-02  ' , 0 , true ),
  608. ('0008' , '0008 ' , '52125191105' ,121 , '2016-04-02 ' , 0 , true ),
  609. ('0009' , '0009 ' , '25251444444' ,133 , '2016-04-02 ' , 3.2 , true ),
  610. ('0010' , '0010 ' , '54501233290' ,154 , '2016-04-02 ' , 0 , true );
  611.  
  612.  
  613.  
  614.  
  615. -- TABELA DE COMPRA COMUM
  616.  
  617. CREATE TABLE compra_comum (
  618.  
  619.     cod char(4),
  620.     primary key(cod),
  621.     CONSTRAINT fk_compracomum foreign key (cod) references compra (codd) ON DELETE CASCADE ON UPDATE CASCADE
  622. );
  623.  
  624.  
  625. -- inserindo valores na tabela de compra comum
  626.  
  627. INSERT INTO compra_comum (cod ) VALUES
  628.  
  629. ('0001'),
  630. ('0003'),
  631. ('0005'),
  632. ('0006'),
  633. ('0008');
  634.  
  635. -- TABELA DE COMPRA PROGRAMADA
  636.  
  637. CREATE TABLE compra_programada (
  638.  
  639.     cod char(4),
  640.     data_1 date not null,
  641.     data2 date,
  642.     esta_ativa boolean default true,
  643.     CONSTRAINT primary key (cod),
  644.     CONSTRAINT fk_compraprogramada foreign key (cod) references compra (codd) ON DELETE CASCADE ON UPDATE CASCADE
  645. );
  646.  
  647. -- inserindo valores na tabela de compra programada
  648.  
  649. INSERT INTO compra_programada (cod , data_1 , data2 , esta_ativa ) VALUES
  650.  
  651. ('0002' , '2016-04-09' , '2017-04-09' , true ),
  652. ('0004' , '2016-04-09' , '2017-01-09' , true ),
  653. ('0007' , '2016-04-02' , '2016-11-02' , true ),
  654. ('0009' , '2016-04-02' , '2017-02-02' , true ),
  655. ('0010' , '2016-04-02' , '2017-04-02' , true );
  656.    
  657.  
  658. -- TABELA DE GARAGEM
  659.  
  660. CREATE TABLE garagem (
  661.  
  662.     cod char(4),
  663.     seq_filial char(4),
  664.     CNPJ_matriz char(14),
  665.     descricao varchar(15),
  666.     capacidade int(3) not null,
  667.     num_veiculos_atual int(3),
  668.     primary key(cod),
  669.     foreign key(seq_filial , CNPJ_matriz ) references filial (seq , CNPJ_Matriz)
  670.    
  671.  
  672. );
  673.  
  674. -- inserindo valores na tabela garagem
  675.  
  676. INSERT INTO garagem ( cod , seq_filial , CNPJ_matriz , descricao , capacidade , num_veiculos_atual ) VALUES
  677.  
  678. ( '0001' , '0001' ,  '23416393000114' , null , 4 , 2 ),
  679. ( '0002' , '0002' ,  '23416393000114' , null , 4 , 2 ),
  680. ( '0003' , '0003' ,  '23416393000140' , null , 4 , 2 ),
  681. ( '0004' , '0004' ,  '23416393000169' , null , 4 , 2 );
  682.    
  683.  
  684. -- TABELA DE VEICULO
  685.  
  686. CREATE TABLE veiculo (
  687.  
  688.     placa char(7),
  689.     seq_filial char(4),
  690.     CNPJ_matriz char(14),
  691.     cod_garagem char(4) ,
  692.     modelo varchar(15),
  693.     descricao varchar(15),
  694.     cor varchar(10),
  695.     ano year,
  696.     statuss boolean default true,
  697.     primary key (placa),
  698.     foreign key(seq_filial , CNPJ_matriz ) references filial(seq , CNPJ_Matriz),
  699.     foreign key(cod_garagem) references garagem (cod)
  700. );
  701.  
  702. -- inserindo valores na tabela veiculo
  703.  
  704. INSERT INTO veiculo ( placa , seq_filial , CNPJ_matriz , cod_garagem , modelo , descricao , cor , ano , statuss ) VALUES
  705.  
  706. ( 'PEX0220 ' ,  '0001' ,  '23416393000114' , '0001' , 'Caminhão' , 'Mercedes-benz' , 'preto' , '2014' , true),
  707. ( 'PEX2030 ' ,  '0001' ,  '23416393000114' , '0001' , 'Caminhão' , 'Mercedes-benz' , 'preto' , '2014' , true),
  708. ( 'VET4320 ' ,  '0002' ,  '23416393000114' , '0002' , 'Caminhão' , 'Mercedes-benz' , 'preto' , '2014' , true),
  709. ( 'VET3240 ' ,  '0002' ,  '23416393000114' , '0002' , 'Caminhão' , 'Mercedes-benz' , 'preto' , '2014' , true),
  710. ( 'WCV0943'  ,  '0003' ,  '23416393000140' , '0003' , 'Caminhão' , 'Mercedes-benz' , 'preto' , '2014' , true),
  711. ( 'WCV3344 ' ,  '0003' ,  '23416393000140' , '0003' , 'Caminhão' , 'Mercedes-benz' , 'preto' , '2014' , true),
  712. ( 'HLT0032 ' ,  '0004' ,  '23416393000169' , '0004' , 'Caminhão' , 'Mercedes-benz' , 'preto' , '2014' , true),
  713. ( 'HLT3994 ' ,  '0004' ,  '23416393000169' , '0004' , 'Caminhão' , 'Mercedes-benz' , 'preto' , '2014' , true);
  714.  
  715.  
  716.  
  717.  
  718. -- TABELA DE ESTOQUE
  719.  
  720. CREATE TABLE estoque (
  721.  
  722.     id char(4),
  723.     seq_filial char(7),
  724.     cnpj_matriz char(14),
  725.     descricao varchar(80),
  726.     dt_ultima_entrada date,
  727.     primary key (id),
  728.     foreign key (seq_filial , cnpj_matriz) references filial (seq , CNPJ_Matriz)
  729.    
  730. );
  731.  
  732. -- inserindo valores na tabela estoque
  733.  
  734. INSERT INTO estoque ( id , seq_filial , cnpj_matriz , descricao , dt_ultima_entrada ) VALUES
  735.  
  736. ( '0001' , '0001' ,  '23416393000114' , null , '2018-01-04'),
  737. ( '0002' , '0002' ,  '23416393000114' , null , '2018-01-04'),
  738. ( '0003' , '0003' ,  '23416393000140' , null , '2018-01-04'),
  739. ( '0004' , '0004' ,  '23416393000169' , null , '2018-01-04');
  740.  
  741.  
  742. -- TABELA MAQUINA
  743.  
  744. CREATE TABLE maquina (
  745.  
  746.     id char(4),
  747.     id_estoque char(4),
  748.     id_operador char(11),
  749.     ano year ,
  750.     combustivel varchar(30),
  751.     modelo varchar(30),
  752.     capacidade float CHECK( capacidade > 0 ) ,
  753.     elevavao_max float  CHECK ( elevavao_max > 0 ),
  754.     garantia date,
  755.     tipo varchar(30),
  756.     comprimento float ,
  757.     primary key (id),
  758.     foreign key (id_estoque) references estoque (id),
  759.     foreign key (id_operador) references estoquista (cpf)
  760. );
  761.  
  762. -- inserindo valores na tabela maquina
  763.  
  764. INSERT INTO maquina ( id , id_estoque , id_operador , ano, combustivel , modelo , capacidade , elevavao_max ,
  765. garantia , tipo , comprimento ) VALUES
  766.  
  767. ( '0001' , '0001' , '77491222226' , '2014' , 'diesel', null , 600 , 5 , '2019-02-02' , 'empilhadeira' , 2.20 ),
  768. ( '0002' , '0002' , '57859332507' , '2014' , 'diesel', null , 600 , 5 , '2019-02-02' , 'empilhadeira' , 2.20 ),
  769. ( '0003' , '0003' , '16565525749' , '2014' , 'diesel', null , 600 , 5 , '2019-02-02' , 'empilhadeira' , 2.20 ),
  770. ( '0004' , '0004' , '98243208909' , '2014' , 'diesel', null , 600 , 5 , '2019-02-02' , 'empilhadeira' , 2.20 );
  771.  
  772. -- TABELA DE AVARIA
  773.  
  774. CREATE TABLE avaria (
  775.    
  776.     id char(4),
  777.     causa varchar(20) not null,
  778.     preco float(3) CHECK ( preco > 0 ),
  779.     obs varchar(40),
  780.     primary key(id)
  781.    
  782. );
  783.  
  784. -- inserindo valores na tabela avaria
  785.  
  786. INSERT INTO avaria (id , causa , preco , obs ) VALUES
  787.  
  788. ('0001' , 'Queda' , 30 , null ),
  789. ('0002' , 'Queda' , 10 , null ),
  790. ('0003' , 'Queda' , 200 , null ),
  791. ('0004' , 'Queda' , 32.2 , null ),
  792. ('0005' , 'Queda' , 230 , null ),
  793. ('0006' , 'Queda' , 92.2 , null );
  794.  
  795. -- TABELA DE PRATELEIRA
  796.  
  797. CREATE TABLE prateleira (
  798.  
  799.     codigo char(4),
  800.     altura float(2) not null,
  801.     comprimento float(2) not null,
  802.     posicao_nivel char(2),
  803.     primary key(codigo)
  804.  
  805. );
  806.  
  807. -- inserindo valores na tabela prateleira  
  808.  
  809. INSERT INTO prateleira ( codigo , altura , comprimento , posicao_nivel ) VALUES
  810.  
  811. ('0001' , 5 , 10 , 'A1' ),
  812. ('0002' , 5 , 10 , 'A2' ),
  813. ('0003' , 5 , 10 , 'A3' ),
  814. ('0004' , 5 , 10 , 'A4' ),
  815. ('0005' , 5 , 10 , 'A5' ),
  816. ('0006' , 5 , 10 , 'B1' ),
  817. ('0007' , 5 , 10 , 'B2' ),
  818. ('0008' , 5 , 10 , 'B3' ),
  819. ('0009' , 5 , 10 , 'B4' ),
  820. ('0010' , 5 , 10 , 'B5' ),
  821. ('0011' , 5 , 10 , 'C1' ),
  822. ('0012' , 5 , 10 , 'C2' ),
  823. ('0013' , 5 , 10 , 'C3' ),
  824. ('0014' , 5 , 10 , 'C4' ),
  825. ('0015' , 5 , 10 , 'C5' );
  826.  
  827.  
  828.  
  829. -- TABELA DE NCM DE PRODUTO
  830.  
  831. CREATE TABLE NCM (
  832.    
  833.     id char(4),
  834.     descricao varchar(40),
  835.     cod_mercosul char(9) not null,
  836.     primary key(id)
  837.  
  838. );
  839.  
  840. -- inserindo valores na tabela NCM
  841.  
  842. INSERT INTO NCM ( id , descricao , cod_mercosul) VALUES
  843.  
  844. ('0001', ' ' , '000000001'),
  845. ('0002' , ' ' , '000000002'),
  846. ('0003' , ' ', '000000003'),
  847. ('0004' , ' ', '000000004'),
  848. ('0005', ' ' , '000000005');
  849.  
  850. -- TABELA DE UNIDADE DE PRODUTO  
  851.  
  852. CREATE TABLE unidade (
  853.  
  854.     cod char(4),
  855.     descricao varchar(30),
  856.     sigla char(2) not null,
  857.     primary key(cod)
  858.    
  859. );
  860.  
  861. -- inserindo valores na tabela unidade  
  862.  
  863. INSERT INTO unidade (cod, descricao , sigla) VALUES
  864.  
  865. ('0001', 'quilogramas', 'kg'),
  866. ('0002' , 'mililitros', 'mL'),
  867. ('0003', 'gramas' , 'g'),
  868. ('0004', 'litros' , 'L'),
  869. ('0005', 'miligramas' , 'mg');
  870.  
  871. -- TABELA DE CATEGORIA DE PRODUTO
  872.  
  873. CREATE TABLE categoria (
  874.    
  875.     codcategoria char(4),
  876.     descricaocategoria varchar(30) not null,
  877.     primary key (codcategoria)
  878.  
  879. );
  880.  
  881. -- inserindo valores na tabela categoria
  882.  
  883. INSERT INTO categoria ( codcategoria , descricaocategoria ) VALUES
  884.  
  885. ('0001', 'Condimentos' ),
  886. ('0002' , 'Laticinios'),
  887. ('0003', 'HortiFruti'),
  888. ('0004' , 'Conservas'),
  889. ('0005', 'Limpeza'),
  890. ('0006' , 'Bebidas Alcoolicas'),
  891. ('0007' , 'Bebidas nao Alcoolicas'),
  892. ('0008' , 'Graos e Cereais');
  893.  
  894. -- TABELA DE SUBCATEGORIA DE PRODUTO
  895.  
  896. CREATE TABLE subcategoria (
  897.  
  898.     codsubcategoria char(4),
  899.     cod_categoria char(4),
  900.     descricaosubcategoria varchar(30) not null,
  901.     primary key(codsubcategoria),
  902.     foreign key (cod_categoria) references categoria(codcategoria)
  903.  
  904. );
  905.  
  906. -- inserindo valores na tabela subcategoria
  907.  
  908. INSERT INTO subcategoria ( codsubcategoria , cod_categoria , descricaosubcategoria ) VALUES
  909.  
  910. ('0001' , '0001' , 'Vinagres'),
  911. ('0002' , '0001' , 'Temperos'),
  912. ('0003' , '0001' , 'Sal' ),
  913. ('0004' , '0001', 'Azeites'),
  914. ('0005' , '0001', 'Oleos'),
  915. ('0006' , '0001', 'Especiarias'),
  916. ('0007' , '0002', 'Leites'),
  917. ('0008' , '0002', 'Iogurtes'),
  918. ('0009' , '0002', 'Fermentados'),
  919. ('0010' , '0002', 'Queijos'),
  920. ('0011' , '0003', 'Ovos'),
  921. ('0012' , '0003', 'Frutas secas'),
  922. ('0013' , '0003', 'Legumes'),
  923. ('0014' , '0004' , 'Frutas'),
  924. ('0015' , '0004', 'Peixes'),
  925. ('0016' , '0004', 'Vegetais'),
  926. ('0017' , '0004', 'Cogumelos'),
  927. ('0018' , '0005', 'Detergente'),
  928. ('0019' , '0005', 'Desinfetante'),
  929. ('0020' , '0005', 'Sabao em po'),
  930. ('0021' , '0005', 'Sabao em barra'),
  931. ('0022' , '0005', 'Amaciante');
  932.  
  933.  -- TABELA DE MARCA DE PRODUTO
  934.  
  935. CREATE TABLE marca (
  936.  
  937.     codmarca char(4),
  938.     descricaomarca varchar(40),
  939.     primary key(codmarca)
  940.  
  941. );
  942.  
  943. -- inserindo valores na tabela marca  
  944.  
  945. INSERT INTO marca ( codmarca , descricaomarca ) VALUES
  946.  
  947. ('0001', 'Sadia'),
  948. ('0002' , 'Knorr'),
  949. ('0003' , 'Camponesa'),
  950. ('0004' , 'Kicaldo'),
  951. ('0005' , 'Vitarela'),
  952. ('0006' , 'Bauduco'),
  953. ('0007', 'OMO'),
  954. ('0008' , 'Dona Benta'),
  955. ('0009', 'Nestle');
  956.  
  957.  
  958. -- TABELA DE FORNECEDOR
  959.  
  960. CREATE TABLE fornecedor (
  961.  
  962.     cod char(4),
  963.     nome varchar(20) not null,
  964.     CNPJ char(14) not null,
  965.     rua varchar(20),
  966.     bairro varchar(15),
  967.     CEP char(8),
  968.     estado varchar(15) not null,
  969.     ativo boolean default true,
  970.     primary key(cod )
  971. );
  972.  
  973. -- inserindo valores na tabela fornecedor
  974.  
  975. INSERT INTO fornecedor ( cod , nome , CNPJ , rua , bairro , CEP , estado , ativo ) VALUES
  976.  
  977. ('0001' , 'Sadia'     , '55274471000180' , 'Rua valadares' , 'ipsep', '54330315' , 'Pernambuco' , true ) ,
  978. ('0002' , 'Pampers'   , '26724671000180' , 'Rua cartomante' , 'ibura', '54202010' , 'Pernambuco' , true ) ,
  979. ('0003' , 'Vitarela'  , '21712241000162' , 'Rua maniac' , 'algodão', '54215322' , 'São paulo' , true ) ,
  980. ('0004' , 'Coca-cola' , '26804531000180' , 'Rua argola' , 'vale tinhaem ', '51215020' , 'Rio de janeiro' , true ) ,
  981. ('0005' , 'Helmans'   , '77411981000180' , 'Rua sartre' , 'gitacity ', '44650201' , 'São paulo' , true ),
  982. ('0006' , 'Bombril'   , '53686527000188' , 'Rua 3' , 'Varzea' , '50980320' , 'Recife' , true );
  983.  
  984.  -- TABELA DE TELEFONE DE FORNECEDOR
  985.  
  986. CREATE TABLE telefone_fornecedor(
  987.    
  988.     cod_fornecedor char(4),
  989.     telefone char(11),
  990.     primary key( cod_fornecedor, telefone),
  991.     CONSTRAINT fk_forn foreign key(cod_fornecedor) references fornecedor (cod) ON DELETE CASCADE ON UPDATE CASCADE
  992.  
  993. );
  994.  
  995. -- inserindo valores na tabela telefone_fornecedor
  996.  
  997. INSERT INTO telefone_fornecedor (cod_fornecedor, telefone) VALUES
  998.  
  999. ('0001' , '8133115544'),
  1000. ('0002' ,  '8133468952'),
  1001. ('0003' , '1140025632'),
  1002. ('0004', '1432456895'),
  1003. ('0005', '1121452062');
  1004.  
  1005.  
  1006. -- TABELA  DE PRODUTO REF  
  1007.  
  1008. CREATE TABLE produto_ref (
  1009.  
  1010.     cod char(4),
  1011.     id_unidade char(4),
  1012.     id_marca char(4),
  1013.     id_ncm char(4),
  1014.     id_categoria char(4),
  1015.     id_subcategoria char(4),
  1016.     id_fornecedor char(4),
  1017.     qtd_estoque int ,
  1018.     ICMS float,
  1019.     CST varchar(3),
  1020.     preco_por_tabela float,
  1021.     cod_barra char(13),
  1022.     freq_pedido float NULL,
  1023.     descricao varchar(30),
  1024.     qtd_min int,
  1025.     qtd_total_estoque int,
  1026.     preco_ult_compra float,
  1027.     primary key (cod),
  1028.     foreign key (id_unidade) references unidade (cod),
  1029.     foreign key (id_marca) references marca (codmarca),
  1030.     foreign key (id_ncm) references ncm (id),
  1031.     foreign key (id_categoria) references categoria (codcategoria),
  1032.     foreign key (id_subcategoria) references subcategoria (codsubcategoria),
  1033.     foreign key (id_fornecedor) references fornecedor (cod)
  1034.  
  1035. );
  1036.  
  1037. INSERT INTO produto_ref (cod, id_unidade , id_marca , id_ncm , id_categoria, id_subcategoria, id_fornecedor , qtd_estoque ,
  1038.  ICMS , CST , preco_por_tabela , cod_barra , freq_pedido , descricao , qtd_min, qtd_total_estoque , preco_ult_compra) VALUES
  1039.  
  1040. ('0001' , '0001' , '0001', '0001' , '0001' , '0001' , '0001' , 30 , 4.5 , '00' , 8.69  , '0201254123151' ,  null , null , 3 , 80 , 8.20 ),
  1041. ('0002' , '0001' , '0001', '0001' , '0001' , '0002' , '0001' , 31 , 1.1 , '00' , 3.69  , '0201223412341' ,  null , null , 3 , 81 , 8 ),
  1042. ('0003' , '0001' , '0001', '0001' , '0001' , '0001' , '0001' , 32 , 1.4 , '00' , 4.69  , '0201254353434' ,  null , null , 3 , 82 , 7 ),
  1043. ('0004' , '0001' , '0002', '0001' , '0001' , '0002' , '0002' , 33 , 2.4 , '00' , 5.69  , '0243453453453' ,  null , null , 3 , 83 , 6.20 ),
  1044. ('0005' , '0005' , '0002', '0002' , '0001' , '0003' , '0002' , 34 , 3.7 , '00' , 6.69  , '0256523435134' ,  null , null , 3 , 84 , 5.20 ),
  1045. ('0006' , '0002' , '0002', '0002' , '0001' , '0003' , '0002' , 35 , 1.7 , '00' , 7.69  , '3424343423434' ,  null , null , 3 , 85 , 8.20 ),
  1046. ('0007' , '0002' , '0003', '0002' , '0001' , '0004' , '0003' , 36 , 4.8 , '00' , 8.69  , '0201545213412' ,  null , null , 3 , 86 , 9.20 ),
  1047. ('0008' , '0002' , '0003', '0002' , '0001' , '0005' , '0003' , 37 , 4.7 , '00' , 21.69 , '0201265243565' ,  null , null , 3 , 87 , 7.20 ),
  1048. ('0009' , '0002' , '0003', '0003' , '0001' , '0006' , '0003' , 38 , 5.4 , '00' , 10.69 , '0201256435453' ,  null , null , 3 , 88 , 1.20 ),
  1049. ('0010' , '0005' , '0004', '0003' , '0001' , '0001' , '0004' , 39 , 9.3 , '00' , 69    , '0201250454544' ,  null , null , 3 , 89 , 82.20 ),
  1050. ('0011' , '0003' , '0004', '0003' , '0001' , '0002' , '0004' , 20 , 7.1 , '00' , 23.69 , '0201254177575' ,  null , null , 3 , 90 , 2.20 ),
  1051. ('0012' , '0003' , '0005', '0003' , '0002' , '0007' , '0004' , 30 , 6.4 , '00' , 8     , '0201254199997' ,  null , null , 3 , 99 , 3.20 ),
  1052. ('0013' , '0003' , '0005', '0004' , '0002' , '0007' , '0005' , 40 , 1.5 , '00' , 0.69  , '0201255455555' ,  null , null , 3 , 98 , 8.20 ),
  1053. ('0014' , '0003' , '0006', '0004' , '0002' , '0007' , '0005' , 50 , 3.4 , '00' , 2.69  , '0201254176777' ,  null , null , 3 , 97 , 99.20 ),
  1054. ('0015' , '0005' , '0006', '0004' , '0002' , '0008' , '0005' , 70 , 7.9 , '00' , 3.69  , '0201254543543' ,  null , null , 3 , 96 , 20 ),
  1055. ('0016' , '0004' , '0007', '0004' , '0002' , '0007' , '0006' , 50 , 4.2 , '00' , 4.69  , '0201256555555' ,  null , null , 3 , 95 , 12.20 ),
  1056. ('0017' , '0004' , '0007', '0005' , '0002' , '0008' , '0006' , 30 , 6.3 , '00' , 8.69  , '0201255523151' ,  null , null , 3 , 94 , 20 ),
  1057. ('0018' , '0004' , '0008', '0005' , '0002' , '0007' , '0006' , 60 , 5.2 , '00' , 89.69 , '0205465793151' ,  null , null , 3 , 93 , 21.20 ),
  1058. ('0019' , '0004' , '0009', '0005' , '0002' , '0007' , '0006' , 40 , 7.1 , '00' , 87.69 , '0201888863151' ,  null , null , 3 , 92 , 23 ),
  1059. ('0020' , '0005' , '0008', '0005' , '0002' , '0008' , '0006' , 57 , 4.1 , '00' , 46    , '0201999923151' ,  null , null , 3 , 91 , 42 );
  1060.  
  1061.  
  1062. -- TABELA ITEM DE COMPRA
  1063.  
  1064. CREATE TABLE item_compra (
  1065.  
  1066.     cod_compra char(4),
  1067.     cod_produto char(4),
  1068.     quantidade int CHECK (quantidade > 0) ,
  1069.     valor_desconto float default 0 CHECK (valor_desconto >= 0),
  1070.     valor_unitario float CHECK (valor_unitario > 0),
  1071.     primary key (cod_compra , cod_produto),
  1072.     foreign key (cod_compra) references compra (codd),
  1073.     foreign key (cod_produto) references produto_ref (cod)
  1074. );
  1075.  
  1076.  -- ITEM COMPRA
  1077.  
  1078. INSERT INTO item_compra (cod_compra , cod_produto , quantidade , valor_desconto , valor_unitario ) VALUES
  1079. ('0001' , '0001' , 3 , null , 8.69 ),
  1080. ('0002' , '0001' , 5 , null , 8.69 ),
  1081. ('0003' , '0001' , 8 , null , 8.69 ),
  1082. ('0004' , '0003' , 5 , null , 4.69 ),
  1083. ('0005' , '0004' , 2 , null , 5.69 ),
  1084. ('0006' , '0005' , 4 , null , 6.69 ),
  1085. ('0007' , '0006' , 5 , null , 7.69 ),
  1086. ('0008' , '0007' , 4 , null , 8.69 );
  1087.  
  1088.  
  1089.  
  1090. -- TABELA DE INCIDENTE  
  1091.  
  1092. CREATE TABLE incidente (
  1093.  
  1094.     cod char(4),
  1095.     seq_entrega char(4),
  1096.     dataa date not null,
  1097.     relatorio varchar(200),
  1098.     hora time ,
  1099.     primary key(cod),
  1100.     foreign key (seq_entrega) references entrega (seq)
  1101.  
  1102. );
  1103.  
  1104. -- inserindo valores na tabela incidente
  1105.  
  1106. INSERT INTO incidente (cod , seq_entrega , dataa, relatorio , hora ) VALUES
  1107.  
  1108. ('0001' , '0003' , '2016-04-09 ' , ' Tentativa de assalto ' , '18:06:00' ),
  1109. ('0002' , '0005' , '2016-04-09 ' , ' Tentativa de assalto ' , '13:34:00' ),
  1110. ('0003' , '0006' , '2016-04-02 ' , ' Tentativa de assalto ' , '06:32:00' );
  1111.  
  1112. -- TABELA DOCS
  1113.  
  1114.  
  1115. CREATE TABLE docs (
  1116.    
  1117.     cod_incidente char(4),
  1118.     docs varchar(20),
  1119.     primary key (cod_incidente , docs),
  1120.     CONSTRAINT fk_incidente foreign key (cod_incidente) references incidente (cod ) ON DELETE CASCADE ON UPDATE CASCADE
  1121. );
  1122.  
  1123.  -- inserindo valores na tabela docs
  1124.  
  1125. INSERT INTO docs ( cod_incidente , docs ) VALUES
  1126.  
  1127. ( '0001', 'something' ) ,
  1128. ('0002' , 'something' ) ,
  1129. ('0003' , 'something');
  1130.  
  1131. -- TABELA DE TIPO DE PAGAMENTO
  1132.  
  1133. CREATE TABLE tipo_pagamento (
  1134.    
  1135.     cod mediumint AUTO_INCREMENT,
  1136.     descricao varchar(30),
  1137.     primary key(cod)
  1138.  
  1139.  );
  1140.  
  1141.  -- inserindo valores na tabela tipo_pagamento  
  1142.  
  1143. INSERT INTO tipo_pagamento ( descricao ) VALUES
  1144.  
  1145. ( 'Boleto Bancario'),
  1146. ( 'Cartao de Credito'),
  1147. ('Cartao de Debito');
  1148.  
  1149. -- TABELA DE PEDIDO FORNECEDOR
  1150.  
  1151. CREATE TABLE pedido_fornecedor (
  1152.    
  1153.     cod char(4),
  1154.     CPF_gerente char(11),
  1155.     total_desconto float CHECK ( total_desconto >= 0 ),
  1156.     valor_total_IPI float CHECK ( valor_total_IPI >= 0 ),
  1157.     CFOP char(4) not null,
  1158.     valor_total float CHECK ( valor_total >= 0),
  1159.     dia date not null,
  1160.     statos boolean default true,
  1161.     valor_frete float CHECK (valor_frete >= 0),
  1162.     primary key (cod),
  1163.     foreign key (CPF_gerente) references gerente (CPF)
  1164. );
  1165.  
  1166.  
  1167. -- inserindo valores na tabela pedido_fornecedor
  1168.  
  1169. INSERT INTO pedido_fornecedor ( cod, CPF_gerente , total_desconto , valor_total_IPI, CFOP, dia , statos, valor_frete ) VALUES
  1170.  
  1171. ('0001' , '96202875763' , 12380.00, 354.20 , '0001', '2017-12-09' , true , 12.90),
  1172. ('0002', '96202875763' , 3456.40 , 123.20 , '0002', '2017-11-09' , true , 10.30),
  1173. ('0003' , '96202875763' , 2280.25 , 220.00,'0003',  '2017-06-13', true , 28.50),
  1174. ('0004' , '96202875763' , 12200.00, 123.99,'0004',  '2017-07-04', true , 9.89),
  1175. ('0005' , '96202875763' , 990.00 , 19.89,  '0005',  '2016-12-09', true , 12.43),
  1176. ('0006' , '96202875763' , 5660.00, 56.70 , '0006', '2015-08-27' , true , 6.98),
  1177. ('0007' , '96202875763' , 2890.00 , 35.47 , '0007', '2016-04-12' , true , 10.20),
  1178. ('0008' , '96202875763' , 1873.20 , 190.00 ,'0008' , '2017-01-04' , true , 8.33),
  1179. ('0009' , '96202875763' , 2348.80, 111.10 , '0009' ,'2017-06-09' , true , 10.22),
  1180. ('0010' , '02123011878' , 1300.00, 132.30 , '0010','2017-08-27' , true , 11.30);
  1181.  
  1182.  
  1183.  
  1184.  
  1185. -- TABELA DE FATURA
  1186.  
  1187. CREATE TABLE fatura (
  1188.  
  1189.     id mediumint auto_increment,
  1190.     cod_pedido_fornecedor char(4),
  1191.     data_vencimento date,
  1192.     valor_pago_atual float CHECK (valor_pago_atual >= 0 ),
  1193.     valor_total_final float CHECK (valor_total_final >= 0),
  1194.     data_emissao date,
  1195.     statos boolean default true,
  1196.     data_paga date,
  1197.     multa float,
  1198.     primary key (id),
  1199.     foreign key (cod_pedido_fornecedor) references pedido_fornecedor (cod)
  1200.  
  1201. );
  1202. -- inserindo valores na tabela fatura
  1203.  
  1204. INSERT INTO fatura ( cod_pedido_fornecedor , data_vencimento , valor_pago_atual, valor_total_final ,
  1205.  data_emissao , statos , data_paga , multa ) VALUES
  1206.  
  1207.  ( '0001', '2018-03-03' , 200.30 , 600 , '2017-11-13' , true , null , 0),
  1208.  ( '0002', '2018-02-03' , 343 , 800 , '2017-11-17' , true , null , 0),
  1209.  ( '0003', '2018-05-03' , 20.10 , 200 , '2017-11-16' , true , null , 0),
  1210.  ( '0004', '2018-07-03' , 40 , 300 , '2017-12-20' , true , null , 0),
  1211.  ( '0005', '2018-08-03' , 234 , 400 , '2018-01-13' , true , null , 0),
  1212.  ( '0006', '2018-09-03' , 120 , 500 , '2017-11-22' , true , null , 0);
  1213.  
  1214.  
  1215.  
  1216.  
  1217. -- TABELA DE NOTA FISCAL
  1218.  
  1219. CREATE TABLE nota_fiscal (
  1220.  
  1221.     NFE char(9),
  1222.     ICMS float not null,
  1223.     valor_total float not null CHECK ( valor_total > 0 ),
  1224.     valor_total_desconto float CHECK ( valor_total_desconto >= 0 ),
  1225.     dia date not null,
  1226.     valor_frete float,
  1227.     primary key (NFE)
  1228. );
  1229.  
  1230. --  inserindo valores na tabela nota_fiscal
  1231.  
  1232. INSERT INTO nota_fiscal (NFE , ICMS , valor_total, valor_total_desconto, dia , valor_frete) VALUES
  1233.  
  1234. ('111111109' ,  0.04 , 143.98 , 139.90 , '2016-11-12' , 3.78),
  1235. ('111111104' ,  0.06 , 35.80 , 35.80, '2017-10-21' , 7.90),
  1236. ('111111113' ,  0.04 , 50.21 , 49.95 , '2012-03-27' , 10.32),
  1237. ('111111105' ,  0.06 , 173.98, 173.98 , '2010-06-13' , 8.50),
  1238. ('111111107' ,  0.02 , 50.43 , 50.43 , '2002-04-01' , 2.21),
  1239. ('111111108' ,  0.03 , 283.10 , 279.50 , '2011-11-11' , 4.30),
  1240. ('111111139' ,  0.02 , 123.54 , 111.20 , '2016-11-12' , 6.80),
  1241. ('111111199' ,  0.02 , 12.99 , 12.99 , '2016-11-13' , 8.30),
  1242. ('111111129' ,  0.03 , 346.10 , 336.10 , '2016-11-10' , 2.12);
  1243.  
  1244. -- TABELA DE PAGAMENTO  
  1245.  
  1246. CREATE TABLE pagamento (
  1247.    
  1248.     cod mediumint not null auto_increment,
  1249.     id_fatura mediumint,
  1250.     cod_compra char(4),
  1251.     cod_tipo_pagamento mediumint,
  1252.     valor_pago float CHECK ( valor_pago >= 0 ),
  1253.     dia date,
  1254.     statos boolean default true ,
  1255.     tipo_pagamento enum ('Cartao credito', 'Cartao Debito', 'Boleto'),
  1256.     primary key (cod),
  1257.     foreign key (id_fatura) references fatura (id),
  1258.     foreign key (cod_compra) references compra (codd),
  1259.     foreign key (cod_tipo_pagamento) references tipo_pagamento ( cod)
  1260.    
  1261. );
  1262.  
  1263.  
  1264. INSERT INTO pagamento ( id_fatura ,cod_compra , cod_tipo_pagamento  , valor_pago , dia ,  statos, tipo_pagamento ) VALUES
  1265.  
  1266. ( 1, '0001' , 1 , 100 , '2017-11-03' , true , null ),  
  1267. ( 2 , '0002' , 2 , 80 , '2017-11-13' , true , null ),
  1268. ( 3 , '0003' , 3 , 200 , '2017-11-14' , true , null ),
  1269. ( 4 , '0004' , 2 , 49 , '2017-11-20' , true , null ),
  1270. ( 5 , '0005' , 3 , 90.20 , '2017-12-03' , true , null );
  1271.  
  1272.  
  1273. -- TABELA DA NOTA FISCAL FORNECEDOR
  1274.  
  1275. CREATE TABLE nota_fiscal_fornecedor (
  1276.  
  1277.     NFE char(9),
  1278.     cod_pagamento mediumint,
  1279.     CFOP char(4) not null,
  1280.     IPI float,
  1281.     primary key(NFE),
  1282.     CONSTRAINT fk_ntfiscalfornecedor foreign key (NFE) references nota_fiscal (NFE) ON DELETE CASCADE ON UPDATE CASCADE,
  1283.     foreign key (cod_pagamento) references pagamento (cod)
  1284.  );
  1285.  
  1286.  
  1287. INSERT INTO nota_fiscal_fornecedor (NFE, cod_pagamento , CFOP , IPI ) VALUES
  1288. ('111111109' , 1, '1100' , 5.4),
  1289. ( '111111104' , 2, '1100' , 6.4),
  1290. ('111111113' ,  3, '1100' , 7.4),
  1291. ( '111111105' , 4 , '1100' , 3.2);
  1292.  
  1293. -- TABELA NOTA FISCAL COMPRA
  1294.  
  1295. CREATE TABLE nota_fiscal_compra (
  1296.  
  1297.     NFE char(9),
  1298.     cod_pagamento mediumint,
  1299.     primary key (NFE),
  1300.     CONSTRAINT fk_ntfiscalcompra foreign key (NFE) references nota_fiscal (NFE) ON DELETE CASCADE ON UPDATE CASCADE,
  1301.     foreign key (cod_pagamento) references pagamento (cod)
  1302.  
  1303. );
  1304.  
  1305. -- inserindo valores na tabela nota_fical_compra
  1306.  
  1307. INSERT INTO nota_fiscal_compra ( NFE , cod_pagamento ) VALUES
  1308. ('111111129' , 1),
  1309. ('111111107' , 2 ),
  1310. ('111111108' , 3 ),
  1311. ('111111139' , 4 ),
  1312. ('111111199' , 5 );
  1313.  
  1314.  
  1315.  
  1316. -- TABELA DE CLIENTE  
  1317.  
  1318. CREATE TABLE cliente (
  1319.  
  1320.     CPF char(11),
  1321.     seq_filial char(4),
  1322.     cnpj_matriz char(14),
  1323.     cep char(8) not null,
  1324.     cidade varchar(30),
  1325.     numero char(3),
  1326.     descricao varchar(30),
  1327.     valor_credito float,
  1328.     p_nome varchar(20) not null,
  1329.     m_nome varchar(20) ,
  1330.     u_nome varchar(30) not null,
  1331.     rg char(7) not null UNIQUE,
  1332.     senha varchar(12) not null default '123456',
  1333.     tem_clube_desconto boolean default false,
  1334.     data_cadastro date,
  1335.     email varchar(40) not null,
  1336.     data_nascimento date not null,
  1337.     primary key (CPF),
  1338.     foreign key (seq_filial , cnpj_matriz) references filial (seq , CNPJ_Matriz)
  1339.    
  1340. );
  1341.  
  1342.  
  1343. INSERT INTO cliente ( CPF , seq_filial , cnpj_matriz ,cep, cidade, numero, descricao , valor_credito, p_nome , m_nome , u_nome, rg , senha , tem_clube_desconto , data_cadastro , email , data_nascimento) VALUES
  1344.  
  1345. ('54501233290' , '0001' , '23416393000114', '52291045' , 'Santo andre'  , '111' ,  'Rua Jaguaribara'         , 1200.00 , 'Melissa'  , 'Andreia'    , 'Nascimento', '1908234' , '3214'    , false , '2016-02-12' , 'manasc@gmail.com'            , '1999-06-13'),
  1346. ('25251145314' , '0002' , '23416393000114', '50999321' , 'Sao bernardo' , '123' , 'Rua de Saão Bento'       , 0       , 'Marcos'   , 'Andre'      , 'Marques'   , '9123786' , 'xxx9'    , false , '2015-03-11' , 'marcossantos@hotmail.com'    , '1980-03-25' ),
  1347. ('13345509287' , '0001' , '23416393000114', '50348567' , 'Sao caetano'  , '453' , 'Rua Real da Torre '       , 0       , 'Fatima'   , 'Caixias'    , 'Laffaiete' , '1866630' , '1233'    , false , '2017-02-11' , 'falcaix@gmail.com'           , '1972-09-09'),
  1348. ('77658476358' , '0001' , '23416393000114', '63900435' , 'Sao bernardo' , '434' , 'Rua Chile'                , 0       , 'Renato'   , 'Murilo'     , 'Dias'      , '1266839' , '34g4'    , false , '2017-07-04' , 'murilodias@gmail.com'        , '1989-12-29'),
  1349. ('50519774647' , '0002' , '23416393000114', '56332078' , 'Sao bernardo' ,  null , 'Rua Quarenta e Seis'      , 22.30   , 'Joaquim'  , 'Luiz'       , 'Campos'    , '2871019' , '88uh'    , true  , '2016-04-04' , 'uizjoaquim@hotmail.com'      , '1990-04-30'),
  1350. ('79317880797' , '0001' , '23416393000114', '82600130' , 'Sao bernardo' , null  , 'Rua Bernardo Rosenmann'   , 0       , 'Alana'    , 'Rodrigues'  , 'Mello'     , '2368088' ,  '34f4'   , true  , '2016-09-11' , 'alana_rodriques@gmail.com'   , '1993-06-06'),
  1351. ('52125191105' , '0002' , '23416393000114', '77820026' , 'Sao bernardo' , null  , 'Rua 16'                   , 0       , 'Luana'    , 'Barbosa'    , 'Pinto'     , '1994566' , '9jf4'    , true  , '2016-09-11' , 'luaninha_babrbosa@gmail.com' , '1996-06-06'),
  1352. ('50130428361' , '0001' , '23416393000114', '77016638' , 'Sao bernardo' , null  , 'Quadra 509 Sul Alameda 7' , 0       , 'Agatha'   , 'Vasconcelos', 'Belarmino' , '4526174' , 'f56g'    , true  , '2016-03-04' , 'agatinha_bb@gmail.com'       , '1999-09-09'),
  1353. ('96468793068' , '0001' , '23416393000114', '49069186' , 'Santo Andre'  , null  , 'Rua do Sol'               , 0       , 'Camila'   , 'Castro'     , 'Raimundo'  , '3199788' , 'kj004'   , true  , '2016-09-18' , 'camila_racastro@gmail.com'   , '1992-02-20'),
  1354. ('16512268903' , '0002',  '23416393000114', '72876134' , 'Sao bernardo' , null  , 'Quadra 37'                , 23.33   , 'Valkiria' , 'Queiroz'    , 'Calado'    , '4241822' , 'kd3io4'  , true  , '2016-02-12' , 'valqueiros@outlook.com'      , '1960-03-07'),
  1355. ('07617589689' , '0002' , '23416393000114', '89280577' , 'Sao bernardo' , null  , 'Rua Zanzibar'             , 0       , 'Angelica' , 'Meneses'    , 'da Silva'  , '4764955' , 'i4hfi4h' , false , '2017-02-05' , 'angelica_mene@outlook.com'   , '1992-12-11'),
  1356.  
  1357. ('54501212341' , '0004' , '23416393000169', '52291032' , 'Ipanema'      , '131' , 'Rua dreamer'              , 200.00  , 'Felissia' , 'Andreia'    , 'Nasciso'   , '1908312' , '3214'    , true  , '2016-02-12' , 'xxxxx@gmail.com'             , '1978-06-13'),
  1358. ('25251444444' , '0004' , '23416393000169', '50999399' , 'Ipanema'      , '163' , 'Rua gastino'              , 123     , 'Maycomu'  , 'Andre'      , 'feliciano' , '9123321' , 'xxx9'    , true  , '2015-03-11' , 'yyyyy@hotmail.com'           , '1987-03-25' ),
  1359. ('13345555555' , '0004' , '23416393000169', '50348577' , 'Ipanema'      , '543' , 'Rua malaneti'             , 214     , 'Fatima'   , 'Bernardes'  , 'Laffaiete' , '1864568' , '1233'    , false , '2017-02-11' , 'zzzzz@gmail.com'             , '1967-09-09'),
  1360. ('77656666666' , '0004' , '23416393000169', '63900442' , 'Ipanema'      , '544' , 'Rua valadares'            , 54      , 'Renan'    , 'Murilo'     , 'Anos'      , '1264444' , '34g4'    , false , '2017-07-04' , 'hhhhh@gmail.com'             , '1989-12-29'),
  1361. ('50519434234' , '0004' , '23416393000169', '56332011' , 'Ipanema'      , '003' , 'Rua noventa e sete'       , 22.30   , 'Joaquim'  , 'Nabuco'     , 'Oliveira'  , '2875412' , '88uh'    , true  , '2016-04-04' , 'jjjjj@hotmail.com'           , '1998-04-30'),
  1362. ('79317887675' , '0004' , '23416393000169', '82600131' , 'Ipanema'      , null  , 'Rua Bernardo manoel'      , 0       , 'Alana'    , 'Guedes'     , 'Mello'     , '2364811' ,  '34f4'   , true  , '2016-09-11' , 'iiiii@gmail.com'             , '1976-06-06'),
  1363. ('52125199997' , '0003' , '23416393000140', '77820021' , 'Recife'       , null  , 'Rua 16'                   , 21      , 'Valdemir' , 'Barbosa'    , 'Pinto'     , '1993333' , '9jf4'    , true  , '2016-09-11' , 'ppppp@gmail.com'             , '1956-06-06'),
  1364. ('50130400000' , '0003' , '23416393000140', '77016614' , 'Recife'       , null  , 'Rua vital dos sonhos'     , 100     , 'Agatha'   , 'Cristh'     , 'Belarmino' , '4527777' , 'f56g'    , true  , '2016-03-04' , 'bbbbb@gmail.com'             , '1978-09-09'),
  1365. ('96468754345' , '0003' , '23416393000140', '49069112' , 'Recife'       , null  , 'Rua nevermore'            , 0       , 'Camila'   , 'Fagundes'   , 'Nonato'    , '3194514' , 'kj004'   , true  , '2016-09-18' , 'ccccc@gmail.com'             , '1998-02-20'),
  1366. ('16512211111' , '0003',  '23416393000140', '72876146' , 'Olinda'       , null  , 'Quadra 98'                , 20.33   , 'Marilia'  , 'Queiroz'    , 'Falado'    , '4241847' , 'kd3io4'  , true  , '2016-02-12' , 'aaaaa@outlook.com'           , '1978-03-07'),
  1367. ('07617555555' , '0003' , '23416393000140', '89284561' , 'Olinda'       , null  , 'Rua gandalf'              , 0       , 'Diabolica', 'Meneses'    , 'da Cunha'  , '4764454' , 'i4hfi4h' , false , '2017-02-05' , 'mmmmm@outlook.com'           , '1997-12-11');
  1368.  
  1369.  
  1370. -- TABELA DE TELEFONE DE CLIENTE
  1371.  
  1372. CREATE TABLE telefone_cliente (
  1373.    
  1374.     CPF char(11) not null,
  1375.     telefone char(10),
  1376.     CONSTRAINT telefone_cliente_pk primary key (CPF, telefone),
  1377.     CONSTRAINT fk_telefonecliente foreign key (CPF) references cliente (CPF) ON DELETE CASCADE ON UPDATE CASCADE
  1378. );
  1379.  
  1380. -- inserindo valores na tabela telefone_cliente
  1381.  
  1382. INSERT INTO telefone_cliente ( CPF, telefone) VALUES
  1383.  
  1384.  ('54501233290' , '8134564432'),
  1385.  ('25251145314' , '8199890765'),
  1386.  ('77658476358' , '8599690359'),
  1387.  ('07617589689' , '4899097820'),
  1388.  ('07617589689' , '8191913012'),
  1389.  ('96468793068' , '7934876650'),
  1390.  ('96468793068' , '7988786534'),
  1391.  ('50519774647' , '8133234567');
  1392.  
  1393. -- TABELA DE SUGESTAO
  1394.  
  1395. CREATE TABLE sugestao (
  1396.    
  1397.     CPF_cliente char(11),
  1398.     id char(4),
  1399.     dia date,
  1400.     descricao varchar(30) not null,
  1401.     primary key (id , CPF_cliente),
  1402.     CONSTRAINT fk_sugestao foreign key (CPF_cliente) references cliente (CPF) ON DELETE CASCADE ON UPDATE CASCADE
  1403. );
  1404.  
  1405. -- inserindo valores na tabela sugestao
  1406.  
  1407. INSERT INTO  sugestao (CPF_cliente, id, dia , descricao) VALUES
  1408.  
  1409. ('54501233290' , '0001' , '2016-02-12' , 'mais entregadores'),
  1410. ('54501233290' , '0002' , '2016-03-09' , 'mais entregadores'),
  1411. ('54501233290' , '0003' , '2016-03-09' , 'mais entregadores'),
  1412. ('54501233290' , '0004' , '2016-03-09' , 'mais entregadores'),
  1413. ('54501233290' , '0005' , '2016-03-09' , 'mais entregadores'),
  1414. ('54501233290' , '0006' , '2016-03-09' , 'mais entregadores'),
  1415. ('54501233290' , '0007' , '2016-03-09' , 'mais entregadores'),
  1416. ('54501233290' , '0008' , '2016-03-09' , 'mais entregadores'),
  1417. ('54501233290' , '0009' , '2016-03-09' , 'mais entregadores');
  1418.  
  1419.  
  1420. -- TABELA DE RECLAMACAO
  1421.  
  1422. CREATE TABLE reclamacao (
  1423.  
  1424.     CPF_cliente char(11),
  1425.     id char(4),
  1426.     descricao varchar(30),
  1427.     motivo varchar(70),
  1428.     data_ocorrido date,
  1429.     data_reclamacao date,
  1430.     primary key (id, CPF_cliente),
  1431.     CONSTRAINT fk_reclamacao foreign key (CPF_cliente) references cliente (CPF) ON DELETE CASCADE ON UPDATE CASCADE
  1432. );
  1433.  
  1434. -- inserindo valores na tabela reclamacao
  1435.  
  1436. INSERT INTO reclamacao ( CPF_cliente , id , motivo, data_ocorrido, data_reclamacao) VALUES
  1437.  
  1438. ('54501233290' , '0001' , 'atendimento demorado', '2016-03-09' , '2016-03-13'),
  1439. ('54501233290' , '0002' , 'perto de vencer', '2016-03-09' , '2016-03-13'),
  1440. ('54501233290' , '0003' , 'o entregador nao chegou', '2016-03-01' , '2016-03-20'),
  1441. ('54501233290' , '0004' , 'erraram o endereco de entrega', '2016-03-09' , '2016-03-13'),
  1442. ('54501233290' , '0005' , 'atendimento muito demorado', '2016-03-09' , '2016-03-10'),
  1443. ('54501233290' , '0006' , 'site fora do ar', '2016-04-01' , '2016-04-01'),
  1444. ('54501233290' , '0007' , 'atendimento muito demorado', '2016-03-09' , '2016-03-13');
  1445.  
  1446.  
  1447. -- TABELA DE PROMOCAO
  1448.  
  1449. CREATE TABLE promocao (
  1450.  
  1451.     cod char(4),
  1452.     seq_filial char(4),
  1453.     cnpj_matriz char(14),
  1454.     nome varchar(30),
  1455.     data_inicio date not null,
  1456.     percentual_reducao float not null CHECK ( percentual_reducao > 0 ),
  1457.     data_fim date,
  1458.     obs varchar(30),
  1459.     descricao varchar(50),
  1460.     primary key (cod),
  1461.     foreign key (seq_filial, cnpj_matriz ) references filial (seq , CNPJ_Matriz)
  1462.  
  1463. );
  1464.  
  1465. -- inserindo valores na tabela promocao
  1466.  
  1467. INSERT INTO promocao (cod, seq_filial , cnpj_matriz , nome , data_inicio, percentual_reducao , data_fim, obs, descricao) VALUES
  1468.  
  1469. ('0001' , '0001 ',  '23416393000114', 'Um barato no pedaco' , '2017-02-05', 0.13 , '2017-03-05' , 'n inclui importados' , 'promocao para laticinios'),
  1470. -- ('0002' , '0001' ,  '23416393000114', 'Queima de estoque' , '2016-12-22' , 0.33 , '2016-12-31' , NULL , NULL),
  1471. ('0003' , '0002',  '23416393000114','Mes da verdura' , '2017-08-12' , 0.26 , '2017-10-12' , 'n inclui conservas' , 'promocao para hortifruti' ),
  1472. ('0004' , '0002' ,  '23416393000114', 'Mes da conserva' , '2017-11-20' , 0.21 , '2017-12-20', NULL , 'apenas conservas' ),
  1473. -- ('0005 ', '0001' , '23416393000114', 'Desinfetantes em queima' , '2018-01-02' , 0.24 , '2018-02-02' , NULL , 'apenas desinfetantes'),
  1474. ('0006' , '0002' , '23416393000114', 'Promocao de amaciante' , '2017-09-15' , 0.12, '2017-11-15' , NULL , 'amaciantes nacionais'),
  1475. -- ('0007' , '0001' , '23416393000114', 'Laticinios vao a loucura' , '2018-01-02', 0.29, '2018-03-02' , NULL , 'reducao de preco laticinios'),
  1476. ('0008' , '0002' ,  '23416393000114','Promocao de Sabao em po' , '2017-06-15' , 0.19 , '2017-07-15' , NULL , 'promocao sabao em po');
  1477.  
  1478.  
  1479. -- TABELA DE ITEM PEDIDO
  1480.  
  1481. CREATE TABLE item_pedido (
  1482.  
  1483.     seq char(4),
  1484.     cod_produto_ref char(4),
  1485.     cod_pedido_fornecedor char(4)   ,
  1486.     quantidade int not null CHECK (quantidade > 0) ,
  1487.     preco_unitario float  not null CHECK (preco_unitario  > 0),
  1488.     primary key (cod_produto_ref , cod_pedido_fornecedor),
  1489.     key(seq),
  1490.     foreign key (cod_produto_ref) references produto_ref (cod),
  1491.     foreign key (cod_pedido_fornecedor) references pedido_fornecedor(cod)
  1492. );
  1493.  
  1494.  
  1495.  
  1496. INSERT INTO item_pedido (seq , cod_produto_ref , cod_pedido_fornecedor, quantidade , preco_unitario) VALUES
  1497.  
  1498. ('0001' , '0001' ,'0001' , 10 , 10.4),
  1499. ('0002' , '0002' ,'0002' , 5 , 4.60),
  1500. ('0003' , '0003' ,'0003' , 40 , 5.44),
  1501. ('0004' , '0004' ,'0004' , 12 , 4.41),
  1502. ('0005' , '0005' ,'0005' , 7 , 6.43),
  1503. ('0006' , '0006' ,'0006' , 9 , 7.2),
  1504. ('0007' , '0007' ,'0007' , 3 , 8.3),
  1505. ('0008' , '0008' ,'0008' , 5 , 9);
  1506.  
  1507.  
  1508.  
  1509.  
  1510. -- TABELA DE LOTE
  1511. CREATE TABLE lote (
  1512.    
  1513.     cod char(4),
  1514.     cod_pedido_fornecedor char(4),
  1515.     descricao varchar(30),
  1516.     data_chegada date not null,
  1517.     primary key( cod) ,
  1518.     foreign key (cod_pedido_fornecedor) references pedido_fornecedor (cod)
  1519.  
  1520. );
  1521.  
  1522.  
  1523. INSERT INTO lote ( cod, cod_pedido_fornecedor, descricao , data_chegada) VALUES
  1524.  
  1525. ( '0001' , '0001', 'Lote de coca-cola ' , '2017-03-03' ),
  1526. ( '0002' , '0002', 'Lote de pepsi ' , '2017-03-20' ),
  1527. ( '0003' , '0003', 'Lote de margarina deline ' , '2017-05-03' ),
  1528. ( '0004' , '0004', 'Lote de açucar pretinho ' , '2017-02-03' ),
  1529. ( '0005' , '0005', null , '2017-04-03' );
  1530.  
  1531.  
  1532. -- TABELA DE ITEM DE ESTOQUE
  1533.  
  1534. CREATE TABLE item_estoque (
  1535.    
  1536.     cod_lote char(4),
  1537.     id_estoque char(4),
  1538.     cod_produto char(4),
  1539.     id_avaria char(4),
  1540.     id_prateleira char(4),
  1541.     data_validade date ,
  1542.     data_fabricacao date,
  1543.     data_entrada date not null,
  1544.     valor_compra float CHECK (valor_compra > 0),
  1545.     quantidade int CHECK (quantidade > 0),
  1546.     primary key (cod_lote, id_estoque , cod_produto),
  1547.     foreign key (cod_lote) references lote (cod),
  1548.     foreign key (id_estoque) references estoque (id),
  1549.     CONSTRAINT fk_produtoestoque foreign key ( cod_produto ) references produto_ref( cod) ON DELETE CASCADE ON UPDATE CASCADE,
  1550.     foreign key (id_avaria ) references avaria ( id),
  1551.     foreign key (id_prateleira ) references prateleira ( codigo)
  1552.    
  1553. );
  1554.  
  1555.  
  1556. INSERT INTO item_estoque (cod_lote, id_estoque , cod_produto , id_avaria , id_prateleira , data_validade ,
  1557.  data_fabricacao , data_entrada , valor_compra, quantidade)  VALUES
  1558.  ('0001' , '0001' , '0001' , null , '0001', '2018-04-04' , '2018-01-10' , '2018-01-14' , 60.30 , 8 ),
  1559.  ('0002' , '0001' , '0002' , null , '0010', '2018-03-04' , '2018-01-04' , '2018-01-14' , 200 , 20 ),
  1560.  ('0003' , '0002' , '0003' , null , '0006', '2018-04-05' , '2018-01-02' , '2018-01-14' , 43.70 , 10 ),
  1561.  ('0004' , '0003' , '0004' , null , '0011', '2018-02-07' , '2018-01-10' , '2018-01-14' , 24.20 , 4 ),
  1562.  ('0005' , '0004' , '0005' , null , '0013', '2018-01-01' , '2017-11-10' , '2018-01-14' , 90.32 , 12 );
  1563.  
  1564.  
  1565.  
  1566. -- TABELA DE PERDA DE PRODUTO
  1567.  
  1568. CREATE TABLE perda (
  1569.  
  1570.     seq char(4),
  1571.     cod_lote char(4),
  1572.     cod_produto char(4),
  1573.     id_estoque char(4),
  1574.     cpf_gerente char(11),
  1575.     dia date not null ,
  1576.     quantidade_perdida int CHECK (quantidade_perdida > 0) ,
  1577.     motivo varchar(80),
  1578.     primary key (seq , cod_lote , cod_produto , id_estoque),
  1579.     foreign key (cpf_gerente) references gerente (cpf),
  1580.     CONSTRAINT fk_perdaitestoq foreign key ( id_estoque ) references item_estoque(id_estoque ),
  1581.     CONSTRAINT fk_perdalote foreign key (cod_lote) references item_estoque (cod_lote),
  1582.     CONSTRAINT fk_perdaprod foreign key (cod_produto) references item_estoque (cod_produto)  ON DELETE CASCADE ON UPDATE CASCADE
  1583. );
  1584.  
  1585. INSERT INTO perda ( seq , cod_lote, cod_produto, id_estoque, cpf_gerente , dia , quantidade_perdida , motivo) VALUES
  1586. ( '0001' , '0001' , '0002', '0001' , null , '2018-03-04' , 2 , ' nao descoberto ' ),
  1587. ( '0002' , '0003' , '0004', '0002' , null , '2018-03-06' , 6 , ' nao descoberto ' );
  1588.  
  1589.  
  1590.  
  1591. -- TABELA DE RELACIONAMENTO TEM DE FUNCIONARIO
  1592.  
  1593. CREATE TABLE tem (
  1594.      
  1595.     id_jornada char(4),
  1596.     id_turno char(4),
  1597.     id_dia char(4),
  1598.     primary key (id_jornada , id_turno , id_dia ),
  1599.     foreign key (id_jornada) references jornadatrabalho(id),
  1600.     foreign key (id_turno) references turno(cod),
  1601.     foreign key (id_dia) references dia (sequencial)
  1602.    
  1603. );
  1604.  
  1605. -- inserindo valores na tabela tem
  1606.  
  1607. INSERT INTO tem (id_jornada , id_turno, id_dia ) VALUES
  1608.  
  1609. ('0001' , '0001' , '0001' ),
  1610. ('0001' , '0002' , '0002' ),
  1611. ('0001' , '0003' , '0003' ),
  1612. ('0001' , '0002' , '0004' ),
  1613. ('0001' , '0002' , '0005' ),
  1614. ('0001' , '0002' , '0006' ),
  1615. ('0001' , '0001' , '0007' ),
  1616. ('0002' , '0010' , '0001' ),
  1617. ('0002' , '0006' , '0005' ),
  1618. ('0003' , '0009' , '0006' ),
  1619. ('0004' , '0001' , '0001' );
  1620.  
  1621.  
  1622. -- TABELA RELACIONAMENTO REALIZA CURSO
  1623.  
  1624. CREATE TABLE realizacurso (
  1625.    
  1626.     cpf_fun char(11),
  1627.     id_curso char(4),
  1628.     dt_inicio date,
  1629.     dt_fim date,
  1630.     primary key (cpf_fun , id_curso),
  1631.     foreign key (cpf_fun) references funcionario(CPF),
  1632.     foreign key (id_curso) references curso (id)
  1633. );
  1634.  
  1635. -- inserindo valores na tabela realiza_curso
  1636.  
  1637. INSERT INTO realizacurso ( cpf_fun , id_curso ,  dt_inicio , dt_fim) VALUES
  1638.  
  1639. ( '77491222226', '2101' , '2016-02-13' , '2016-03-15' ),
  1640. ('77491222226' , '8012' , '2016-04-09' , '2016-06-09' ),
  1641. ( '32568071001' , '2101' ,'2016-02-13' , '2016-03-15' ),
  1642. ( '45321186898' , '8012' , '2016-04-09' , '2016-06-09' ),
  1643. ('14073416260' , '8012' , '2016-04-09' , '2016-06-09' ),
  1644. ( '88356795591' , '8012' , '2016-04-09' , '2016-06-09' );
  1645.  
  1646. -- TABELA DE GERENCIA DE ESTOQUE/* createdyes 13 show
  1647.  
  1648. CREATE TABLE gerencia_estoque (
  1649.    
  1650.     cpf_super_estoque char(11),
  1651.     id_estoque char(4),
  1652.     dt_fim date,
  1653.     dt_inicio date,
  1654.     primary key (cpf_super_estoque , id_estoque),
  1655.     foreign key (cpf_super_estoque) references SupervisorEstoque (CPF),
  1656.     foreign key (id_estoque) references estoque(id)
  1657.    
  1658. );
  1659.  
  1660.  
  1661.  
  1662. -- inserindo valores na tabela gerencia_estoque
  1663.  
  1664. INSERT INTO gerencia_estoque (cpf_super_estoque, id_estoque , dt_fim, dt_inicio) VALUES
  1665.  
  1666. ('14073416260' , '0001' ,'2017-03-02' ,'2016-03-02'),
  1667. ( '85902755239' , '0002' , '2017-04-15' , '2016-02-15'),
  1668. ( '88356795591' , '0003' , '2016-01-01' , '2014-02-13'),
  1669. ( '14073416260' , '0002 ' , '2013-06-22' , '2012-06-10'),
  1670. ( '85902755239' , '0003'  ,'2017-09-02', '2016-01-02' );
  1671.  
  1672.  
  1673.  
  1674.  
  1675. -- VIEWS
  1676.  
  1677. create view produtodate as select * from produto_ref inner join item_estoque on produto_ref.cod = item_estoque.cod_produto
  1678. left join avaria on item_estoque.id_avaria = avaria.id;
  1679.  
  1680. create view comprabetweendates as select * from compra inner join item_compra on compra.codd = item_compra.cod_compra inner join produto_ref
  1681. on produto_ref.cod = item_compra.cod_produto inner join marca on marca.codmarca = produto_ref.id_marca
  1682. inner join categoria on categoria.codcategoria = produto_ref.id_categoria
  1683. inner join subcategoria on subcategoria.cod_categoria = categoria.codcategoria
  1684. and subcategoria.codsubcategoria = produto_ref.id_subcategoria;
  1685.  
  1686.  
  1687.  
  1688.  
  1689. -- FUNCOES, TRIGGERS E PROCEDURES
  1690.  
  1691.  
  1692.     -- Função que retorna o total a ser pago pelo Pedido do Fornecedor
  1693.     -- recebe como argumentos
  1694. delimiter ;;
  1695.  
  1696. CREATE function totalPedidoFornecedor ( cod char(4) )
  1697. returns float
  1698. deterministic
  1699. begin
  1700.        
  1701.         declare valor float;
  1702.         declare quatd int;
  1703.                     select SUM( preco_unitario ) into valor
  1704.                     FROM item_pedido
  1705.                     WHERE cod_pedido_fornecedor = cod;
  1706.                    
  1707.                     select SUM( quantidade ) into quatd
  1708.                     FROM item_pedido
  1709.                     WHERE cod_pedido_fornecedor = cod;
  1710.                    
  1711.         return valor*quatd;
  1712.        
  1713.        
  1714.        
  1715. end ;;
  1716.  
  1717.  
  1718.  
  1719.  
  1720.  
  1721. /*  Função que retorna se o produto está fora da validade
  1722.     Caso o produto esteja vencido , ele retorna TRUE ,
  1723.     caso contrário retorna FALSE
  1724. */
  1725.  
  1726. delimiter ;;
  1727.  
  1728. create function estaVencido ( cod char(4), numeroDias int )
  1729. returns boolean
  1730.  
  1731. begin
  1732.  
  1733.         declare result boolean;
  1734.         declare validade date;
  1735.         declare hoje date;
  1736.  
  1737.         select data_validade into validade
  1738.         from item_estoque
  1739.         where cod_produto = cod;
  1740.        
  1741.        
  1742.         select date_add(CURDATE(), INTERVAL numeroDias DAY) into hoje;
  1743.  
  1744.        
  1745.         if datediff(validade , hoje ) < 0 then
  1746.             set result = true;
  1747.        
  1748.         else
  1749.             set result = false;
  1750.         end if;
  1751.        
  1752.         return result;
  1753. end ;;
  1754.  
  1755.  
  1756.  
  1757.  
  1758. delimiter ;;
  1759.  
  1760. create function produtosuficiente ( cod char(4) , quantidade2 int )
  1761. returns boolean
  1762.  
  1763. begin
  1764.    
  1765.     declare resultado boolean;
  1766.     declare qtdcomp int;
  1767.    
  1768.    
  1769.     select SUM(quantidade) into qtdcomp
  1770.     from item_estoque
  1771.     where cod_produto = cod;
  1772.    
  1773.     if quantidade2 > qtdcomp then
  1774.         set resultado = false;
  1775.    
  1776.     else
  1777.         set resultado = true;
  1778.        
  1779.     end if;
  1780.    
  1781.     return resultado;
  1782.    
  1783. end ;;
  1784.  
  1785.  
  1786.  
  1787. delimiter ;;
  1788.  
  1789. create trigger funcionarioadicionado
  1790. after insert on funcionario
  1791. for each row
  1792. begin
  1793.    
  1794.     if new.seq_filial is not null then
  1795.         update filial
  1796.         set qtd_func = qtd_func + 1
  1797.         where seq = new.seq_filial;
  1798.    
  1799.     end if;
  1800.    
  1801. end ;;
  1802.  
  1803. delimiter ;;
  1804.  
  1805. create trigger funcionarioremovido
  1806. after delete on funcionario
  1807. for each row
  1808. begin
  1809.  
  1810.     if old.seq_filial is not null then
  1811.         update filial
  1812.         set qtd_func = qtd_func - 1
  1813.         where seq = old.seq_filial;
  1814.        
  1815.     end if;
  1816.  
  1817. end ;;
  1818.  
  1819.  
  1820. delimiter ;;
  1821.  
  1822. create trigger adicionaveiculogaragem
  1823. after insert on veiculo
  1824. for each row
  1825. begin
  1826.        
  1827.         if new.cod_garagem is not null then
  1828.             update garagem
  1829.             set num_veiculos_atual = num_veiculos_atual + 1
  1830.             where cod = new.cod_garagem;
  1831.        
  1832.         end if;
  1833.  
  1834.  
  1835. end ;;
  1836.  
  1837. delimiter ;;
  1838.  
  1839. create trigger removeveiculogaragem
  1840. after delete on veiculo
  1841. for each row
  1842. begin
  1843.  
  1844.     if old.cod_garagem is not null then
  1845.         update garagem
  1846.         set num_veiculos_atual = num_veiculos_atual - 1
  1847.         where cod = old.cod_garagem;
  1848.        
  1849.     end if;
  1850.  
  1851. end ;;
  1852.  
  1853.  
  1854.  
  1855.  
  1856. delimiter ;;
  1857.  
  1858. create trigger descontocompraprogamada
  1859. after insert on compra_programada
  1860. for each row
  1861. begin
  1862.    
  1863.     declare percentualdesconto float;
  1864.    
  1865.     if new.cod is not null then
  1866.    
  1867.     select percentual_reducao into percentualdesconto
  1868.     from promocao
  1869.     join filial
  1870.     on promocao.seq_filial = filial.seq
  1871.     join cliente
  1872.     on cliente.seq_filial = filial.seq
  1873.     join compra
  1874.     on cliente.CPF = compra.cpfcliente
  1875.     where new.cod = compra.codd;
  1876.    
  1877.     update compra
  1878.     set valor_total_desconto = valor_total * percentualdesconto
  1879.     where new.cod = compra.codd;
  1880.    
  1881.    
  1882.     end if;
  1883.  
  1884. end ;;
  1885.  
  1886.  
  1887.  
  1888. delimiter ;;
  1889.  
  1890. create procedure updateaftercompra( in codcompra char(4) )
  1891. begin
  1892.    
  1893.         declare done int default 0;
  1894.         declare qtd int;
  1895.         declare codigoproduto char(4);
  1896.         declare itemcompraqtd cursor for select quantidade , cod_produto
  1897.                                         from item_compra
  1898.                                         where item_compra.cod_compra = codcompra;
  1899.                                        
  1900.         declare continue handler for not found set done = 1;
  1901.        
  1902.         open itemcompraqtd;
  1903.         stoop : LOOP
  1904.            
  1905.             fetch itemcompraqtd into qtd, codigoproduto;
  1906.            
  1907.             if done = 1 then
  1908.             leave stoop;
  1909.             end if;
  1910.        
  1911.            
  1912.             update item_estoque
  1913.             set quantidade =  (item_estoque.quantidade-qtd)
  1914.             where item_estoque.cod_produto = codigoproduto and item_estoque.quantidade > qtd;
  1915.            
  1916.            
  1917.         END LOOP stoop;
  1918.            
  1919.            
  1920.         close itemcompraqtd;
  1921.  
  1922. end ;;
  1923.  
  1924.  
  1925.  
  1926. delimiter ;;
  1927.  
  1928. create procedure gerarfaturaepagamento( in cod char(4) , in dataemissao date  )
  1929. begin
  1930.        
  1931.         declare datavencimento date;
  1932.         declare codfatura mediumint;
  1933.        
  1934.         select date_add(dataemissao, INTERVAL 15 DAY) into datavencimento;
  1935.        
  1936.        
  1937.         insert into fatura  ( cod_pedido_fornecedor , data_vencimento , valor_pago_atual, valor_total_final ,
  1938.         data_emissao , statos , data_paga , multa ) values
  1939.         (cod , datavencimento , null , null , dataemissao , false , null , null );
  1940.        
  1941.         select id into codfatura
  1942.         from fatura
  1943.         where fatura.cod_pedido_fornecedor = cod;
  1944.  
  1945.        
  1946.         insert into pagamento ( id_fatura ,cod_compra , cod_tipo_pagamento  , valor_pago , dia ,  statos, tipo_pagamento ) values
  1947.         (codfatura , null , null , null , null , false , null);
  1948.    
  1949.  
  1950.  
  1951. end ;;
  1952.  
  1953.  
  1954.  
  1955.  
  1956.  
  1957. SET foreign_key_checks = 1;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement