generated from microverseinc/curriculum-template-databases
-
Notifications
You must be signed in to change notification settings - Fork 0
/
schema.sql
83 lines (66 loc) · 2.01 KB
/
schema.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
/* Database schema to keep the structure of entire database. */
CREATE TABLE animals (
id SERIAL PRIMARY KEY,
name VARCHAR(100),
date_of_birth DATE,
escape_attempts INTEGER,
neutered BOOLEAN,
weight_kg DECIMAL,
species VARCHAR(255)
);
CREATE TABLE owners (
id INTEGER PRIMARY KEY,
full_name VARCHAR(255),
age INT
);
CREATE TABLE species (
id INTEGER PRIMARY KEY,
name VARCHAR(255)
);
-- Add a new serial column
ALTER TABLE animals ADD COLUMN id_serial SERIAL;
-- Copy data from the existing id to the new serial column
UPDATE animals SET id_serial = id;
-- Drop the old id column
ALTER TABLE animals DROP COLUMN id;
-- Rename the new column to id
ALTER TABLE animals RENAME COLUMN id_serial TO id;
-- Set it as the primary key
ALTER TABLE animals ADD PRIMARY KEY (id);
-- drop col specied
ALTER TABLE animals DROP COLUMN species;
-- create foreign keys
ALTER TABLE animals ADD COLUMN species_id INTEGER;
ALTER TABLE animals ADD FOREIGN KEY (species_id) REFERENCES species(id);
ALTER TABLE animals ADD COLUMN owners_id INTEGER;
ALTER TABLE animals ADD FOREIGN KEY (owners_id) REFERENCES owners(id);
-- create vet table
CREATE TABLE vets (
id SERIAL PRIMARY KEY,
name VARCHAR(255),
age INTEGER,
date_of_graduation DATE
);
-- specializations table
CREATE TABLE specializations (
id SERIAL PRIMARY KEY,
vet_id INTEGER,
specie_name VARCHAR(255),
FOREIGN KEY (vet_id) REFERENCES vets(id),
FOREIGN key (specie_name) REFERENCES species(name)
);
-- ADD CONSTRAINT
ALTER TABLE species ADD CONSTRAINT name_constraint UNIQUE (name);
--
CREATE TABLE visits (
id SERIAL PRIMARY KEY,
vet_id INTEGER,
animal_name VARCHAR(255),
visit_date DATE,
FOREIGN KEY (vet_id) REFERENCES vets(id),
FOREIGN key (animal_name) REFERENCES animals(name)
);
-- statements for optimizing the visits and owners tables
CREATE INDEX idx_visits_animal_id ON visits(animal_id);
CREATE INDEX idx_visits_vet_id ON visits(vet_id);
CREATE INDEX idx_owners_email ON owners(email);