460 lines
13 KiB
Transact-SQL
460 lines
13 KiB
Transact-SQL
CREATE TABLE [usuarios] (
|
|
[id] int PRIMARY KEY NOT NULL IDENTITY(1, 1),
|
|
[saldo] decimal(10,2) NOT NULL,
|
|
[valor_gasto] decimal(10,2),
|
|
[login] varchar(15) NOT NULL,
|
|
[senha] varchar(255) NOT NULL,
|
|
[token] varchar(64) NOT NULL,
|
|
[email] varchar(32) NOT NULL,
|
|
[codigo_email] int NOT NULL,
|
|
[status] int NOT NULL,
|
|
[nivel] int NOT NULL,
|
|
[ultima_atividade] datetime NOT NULL
|
|
)
|
|
GO
|
|
|
|
CREATE TABLE [clientes] (
|
|
[id] int PRIMARY KEY NOT NULL IDENTITY(1, 1),
|
|
[nome] varchar(32) NOT NULL,
|
|
[email] varchar(32) NOT NULL,
|
|
[saldo] decimal(10,2) NOT NULL,
|
|
[valor_gasto] decimal(10,2),
|
|
[slug] varchar(15) NOT NULL,
|
|
[celular] varchar(15) NOT NULL,
|
|
[codigo_email] tinyint NOT NULL,
|
|
[codigo_sms] tinyint NOT NULL,
|
|
[documento] int NOT NULL,
|
|
[observacao] text NOT NULL,
|
|
[insento_fiscal] tinyint NOT NULL,
|
|
[status] int NOT NULL,
|
|
[senha] varchar(255) NOT NULL,
|
|
[token] varchar(64) NOT NULL,
|
|
[ultima_atividade] datetime NOT NULL,
|
|
[email_marketing] tinyint NOT NULL,
|
|
[cartao_presente_id] int NOT NULL
|
|
)
|
|
GO
|
|
|
|
CREATE TABLE [produtos] (
|
|
[id] int PRIMARY KEY NOT NULL IDENTITY(1, 1),
|
|
[nome] varchar(18) NOT NULL,
|
|
[preco] decimal(10,0) NOT NULL,
|
|
[estoque] int NOT NULL,
|
|
[categoria_id] int NOT NULL,
|
|
[status] int NOT NULL,
|
|
[img] varchar(30) NOT NULL,
|
|
[descricao] text NOT NULL,
|
|
[tipo_produto] int NOT NULL,
|
|
[peso] float NOT NULL,
|
|
[pais_origem] varchar(12) NOT NULL,
|
|
[marca] varchar(18) NOT NULL,
|
|
[data_cadastro_produto] datetime NOT NULL
|
|
)
|
|
GO
|
|
|
|
CREATE TABLE [categorias] (
|
|
[id] int PRIMARY KEY NOT NULL IDENTITY(1, 1),
|
|
[nome] varchar(18) NOT NULL,
|
|
[descricao] text NOT NULL
|
|
)
|
|
GO
|
|
|
|
CREATE TABLE [carrinho] (
|
|
[id] int PRIMARY KEY NOT NULL IDENTITY(1, 1),
|
|
[cliente_id] int NOT NULL,
|
|
[quantidade] int NOT NULL,
|
|
[preco] decimal(10,2) NOT NULL,
|
|
[data] datetime NOT NULL,
|
|
[status] int NOT NULL
|
|
)
|
|
GO
|
|
|
|
CREATE TABLE [fornecedores] (
|
|
[id] int PRIMARY KEY NOT NULL IDENTITY(1, 1),
|
|
[nome] varchar(18) NOT NULL,
|
|
[descricao] text NOT NULL,
|
|
[documento] int NOT NULL,
|
|
[endereco] varchar(32) NOT NULL,
|
|
[numero] int NOT NULL,
|
|
[bairro] varchar(32) NOT NULL,
|
|
[complemento] varchar(15),
|
|
[cep] int NOT NULL,
|
|
[cidade] varchar(32) NOT NULL,
|
|
[estado] varchar(15) NOT NULL,
|
|
[nome_empresa] varchar(18) NOT NULL
|
|
)
|
|
GO
|
|
|
|
CREATE TABLE [vendas] (
|
|
[id] int PRIMARY KEY NOT NULL IDENTITY(1, 1),
|
|
[data_venda] datetime NOT NULL,
|
|
[valor] decimal(10,2) NOT NULL,
|
|
[cliente_id] int NOT NULL,
|
|
[nome_destinatario] varchar(32) NOT NULL,
|
|
[metodo_pagamento] varchar(18) NOT NULL,
|
|
[desconto] decimal(10,2) NOT NULL,
|
|
[lucro_bruto] decimal(10,2),
|
|
[comentarios_cliente] text,
|
|
[end_to_end] varchar(32) NOT NULL,
|
|
[ip_cliente] varchar(32),
|
|
[status] int NOT NULL,
|
|
[cartao_presente_id] int NOT NULL
|
|
)
|
|
GO
|
|
|
|
CREATE TABLE [venda_detalhes] (
|
|
[id] int PRIMARY KEY NOT NULL IDENTITY(1, 1),
|
|
[vendas_id] int NOT NULL,
|
|
[nome_produto] varchar(18) NOT NULL,
|
|
[preco_unitario] decimal(10,2) NOT NULL,
|
|
[quantidade] int NOT NULL,
|
|
[endereco_entrega] varchar(32) NOT NULL,
|
|
[numero_entrega] int NOT NULL,
|
|
[bairro_entrega] varchar(32) NOT NULL,
|
|
[complemento_entrega] varchar(15),
|
|
[cep_entrega] int NOT NULL,
|
|
[cidade_entrega] varchar(32) NOT NULL,
|
|
[estado_entrega] varchar(15) NOT NULL,
|
|
[metodo_envio] varchar(32) NOT NULL,
|
|
[nome_transportadora] varchar(32) NOT NULL,
|
|
[custo_envio] decimal(10,2) NOT NULL,
|
|
[codigo_rastreio] int NOT NULL
|
|
)
|
|
GO
|
|
|
|
CREATE TABLE [vendas_pagamentos] (
|
|
[id] int PRIMARY KEY NOT NULL IDENTITY(1, 1),
|
|
[vendas_id] int NOT NULL,
|
|
[numero_cartao] varchar(32),
|
|
[nome_titular_cartao] varchar(32),
|
|
[data_vencimento_cartao] date,
|
|
[codigo_seguranca_cartao] int,
|
|
[cpf_titular_cartao] int,
|
|
[bandeira_cartao] int,
|
|
[codigo_barras_boletos] int,
|
|
[numero_parcelas] int,
|
|
[valor_parcelas] decimal(10,2),
|
|
[juros] decimal(10,2)
|
|
)
|
|
GO
|
|
|
|
CREATE TABLE [vendas_abandonadas] (
|
|
[id] int PRIMARY KEY NOT NULL IDENTITY(1, 1),
|
|
[vendas_id] int NOT NULL,
|
|
[produto_id] int NOT NULL,
|
|
[data_hora] datetime NOT NULL,
|
|
[motivo_abandono] text,
|
|
[valor_total] decimal(10,2) NOT NULL,
|
|
[forma_pagamento] varchar(32) NOT NULL,
|
|
[codigo_desconto] int,
|
|
[observacoes] text
|
|
)
|
|
GO
|
|
|
|
CREATE TABLE [transportadora] (
|
|
[id] int PRIMARY KEY NOT NULL IDENTITY(1, 1),
|
|
[nome_transportadora] varchar(32) NOT NULL,
|
|
[documento_cnpj] int NOT NULL,
|
|
[endereco] varchar(32) NOT NULL,
|
|
[numero] int NOT NULL,
|
|
[bairro] varchar(32) NOT NULL,
|
|
[complemento] varchar(15),
|
|
[cep] int NOT NULL,
|
|
[cidade] varchar(32) NOT NULL,
|
|
[estado] varchar(15) NOT NULL,
|
|
[telefone] varchar(32) NOT NULL,
|
|
[website] varchar(32) NOT NULL,
|
|
[email] varchar(32) NOT NULL,
|
|
[custo_frete] decimal(10,2) NOT NULL,
|
|
[estados_atuacao] varchar(32) NOT NULL,
|
|
[status] int NOT NULL
|
|
)
|
|
GO
|
|
|
|
CREATE TABLE [compras] (
|
|
[id] int PRIMARY KEY NOT NULL IDENTITY(1, 1),
|
|
[data_compra] datetime NOT NULL,
|
|
[fornecedores_id] int NOT NULL,
|
|
[valor_total] decimal(10,2) NOT NULL,
|
|
[status] int NOT NULL,
|
|
[metodo_pagamento] int NOT NULL,
|
|
[data_entrega] datetime NOT NULL,
|
|
[numero_pedido] varchar(32) NOT NULL,
|
|
[observacoes] text,
|
|
[nome_produto] varchar(32) NOT NULL
|
|
)
|
|
GO
|
|
|
|
CREATE TABLE [descontos] (
|
|
[id] int PRIMARY KEY NOT NULL IDENTITY(1, 1),
|
|
[codigo_desconto] int NOT NULL,
|
|
[tipo_desconto] varchar(32) NOT NULL,
|
|
[valor_desconto] decimal(10,2) NOT NULL,
|
|
[data_inicio] datetime NOT NULL,
|
|
[data_fim] datetime NOT NULL,
|
|
[status] int NOT NULL,
|
|
[descricao] text,
|
|
[uso_unico] int NOT NULL,
|
|
[valor_minimo] decimal(10,2)
|
|
)
|
|
GO
|
|
|
|
CREATE TABLE [taxas] (
|
|
[id] int PRIMARY KEY NOT NULL IDENTITY(1, 1),
|
|
[nome] varchar(32) NOT NULL,
|
|
[tipo_taxa] varchar(10) NOT NULL,
|
|
[valor] decimal(10,2) NOT NULL,
|
|
[estado] varchar(10),
|
|
[cidade] varchar(10),
|
|
[status] int(10),
|
|
[descricao] text,
|
|
[data_inicio] datetime,
|
|
[data_fim] datetime
|
|
)
|
|
GO
|
|
|
|
CREATE TABLE [tributos] (
|
|
[id] int PRIMARY KEY NOT NULL IDENTITY(1, 1),
|
|
[nome] varchar(32) NOT NULL,
|
|
[tipo_tributo] enum(Federal,Estadual,Municipal) NOT NULL,
|
|
[alicota] decimal(10,2) NOT NULL,
|
|
[base_calculo] enum(valor total venda,valor produto) NOT NULL,
|
|
[codigo_tributo] varchar(20) NOT NULL,
|
|
[status] int(10) NOT NULL,
|
|
[descricao] text,
|
|
[data_inicio] datetime,
|
|
[data_fim] datetime,
|
|
[regras_aplicacao] text
|
|
)
|
|
GO
|
|
|
|
CREATE TABLE [cartao_presente] (
|
|
[id] int PRIMARY KEY NOT NULL IDENTITY(1, 1),
|
|
[codigo] varchar(50) NOT NULL,
|
|
[valor] decimal(10,2) NOT NULL,
|
|
[data_emissao] datetime NOT NULL,
|
|
[data_validade] datetime NOT NULL,
|
|
[status] enum(ativo,inativo,usado,expirado,cancelado) NOT NULL DEFAULT 'ativo',
|
|
[nome_destinatario] varchar(32) NOT NULL,
|
|
[mensagem_presente] text,
|
|
[data_ativacao] datetime NOT NULL,
|
|
[regras_uso] text,
|
|
[historico_uso] JSON
|
|
)
|
|
GO
|
|
|
|
CREATE TABLE [clientes_address] (
|
|
[id] int PRIMARY KEY NOT NULL IDENTITY(1, 1),
|
|
[cliente_id] int NOT NULL,
|
|
[endereco] varchar(32) NOT NULL,
|
|
[numero] int NOT NULL,
|
|
[bairro] varchar(32) NOT NULL,
|
|
[complemento] varchar(15),
|
|
[cep] int NOT NULL,
|
|
[cidade] varchar(32) NOT NULL,
|
|
[estado] varchar(15) NOT NULL
|
|
)
|
|
GO
|
|
|
|
CREATE TABLE [clientes_produtos] (
|
|
[id] int PRIMARY KEY NOT NULL IDENTITY(1, 1),
|
|
[cliente_id] int NOT NULL,
|
|
[produto_id] int NOT NULL
|
|
)
|
|
GO
|
|
|
|
CREATE TABLE [carrinho_itens] (
|
|
[id] int PRIMARY KEY NOT NULL IDENTITY(1, 1),
|
|
[carrinho_id] int NOT NULL,
|
|
[produto_id] int NOT NULL
|
|
)
|
|
GO
|
|
|
|
CREATE TABLE [fornecedores_produtos] (
|
|
[id] int PRIMARY KEY NOT NULL IDENTITY(1, 1),
|
|
[fornecedor_id] int NOT NULL,
|
|
[produto_id] int NOT NULL,
|
|
[preco] decimal(10,2) NOT NULL,
|
|
[prazo_entrega] datetime NOT NULL
|
|
)
|
|
GO
|
|
|
|
CREATE TABLE [vendas_produtos] (
|
|
[id] int PRIMARY KEY NOT NULL IDENTITY(1, 1),
|
|
[vendas_id] int NOT NULL,
|
|
[produto_id] int NOT NULL,
|
|
[qtd_vendidos] int NOT NULL
|
|
)
|
|
GO
|
|
|
|
CREATE TABLE [vendas_transportadoras] (
|
|
[id] int PRIMARY KEY NOT NULL IDENTITY(1, 1),
|
|
[vendas_id] int NOT NULL,
|
|
[transportadora_id] int NOT NULL,
|
|
[data_inicio] datetime NOT NULL,
|
|
[data_fim] datetime NOT NULL,
|
|
[status] int NOT NULL
|
|
)
|
|
GO
|
|
|
|
CREATE TABLE [compras_produtos] (
|
|
[id] int PRIMARY KEY NOT NULL IDENTITY(1, 1),
|
|
[compras_id] int NOT NULL,
|
|
[produtos_id] int NOT NULL,
|
|
[quantidade_produto] int NOT NULL
|
|
)
|
|
GO
|
|
|
|
CREATE TABLE [descontos_produtos] (
|
|
[id] int PRIMARY KEY NOT NULL IDENTITY(1, 1),
|
|
[desconto_id] int NOT NULL,
|
|
[produto_id] int NOT NULL
|
|
)
|
|
GO
|
|
|
|
CREATE TABLE [descontos_categorias] (
|
|
[id] int PRIMARY KEY NOT NULL IDENTITY(1, 1),
|
|
[desconto_id] int NOT NULL,
|
|
[categoria_id] int NOT NULL
|
|
)
|
|
GO
|
|
|
|
CREATE TABLE [taxas_produtos] (
|
|
[id] int PRIMARY KEY NOT NULL IDENTITY(1, 1),
|
|
[taxa_id] int NOT NULL,
|
|
[produto_id] int NOT NULL
|
|
)
|
|
GO
|
|
|
|
CREATE TABLE [taxas_categorias] (
|
|
[id] int PRIMARY KEY NOT NULL IDENTITY(1, 1),
|
|
[taxa_id] int NOT NULL,
|
|
[categoria_id] int NOT NULL
|
|
)
|
|
GO
|
|
|
|
CREATE TABLE [taxas_vendas] (
|
|
[id] int PRIMARY KEY NOT NULL IDENTITY(1, 1),
|
|
[vendas_id] int NOT NULL,
|
|
[taxas_id] int NOT NULL,
|
|
[valor_taxa_item] decimal(10,2) NOT NULL
|
|
)
|
|
GO
|
|
|
|
CREATE TABLE [tributos_taxas] (
|
|
[id] int PRIMARY KEY NOT NULL IDENTITY(1, 1),
|
|
[tributo_id] int NOT NULL,
|
|
[taxas_id] int NOT NULL
|
|
)
|
|
GO
|
|
|
|
CREATE TABLE [tributos_vendas] (
|
|
[id] int PRIMARY KEY NOT NULL IDENTITY(1, 1),
|
|
[tributo_id] int NOT NULL,
|
|
[vendas_id] int NOT NULL,
|
|
[valor_tributo_item] decimal(10,2) NOT NULL
|
|
)
|
|
GO
|
|
|
|
ALTER TABLE [clientes_address] ADD CONSTRAINT [clientes_address_fk1] FOREIGN KEY ([cliente_id]) REFERENCES [clientes] ([id])
|
|
GO
|
|
|
|
ALTER TABLE [produtos] ADD CONSTRAINT [produtos_fk1] FOREIGN KEY ([categoria_id]) REFERENCES [categorias] ([id])
|
|
GO
|
|
|
|
ALTER TABLE [carrinho] ADD CONSTRAINT [carrinho_fk1] FOREIGN KEY ([cliente_id]) REFERENCES [clientes] ([id])
|
|
GO
|
|
|
|
ALTER TABLE [clientes_produtos] ADD CONSTRAINT [clientes_produtos_fk1] FOREIGN KEY ([cliente_id]) REFERENCES [clientes] ([id])
|
|
GO
|
|
|
|
ALTER TABLE [clientes_produtos] ADD CONSTRAINT [clientes_produtos_fk2] FOREIGN KEY ([produto_id]) REFERENCES [produtos] ([id])
|
|
GO
|
|
|
|
ALTER TABLE [carrinho_itens] ADD CONSTRAINT [carrinho_itens_fk1] FOREIGN KEY ([carrinho_id]) REFERENCES [carrinho] ([id])
|
|
GO
|
|
|
|
ALTER TABLE [carrinho_itens] ADD CONSTRAINT [carrinho_itens_fk2] FOREIGN KEY ([produto_id]) REFERENCES [produtos] ([id])
|
|
GO
|
|
|
|
ALTER TABLE [fornecedores_produtos] ADD CONSTRAINT [fornecedor_produtos_fk1] FOREIGN KEY ([fornecedor_id]) REFERENCES [fornecedores] ([id])
|
|
GO
|
|
|
|
ALTER TABLE [fornecedores_produtos] ADD CONSTRAINT [fornecedor_produtos_fk2] FOREIGN KEY ([produto_id]) REFERENCES [produtos] ([id])
|
|
GO
|
|
|
|
ALTER TABLE [vendas] ADD CONSTRAINT [vendas_clientes_fk1] FOREIGN KEY ([cliente_id]) REFERENCES [clientes] ([id])
|
|
GO
|
|
|
|
ALTER TABLE [vendas_produtos] ADD CONSTRAINT [vendas_produtos_fk1] FOREIGN KEY ([produto_id]) REFERENCES [produtos] ([id])
|
|
GO
|
|
|
|
ALTER TABLE [vendas_produtos] ADD CONSTRAINT [vendas_produtos_fk2] FOREIGN KEY ([vendas_id]) REFERENCES [vendas] ([id])
|
|
GO
|
|
|
|
ALTER TABLE [venda_detalhes] ADD CONSTRAINT [venda_detalhes_fk1] FOREIGN KEY ([vendas_id]) REFERENCES [vendas] ([id])
|
|
GO
|
|
|
|
ALTER TABLE [vendas_pagamentos] ADD CONSTRAINT [vendas_pagamentos_fk1] FOREIGN KEY ([vendas_id]) REFERENCES [vendas] ([id])
|
|
GO
|
|
|
|
ALTER TABLE [vendas_transportadoras] ADD CONSTRAINT [vendas_transportadoras_fk1] FOREIGN KEY ([transportadora_id]) REFERENCES [transportadora] ([id])
|
|
GO
|
|
|
|
ALTER TABLE [vendas_transportadoras] ADD CONSTRAINT [vendas_transportadoras_fk2] FOREIGN KEY ([vendas_id]) REFERENCES [vendas] ([id])
|
|
GO
|
|
|
|
ALTER TABLE [vendas_abandonadas] ADD CONSTRAINT [vendas_abandonadas_fk1] FOREIGN KEY ([vendas_id]) REFERENCES [vendas] ([id])
|
|
GO
|
|
|
|
ALTER TABLE [compras] ADD CONSTRAINT [compras_fornecedores_fk1] FOREIGN KEY ([fornecedores_id]) REFERENCES [fornecedores] ([id])
|
|
GO
|
|
|
|
ALTER TABLE [compras_produtos] ADD CONSTRAINT [compras_produtos_fk1] FOREIGN KEY ([compras_id]) REFERENCES [compras] ([id])
|
|
GO
|
|
|
|
ALTER TABLE [compras_produtos] ADD CONSTRAINT [compras_produtos_fk2] FOREIGN KEY ([produtos_id]) REFERENCES [produtos] ([id])
|
|
GO
|
|
|
|
ALTER TABLE [descontos_produtos] ADD CONSTRAINT [descontos_produtos_fk1] FOREIGN KEY ([desconto_id]) REFERENCES [descontos] ([id])
|
|
GO
|
|
|
|
ALTER TABLE [descontos_produtos] ADD CONSTRAINT [descontos_produtos_fk2] FOREIGN KEY ([produto_id]) REFERENCES [produtos] ([id])
|
|
GO
|
|
|
|
ALTER TABLE [descontos_categorias] ADD CONSTRAINT [descontos_categorias_fk1] FOREIGN KEY ([desconto_id]) REFERENCES [descontos] ([id])
|
|
GO
|
|
|
|
ALTER TABLE [descontos_categorias] ADD CONSTRAINT [descontos_categorias_fk2] FOREIGN KEY ([categoria_id]) REFERENCES [categorias] ([id])
|
|
GO
|
|
|
|
ALTER TABLE [taxas_produtos] ADD CONSTRAINT [taxas_produtos_fk1] FOREIGN KEY ([taxa_id]) REFERENCES [taxas] ([id])
|
|
GO
|
|
|
|
ALTER TABLE [taxas_produtos] ADD CONSTRAINT [taxas_produtos_fk2] FOREIGN KEY ([produto_id]) REFERENCES [produtos] ([id])
|
|
GO
|
|
|
|
ALTER TABLE [taxas_categorias] ADD CONSTRAINT [taxas_categorias_fk1] FOREIGN KEY ([taxa_id]) REFERENCES [taxas] ([id])
|
|
GO
|
|
|
|
ALTER TABLE [taxas_categorias] ADD CONSTRAINT [taxas_categorias_fk2] FOREIGN KEY ([categoria_id]) REFERENCES [categorias] ([id])
|
|
GO
|
|
|
|
ALTER TABLE [taxas_vendas] ADD CONSTRAINT [taxas_vendas_fk1] FOREIGN KEY ([vendas_id]) REFERENCES [vendas] ([id])
|
|
GO
|
|
|
|
ALTER TABLE [taxas_vendas] ADD CONSTRAINT [taxas_vendas_fk2] FOREIGN KEY ([taxas_id]) REFERENCES [taxas] ([id])
|
|
GO
|
|
|
|
ALTER TABLE [tributos_taxas] ADD CONSTRAINT [tributos_taxas_fk1] FOREIGN KEY ([tributo_id]) REFERENCES [tributos] ([id])
|
|
GO
|
|
|
|
ALTER TABLE [tributos_taxas] ADD CONSTRAINT [tributos_taxas_fk2] FOREIGN KEY ([taxas_id]) REFERENCES [taxas] ([id])
|
|
GO
|
|
|
|
ALTER TABLE [tributos_vendas] ADD CONSTRAINT [tributos_vendas_fk1] FOREIGN KEY ([tributo_id]) REFERENCES [tributos] ([id])
|
|
GO
|
|
|
|
ALTER TABLE [tributos_vendas] ADD CONSTRAINT [tributos_vendas_fk2] FOREIGN KEY ([vendas_id]) REFERENCES [vendas] ([id])
|
|
GO
|