官术网_书友最值得收藏!

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.

主站蜘蛛池模板: 来宾市| 大英县| 湖南省| 城步| 泾川县| 保亭| 阿勒泰市| 庄浪县| 南部县| 冀州市| 商城县| 普兰店市| 镇安县| 兴山县| 志丹县| 阜宁县| 历史| 昌图县| 蓝田县| 天等县| 建平县| 新密市| 绥阳县| 伊吾县| 调兵山市| 连州市| 怀安县| 庐江县| 德庆县| 凤冈县| 宜良县| 洱源县| 肇东市| 宝山区| 宁安市| 项城市| 扬中市| 星子县| 上饶县| 凤台县| 江阴市|