-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfollow_along.sql
More file actions
168 lines (159 loc) · 6.1 KB
/
follow_along.sql
File metadata and controls
168 lines (159 loc) · 6.1 KB
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
-- # Guide to Database Design
--
-- #### Normalization
--
-- The process of "normalizing" a relational database design. Different levels of normalization are possible, each level further seperating concerns and abstracting data over multiple tables.
--
-- #### Entity Relationship Diagram (ERD)
--
-- A way to visually describe the schema of a database and the relationships between tables.
--
-- #### One-to-One Relationship
-- - ***Definition:*** a relationship where one record on a table matches with one record on another table.
-- - ***Example:*** users and user_details
-- - ***Common strategy to implement:*** the owner table of the relationship should have a foreign key to the non-owner.
--
-- #### One-to-Many Relationship
--
-- - ***Definition:*** a relationship where one record on a table matches with several records on another table.
-- - ***Example:*** users and posts
-- - ***Common strategy to implement:*** the table on the many side should have a foreign key pointing to the single owner from the owning-side table.
--
-- #### Many-to-Many Relationship
--
-- - ***Definition:*** a relationship where one record on a table matches with several records on another table and a record on the other table matches with several records on the first table.
-- - ***Example:*** users and events
-- - ***Common strategy to implement:*** neither table should have a foreign key to the other and instead, an associative (join) table should be used to link the two together via two foreign key columns, each linking to a primary key of both tables in the many-to-many relationship.
--
-- <div style="page-break-after: always;"></div>
--
-- ## DB Design Conventions to Follow
--
-- 1. All tables except associative tables should have a single primary key called id.
--
-- ```
-- users
-- id
-- username
-- email
-- password
-- ```
--
-- 2. All table cells must contain atomic data (a single cell cannot contain multiple values).
--
-- ```
-- users
-- id
-- username
-- phone_numbers
--
-- 1 | bob123| 2105554545, 2105556767, 2105553232 <--- AVOID THIS!
-- ```
--
--
-- 3. All columns within a single row should be independent from one another (if not, another table may be needed).
--
-- ```
-- users
-- id
-- name
-- favorite_color_1
-- favorite_color_2
--
-- A better alternative...
--
-- users
-- id
-- name
--
-- colors
-- id
-- name
--
-- color_user
-- color_id
-- user_id
-- ```
--
-- 4. When duplicate values exist accross multiple rows, consider breaking out the column(s) into another table.
--
-- ```
-- users
-- id
-- username
-- email
-- password
-- home_state
--
-- 1 | bob123 | bob@email.com | JKL3J2JHSURNZ931H | Texas
-- 2 | kev123 | kev@email.com | JKL3J2JHSURNZ931H | Texas <--- duplicate!
-- 3 | dan123 | dan@email.com | JKL3J2JHSURNZ931H | Arizona
-- 4 | mat123 | mat@email.com | JKL3J2JHSURNZ931H | Washington
-- 5 | rob123 | rob@email.com | JKL3J2JHSURNZ931H | Texas <--- duplicate!
--
-- Consider a states table.
-- ```
--
-- ## Helpful Links
--
-- ##### Additional Info
--
-- - ERDs Explained: [https://www.lucidchart.com/pages/er-diagrams](https://www.lucidchart.com/pages/er-diagrams)
-- - Guide to Crow's Foot notation: [https://www.vertabelo.com/blog/crow-s-foot-notation/](https://www.vertabelo.com/blog/crow-s-foot-notation/)
--
-- ##### ERD Creation Apps
--
-- - [draw.io](https://google.com)
-- - [lucidchart](https://www.lucidchart.com)
-- - [sqldbm](https://sqldbm.com)
-- - Don't forget about good 'ol pencil and paper or a whiteboard!
use codeup_test_db;
CREATE TABLE authors (
id INT UNSIGNED NOT NULL AUTO_INCREMENT,
first_name VARCHAR(50),
last_name VARCHAR(100) NOT NULL,
PRIMARY KEY (id));
INSERT INTO authors(first_name, last_name) VALUES
('Douglas', 'Adams'),
('Mark', 'Twain'),
('Kurt', 'Vonnegut');
CREATE TABLE quotes (
id INT UNSIGNED NOT NULL AUTO_INCREMENT,
content TEXT NOT NULL,
author_id INT UNSIGNED NOT NULL,
PRIMARY KEY (id),
FOREIGN KEY (author_id) REFERENCES authors (id)
);
INSERT INTO quotes (author_id, content)
VALUES ((select id from authors where first_name = 'Douglas' and last_name = 'Adams'),
'I love deadlines. I love the whooshing noise they make as they go by.');
INSERT INTO quotes (author_id, content)
VALUES ((select id from authors where first_name = 'Douglas' and last_name = 'Adams'),
'Time is an illusion. Lunchtime doubly so.');
INSERT INTO quotes (author_id, content)
values ((select id from authors where first_name = 'Mark' and last_name = 'Twain'),
'Clothes make the man. Naked people have little or no influence on society.');
INSERT INTO quotes (author_id, content)
values ((select id from authors where first_name = 'Kurt' and last_name = 'Vonnegut'),
'The universe is a big place, perhaps the biggest.');
INSERT INTO quotes (author_id, content)
VALUES ((select id from authors where first_name = 'Douglas' and last_name = 'Adams'), 'Don''t Panic.');
CREATE TABLE topics (
id INT UNSIGNED NOT NULL AUTO_INCREMENT,
name VARCHAR(255),
PRIMARY KEY (id)
);
INSERT INTO topics(name) VALUES
('Space and Time'),
('Humor'),
('Office Life'),
('Hitchiker''s Guide to the Galaxy');
CREATE TABLE quote_topic (
quote_id INTEGER UNSIGNED NOT NULL,
topic_id INTEGER UNSIGNED NOT NULL,
FOREIGN KEY (quote_id) REFERENCES quotes(id),
FOREIGN KEY (topic_id) REFERENCES topics(id)
);
INSERT INTO quote_topic(quote_id, topic_id)
VALUES (1, 2), (1, 3), (2, 4), (3, 1),
(3, 2), (3, 3), (3, 4), (4, 2), (5, 1);