12 Nov 2018 16:02
* cidades
if ascan(aTabelasExistentes,lower('cidades')) != 0
else
cQuery := 'CREATE TABLE cidades (id INT UNSIGNED NOT NULL AUTO_INCREMENT,'+;
'codigo INT,'+;
'municipio VARCHAR(50),'+;
'uf CHAR(2),'+;
'data_cad date,'+;
'hora_cad char(08),'+;
'PRIMARY KEY (id)) ENGINE=InnoDB DEFAULT CHARSET=utf8'
oQuery := oServer:Query(cQuery)
if oServer:NetErr()
msginfo('Erro criando tabela : cidades : '+oServer:Error(),'Atenção')
form_main.release
endif
oQuery:Destroy()
endif
* empresas
if ascan(aTabelasExistentes,lower('empresas')) != 0
else
cQuery := 'CREATE TABLE empresas (id INT UNSIGNED NOT NULL AUTO_INCREMENT,'+;
'NOME VARCHAR(50),'+;
'CGC CHAR(18),'+;
'FANTASIA VARCHAR(50),'+;
'TELEFONE CHAR(11),'+;
'ID_CIDADE INT,'+;
'data_cad date,'+;
'hora_cad char(08),'+;
'PRIMARY KEY (id)) ENGINE=InnoDB DEFAULT CHARSET=utf8'
oQuery := oServer:Query(cQuery)
if oServer:NetErr()
msginfo('Erro criando tabela : empresas : '+oServer:Error(),'Atenção')
form_main.release
endif
oQuery:Destroy()
endif
12 Nov 2018 17:19
12 Nov 2018 17:39
12 Nov 2018 18:05
12 Nov 2018 18:53
cQuery := ' ALTER TABLE empresas ADD CONSTRAINT fk_idcidade FOREIGN KEY (idcidade)
REFERENCES cidades (id) '
oQuery := oServer:Query(cQuery)
if oServer:NetErr()
msginfo('Erro criando relacionamento : '+oServer:Error(),'Atenção')
form_main.release
endif
oQuery:Destroy()
13 Nov 2018 07:22
13 Nov 2018 08:07
13 Nov 2018 10:33
Marcos escreveu:Susviela, para o relacionamento dar certo os campos de Cidades (id) e Empresas (id_cidades) devem estar como KEY, como criar as tabelas já com os campos KEY marcado ?
cSql := "create table teste .... ; " +;
"ALTE TABLE teste .... ; " +;
"INSERT INTO teste .... ; "
16 Nov 2018 00:30
select v.DATA_VENDA AS ACUMULADO,m.ID_IMPRESSORA,p.DESCRICAO AS TIPO_PGTO,
COALESCE(sum(tp.VALOR), 0) - COALESCE(sum(v.troco),0) AS TOTAL,
COALESCE(sum(v.valor_recebido),0) AS RECEBIDO,
COALESCE(sum(v.troco),0) AS TROCO,
(select sum(valor) from sangria sg where m.id=sg.id_movimento AND sg.id_impressora=m.id_impressora AND sg.id_tipo_pagamento=p.id) as sangria,
(select sum(valor) from suprimento sp where m.id=sp.id_movimento AND sp.id_impressora=m.id_impressora AND sg.id_tipo_pagamento=p.id) as suprimento,
m.*
from venda_cabecalho v
LEFT JOIN movimento m ON (v.ID_MOVIMENTO = m.ID)
LEFT JOIN total_tipo_pgto tp ON (v.ID = tp.ID_VENDA_CABECALHO)
LEFT JOIN tipo_pagamento p ON (tp.ID_TIPO_PAGAMENTO = p.ID)
WHERE V.STATUS_VENDA <> 'C' AND v.DATA_VENDA BETWEEN '2018-11-01' AND '2018-11-31'
GROUP BY v.DATA_VENDA,m.ID_IMPRESSORA,p.id
ORDER BY v.DATA_VENDA,m.ID_IMPRESSORA,p.descricao
16 Nov 2018 08:50
dbsh escreveu:Exemplo de relacionamento sem mudar a estrutura do banco de dado