This repository has been archived by the owner on Oct 19, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcreate2.sql
107 lines (97 loc) · 3.83 KB
/
create2.sql
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
-- Drop tables if they exist
DROP TABLE IF EXISTS Pessoa;
DROP TABLE IF EXISTS Cliente;
DROP TABLE IF EXISTS Vendedor;
DROP TABLE IF EXISTS Venda;
DROP TABLE IF EXISTS Veiculo;
DROP TABLE IF EXISTS Pagamento;
DROP TABLE IF EXISTS Marca;
DROP TABLE IF EXISTS Representante;
DROP TABLE IF EXISTS RepresentanteMarca;
DROP TABLE IF EXISTS RepresentanteVendedor;
-- Tabela Pessoa
CREATE TABLE Pessoa (
idPessoa INTEGER NOT NULL PRIMARY KEY,
nome TEXT,
dataNascimento DATE NOT NULL,
telemovel INTEGER,
CONSTRAINT Telemovel_UNIQUE UNIQUE (telemovel),
CONSTRAINT num_Telemovel CHECK (telemovel >= 100000000 AND telemovel <= 999999999)
);
-- Tabela Cliente
CREATE TABLE Cliente (
idCliente INTEGER NOT NULL PRIMARY KEY,
idPessoa INTEGER,
FOREIGN KEY (idPessoa) REFERENCES Pessoa (idPessoa) ON UPDATE CASCADE ON DELETE SET NULL
);
-- Tabela Vendedor
CREATE TABLE Vendedor (
idVendedor INTEGER NOT NULL PRIMARY KEY,
dataInicio DATE,
numVendas INTEGER,
salario DECIMAL,
idPessoa INTEGER,
FOREIGN KEY (idPessoa) REFERENCES Pessoa (idPessoa) ON UPDATE CASCADE ON DELETE SET NULL
);
-- Tabela Veiculo
CREATE TABLE Veiculo (
matricula TEXT NOT NULL PRIMARY KEY,
ano INTEGER,
condicao TEXT,
garantia INTEGER,
modelo TEXT,
idMarca INTEGER,
FOREIGN KEY (idMarca) REFERENCES Marca (idMarca) ON UPDATE CASCADE,
CONSTRAINT rest_Matricula CHECK (matricula GLOB '[A-Z][A-Z]-[0-9][0-9]-[A-Z][A-Z]' OR matricula GLOB '[0-9][0-9]-[A-Z][A-Z]-[0-9][0-9]' OR matricula GLOB '[0-9][0-9]-[0-9][0-9]-[A-Z][A-Z]' OR matricula GLOB '[A-Z][A-Z]-[0-9][0-9]-[0-9][0-9]')
);
-- Tabela Pagamento
CREATE TABLE Pagamento (
idPagamento INTEGER NOT NULL PRIMARY KEY,
metodoPagamento TEXT,
data DATE NOT NULL,
valor DECIMAL NOT NULL
);
-- Tabela Venda
CREATE TABLE Venda (
idVenda INTEGER NOT NULL PRIMARY KEY,
data DATE NOT NULL,
comissao DECIMAL NOT NULL,
lucro DECIMAL,
idCliente INTEGER DEFAULT 0,
idVendedor INTEGER,
matricula TEXT,
idPagamento INTEGER,
FOREIGN KEY (idCliente) REFERENCES Cliente (idCliente) ON UPDATE CASCADE ON DELETE SET NULL,
FOREIGN KEY (idVendedor) REFERENCES Vendedor (idVendedor) ON UPDATE CASCADE ON DELETE SET NULL,
FOREIGN KEY (idPagamento) REFERENCES Pagamento (idPagamento) ON UPDATE CASCADE,
FOREIGN KEY (matricula) REFERENCES Veiculo (matricula) ON UPDATE CASCADE
);
--Tabela Marca
CREATE TABLE Marca (
idMarca INTEGER NOT NULL PRIMARY KEY,
nomeMarca TEXT NOT NULL,
pais INTEGER,
ano INTEGER
);
--Tabela Representante
CREATE TABLE Representante (
idRepresentante INTEGER NOT NULL PRIMARY KEY,
nomeRepresentante TEXT NOT NULL,
dataInicio DATE
);
-- Associação entre Representante e Marca
CREATE TABLE RepresentanteMarca (
idRepresentante INTEGER,
idMarca INTEGER,
PRIMARY KEY (idRepresentante, idMarca),
FOREIGN KEY (idRepresentante) REFERENCES Representante (idRepresentante) ON UPDATE CASCADE,
FOREIGN KEY (idMarca) REFERENCES Marca (idMarca) ON UPDATE CASCADE
);
-- Tabela RepresentanteVendedor
CREATE TABLE RepresentanteVendedor (
idRepresentante INTEGER,
idVendedor INTEGER,
PRIMARY KEY (idRepresentante, idVendedor),
FOREIGN KEY (idRepresentante) REFERENCES Representante (idRepresentante) ON UPDATE CASCADE,
FOREIGN KEY (idVendedor) REFERENCES Vendedor (idVendedor) ON UPDATE CASCADE ON DELETE CASCADE
);