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

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.

主站蜘蛛池模板: 梓潼县| 定结县| 珠海市| 革吉县| 柳河县| 昌邑市| 闽侯县| 崇信县| 宝兴县| 渑池县| 苏尼特左旗| 望都县| 庆城县| 平和县| 萨嘎县| 四川省| 衡水市| 潞城市| 神农架林区| 松阳县| 丰原市| 光山县| 新和县| 肇东市| 江永县| 同德县| 克东县| 平舆县| 宝兴县| 独山县| 天峨县| 开远市| 罗城| 金秀| 皋兰县| 丰原市| 于田县| 陈巴尔虎旗| 云安县| 云阳县| 巴中市|