-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathschema.sql
More file actions
72 lines (58 loc) · 1.51 KB
/
Copy pathschema.sql
File metadata and controls
72 lines (58 loc) · 1.51 KB
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
/* Database schema to keep the structure of entire database. */
CREATE TABLE animals(
id INT GENERATED BY DEFAULT AS IDENTITY,
name VARCHAR(250),
date_of_birth DATE,
escape_attempts INT,
neutered BOOLEAN,
weight_kg FLOAT,
species VARCHAR (50)
);
CREATE TABLE owners (
id SERIAL PRIMARY KEY,
full_name VARCHAR(255)
age INT
);
CREATE TABLE species (
id SERIAL PRIMARY KEY,
name VARCHAR(255)
);
ALTER TABLE animals ADD PRIMARY KEY(id);
ALTER TABLE animals DROP COLUMN species;
ALTER TABLE animals ADD COLUMN species_id INT REFERENCES species(id);
ALTER TABLE animals ADD COLUMN owner_id INT REFERENCES owners(id);
create table vets(
id serial primary key,
name varchar(255),
age INT,
date_of_graduation date
)
create table specializations (
id serial primary key,
vet_id INT,
species_id INT,
foreign key (vet_id) references vets(id),
foreign key (species_id) references species(id)
)
create table visits (
id serial primary key,
animals_id int,
vet_id int,
visit_date date,
foreign key (animals_id) references animals(id),
foreign key (vet_id) references vets(id)
)
create index on visits (animals_id);
CREATE TABLE visits_clustered (
id SERIAL PRIMARY KEY,
animals_id INTEGER,
vet_id INTEGER,
visit_date TIMESTAMP
);
CREATE INDEX visits_clustered_vet_id_idx ON visits_clustered (vet_id);
CLUSTER visits_clustered USING visits_clustered_vet_id_idx;
DROP TABLE visits;
ALTER TABLE visits_clustered RENAME TO visits;
CREATE INDEX email_idx ON owners (email);
ANALYZE owners;
CLUSTER owners USING email_idx;