-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathqueries.sql
61 lines (50 loc) · 1.04 KB
/
queries.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
-- Family Travel Tracker Queries AND SETUP --
DROP TABLE IF EXISTS visited_countries, users;
CREATE TABLE users(
id SERIAL PRIMARY KEY,
name VARCHAR(15) UNIQUE NOT NULL,
color VARCHAR(15)
);
/* one to many relation -
by creating a foreign key that points to the primary key of the other table.
(REFERENCES - set user_id to foreign key)
*/
CREATE TABLE visited_countries(
id SERIAL PRIMARY KEY,
country_code CHAR(2) NOT NULL,
user_id INTEGER REFERENCES users(id)
);
/*
vs previous version:
CREATE TABLE visited_countries(
id SERIAL PRIMARY KEY,
country_code CHAR (2)
);
*/
INSERT INTO users (name, color)
VALUES ('Shani', 'teal'), ('Jack', 'powderblue');
INSERT INTO visited_countries (country_code, user_id)
VALUES ('FR', 1), ('GB', 1), ('CA', 2), ('FR', 2 );
SELECT *
FROM visited_countries
JOIN users
ON users.id = user_id;
/*
"id" "country_code" "user_id" "id" "name" "color"
1 "FR" 1 1 "Shani" "teal"
2 "IL" 1 1 "Shani" "teal"
3 "CA" 2 2 "Jack" "powderblue"
4 "FR" 2 2 "Jack" "powderblue"
*/
/*
"FR"
"DE"
"GR"
"IL"
"IT"
"NL"
"US"
"SZ"
"AT"
"BG"
*/