六角學院 - HexSchool
      • Sharing URL Link copied
      • /edit
      • View mode
        • Edit mode
        • View mode
        • Book mode
        • Slide mode
        Edit mode View mode Book mode Slide mode
      • Customize slides
      • Note Permission
      • Read
        • Owners
        • Signed-in users
        • Everyone
        Owners Signed-in users Everyone
      • Write
        • Owners
        • Signed-in users
        • Everyone
        Owners Signed-in users Everyone
      • Engagement control Commenting, Suggest edit, Emoji Reply
    • Invite by email
      Invitee
    • Publish Note

      Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

      Your note will be visible on your profile and discoverable by anyone.
      Your note is now live.
      This note is visible on your profile and discoverable online.
      Everyone on the web can find and read all notes of this public team.
      See published notes
      Unpublish note
      Please check the box to agree to the Community Guidelines.
      View profile
    • Commenting
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
      • Everyone
    • Suggest edit
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
    • Emoji Reply
    • Enable
    • Versions and GitHub Sync
    • Note settings
    • Engagement control
    • Transfer ownership
    • Delete this note
    • Insert from template
    • Import from
      • Dropbox
      • Google Drive
      • Gist
      • Clipboard
    • Export to
      • Dropbox
      • Google Drive
      • Gist
    • Download
      • Markdown
      • HTML
      • Raw HTML
Menu Note settings Versions and GitHub Sync Sharing URL Help
Menu
Options
Engagement control Transfer ownership Delete this note
Import from
Dropbox Google Drive Gist Clipboard
Export to
Dropbox Google Drive Gist
Download
Markdown HTML Raw HTML
Back
Sharing URL Link copied
/edit
View mode
  • Edit mode
  • View mode
  • Book mode
  • Slide mode
Edit mode View mode Book mode Slide mode
Customize slides
Note Permission
Read
Owners
  • Owners
  • Signed-in users
  • Everyone
Owners Signed-in users Everyone
Write
Owners
  • Owners
  • Signed-in users
  • Everyone
