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

Interacting only with the static columns

We've seen that static columns give us the ability to associate data with all rows that share the same partition key. However, what if we want to treat the data associated with that partition key as a discrete value rather than some extra information attached to each clustering column value?

More concretely, if we want to display an interface for alice to edit her user profile, all we really need to do is get the information associated with her partition key. We don't care if there are one, 10, or 1,000 status updates; we just need a single row with her username, e-mail address, and so on. With users as a separate table, this is trivial; we just select the single row with the partition key value alice. Let's see what happens when we do the equivalent in users_with_status_updates:

SELECT "username", "email", "encrypted_password" 
FROM "users_with_status_updates"
WHERE "username" = 'alice';

Note that we've omitted the id and body fields, which contain distinct values in each row; we only select the partition key, username, and the static columns, email and encrypted_password. Unfortunately, we don't quite get what we're looking for:

The result is two rows; the rows contain identical data because all of the selected columns are per-partition-key, but we still get duplicate results when we only want one. Fortunately, the DISTINCT keyword allows us to retrieve a result in the format that we'd like:

SELECT DISTINCT "username", "email", "encrypted_password" 
FROM "users_with_status_updates"
WHERE "username" = 'alice';

Now, the result takes the form we'd like—a single row containing the information specific to the alice partition key:

By formulating our SELECT statement correctly, we can basically ignore the fact that there's a clustering column at all; the result here is exactly the same as it would have been if we had selected alice's row out of the users table. To complete this analogy, we'd also like to be able to write static columns to our table without thinking about the non-static columns.

主站蜘蛛池模板: 阿拉善左旗| 鹤庆县| 玉树县| 高邮市| 亳州市| 黄山市| 翼城县| 特克斯县| 辽中县| 恭城| 孝昌县| 涟水县| 广南县| 上犹县| 介休市| 葵青区| 龙泉市| 思南县| 陵川县| 珠海市| 白沙| 滦平县| 新田县| 湟中县| 大名县| 大宁县| 濉溪县| 滦南县| 章丘市| 宜宾市| 黄龙县| 疏勒县| 沈阳市| 介休市| 南开区| 定陶县| 三门峡市| 汕头市| 东平县| 亳州市| 五寨县|