generated from microverseinc/curriculum-template-databases
-
Notifications
You must be signed in to change notification settings - Fork 0
/
schema.sql
93 lines (77 loc) · 2.5 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
84
85
86
87
88
89
90
91
92
93
/* Database schema to keep the structure of entire database. */
-- Create database
CREATE DATABASE vet_clinic;
-- Create animals table
CREATE TABLE IF NOT EXISTS animals (
id serial PRIMARY KEY,
name varchar(100) NOT NULL,
date_of_birth date NOT NULL,
escape_attempts int,
neutered boolean,
weight_kg decimal(5, 2),
);
-- Add species column to the animals table
ALTER TABLE animals ADD species VARCHAR (100);
-- Create the owners table
CREATE TABLE IF NOT EXISTS owners(
id SERIAL PRIMARY KEY,
full_name VARCHAR(255) NOT NULL,
age INT
);
-- Create the species table
CREATE TABLE IF NOT EXISTS species(
id SERIAL PRIMARY KEY,
name VARCHAR(200) NOT NULL
);
-- Remove the column species from the animals table
ALTER TABLE animals DROP COLUMN species;
-- Add the species_id and owner_id columns to the animals table
ALTER TABLE animals ADD species_id INT;
ALTER TABLE animals ADD owner_id INT;
-- Add the foreign keys and and the restrictions to the animals table
ALTER TABLE animals ADD CONSTRAINT fk_species FOREIGN KEY (species_id) REFERENCES species(id) ON DELETE CASCADE;
ALTER TABLE animals ADD CONSTRAINT fk_owner FOREIGN KEY (owner_id) REFERENCES owners(id) ON DELETE CASCADE;
-- Create the vets table
CREATE TABLE IF NOT EXISTS vets(
id SERIAL PRIMARY KEY,
name VARCHAR(200) NOT NULL,
age INT,
date_of_graduation DATE
);
-- Create the specializations table
CREATE TABLE IF NOT EXISTS specializations(
vet_id INT,
species_id INT,
PRIMARY KEY (vet_id, species_id),
CONSTRAINT fk_vets_id
FOREIGN KEY (vet_id)
REFERENCES vets(id)
ON DELETE CASCADE,
CONSTRAINT fk_species_id
FOREIGN KEY (species_id)
REFERENCES species(id)
ON DELETE CASCADE
);
-- Create the visits table
CREATE TABLE IF NOT EXISTS visits(
animal_id INT,
vet_id INT,
date_of_visit DATE,
PRIMARY KEY (animal_id, vet_id, date_of_visit),
CONSTRAINT fk_animal_id
FOREIGN KEY (animal_id)
REFERENCES animals(id)
ON DELETE CASCADE,
CONSTRAINT fk_vet_id
FOREIGN KEY (vet_id)
REFERENCES vets(id)
ON DELETE CASCADE
);
-- Add the email column to the owners table
ALTER TABLE owners ADD COLUMN email VARCHAR(120);
-- Add index for the animal_id column in the visits table
CREATE INDEX idx_visits_animal_id ON visits (animal_id);
-- Add index for the vet_id column in the visits table
CREATE INDEX idx_visits_vet_id ON visits (vet_id);
-- Add index for the email column in the owners table
CREATE INDEX idx_owner_email ON owners(email);