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

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.

主站蜘蛛池模板: 共和县| 靖安县| 兴文县| 周宁县| 且末县| 嵊泗县| 宁晋县| 保靖县| 开远市| 乌兰察布市| 台北县| 二连浩特市| 苍溪县| 牙克石市| 珠海市| 云安县| 东城区| 南阳市| 广东省| 永福县| 白水县| 漠河县| 调兵山市| 磐安县| 白山市| 车险| 玛纳斯县| 车险| 南木林县| 临夏市| 灵山县| 华宁县| 普兰县| 海南省| 济源市| 喀喇沁旗| 根河市| 安新县| 盐津县| 舟山市| 泾川县|