What You’ll Learn: Building on the previous lesson where you were able to create a table, this unit will go into more detail on CQL, where you will execute commands to INSERT data into your created tables as well as run SELECT statements to query your table.

 

 

CQL has many commands. Let’s insert some rows of data into our newly created ‘users’ table with an INSERT INTO command.

 

  • Type ENTER after each statement to insert the row into the table:

cqlsh> INSERT INTO users (firstname, lastname, age, email, city) VALUES (‘John’, ‘Smith’, 46, ‘johnsmith@email.com’, ‘Sacramento’);

cqlsh> INSERT INTO users (firstname, lastname, age, email, city) VALUES (‘Jane’, ‘Doe’, 36, ‘janedoe@email.com’, ‘Beverly Hills’);

cqlsh> INSERT INTO users (firstname, lastname, age, email, city) VALUES (‘Rob’, ‘Byrne’, 24, ‘robbyrne@email.com’, ‘San Diego’);

 

  • Now that we have a few rows of data in our table, let’s perform some queries against it. Using a SELECT statement will let us take a peek inside our table. To see all the rows from the users table we’ve created, type:

cqlsh:demo> SELECT * FROM users;

You should see:

insert-select-1[1]

 

Cassandra allows you to look up rows by their primary key. Remember we set the the last name as the row key, or primary key in CQL terms. Primary key determines where the row is located on disk. When we use a WHERE clause with a SELECT statement, we can query the table by row key.

 

  • Get the user record for the individual with the last name “Doe”.

 

cqlsh:demo> SELECT * FROM users WHERE lastname= ‘Doe’;

insert-select-2[1]