Copy this recipe template to design and create two related database tables from a specification.
https://trello.com/b/PQZ5D50O/makers-bnb
Nouns:
users (guest, host), spaces, price per night, dates, description, avaibility, name of space,
email, password, username
Put the different nouns in this table. Replace the example with your own nouns.
Record | Properties |
---|---|
users | id, email, password, username |
spaces | id, name, description, price_per_night, availiable_dates, user_id |
bookings | id, user_id_guest, space_id, date |
-
Name of the first table (always plural):
users
Column names:
id
,email
,password
,username
-
Name of the second table (always plural):
spaces
Column names:
id
,name
,description
,price_per_night
,availability_start
,availability_end
,user_id_host
-
Name of the third table (always plural):
bookings
Column names:
id
,user_id_guest
,space_id
,date
Here's a full documentation of PostgreSQL data types.
Most of the time, you'll need either text
, int
, bigint
, numeric
, or boolean
. If you're in doubt, do some research or ask your peers.
Remember to always have the primary key id
as a first column. Its type will always be SERIAL
.
# EXAMPLE:
Table: users
id: SERIAL
email: text
password: text
username: text
Table: spaces
id: SERIAL
name: text
description: text
price_per_night: int
availability_start: date
availability_end: date
user_id: int
Table: bookings
id: SERIAL
space_id: int
user_id: int
booking_date: date
Most of the time, you'll be using a one-to-many relationship, and will need a foreign key on one of the two tables.
To decide on which one, answer these two questions:
- Can one [TABLE ONE] have many [TABLE TWO]? (Yes/No)
- Can one [TABLE TWO] have many [TABLE ONE]? (Yes/No)
You'll then be able to say that:
- [A] has many [B]
- And on the other side, [B] belongs to [A]
- In that case, the foreign key is in the table [B]
Replace the relevant bits in this example with your own:
one to many
Users ────────────────► Spaces
o │ │ o
n │ │ n
e │ │ e
│ │
t │ │ t
o │ │ o
│ │
m │ │ m
a │ │ a
n └────────► Bookings ◄──────┘ n
y y
Table: foreign_key
spaces: user
bookings: user, space
If you can answer YES to the two questions, you'll probably have to implement a Many-to-Many relationship, which is more complex and needs a third table (called a join table).
-- EXAMPLE
-- file: albums_table.sql
-- Replace the table name, columm names and types.
-- Create the table without the foreign key first.
CREATE TABLE users (
id SERIAL PRIMARY KEY,
name text,
);
-- Then the table with the foreign key second.
CREATE TABLE albums (
id SERIAL PRIMARY KEY,
title text,
release_year int,
-- The foreign key name is always {other_table_singular}_id
artist_id int,
constraint fk_artist foreign key(artist_id)
references artists(id)
on delete cascade
);
psql -h 127.0.0.1 makersbnb_db < seeds/makersbnb.sql