CREATE TABLE user_playlist (
username text,
playlist_name text,
song_name text,
artist text,
list_order int,
songid UUID
PRIMARY KEY (username, playlist_name, song_name)
);
At first glance, it would appear that we are creating a table that will contain multiple rows with 3
unique fields. The first element 'username', will be used as the row key. The remaining elements
will be be used by CQL to create unique columns in the underlying storage engine. The result set
presented to the user is a large table of data similar to a relational database. Let's insert some
data and see how it works:
INSERT INTO user_playlist (username, playlist_name, song_name, artist,
list_order, songid)
VALUES ('cstar','Classic Rock','Stairway to heaven','Led
Zepplin',1,c9df5407-7dd8-46a4-84ee-1c358106c926);
INSERT INTO user_playlist (username, playlist_name, song_name, artist,
list_order, songid)
VALUES ('cstar','Classic Rock','Sweet home Alabama','Lynyrd
Skynyrd',2,d2fb151c-cba8-46ff-bc03-66b32c45434a);
INSERT INTO user_playlist (username, playlist_name, song_name, artist,
list_order, songid)
VALUES ('cstar','Alternative','The Queen is dead','The
Smiths',1,24106aa0-10d5-4de1-9dab-a3a652e32c49);
INSERT INTO user_playlist (username, playlist_name, song_name, artist,
list_order, songid)
VALUES ('cstar','Alternative','Blue Monday','New Order',2,7a0b82ba-
5a24-4554-903c-1970d1e3aaea);
All of the data inserted will be on one row with the key 'cstar', however, we we access that data with CQL,
it will appear as many rows.
SELECT playlist_name,song_name,artist,list_order from user_playlist
WHERE username='cstar';
playlist_name | song_name | artist | list_order
---------------+--------------------+----------------+------------
Alternative | Blue Monday | New Order | 2
Alternative | The Queen is dead | The Smiths | 1
Classic Rock | Stairway to heaven | Led Zepplin | 1
Classic Rock | Sweet home Alabama | Lynyrd Skynyrd | 2
The output is nicely formatted and easier to read by the user. The underlying storage engine has actually
created a single row of data using unique column names derived from the PRIMARY KEY definition. The
result is a faster access pattern on reads when all data is co-located on the same node and sequentially read
from disk. You can read more in-depth here: http://www.datastax.com/docs/1.2/ddl/table