ER Diagram for a clone Spotify app

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 3

ER Diagram for a clone Spotify app.

‘User Account’ Entity has 4 attributes:

- ‘User id’ which is the PRIMARY KEY.


- ‘Username’ which has a TEXT datatype, which is NOT NULL and UNIQUE.

- ‘Password’ which has a TEXT datatype, which is NOT NULL and UNIQUE.

- ‘Membership’ which has a BOOLEAN datatype.

CREATE TABLE UserAccount (


User_id SERIAL PRIMARY KEY,
Username TEXT NOT NULL UNIQUE,
Password TEXT NOT NULL UNIQUE,
Membership BOOLEAN
);

‘Playlist’ Entity has 3 attributes:

- ‘Playlist id’ which is the PRIMARY KEY.

- ‘Name’ which is a TEXT datatype, which is NOT NULL.

- ‘No. of Songs’ which is a INTEGER datatype.

CREATE TABLE Playlist (


Playlist_id SERIAL PRIMARY KEY,
Name TEXT NOT NULL,
No_Songs INT
);

‘Songs’ Entity has 4 attributes:

- ‘Song id’ which is the PRIMARY KEY.

- ‘Title’ which has a TEXT datatype, which is NOT NULL.

- ‘Genre’ which has a TEXT datatype.

- ‘Length’ which has an INTEGER datatype.

CREATE TABLE Songs (


Song_id SERIAL PRIMARY KEY,
Title TEXT NOT NULL,
Genre TEXT,
Length INT
);
‘Artist’ Entity has 3 attributes:

-‘Artist id’ is the PRIMARY KEY.

-‘Name’ has a TEXT datatype and is NOT NULL.

-‘Monthly Listeners’ has an INTEGER datatype.

CREATE TABLE Artist (


Artist_id SERIAL PRIMARY KEY,
Name TEXT NOT NULL,
Monthly_Listeners INT
);
‘Album’ Entity has 3 attributes:

-‘Album id’ is the PRIMARY KEY.

-‘Title’ has a TEXT datatype and is NOT NULL.

-‘No. of Songs’ has an INTEGER datatype and is NOT NULL.

CREATE TABLE Album (


Album_id SERIAL PRIMARY KEY,
Title TEXT NOT NULL,
No_Songs INT NOT NULL
);

***THIS BIT IM NOT SURE ABOUT

RELATIONSHIPS BETWEEN ENTITIES:

User Account -> Playlist – many to many – called ‘Creates’


CREATE TABLE UserAccount_CreatePlaylist (
UserAccountId INTEGER,
PlaylistId INTEGER,
PRIMARY KEY (UserAccountId, PlaylistId),
FOREIGN KEY (UserAccountId) REFERENCES UserAccount(UserId),
FOREIGN KEY (PlaylistId) REFERENCES Playlist(PlaylistId)

User Account -> Playlist – many to many – called ‘Shares’


CREATE TABLE UserAccount_SharedPlaylist (
UserAccountId INTEGER,
PlaylistId INTEGER,
PRIMARY KEY (UserAccountId, PlaylistId),
FOREIGN KEY (UserAccountId) REFERENCES UserAccount(UserId),
FOREIGN KEY (PlaylistId) REFERENCES Playlist(PlaylistId)
);

User Account -> Songs - many to many – called ‘Plays’


CREATE TABLE UserAccount_Songs (
UserAccountId INTEGER,
SongId INTEGER,
PRIMARY KEY (UserAccountId, SongId),
FOREIGN KEY (UserAccountId) REFERENCES UserAccount(UserId),
FOREIGN KEY (SongId) REFERENCES Songs(SongId)
);
User Account -> Artist – 1 to 1 – called ‘Has a’
CREATE TABLE UserAccount_Artist (
UserAccountId INTEGER PRIMARY KEY,
ArtistId INTEGER UNIQUE,
FOREIGN KEY (UserAccountId) REFERENCES UserAccount(UserId),
FOREIGN KEY (ArtistId) REFERENCES Artist(ArtistId)
);

Playlist -> Songs – 1 to many – called ‘Comprises of’


CREATE TABLE Playlist_Songs (
PlaylistId INTEGER,
SongId INTEGER,
PRIMARY KEY (PlaylistId, SongId),
FOREIGN KEY (PlaylistId) REFERENCES Playlist(PlaylistId),
FOREIGN KEY (SongId) REFERENCES Songs(SongId)
);

Songs -> Artist – many to many – called ‘Created by’


CREATE TABLE Songs_Artist (
SongId INTEGER,
ArtistId INTEGER,
PRIMARY KEY (SongId, ArtistId),
FOREIGN KEY (SongId) REFERENCES Songs(SongId),
FOREIGN KEY (ArtistId) REFERENCES Artist(ArtistId)
);

Artist-> Album – many to many – called ‘Creates’


CREATE TABLE Artist_Album (
ArtistId INTEGER,
AlbumId INTEGER,
PRIMARY KEY (ArtistId, AlbumId),
FOREIGN KEY (ArtistId) REFERENCES Artist(ArtistId),
FOREIGN KEY (AlbumId) REFERENCES Album(AlbumId)
);

Songs -> Album – many to 1 – called ‘Belongs to’


CREATE TABLE Songs_Album (
SongId INTEGER,
AlbumId INTEGER,
FOREIGN KEY (SongId) REFERENCES Songs(SongId),
FOREIGN KEY (AlbumId) REFERENCES Album(AlbumId)
);

1-many - you need to have the foreign key on the many entity

1-1 - both sides need a foreign key

Many – many - need a bridge table with a primary key and both foreign keys

You might also like