Skip to content

Instantly share code, notes, and snippets.

@marcellobenigno
Last active August 16, 2023 13:03
Show Gist options
  • Save marcellobenigno/ec245ff68c71f325ada5cfad35ce13b8 to your computer and use it in GitHub Desktop.
Save marcellobenigno/ec245ff68c71f325ada5cfad35ce13b8 to your computer and use it in GitHub Desktop.
GeoEduc - Consultas
DROP TABLE IF EXISTS proprietario, imovel_rural, variedade, area_plantada;
-- Tabela proprietario
CREATE TABLE proprietario (
id serial,
nome varchar(60) NOT NULL,
sobrenome varchar(60) NOT NULL,
cpf char(11) NOT NULL UNIQUE,
sexo char(9) CHECK(sexo IN ('Masculino', 'Feminino')),
PRIMARY KEY(id)
);
-- Tabela imovel_rural
CREATE TABLE imovel_rural (
id serial,
nome varchar(60) NOT NULL,
area_ha numeric(10,2) CHECK(area_ha > 0),
proprietario_id integer NOT NULL,
PRIMARY KEY (id),
FOREIGN KEY (proprietario_id) REFERENCES proprietario (id)
);
-- Tabela variedade
CREATE TABLE variedade (
id serial,
nome_variedade varchar(60) NOT NULL,
PRIMARY KEY (id)
);
-- Tabela area_plantada
CREATE TABLE area_plantada (
id serial,
data_plantio date NOT NULL,
area_ha numeric(10,2) CHECK(area_ha > 0),
imovel_rural_id integer NOT NULL,
variedade_id integer NOT NULL,
PRIMARY KEY (id),
FOREIGN KEY (imovel_rural_id) REFERENCES imovel_rural (id),
FOREIGN KEY (variedade_id) REFERENCES variedade (id)
);
-- Dados da tabela proprietario
INSERT INTO proprietario (nome, sobrenome, cpf, sexo) VALUES
('João', 'da Silva', '28001238938', 'Masculino'),
('Renata', 'Melo', '17111233544', 'Feminino'),
('Maria', 'Rodrigues', '04345124510', 'Feminino'),
('Carlos', 'da Silva', '10222136539', 'Masculino');
-- Dados da tabela imovel_rural
INSERT INTO imovel_rural (nome, area_ha, proprietario_id) VALUES
('Riacho Bonito', 2600.50, 1),
('Belo Monte', 2200, 1),
('Azulão', 1900.45, 2),
('Alegria', 1600.00, 3),
('Amor Campestre', 1000.23, 3),
('Morada da Felicidade', 900.10, 4);
-- Dados da tabela variedade
INSERT INTO variedade (nome_variedade) VALUES
('Feijão'),
('Milho'),
('Arroz'),
('Cana de Açúcar');
-- Dados da tabela area_plantada
INSERT INTO area_plantada (data_plantio, area_ha, imovel_rural_id, variedade_id) VALUES
('2023/05/01', 50.00, 1, 2),
('2023/10/02', 70.00, 1, 3),
('2023/12/02', 90.00, 1, 4),
('2023/03/03', 110.00, 2, 1),
('2023/10/03', 90.00, 3, 3),
('2023/09/04', 100.00, 4, 2),
('2023/01/03', 200.50, 4, 3),
('2023/11/02', 150.50, 4, 4),
('2023/10/01', 380.00, 5, 1),
('2023/03/03', 280.50, 6, 4);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment