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

Working with static columns

Let's say that we've decided, in our MyStatus application, that when a user creates a new account, we also require them to create an initial status update. In this workflow, we'll end up creating a new user record at the same time as when we create their first status update, which is to say that we'll be populating all the columns of the users_with_status_updates table in a single statement:

INSERT INTO "users_with_status_updates" 
("username", "id", "email", "encrypted_password", "body")
VALUES (
'alice',
76e7a4d0-e796-11e3-90ce-5f98e903bf02,
'alice@gmail.com',
0x8914977ed729792e403da53024c6069a9158b8c4,
'Learning Cassandra!'
);

It's worth emphasizing that even though this is a single INSERT statement, we're actually doing two separate things:

  • Assigning the email and encrypted_password values to every row whose username value is alice
  • Creating a new row in the alice partition, with Learning Cassandra! as the value of body

Let's take a look at the contents of the table so far:

SELECT * FROM "users_with_status_updates";

Looking at the output, we'll observe that it's just what we'd expect (I've omitted the encrypted_password column to save space):

The single row shown in the preceding figure is precisely what we wrote with the INSERT statement earlier. So far, we haven't seen any special behavior for static columns. To see static columns in action, let's add another status update to the table:

INSERT INTO "users_with_status_updates" 
("username", "id", "body")
VALUES ('alice', NOW(), 'Another status update');

Note that in this case, we did not specify values for either of the static columns; we provided the partition key, the clustering column, and a value for the non-static data column body. Now, let's take a look at the contents of the table:

SELECT * FROM "users_with_status_updates";

The output is as follows:

Now, the effect of the STATIC column declaration is clear: both rows have the e-mail address we assigned to the first row even though we did not specify an email value in our second INSERT query.

主站蜘蛛池模板: 灵山县| 乐业县| 巴彦县| 临泽县| 克什克腾旗| 鲁甸县| 兴业县| 建瓯市| 灌云县| 南京市| 达拉特旗| 陈巴尔虎旗| 抚远县| 高清| 罗江县| 襄樊市| 阳曲县| 鲁甸县| 双辽市| 阳高县| 桃源县| 长子县| 台中市| 邓州市| 荆州市| 阿鲁科尔沁旗| 且末县| 北海市| 台东县| 双城市| 石狮市| 大邑县| 香港| 荔浦县| 青岛市| 大渡口区| 同仁县| 上思县| 凤凰县| 贵定县| 海原县|