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

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.

主站蜘蛛池模板: 新宾| 莆田市| 黔西| 德令哈市| 呼图壁县| 泰州市| 赤峰市| 时尚| 射阳县| 蓬溪县| 平度市| 凤翔县| 株洲市| 治多县| 阿尔山市| 永城市| 祥云县| 开远市| 临洮县| 浮梁县| 东明县| 彰化县| 宜春市| 仙游县| 萝北县| 巩义市| 宜阳县| 宜阳县| 婺源县| 永定县| 泰州市| 芜湖县| 广昌县| 鞍山市| 瑞安市| 若尔盖县| 内丘县| 毕节市| 金川县| 会同县| 栖霞市|