generated from microverseinc/curriculum-template-databases
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathschema.sql
59 lines (42 loc) · 1.4 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
/* Database schema to keep the structure of entire database. */
CREATE TABLE animals (
id integer primary key generated always as identity,
name varchar(100),
date_of_birth date,
escape_attempts integer,
neutered boolean,
weight_kg decimal,
);
ALTER TABLE animals ADD COLUMN species varchar(40);
CREATE TABLE owners (
id INT PRIMARY KEY GENERATED ALWAYS AS IDENTITY,
full_name VARCHAR (50) NOT NULL,
age INT NOT NULL
);
CREATE TABLE species(
id INT PRIMARY KEY GENERATED ALWAYS AS IDENTITY,
name VARCHAR(50) NOT NULL
);
ALTER TABLE animals DROP COLUMN species;
ALTER TABLE animals ADD CONSTRAINT species_id FOREIGN KEY (species_id) REFERENCES species(id);
ALTER TABLE animals ADD CONSTRAINT owner_id FOREIGN KEY (owner_id) REFERENCES owners(id);
CREATE TABLE vets (
id INT PRIMARY KEY GENERATED ALWAYS AS IDENTITY,
name VARCHAR (50),
age INT,
date_of_graduation date
);
CREATE TABLE specializations (
species_id int REFERENCES species (id),
vets_id int REFERENCES vets (id)
);
CREATE TABLE visits (
animal_id int REFERENCES animals (id),
vets_id int REFERENCES vets (id),
date_of_visit date
);
ALTER TABLE owners ALTER COLUMN age DROP NOT NULL;
ALTER TABLE owners ADD COLUMN email VARCHAR(120);
CREATE INDEX visits_by_animal_id ON visits(animal_id ASC);
CREATE INDEX visits_by_vets_id ON visits(vets_id ASC);
CREATE INDEX owners_by_email ON owners(email ASC);