generated from microverseinc/curriculum-template-databases
-
Notifications
You must be signed in to change notification settings - Fork 0
/
schema.sql
79 lines (70 loc) · 1.53 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
/* Database schema to keep the structure of entire database. */
CREATE DATABASE vet_clinic;
CREATE TABLE animals (
id INT,
name TEXT,
date_of_birth DATE,
escape_attempts INT,
neutered BOOLEAN,
weight_kg DECIMAL,
PRIMARY KEY(id)
);
ALTER TABLE animals
ADD species VARCHAR;
CREATE TABLE owners(
id SERIAL PRIMARY KEY,
full_name VARCHAR,
age INT
);
CREATE TABLE species(
id SERIAL PRIMARY KEY,
name VARCHAR
);
ALTER TABLE animals DROP species;
ALTER TABLE animals
ADD COLUMN species_id INT,
ADD CONSTRAINT fk_species
FOREIGN KEY (species_id)
REFERENCES species(id)
;
ALTER TABLE animals
ADD COLUMN owners_id INT,
ADD CONSTRAINT fk_owners
FOREIGN KEY (owners_id)
REFERENCES owners(id)
;
SELECT * FROM animals;
CREATE TABLE vets(
id SERIAL PRIMARY KEY,
name VARCHAR,
age INT,
date_of_graduation DATE
);
CREATE TABLE specializations(
species_id INT,
vets_id INT,
FOREIGN KEY (species_id)
REFERENCES species(id)
ON DELETE RESTRICT
ON UPDATE CASCADE,
FOREIGN KEY (vets_id)
REFERENCES vets(id)
ON DELETE RESTRICT
ON UPDATE CASCADE,
PRIMARY KEY (species_id, vets_id)
);
CREATE TABLE visits(
animals_id INT,
vets_id INT,
date_of_visit DATE,
id INT GENERATED ALWAYS AS IDENTITY,
FOREIGN KEY (animals_id)
REFERENCES animals (id)
ON DELETE RESTRICT
ON UPDATE CASCADE,
FOREIGN KEY (vets_id)
REFERENCES vets (id)
ON DELETE RESTRICT
ON UPDATE CASCADE,
PRIMARY KEY(id)
);