33-- Área por estado: https://pt.wikipedia.org/wiki/Lista_de_unidades_federativas_do_Brasil_por_área
44-- População por estado (prévio censo 2022): https://pt.wikipedia.org/wiki/Lista_de_unidades_federativas_do_Brasil_por_população
55
6+ drop table if exists cliente;
7+ drop table if exists funcionario;
8+ drop table if exists loja;
9+ drop table if exists item_venda;
10+ drop table if exists venda;
11+ drop table if exists estoque;
12+ drop table if exists produto;
13+ drop table if exists marca;
14+
615drop table if exists cidade;
716drop table if exists estado;
8-
917drop table if exists regiao_geografica;
1018
1119CREATE TABLE regiao_geografica (
@@ -38,6 +46,84 @@ CREATE TABLE cidade (
3846
3947CREATE UNIQUE INDEX ix_cidade ON cidade (nome, estado_id);
4048
49+ create table cliente (
50+ id serial primary key not null ,
51+ nome varchar (75 ) not null ,
52+ cpf varchar (11 ) not null ,
53+ cidade_id int not null ,
54+ data_nascimento date not null ,
55+ constraint fk_cliente_cidade foreign key (cidade_id) references cidade(id)
56+ );
57+
58+ create unique INDEX ix_cpf_cliente on cliente (cpf);
59+
60+ create table loja (
61+ id serial primary key not null ,
62+ cidade_id int not null ,
63+ data_inauguracao date not null ,
64+ constraint fk_loja_cidade foreign key (cidade_id) references cidade(id)
65+ );
66+
67+
68+ create table funcionario (
69+ id serial primary key not null ,
70+ nome varchar (75 ) not null ,
71+ cpf varchar (11 ) not null ,
72+ loja_id int not null ,
73+ data_nascimento date not null ,
74+ constraint fk_funcionario_loja foreign key (loja_id) references loja(id)
75+ );
76+
77+ create unique INDEX ix_cpf_funcionario on funcionario (cpf);
78+
79+ create table marca (
80+ id serial primary key not null ,
81+ nome varchar (200 ) not null
82+ );
83+
84+ create unique INDEX ix_marca on marca (nome);
85+
86+ create table produto (
87+ id serial primary key not null ,
88+ nome varchar (200 ) not null ,
89+ descricao varchar (5000 ) not null ,
90+ marca_id int not null ,
91+ valor decimal (10 ,2 ) not null ,
92+ constraint fk_produto_marca foreign key (marca_id) references marca(id)
93+ );
94+
95+ create table estoque (
96+ produto_id int not null ,
97+ loja_id int not null ,
98+ quant int not null ,
99+ primary key (produto_id, loja_id),
100+ constraint fk_estoque_produto foreign key (produto_id) references produto(id) on delete cascade ,
101+ constraint fk_estoque_loja foreign key (loja_id) references loja(id)
102+ );
103+
104+ create table venda (
105+ id serial primary key not null ,
106+ loja_id int not null ,
107+ cliente_id int not null ,
108+ funcionario_id int not null ,
109+ data_cadastro timestamp not null default current_timestamp ,
110+ constraint fk_venda_loja foreign key (loja_id) references loja(id),
111+ constraint fk_venda_cliente foreign key (cliente_id) references cliente(id),
112+ constraint fk_venda_funcionario foreign key (funcionario_id) references funcionario(id)
113+ );
114+
115+ create table item_venda (
116+ venda_id int not null ,
117+ produto_id int not null ,
118+ quant int not null ,
119+ valor decimal (10 ,2 ) not null ,
120+ primary key (venda_id, produto_id),
121+ constraint fk_itemvenda_venda foreign key (venda_id) references venda(id) on delete cascade ,
122+ constraint fk_itemvenda_produto foreign key (produto_id) references produto(id)
123+ );
124+
125+ -- ########################################################################################################
126+
41127INSERT INTO regiao_geografica (nome) VALUES (' Norte' ), (' Nordeste' ), (' Centro-Oeste' ), (' Sudeste' ), (' Sul' );
42128
43129INSERT INTO estado (id, nome, uf, regiao_id, area_km2, populacao) VALUES
@@ -5662,4 +5748,4 @@ update cidade set capital = true where id = 4174;
56625748update cidade set capital = true where id = 4500 ;
56635749update cidade set capital = true where id = 5353 ;
56645750update cidade set capital = true where id = 5270 ;
5665- update cidade set capital = true where id = 5514 ;
5751+ update cidade set capital = true where id = 5514 ;
0 commit comments