generated from microverseinc/curriculum-template-databases
-
Notifications
You must be signed in to change notification settings - Fork 1
/
schema.sql
65 lines (48 loc) · 1.28 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
/* Database schema to keep the structure of entire database. */
CREATE TABLE animals (
id INTEGER PRIMARY KEY,
name TEXT,
date_of_birth DATE,
escape_attempts INTEGER,
neutered BOOLEAN,
weight_kg DECIMAL
);
-- FOR PART 2
ALTER TABLE animals
ADD species varchar(255);
-- FOR PART 3
CREATE TABLE owners(
id SERIAL PRIMARY KEY,
full_name VARCHAR(50) NOT NULL, age INT NOT NULL
);
CREATE TABLE species (
id SERIAL PRIMARY KEY,
name VARCHAR(50) NOT NULL
);
ALTER TABLE animals
DROP COLUMN species;
ALTER TABLE animals ADD species_id INT;
-- add owner id column
ALTER TABLE animals ADD owner_id INT;
-- make species_id foreign key with reference from species tbl
ALTER TABLE animals ADD FOREIGN KEY (species_id) REFERENCES species(id);
-- make owners_id foreign key with refrence from owners tbl
ALTER TABLE animals ADD FOREIGN KEY (owner_id) REFERENCES owner(owner_id);
-- FOR PART 4
-- VETS Table Creation
CREATE TABLE vets (id SERIAL PRIMARY KEY,
name varchar,
age INT NOT NULL,
date_of_graduation date );
-- Specialization Table Creation
CREATE TABLE specializations (
species_id int,
vet_id int,
PRIMARY KEY (species_id, vet_id)
);
-- Visits Table Creation.
CREATE TABLE visits (
animal_id int,
vet_id int,
visit_date date,
PRIMARY KEY(animal_id, visit_date));