|
22 | 22 | location varchar NOT NULL,
|
23 | 23 | latitude float,
|
24 | 24 | longitude float)"""
|
| 25 | + |
| 26 | +time_table_create=""" |
| 27 | +CREATE TABLE IF NOT EXISTS time( |
| 28 | + start_time timestamp PRIMARY KEY, |
| 29 | + hour int, |
| 30 | + day int , |
| 31 | + week int, |
| 32 | + month int, |
| 33 | + year int, |
| 34 | + dayofweek int)""" |
| 35 | + |
| 36 | +user_table_create=""" |
| 37 | +CREATE TABLE IF NOT EXISTS users( |
| 38 | + userId int PRIMARY KEY, |
| 39 | + firstName varchar, |
| 40 | + lastName varchar, |
| 41 | + gender varchar, |
| 42 | + level varchar NOT NULL)""" |
| 43 | + |
| 44 | + |
| 45 | +songplay_table_create = (""" CREATE TABLE IF NOT EXISTS songplays ( |
| 46 | +songplay_id serial PRIMARY KEY, |
| 47 | +start_time timestamp REFERENCES time (start_time ) , |
| 48 | +userId int REFERENCES users ( userId ) , |
| 49 | +song_id varchar REFERENCES songs (song_id), |
| 50 | +artist_id varchar REFERENCES artists (artist_id), |
| 51 | +level varchar NOT NULL, |
| 52 | +session_id int NOT NULL, |
| 53 | +location varchar NOT NULL , |
| 54 | +user_agent varchar NOT NULL) |
| 55 | +""") |
| 56 | + |
| 57 | + |
25 | 58 | # insert data
|
26 | 59 | song_table_insert="""
|
27 |
| -INSERT INTO songs (song_id, title, artist_id, year, duration) VALUES (%s, %s, %s, %s, %s) |
| 60 | +INSERT INTO songs (song_id, title, artist_id, year, duration) |
| 61 | +VALUES (%s, %s, %s, %s, %s) |
28 | 62 | ON CONFLICT (song_id) DO NOTHING"""
|
29 | 63 |
|
30 | 64 | artist_table_insert="""
|
31 |
| -INSERT INTO artists (artist_id, name, location, latitude, longitude) VALUES (%s, %s, %s, %s, %s) |
| 65 | +INSERT INTO artists (artist_id, name, location, latitude, longitude) |
| 66 | +VALUES (%s, %s, %s, %s, %s) |
32 | 67 | ON CONFLICT (artist_id) DO NOTHING """
|
33 | 68 |
|
| 69 | + |
| 70 | +time_table_insert=""" |
| 71 | +INSERT INTO time (start_time, hour, day, week, month, year, dayofweek) |
| 72 | +VALUES (%s, %s, %s, %s, %s, %s, %s) |
| 73 | +ON CONFLICT (start_time) DO NOTHING """ |
| 74 | + |
| 75 | +user_table_insert=""" |
| 76 | +INSERT INTO users (userId, firstName, lastName, gender, level) |
| 77 | +VALUES ( %s, %s, %s, %s, %s) |
| 78 | +ON CONFLICT (userId) DO NOTHING """ |
| 79 | + |
| 80 | +songplay_table_insert=""" |
| 81 | +INSERT INTO songplays ( start_time, userId, song_id, artist_id,level, session_id, location, user_agent) |
| 82 | +VALUES ( %s, %s, %s, %s,%s, %s, %s, %s) |
| 83 | +""" |
| 84 | + |
| 85 | + |
| 86 | +song_select=""" |
| 87 | +SELECT songs.song_id, artists.artist_id FROM songs JOIN artists |
| 88 | +ON songs.artist_id=artists.artist_id |
| 89 | +WHERE songs.title=%s AND artists.name=%s AND songs.duration=%s """ |
| 90 | + |
| 91 | + |
34 | 92 | drop_table_queries=[songplay_table_drop, user_table_drop, song_table_drop, artist_table_drop, time_table_drop]
|
35 |
| -create_table_queries=[song_table_create, artist_table_create] |
| 93 | +create_table_queries=[song_table_create, artist_table_create, time_table_create, user_table_create, songplay_table_create] |
0 commit comments