XMRHRX
    • Create new note
    • Create a note from template
      • 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
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Write
        • Only me
        • Signed-in users
        • Everyone
        Only me 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
    • Save as template
    • 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 Create Help
Create Create new note Create a note from template
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
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Write
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me 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
    --- title: 2/4 更多基本SQL & 一些概念 --- ## 更多基本SQL & 一些概念 ### 2021/3/10 王子軒 <!-- 2.基本SQL & 概念 Index, pri/f key INSERT DELETE --> --- ## 快速複習! ``` 欄位名稱跟table名稱要加重音符號(`)! 查詢: SELECT * FROM <table> WHERE <condition> ``` --- ## 觀念:欄位的Key設定 ---- ### Index (索引鍵) * 資料索引,可加快搜尋速度 * 可多欄位設定為 Index NOTE:有點像是看書的時候的頁碼,方便你定位 ---- ### Primary Key(PK): >常用來連接不同table之間的查詢/關聯(F Key) * 是一種 index 但不能為空值(NULL),PK會自動建立 index * 每個 table 只能有一個 PRIMARY Key * 每一筆資料的PK都要獨一無二(不一樣) ---- ![](https://i.imgur.com/J2nd3xl.png) NOTE:你如果有uid,就可以很快定義出具體的某比資料,就如同你有頁碼,就可以很快找到某一頁 ---- ### Foreign Key(~~FK~~): ~~table之間的愛恨情愁~~ * 避免表之間的關聯被破壞 * 防止不符合對應欄位的型態 * 插入的值一定要在對應的表 ---- ![](https://i.imgur.com/C9aRUZU.png) NOTE: 用個例子來解釋一下,這是兩張表,上面的叫做Persons,下面的叫做Orders。PersonID是上面表的PK,它每筆資料都一定會有。下面的表格用到PersonID、用到對方的PK,所以建立起了關聯。它對應到別張表的這個欄位就可以設定成FOREIGN KEY,所以邊PersonID就是Foreign Key,它可以讓資料庫自動避免關聯被破壞,避免像是Persons被刪除,Orders對應不到,或是欄位資料內容不對。 --- #### 哪個是FK? ![](https://i.imgur.com/XbraaV9.png) NOTE: 哪個欄位是FK呢?這邊可以看到有關連的部份只有country_code和code,可以注意到有\*和數字1,星號是說,很多比紀錄的這個欄位可以值一樣,像是我和你分別是兩比紀錄,而且都住台灣,那我們的country_code就會一樣。 而那個1就是指說coutries這個table裡面的每一筆紀錄,code不會重複,就是說不會有兩個country名字不一樣,code卻一樣。答:country_code --- ## 實做一張表 右鍵點擊左邊的"Tables">"Create a table" ![](https://i.imgur.com/Cg6VE2u.png) NOTE:講了那們多,我先來代你們用圖形界面實際做一張表,這樣你如果有想試試的操作就可以自己弄表跟資料了 ---- 1. 打上Table name 2. 點擊下方空白處可以設定column ![](https://i.imgur.com/rDN9cHf.png) ---- >試試看新增一張表,叫做user >裡面有 | 欄位 | 欄位名稱 | | ------------------ | ------------------ | | 使用者 | UID | | 使用者居住地址 | City_Code | | 使用者名字 | username | | 使用者密碼 | password | NOTE:你們覺得哪個欄位要設定什麼型態?哪些要有Key? --- ## query time! NOTE:今天來說說新增跟刪除吧! --- ### 新增 ``` INSERT INTO <table>(<column> [,<column2>,...]) VALUES(<value for col1> [, <value for col2>]) ``` ex: ``` INSERT INTO `customer`(`nickname`, `password`) VALUES ("Temmie", "Hoi_I_AM_TEMMIE") ``` ---- ### 練習! 幫你們剛剛的表新增3筆資料 --- ### 刪除 ``` DELETE FROM <table> WHERE <condition> ``` NOTE:注意,操作資料庫的時候,尤其是刪除表或是資料的時候,一定要注意你的條件,避免勿刪,或是讓整個資料表清空。 ---- ### 試試看 刪掉Taiwan裡,City_code<100的 --- ### 用Query創建表 ---- ### 創建表 ``` CREATE TABLE <table_name> ( <column_name1> <data_type>, <column_name2> <data_type>, <column_name3> <data_type>, ··· ); ``` ---- ### 做出一張表 table name: orders | 名稱 | 型態 | | -------- | -------- | | ID | 數值,PK | | username | 可變長度的字串 | | price | ??? | NOTE: 要注意表的名稱要加s,因為order本身是SQL語法的詞,所以要避開。還有要先點選database指定它後,才能在Open SQL editor,底下用query生出table 這一部份,我想讓你們先是試著自己從網路查怎們樣在創建table時,設定PK <!-- CREATE TABLE orders ( ID INT, username varchar(255), price decimal(10,2), PRIMARY KEY (ID) ); --> --- ### 刪除表 ``` DROP TABLE <table_name>; ``` --- ### 補充資源:diagram https://dbdiagram.io/home NOTE:這是一個可以畫table關聯圖的網站,有興趣可以進去試試看。它預設的table關係可以參考一下,想想看表之間的關係。

    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