generated from microverseinc/curriculum-template-databases
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathschema.sql
60 lines (48 loc) · 1.32 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
/* Database schema to keep the structure of entire database. */
CREATE TABLE animals(
id INT,
name VARCHAR(100),
date_of_birth DATE,
escape_attempts INT,
neutered BOOLEAN,
weight_kg DECIMAL
);
-- add species column
ALTER TABLE animals ADD COLUMN species VARCHAR(50);
-- create owners table
CREATE TABLE owners (
id SERIAL PRIMARY KEY,
full_name VARCHAR(255),
age INTEGER
);
-- create species table
CREATE TABLE species (
id SERIAL PRIMARY KEY,
name VARCHAR(100)
);
ALTER TABLE animals ADD COLUMN species_id INTEGER REFERENCES species(id);
ALTER TABLE animals ADD COLUMN owner_id INTEGER REFERENCES owners(id);
ALTER TABLE animals DROP COLUMN species;
ALTER TABLE animals ADD PRIMARY KEY (id);
CREATE TABLE vets (
id SERIAL PRIMARY KEY,
name VARCHAR(255),
age INTEGER,
date_of_graduation DATE
);
CREATE TABLE specializations (
id SERIAL PRIMARY KEY,
vet_id INTEGER REFERENCES vets(id),
species_id INTEGER REFERENCES species(id),
UNIQUE(vet_id, species_id)
);
CREATE TABLE visits (
id SERIAL PRIMARY KEY,
animal_id INTEGER REFERENCES animals(id),
vet_id INTEGER REFERENCES vets(id),
visit_date DATE
);
ALTER TABLE owners ADD COLUMN email VARCHAR(120);
CREATE INDEX owners_idx ON owners (id);
CREATE INDEX visits_vet_id_idx ON visits (vet_id);
CREATE INDEX emails ON owners(email);