在一個網頁平台上,會有多種角色,而在我們的任務二中的角色(role
)會有:
USER
COACH
在我們的線稿圖中,也有繪製如果他是教練角色時,他可以填寫自己的相關教練資料,其中欄位包含:
CREATE EXTENSION IF NOT EXISTS "uuid-ossp";
CREATE TABLE "USER" (
"id" uuid PRIMARY KEY NOT NULL DEFAULT (gen_random_uuid()),
"name" varchar(50) NOT NULL,
"email" varchar(320) UNIQUE NOT NULL,
"role" varchar(20) NOT NULL,
"created_at" timestamp NOT NULL DEFAULT (CURRENT_TIMESTAMP),
"updated_at" timestamp NOT NULL DEFAULT (CURRENT_TIMESTAMP)
);
CREATE TABLE "COACH" (
"id" uuid PRIMARY KEY NOT NULL DEFAULT uuid_generate_v4(),
"user_id" uuid NOT NULL REFERENCES "USER"(id),
"experience_years" integer,
"description" text,
"profile_image_url" varchar(2048),
"created_at" timestamp NOT NULL DEFAULT (CURRENT_TIMESTAMP),
"updated_at" timestamp NOT NULL DEFAULT (CURRENT_TIMESTAMP),
UNIQUE("user_id")
);
CREATE TABLE "CREDIT_PACKAGE" (
"id" serial PRIMARY KEY,
"name" varchar(50) NOT NULL,
"credit_amount" integer NOT NULL,
"price" numeric(10,2) NOT NULL,
"created_at" timestamp NOT NULL DEFAULT (CURRENT_TIMESTAMP)
);
CREATE TABLE "CREDIT_PURCHASE" (
"id" uuid PRIMARY KEY NOT NULL DEFAULT (gen_random_uuid()),
"user_id" uuid NOT NULL REFERENCES "USER"(id),
"credit_package_id" integer NOT NULL REFERENCES "CREDIT_PACKAGE"(id),
"purchased_credits" integer NOT NULL,
"price_paid" numeric(10,2) NOT NULL,
"created_at" timestamp NOT NULL DEFAULT (CURRENT_TIMESTAMP),
"purchase_at" timestamp NOT NULL DEFAULT (CURRENT_TIMESTAMP)
);
請先將 11/25 每日任務的使用者題目加入到資料庫指令後,再行答題
COACH
1. 新增:在 COACH
資料表新增三筆資料,資料需求如下:
李燕容
新增為教練,並且年資設定為2年(提示:使用 李燕容
的email ,取得 李燕容
的 id
)肌肉棒子
新增為教練,並且年資設定為2年(提示:使用 肌肉棒子
的email ,取得 肌肉棒子
的 id
)Q太郎
新增為教練,並且年資設定為2年(提示:使用 Q太郎
的email ,取得 Q太郎
的 id
)口訣:「多的要設定外來鍵」
流程:從欄位角度去規劃
➡️ 所以在「購買紀錄」表格(多的那方)設定外來鍵
-- 一對多(使用者 -> 購買紀錄)
CREATE TABLE "CREDIT_PURCHASE" (
"user_id" uuid NOT NULL REFERENCES "USER"(id), -- 多的那方加外來鍵
);
口訣:「外來鍵加唯一值」
流程:從欄位角度去規劃
在雙方資料表都只能對應一個資訊時,則會是一對一的資料關係
➡️ 所以在「教練」表格設定外來鍵,並加上 UNIQUE 限制
-- 一對一(使用者 <-> 教練)
CREATE TABLE "COACH" (
"user_id" uuid NOT NULL REFERENCES "USER"(id)
-- ...
UNIQUE("user_id") -- 外來鍵加唯一值
);`
UNIQUE
意思:表示此 user_id
在 COACH
資料表上不能重複,因為一個「教練」只能對應到 一個 使用者
請依照以下資料表線索,來分享:
流程:從欄位角度去規劃
以「」角度:一個「」有 ? 個 「」
以「」角度:一個「」有 ? 個 「」
依照上述描述,他是一對一關係、還是一對多?
以「用戶」角度:一個「用戶」有 多 個 「文章」
以「文章」角度:一個「文章」有 1 個 「用戶」
所以文章設FK、用戶設PK
請下載下方資料表圖片後,用小畫家繪製線條(一對一或一對多)
一對多
CREATE EXTENSION IF NOT EXISTS "uuid-ossp";
CREATE TABLE "USER" (
"id" uuid PRIMARY KEY NOT NULL DEFAULT (gen_random_uuid()),
"name" varchar(50) NOT NULL,
"email" varchar(320) UNIQUE NOT NULL,
"role" varchar(20) NOT NULL,
"created_at" timestamp NOT NULL DEFAULT (CURRENT_TIMESTAMP),
"updated_at" timestamp NOT NULL DEFAULT (CURRENT_TIMESTAMP)
);
CREATE TABLE "BLOG_POST" (
"id" uuid PRIMARY KEY NOT NULL DEFAULT (gen_random_uuid()),
"user_id" uuid NOT NULL REFERENCES "USER"(id),
"title" varchar(255) NOT NULL,
"content" text NOT NULL,
"featured_image_url" varchar(2048),
"category" varchar(20) NOT NULL,
"spend_minutes" smallint NOT NULL,
"created_at" timestamp NOT NULL DEFAULT (CURRENT_TIMESTAMP),
"updated_at" timestamp NOT NULL DEFAULT (CURRENT_TIMESTAMP)
);
or
or
By clicking below, you agree to our terms of service.
New to HackMD? Sign up
Syntax | Example | Reference | |
---|---|---|---|
# Header | Header | 基本排版 | |
- Unordered List |
|
||
1. Ordered List |
|
||
- [ ] Todo List |
|
||
> Blockquote | Blockquote |
||
**Bold font** | Bold font | ||
*Italics font* | Italics font | ||
~~Strikethrough~~ | |||
19^th^ | 19th | ||
H~2~O | H2O | ||
++Inserted text++ | Inserted text | ||
==Marked text== | Marked text | ||
[link text](https:// "title") | Link | ||
 | Image | ||
`Code` | Code |
在筆記中貼入程式碼 | |
```javascript var i = 0; ``` |
|
||
:smile: | ![]() |
Emoji list | |
{%youtube youtube_id %} | Externals | ||
$L^aT_eX$ | LaTeX | ||
:::info This is a alert area. ::: |
This is a alert area. |
On a scale of 0-10, how likely is it that you would recommend HackMD to your friends, family or business associates?
Please give us some advice and help us improve HackMD.
Syncing