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

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.

主站蜘蛛池模板: 玉山县| 泗洪县| 顺平县| 神池县| 萨迦县| 辽宁省| 南充市| 武邑县| 吴旗县| 永泰县| 泰州市| 玛多县| 定日县| 东台市| 桂林市| 南丰县| 峡江县| 农安县| 中西区| 徐州市| 武强县| 河池市| 伽师县| 乳源| 满洲里市| 资中县| 抚州市| 抚宁县| 左贡县| 南宁市| 莲花县| 晋宁县| 昂仁县| 保山市| 丹巴县| 兴宁市| 富宁县| 昌平区| 临夏县| 东阳市| 四平市|