-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcreate-tables-zoboomafoo.sql
185 lines (145 loc) · 5.69 KB
/
create-tables-zoboomafoo.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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
-- Created by: João Felipe Fonseca Nascimento
-- joao.nascimento98@academico.ifs.edu.br - 2020
/* SCRIPT DE CRIAÇÃO DAS TABELA DA PRATICA DE ADM DE BANCO - ZOBOOMAFOO */
USE [Zoboomafoo]
PRINT 'Creating tables with their CONSTRAINTs'
PRINT 'Table creation: Cargos'
CREATE TABLE cargos
(
cod_cargo INT NOT NULL CHECK(cod_cargo > 0),
nom_cargo VARCHAR(40) NOT NULL,
salario NUMERIC(9,2) NOT NULL CHECK(salario > 0),
CONSTRAINT cargo_pk PRIMARY KEY(cod_cargo)
);
--------------------------------------------------------------------
PRINT 'Table creation: Cidades'
CREATE TABLE cidades
(
cod_cidade INT NOT NULL CHECK(cod_cidade > 0),
nm_cidade VARCHAR(50) NOT NULL,
CONSTRAINT cidade_pk PRIMARY KEY(cod_cidade)
);
--------------------------------------------------------------------
PRINT 'Table creation: Enderecos'
CREATE TABLE enderecos
(
cod_endereco INT NOT NULL CHECK(cod_endereco > 0),
cod_cidade INT NOT NULL,
nm_rua VARCHAR(150) NOT NULL,
nm_bairro VARCHAR(50) NOT NULL,
numero NUMERIC(10) NOT NULL,
CONSTRAINT endereco_pk PRIMARY KEY(cod_endereco),
CONSTRAINT endereco_fk_cidade FOREIGN KEY(cod_cidade) REFERENCES cidades (cod_cidade)
);
--------------------------------------------------------------------
PRINT 'Table creation: Funcionarios'
CREATE TABLE funcionarios
(
matricula INT NOT NULL CHECK( matricula > 0 ),
cod_endereco INT NOT NULL,
cod_cargo INT NOT NULL,
rg NUMERIC(10) NOT NULL,
cpf VARCHAR(11) NOT NULL,
crmv NUMERIC(10) NOT NULL,
nome VARCHAR(250) NOT NULL,
dt_nascimento smalldatetime NOT NULL,
CONSTRAINT funcionario_pk PRIMARY KEY(matricula),
CONSTRAINT funcionario_fk_endereco FOREIGN KEY(cod_endereco) REFERENCES enderecos (cod_endereco),
CONSTRAINT funcionario_fk_cargo FOREIGN KEY(cod_cargo) REFERENCES cargos (cod_cargo)
);
--------------------------------------------------------------------
PRINT 'Table creation: Classes'
CREATE TABLE classes
(
cod_classe INT NOT NULL CHECK(cod_classe > 0),
nm_classe VARCHAR(50) NOT NULL,
descricao VARCHAR(500) NOT NULL,
CONSTRAINT classe_pk PRIMARY KEY(cod_classe)
);
--------------------------------------------------------------------
PRINT 'Table creation: Alas'
CREATE TABLE alas
(
cod_ala INT NOT NULL CHECK(cod_ala > 0),
cod_classe INT NOT NULL,
num_ala NUMERIC(10) NOT NULL,
CONSTRAINT ala_pk PRIMARY KEY(cod_ala),
CONSTRAINT ala_fk_classe FOREIGN KEY(cod_classe) REFERENCES classes(cod_classe)
);
--------------------------------------------------------------------
PRINT 'Table creation: Conteiners'
CREATE TABLE conteiners
(
cod_conteiner INT NOT NULL CHECK(cod_conteiner > 0),
cod_ala INT NOT NULL,
tipo VARCHAR(50) NOT NULL,
CONSTRAINT conteiner_pk PRIMARY KEY(cod_conteiner),
CONSTRAINT conteiners_fk_ala FOREIGN KEY(cod_ala) REFERENCES alas(cod_ala)
);
--------------------------------------------------------------------
PRINT 'Table creation: Animais'
CREATE TABLE animais
(
cod_animal INT NOT NULL CHECK(cod_animal > 0),
cod_conteiner INT NOT NULL,
cod_classe INT NOT NULL,
altura NUMERIC(5,2) NOT NULL,
cor VARCHAR(50) NOT NULL,
especie VARCHAR(50) NOT NULL,
nome VARCHAR(150) NOT NULL,
CONSTRAINT animal_pk PRIMARY KEY(cod_animal),
CONSTRAINT animal_fk_conteiner FOREIGN KEY(cod_conteiner) REFERENCES conteiners(cod_conteiner),
CONSTRAINT animal_fk_classe FOREIGN KEY(cod_classe) REFERENCES classes(cod_classe)
);
--------------------------------------------------------------------
PRINT 'Table creation: Historico de Consultas'
CREATE TABLE historico_consultas
(
cod_consulta INT NOT NULL CHECK(cod_consulta > 0),
matricula INT NOT NULL,
cod_animal INT NOT NULL,
dt_consulta smalldatetime NOT NULL,
CONSTRAINT historico_consulta_pk PRIMARY KEY( cod_consulta ),
CONSTRAINT historico_consulta_fk_funcionario FOREIGN KEY (matricula) REFERENCES funcionarios (matricula),
CONSTRAINT historico_consulta_fk_animal FOREIGN KEY (cod_animal) REFERENCES animais (cod_animal)
);
--------------------------------------------------------------------
PRINT 'Table creation: Limpezas'
CREATE TABLE limpezas
(
cod_limpeza INT NOT NULL CHECK(cod_limpeza > 0),
cod_conteiner INT NOT NULL,
cod_classe INT NOT NULL,
dt_limpeza smalldatetime NOT NULL,
CONSTRAINT limpeza_pk PRIMARY KEY(cod_limpeza),
CONSTRAINT limpezas_fk_conteiner FOREIGN KEY (cod_conteiner) REFERENCES conteiners (cod_conteiner),
CONSTRAINT limpezas_fk_classe FOREIGN KEY (cod_classe) REFERENCES classes (cod_classe)
);
--------------------------------------------------------------------
PRINT 'Table creation: Acompanha'
CREATE TABLE acompanha
(
cod_animal INT NOT NULL CHECK(cod_animal > 0),
matricula INT NOT NULL CHECK(matricula > 0),
CONSTRAINT acompanha_pk PRIMARY KEY(cod_animal, matricula),
CONSTRAINT acompanha_fk_animal FOREIGN KEY (cod_animal) REFERENCES animais (cod_animal),
CONSTRAINT acompanha_fk_funcionario FOREIGN KEY (matricula) REFERENCES funcionarios (matricula)
);
--------------------------------------------------------------------
PRINT 'Table creation: adm-log'
CREATE TABLE admlog
(
cod_log INT NOT NULL CHECK(cod_log > 0) IDENTITY(1, 1) PRIMARY KEY,
salario_anterior NUMERIC(9,2) NOT NULL CHECK(salario_anterior > 0),
salario_atual NUMERIC(9,2) NOT NULL CHECK(salario_atual > 0),
updated_at smalldatetime NOT NULL,
ip VARCHAR(150) NOT NULL,
usuario VARCHAR(150) NOT NULL
)
PRINT 'Table creation: sumario-especies'
CREATE TABLE sumario_especies
(
cod_sumario_especies INT NOT NULL CHECK(cod_sumario_especies > 0) IDENTITY(1, 1) PRIMARY KEY,
especie VARCHAR(150) NOT NULL,
quantidade NUMERIC(9) NOT NULL
)