- Learning Apache Cassandra(Second Edition)
- Sandeep Yarabarla
- 270字
- 2021-07-03 00:19:32
Inserts are always upserts
Suppose you try to insert data into a table with a primary key, which already exists; the data doesn't get updated in place. Rather, a new copy of the data is written, and the old copy is kept until it is deleted by some internal Cassandra mechanism. To demonstrate how this works, let's update one of the records using the INSERT statement.
Let's change Alice's email by running the following INSERT statement:
INSERT INTO "users"
("username", "email")
VALUES ('alice', 'alice@yahoo.com');
Let's query for the updated row and see what we get:
SELECT * FROM "users" where "username" = 'alice';
You can see that the e-mail column has been updated even if you use the INSERT statement with a primary key that already exists.
Next, we will delete the row and insert a non-existing row using the UPDATE command and look at the behavior.
First, let's delete the row and verify that it got deleted. To delete the row, execute the following statement:
DELETE FROM "users" WHERE "username" = 'alice';
To check whether it worked, run the preceding SELECT query:
SELECT * FROM "users" where "username" = 'alice';
You will get an empty result set or 0 rows. Next, let's try to use the UPDATE command to update non-existing rows with primary key (username) alice:
UPDATE "users"
SET "email" = 'alice@gmail.com',
"encrypted_password" = 0x8914977ed729792e403da53024c6069a9158b8c4
WHERE "username" = 'alice';
Now to verify, run the aforementioned SELECT query again, and you will see that the data got inserted successfully:
This tells us that the UPDATE statement has the same effect as an INSERT statement.
- Splunk 7 Essentials(Third Edition)
- Mastering Spark for Data Science
- Cinema 4D R13 Cookbook
- Julia 1.0 Programming
- 圖形圖像處理(Photoshop)
- 大數據時代的數據挖掘
- 機器學習與大數據技術
- PIC單片機C語言非常入門與視頻演練
- 21天學通C#
- CompTIA Network+ Certification Guide
- 變頻器、軟啟動器及PLC實用技術260問
- Hands-On Data Warehousing with Azure Data Factory
- INSTANT Puppet 3 Starter
- MongoDB 4 Quick Start Guide
- Hadoop Beginner's Guide