Professional Documents
Culture Documents
02 Designing Databases - DataModel
02 Designing Databases - DataModel
02 Designing Databases - DataModel
On the other hand, now we are only admitting Martian people, because
they are kinder than earthers, but we have created a marketing database
to store Earthers that may be interested in coming to Mars and providing
us more wealth for standing them.
Model proposed
The model proposed for
Martian Empire’s database
is the following:
Table creation
create table bases create table resort_supplies
( (
base_id serial, resort_id int not null references resorts(resort_id),
base_name text not null, supply_id int not null references supplies(supply_id),
primary key(base_id) stock int CHECK (stock > 0),
); primary key (resort_id, supply_id)
);
create table martians
( create table resort_visits
martian_id serial, (
name text not null, visit_id serial,
surname text not null, martian_id int not null references martians(martian_id),
base_id int null references bases(base_id), resort_id int not null references resorts(resort_id),
email text, date_of_visit date,
primary key (martian_id) duration int not null default (2) CHECK (duration > 0),
); primary key (visit_id)
);
create table resorts
( create table earthers
resort_id serial, (
name text not null, earther_id serial,
base_id int not null references bases(base_id), name text not null,
primary key (resort_id) surname text not null,
); email text,
primary key (earther_id)
create table supplies );
(
supply_id serial,
name text not null,
value decimal not null CHECK (value > 0),
primary key (supply_id)
);
Data insertion
insert into bases (base_name) values ('Valles Marineris');
insert into bases (base_name) values ('Tharsis volcanoes'); insert into resort_supplies(resort_id, supply_id, stock) values (1, 1, 5);
insert into bases (base_name) values ('Medusae Fossae'); insert into resort_supplies(resort_id, supply_id, stock) values (1, 3, 2);
insert into bases (base_name) values ('Orbit 1'); insert into resort_supplies(resort_id, supply_id, stock) values (1, 7, 5);
insert into resort_supplies(resort_id, supply_id, stock) values (1, 4, 7);
insert into martians (name, surname, base_id, email) values ('Jane', 'Earther', 1, 'jane@msa.mars'); insert into resort_supplies(resort_id, supply_id, stock) values (1, 2, 8);
insert into martians (name, surname, base_id, email) values ('Antony', 'Morthon', 1, 'amorton@msa.mars');
insert into martians (name, surname, base_id, email) values ('Thomas', 'Evans', 1, 'aevans@msa.mars'); insert into resort_supplies(resort_id, supply_id, stock) values (2, 3, 1);
insert into martians (name, surname, base_id, email) values ('Elon', 'Musk', null, 'elon@msa.mars'); insert into resort_supplies(resort_id, supply_id, stock) values (2, 7, 2);
insert into martians (name, surname, base_id, email) values ('Sophie', 'Leicester', 2, 'sleicester@msa.mars'); insert into resort_supplies(resort_id, supply_id, stock) values (2, 5, 6);
insert into martians (name, surname, base_id, email) values ('Mike', 'Murray', 3, 'mmurray@msa.mars'); insert into resort_supplies(resort_id, supply_id, stock) values (2, 4, 3);
insert into martians (name, surname, base_id, email) values ('Steve', 'Kent', 3, 'skent@msa.mars'); insert into resort_supplies(resort_id, supply_id, stock) values (2, 8, 9);
insert into martians (name, surname, base_id, email) values ('Samantha', 'Jones', 2, 'sjones@msa.mars');
insert into resort_supplies(resort_id, supply_id, stock) values (3, 3, 14);
insert into resorts(name, base_id) values ('Merineris Resort', 1); insert into resort_supplies(resort_id, supply_id, stock) values (3, 8, 12);
insert into resorts(name, base_id) values ('Merineris Martian Golf', 1);
insert into resorts(name, base_id) values ('Orbit Resort', 4); insert into resort_visits (resort_id, martian_id, date_of_visit, duration) values (1, 1, '1/1/2045',7);
insert into resort_visits (resort_id, martian_id, date_of_visit, duration) values (1, 3, '14/10/2045',7);
insert into supplies(name, value) values ('Bread', 2); insert into resort_visits (resort_id, martian_id, date_of_visit, duration) values (1, 2, '1/12/2045',7);
insert into supplies(name, value) values ('Chicken', 4.5); insert into resort_visits (resort_id, martian_id, date_of_visit, duration) values (3, 3, '23/6/2045',7);
insert into supplies(name, value) values ('Oranges', 3); insert into resort_visits (resort_id, martian_id, date_of_visit, duration) values (2, 2, '11/3/2045',7);
insert into supplies(name, value) values ('Apples', 2.86); insert into resort_visits (resort_id, martian_id, date_of_visit, duration) values (2, 4, '13/4/2045',7);
insert into supplies(name, value) values ('Water (1l)', 9.8); insert into resort_visits (resort_id, martian_id, date_of_visit, duration) values (1, 3, '13/6/2046',7);
insert into supplies(name, value) values ('O2', 120); insert into resort_visits (resort_id, martian_id, date_of_visit, duration) values (1, 3, '19/9/2048',7);
insert into supplies(name, value) values ('Pizzas', 21); insert into resort_visits (resort_id, martian_id, date_of_visit, duration) values (3, 6, '1/6/2042',7);
insert into supplies(name, value) values ('Milk', 56);
insert into supplies(name, value) values ('Martian Chicken', 0.65);
insert into supplies(name, value) values ('Cheese', 0.98);
insert into supplies(name, value) values ('Magazines', 12);
insert into supplies(name, value) values ('Bananas', 7);
Script generation
To create a script: