The structure of the table (accessed from psql shell)
4. Copying data from a csv file into the table
This is done from a command line in psql shelll
postgres=# COPY tracks(title, artist, album, genre, rating, )
FROM 'D:\Datasets\library.csv'
WITH DELIMITER ',' CSV;
COPY 296
postgres=#
This table has a lot of vertical replication which is not optimal performance-wise. A more efficient solution would be a relational database including several smaller tables, as demonstrated in the following sections.
5. Creating a new database
CREATEDATABASE music_ WITH OWNER 'postgres' ENCODING 'UTF8';
Modifying the database connection details in RStudio/ Quatro so it connects to a new database
I am creating a diagram made with the drawSQL tool with a specification of primary, logical, and foreign keys.
b) Creating tables
The clause UNIQUE is used with columns that function as logical keys (strings to be indexed).
The expression ON DELETE CASCADE indicates that deleting a row in a table including the referenced primary key (a parent row) will remove corresponding rows in the table that is connected to it with a foreign key (child rows). For example, deleting the genre “rock” from the table “genre” will remove records that are classified as “rock” in the “track” table.
Other ON DELETE choices include
RESTRICT (default) - don’t allow changes that break the constraint;
SET NULL - set the foreign key columns in the child rows to null.
CREATETABLE artist (id SERIAL,name VARCHAR(128) UNIQUE,PRIMARYKEY(id));
CREATETABLE album (id SERIAL,title VARCHAR(128) UNIQUE,artist_id INTEGERREFERENCES artist(id) ONDELETECASCADE,PRIMARYKEY(id));
CREATETABLE genre (id SERIAL,genre VARCHAR (128) UNIQUE,PRIMARYKEY(id));
Selecting album title and artist name requires joining two tables (album, artist) on artist_id (which is the primary key in “artist” table and a foreign key in “album” table).
SELECT album.title, artist.nameFROM album JOIN artist ON album.artist_id = artist.id;
2 records
title
name
Who Made Who
AC/DC
IV
Led Zeppelin
Here is a version of the table showing the inner join.
SELECT album.title, album.artist_id, artist.id, artist.nameFROM album INNERJOIN artist ON album.artist_id = artist.id;
2 records
title
artist_id
id
name
Who Made Who
2
2
AC/DC
IV
1
1
Led Zeppelin
Cross join
Joining the table track with the table genre on CROSS JOIN gives all the combinations of records in these tables:
An inner join in this case would include only the rows with matching id - in a sense an inner join can be seen as a type of a cross join with a WHERE clause.
The join reconstructs the vertical replication in the table, which is often necessary for UI.
An inner join among multiple tables
SELECT track.title, artist.name, album.title, genre.genreFROM trackJOIN genre ON track.genre_id = genre.idJOIN album ON track.album_id = album.idJOIN artist ON album.artist_id = artist.id;