Owners Signed-in users Everyone
Engagement control Commenting, Suggest edit, Emoji Reply
  • Invite by email
    Invitee
  • Publish Note

    Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

    Your note will be visible on your profile and discoverable by anyone.
    Your note is now live.
    This note is visible on your profile and discoverable online.
    Everyone on the web can find and read all notes of this public team.
    See published notes
    Unpublish note
    Please check the box to agree to the Community Guidelines.
    View profile
    Engagement control
    Commenting
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    • Everyone
    Suggest edit
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    Emoji Reply
    Enable
    Import from Dropbox Google Drive Gist Clipboard
       owned this note    owned this note      
    Published Linked with GitHub
    Subscribed
    • Any changes
      Be notified of any changes
    • Mention me
      Be notified of mention me
    • Unsubscribe
    Subscribe
    # 11/27 每日任務 ## 需要必備的知識點 * 觀看 [group by 分組資料](https://hackmd.io/gDseaambSbW1pP-H6Yek8g?view)章節 ## 教練身份講解 在一個網頁平台上,會有多種角色,而在我們的任務二中的角色(`role`)會有: 1. 一般客戶:`USER` 2. 教練:`COACH` 在我們的[線稿圖](https://miro.com/app/board/uXjVLbiDml0=/?share_link_id=404709829822)中,也有繪製如果他是教練角色時,他可以填寫自己的相關教練資料,其中欄位包含: 1. 大頭照 2. 教練姓名 3. 教練資歷 4. 教練簡介 5. 專長(可複選) ![截圖 2024-11-26 下午11.40.24](https://hackmd.io/_uploads/BJOj3P7Q1g.png) ## 資料庫預設資訊與欄位介紹 ![Untitled](https://hackmd.io/_uploads/By78oDQ71g.png) ```sql 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 每日任務的使用者題目](https://hackmd.io/@hexschool/r1Knh0lXke)加入到資料庫指令後,再行答題** ### 教練資料 ,資料表為 `COACH` **1\. 新增**:在 `COACH` 資料表新增三筆資料,資料需求如下: - 將用戶 `李燕容` 新增為教練,並且年資設定為2年(提示:使用 `李燕容` 的email ,取得 `李燕容` 的 `id` ) - 將用戶`肌肉棒子`新增為教練,並且年資設定為2年(提示:使用 `肌肉棒子` 的email ,取得 `肌肉棒子` 的 `id` ) - 將用戶`Q太郎`新增為教練,並且年資設定為2年(提示:使用 `Q太郎` 的email ,取得 `Q太郎` 的 `id` ) ## 題目二:何謂一對多、一對一 ### 解釋一對多關係 口訣:「多的要設定外來鍵」 流程:從欄位角度去規劃 1. 以「使用者」角度:一個「使用者」會有**多筆**購買紀錄 2. 以「購買紀錄」角度:一筆「購買紀錄」只會屬於 一個 使用者 ➡️ 所以在「購買紀錄」表格(多的那方)設定外來鍵 ![截圖_2024-11-27_上午12_01_19](https://hackmd.io/_uploads/Skvz-d7mke.png) ```sql -- 一對多(使用者 -> 購買紀錄) CREATE TABLE "CREDIT_PURCHASE" ( "user_id" uuid NOT NULL REFERENCES "USER"(id), -- 多的那方加外來鍵 ); ``` ### 解釋一對一關係 **口訣:「外來鍵加唯一值」** 流程:從欄位角度去規劃 1. 以「使用者」角度:一個「使用者」最多只能是 **一個** 教練 2. 以「教練」角度:一個「教練」只能對應到 **一個** 使用者 **在雙方資料表都只能對應一個資訊時,則會是一對一的資料關係** ➡️ 所以在「教練」表格設定外來鍵,並加上 UNIQUE 限制 ```sql -- 一對一(使用者 <-> 教練) CREATE TABLE "COACH" ( "user_id" uuid NOT NULL REFERENCES "USER"(id) -- ... UNIQUE("user_id") -- 外來鍵加唯一值 );` ``` `UNIQUE` 意思:表示此 `user_id` 在 `COACH` 資料表上不能重複,因為一個「教練」只能對應到 **一個** 使用者 ![截圖 2024-11-27 上午12.09.17](https://hackmd.io/_uploads/SyXRzOQQye.png) ## 題目二:使用者與部落格文章的資料表關係 請依照以下資料表線索,來分享: 1. 你的分析流程,如下 流程:從欄位角度去規劃 以「」角度:一個「」有 ? 個 「」 以「」角度:一個「」有 ? 個 「」 2. 依照上述描述,他是一對一關係、還是一對多? `以「用戶」角度:一個「用戶」有 多 個 「文章」` `以「文章」角度:一個「文章」有 1 個 「用戶」` `所以文章設FK、用戶設PK` 3. 請下載下方資料表圖片後,用小畫家繪製線條(一對一**或**一對多) >>> 一對多 ```sql= 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) ); ``` ![截圖_2024-11-27_上午12_12_06](https://hackmd.io/_uploads/S15sX_7m1x.png)

    Import from clipboard

    Paste your markdown or webpage here...

    Advanced permission required

    Your current role can only read. Ask the system administrator to acquire write and comment permission.

    This team is disabled

    Sorry, this team is disabled. You can't edit this note.

    This note is locked

    Sorry, only owner can edit this note.

    Reach the limit

    Sorry, you've reached the max length this note can be.
    Please reduce the content or divide it to more notes, thank you!

    Import from Gist

    Import from Snippet

    or

    Export to Snippet

    Are you sure?

    Do you really want to delete this note?
    All users will lose their connection.

    Create a note from template

    Create a note from template

    Oops...
    This template has been removed or transferred.
    Upgrade
    All
    • All
    • Team
    No template.

    Create a template

    Upgrade

    Delete template

    Do you really want to delete this template?
    Turn this template into a regular note and keep its content, versions, and comments.

    This page need refresh

    You have an incompatible client version.
    Refresh to update.
    New version available!
    See releases notes here
    Refresh to enjoy new features.
    Your user state has changed.
    Refresh to load new user state.

    Sign in

    Forgot password

    or

    By clicking below, you agree to our terms of service.

    Sign in via Facebook Sign in via Twitter Sign in via GitHub Sign in via Dropbox Sign in with Wallet
    Wallet ( )
    Connect another wallet

    New to HackMD? Sign up

    Help

    • English
    • 中文
    • Français
    • Deutsch
    • 日本語
    • Español
    • Català
    • Ελληνικά
    • Português
    • italiano
    • Türkçe
    • Русский
    • Nederlands
    • hrvatski jezik
    • język polski
    • Українська
    • हिन्दी
    • svenska
    • Esperanto
    • dansk

    Documents

    Help & Tutorial

    How to use Book mode

    Slide Example

    API Docs

    Edit in VSCode

    Install browser extension

    Contacts

    Feedback

    Discord

    Send us email

    Resources

    Releases

    Pricing

    Blog

    Policy

    Terms

    Privacy

    Cheatsheet

    Syntax Example Reference
    # Header Header 基本排版
    - Unordered List
    • Unordered List
    1. Ordered List
    1. Ordered List
    - [ ] Todo List
    • Todo List
    > Blockquote
    Blockquote
    **Bold font** Bold font
    *Italics font* Italics font
    ~~Strikethrough~~ Strikethrough
    19^th^ 19th
    H~2~O H2O
    ++Inserted text++ Inserted text
    ==Marked text== Marked text
    [link text](https:// "title") Link
    ![image alt](https:// "title") Image
    `Code` Code 在筆記中貼入程式碼
    ```javascript
    var i = 0;
    ```
    var i = 0;
    :smile: :smile: Emoji list
    {%youtube youtube_id %} Externals
    $L^aT_eX$ LaTeX
    :::info
    This is a alert area.
    :::

    This is a alert area.

    Versions and GitHub Sync
    Get Full History Access

    • Edit version name
    • Delete

    revision author avatar     named on  

    More Less

    Note content is identical to the latest version.
    Compare
      Choose a version
      No search result
      Version not found
    Sign in to link this note to GitHub
    Learn more
    This note is not linked with GitHub
     

    Feedback

    Submission failed, please try again

    Thanks for your support.

    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.

     

    Thanks for your feedback

    Remove version name

    Do you want to remove this version name and description?

    Transfer ownership

    Transfer to
      Warning: is a public team. If you transfer note to this team, everyone on the web can find and read this note.

        Link with GitHub

        Please authorize HackMD on GitHub
        • Please sign in to GitHub and install the HackMD app on your GitHub repo.
        • HackMD links with GitHub through a GitHub App. You can choose which repo to install our App.
        Learn more  Sign in to GitHub

        Push the note to GitHub Push to GitHub Pull a file from GitHub

          Authorize again
         

        Choose which file to push to

        Select repo
        Refresh Authorize more repos
        Select branch
        Select file
        Select branch
        Choose version(s) to push
        • Save a new version and push
        • Choose from existing versions
        Include title and tags
        Available push count

        Pull from GitHub

         
        File from GitHub
        File from HackMD

        GitHub Link Settings

        File linked

        Linked by
        File path
        Last synced branch
        Available push count

        Danger Zone

        Unlink
        You will no longer receive notification when GitHub file changes after unlink.

        Syncing

        Push failed

        Push successfully