02 Designing Databases - DataModel

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 8

Designing databases

Martian Empire’s model


2.021 – UAB – DACSO
Pere Pons
Exercise: Create a DB model.
Martian Empire is a company that exploits tourism in Mars.

Martian people have a Martian identifier that is unique. We store the


name, surname and email of people, and every Martian is assigned to a
base, except honorees exceptions. In each base we have some resorts to
spend some holiday, we keep track of everyone that visits our resorts and
store when did he arrive and how long the visitor stayed.

As we have to take care of our visitors, we control the stock of supplies at


resort level. Is to say, we know how many items we have in each resort,
and we control the cost of the stock assuming that all supplies have the
same const independently of the resort that owns it.

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:

• Let’s create a text file with your favorite editor.


• Set its extension to .sql for clearness
• Set the comments after “---”
• Create a section to remove previous data if exists
• Add the structure
• Add the data
How to run it
psql DBeaver
From now on..

We will use this model as a reference in this course

You might also like