Advertisement
LeoMonte

rtghere

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