/
book_tracker.sql
93 lines (85 loc) · 2.38 KB
/
book_tracker.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
-- SQL CREATION
-- DATABASE : book_tracker
-- Copy/paste queries
-- Press CMD-OPT-RTRN to run
--
-- TABLE: book information (title, author, author, language, published date, location)
--
CREATE TABLE books (
id serial PRIMARY KEY,
title varchar(80),
author varchar(80),
series varchar(80),
language varchar(20),
published DATE,
location varchar(80));
--
-- TABLE: user's information
-- First/Last Name
-- Email Address (track users by unique email addresses)
-- Password (password encrypted by bcrypt)
-- Username (option to pick a different username after registration in their profile settings, otherwise default to email address)
--
CREATE TABLE users (
id serial PRIMARY KEY,
first_name varchar(80) NOT NULL,
last_name varchar(80) NOT NULL,
email varchar(100) NOT NULL UNIQUE,
password varchar(80) NOT NULL);
--
-- TABLE: relational table between book id's and user id's
--
CREATE TABLE users_books (
id serial PRIMARY KEY,
wishlist BOOLEANm
book_id INT REFERENCES books(id),
user_id INT REFERENCES users(id));
--
-- TABLE: books borrowed and lent
-- date_initial, date_returned: Track date initially lent/borrowed and dates returned
-- status: TRUE if book is currently lent/borrowed, FALSE if book has been lent/borrowed and has been returned
--
--
CREATE TABLE borrowed_and_lent (
id serial PRIMARY KEY,
date_initial date,
date_returned date,
status boolean,
lent_to INT REFERENCES users(id),
borrowed_from INT REFERENCES users(id),
book_id INT REFERENCES books(id));
--
-- TABLE: wishlist
-- link user id to book id if particular book is on their wishlist
--
CREATE TABLE wishlist (
id serial PRIMARY KEY,
book_id INT REFERENCES books(id),
user_id INT REFERENCES users(id));
--
-- TABLE: favorites
-- link user id to book id if particular book is favorite
--
CREATE TABLE favorites (
id serial PRIMARY KEY,
book_id INT REFERENCES books(id),
user_id INT REFERENCES users(id));
--
-- TABLE: shelves
-- User can create "shelves" (tags) to categorize their books
-- Book can be categorized on many shelves
-- Shelf can have many books
--
CREATE TABLE shelves (
id serial PRIMARY KEY,
shelf_name varchar(100),
user_id INT REFERENCES users(id));
--
-- TABLE: book shelves
-- Link user's created shelf categories to the specified books
--
CREATE TABLE book_shelves (
id serial PRIMARY KEY,
user_id INT REFERENCES users(id),
book_id INT REFERENCES books(id),
shelf_id INT REFERENCES shelves(id));