-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathqueries.sql
More file actions
55 lines (45 loc) · 1.39 KB
/
queries.sql
File metadata and controls
55 lines (45 loc) · 1.39 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
-- In this SQL file, write (and comment!) the typical SQL queries users will run on your database
-- Now time to optimize our queries using indexes
-- Turn scans to searches!
-- EXPLAIN QUERY PLAN (doesn't actually run the query!)
-- Searching for a username
SELECT "username" FROM "users";
--Optimized using userName index
-- Searching an id using a username
SELECT "id" FROM "users"
WHERE "username" = 'admin';
-- Optimized using userName index
-- Searching for a password using id
SELECT "password" FROM "users"
WHERE "id" = 1;
-- Optimized because id is a primary key
-- Getting data about user's wallets
SELECT "name", "amount" FROM "wallets"
WHERE "user_id" = 1;
-- Optimized using userID_index
-- Deposit [2]
UPDATE "wallets"
SET "amount" = "amount" + 50
WHERE "name" = 'meow'
AND "user_id" = 2;
-- Optimized using userID_index
-- Withdraw [3]
UPDATE "wallets"
SET "amount" = "amount" - 50
WHERE "name" = 'meow'
AND "user_id" = 2;
-- Optimized using userID_index
-- Deleting a wallet[6]
DELETE FROM "wallets"
WHERE "name" = 'temporary'
AND "user_id" = 3;
-- Optimized using userID_index
-- Changing Password [7]
UPDATE "users"
SET "password" = '12'
WHERE "id" = 3;
-- Optimized because id is a primary key
-- Deleting an account [9]
DELETE FROM "users"
WHERE "id" = 3;
-- Optimized by both id being a primary key + wallets being cascaded(due to foreign key constraint), and optimized using userID_index