✅ Day 5: Update, Delete, and TTL in Cassandra
🎯 Objective:
Learn how to update, delete, and use TTL (Time to Live) for automatic data expiry in Cassandra.
✏️ 1. UPDATE Rows
Use UPDATE
to change values of existing records using the primary key.
➤ Syntax:
UPDATE keyspace.table_name
SET column1 = value1, column2 = value2
WHERE partition_key = value [AND clustering_key = value];
✅ Example – Update employee department:
UPDATE test_lab.employee
SET department = 'Finance'
WHERE emp_id = <UUID>;
🗑️ 2. DELETE Rows or Columns
➤ Delete a row:
DELETE FROM test_lab.employee
WHERE emp_id = <UUID>;
➤ Delete a column only:
DELETE department FROM test_lab.employee
WHERE emp_id = <UUID>;
⏳ 3. TTL (Time to Live)
TTL automatically expires data after a certain number of seconds.
➤ Insert with TTL:
INSERT INTO test_lab.employee (emp_id, name, department, joined_on)
VALUES (uuid(), 'Temp User', 'IT', toTimestamp(now()))
USING TTL 60; -- Expires in 60 seconds
➤ Update with TTL:
UPDATE test_lab.employee
USING TTL 30
SET department = 'Support'
WHERE emp_id = <UUID>;
➤ Check TTL:
SELECT TTL(department) FROM test_lab.employee WHERE emp_id = <UUID>;
⚠️ Important Notes:
- TTL applies per column or entire row, based on how you insert/update.
- You must use the full primary key when using
UPDATE
orDELETE
.
🧪 Day 5 Lab Tasks
- Insert an employee with TTL = 60 seconds
- Update an employee's department
- Delete one column (
department
) from a record - Delete an entire record using
DELETE FROM
- Use
SELECT TTL(column)
to confirm TTL - Wait and confirm TTL expiry by checking after 1–2 minutes
✅ Checklist
Task | Done |
---|---|
Inserted data with TTL | ✅ |
Used UPDATE with primary key |
✅ |
Deleted specific columns and rows | ✅ |
Verified TTL with SELECT TTL() |
✅ |