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

Creating a table with a compound primary key

The syntax for creating tables with compound primary keys is a bit different from the single-column primary key syntax we saw in the previous chapter. We create a user_status_updates table with a compound primary key, as follows:

CREATE TABLE "user_status_updates" ( 
"username" text,
"id" timeuuid,
"body" text,
PRIMARY KEY ("username", "id")
);

In particular, instead of appending a single-column definition with a PRIMARY KEY modifier, we make a separate PRIMARY KEY declaration at the end of the list of columns, which itself specifies the two columns that make up the compound primary key.

While this is the only way to declare a compound primary key, it's also a perfectly valid way to declare a single-column primary key. So, our users table from the previous chapter could have been declared like this:

CREATE TABLE "users" ( 
"username" text,
"email" text,
"encrypted_password" blob,
PRIMARY KEY ("username")
);

Here, we simply move the PRIMARY KEY declaration to the end of the column list. It's a little less concise, but it has the exact same effect as a single-column definition with a PRIMARY KEY modifier.

主站蜘蛛池模板: 诏安县| 乌恰县| 湟源县| 疏勒县| 乐业县| 密山市| 彰化县| 门源| 来宾市| 抚宁县| 昆山市| 香格里拉县| 稻城县| 巴林左旗| 航空| 扶绥县| 兴国县| 开远市| 清苑县| 咸宁市| 长武县| 黄大仙区| 邳州市| 肥城市| 江油市| 桃园县| 精河县| 玉环县| 乌兰察布市| 白城市| 定兴县| 邢台县| 呼图壁县| 繁昌县| 襄垣县| 凉山| 苏州市| 武清区| 沙河市| 临夏市| 信宜市|