DD513
    • 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

      This note has no invitees

    • 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
    • Note Insights
    • 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 Note Insights 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

    This note has no invitees

  • 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
    1
    Subscribed
    • Any changes
      Be notified of any changes
    • Mention me
      Be notified of mention me
    • Unsubscribe
    Subscribe
    --- tags: 分享 --- # SQL 資料庫基本語法介紹 本次實作以 Mysql 實作 ## 基本語法 ### 創建資料庫、表 > 首先使用SQL語法創建,或者直接運用資料庫創建 創建資料庫使用 CREATE,COLLATE collation_name 指定資料庫的預設定序,這邊使用 utf8_general_ci 處理中文字排序。 創建資料庫: ```sql= CREATE DATABASE 資料庫名稱 COLLATE 編碼; ``` 創建資料表: 創建資料表(這邊建立三個欄位 name、age 和 gender,其後面為該欄位的屬性設定,例如資料型別、長度、是否允許 NULL 值、default 值等),一個資料庫中可以有多張資料表。 ```sql= CREATE TABLE 資料表名稱 ( 欄位名稱 欄位屬性 ); ``` users 資料表 ```sql= CREATE TABLE users ( id int PRIMARY KEY, name varchar(255) NOT NULL, age int NULL, gender char(10) NULL, class_Id int , ); ``` class 資料表 ```sql= CREATE TABLE class ( id int PRIMARY KEY, classname VARCHAR(12) NOT NULL, age INTEGER, gender VARCHAR(6) ); ``` 新增資料欄位 ```sql= INSERT INTO student ( student_id, NAME, science_score, math_score, english_score ) VALUES ( 001, '小明', 60, 70, 80 ), ( 002, '小黃', 80, 90, 80 ), ( 003, '小紅', 50, 20, 30 ), ( 004, '小黑', 20, 30, 100 ), ( 005, '小橘', 93, 53, 64 ); ``` ### 讀取資料表 - 我們並不一定每一次都要將表格內的資料都完全抓出。在許多時候,我們會需要選擇性地抓資料。 就我們的例子來說,我們可能只要抓出營業額超過 $1,000 的資料。要做到這一點,我們就需要用到 WHERE 這個指令。這個指令的語法如下: ``` sql= SELECT "欄位名" FROM "表格名"; ``` ### WHERE * 我們並不一定每一次都要將表格內的資料都完全抓出。在許多時候,我們會需要選擇性地抓資料。 就我們的例子來說,我們可能只要抓出營業額超過 $1,000 的資料。要做到這一點,我們就需要用到 WHERE 這個指令。這個指令的語法如下: ```sql= SELECT "欄位名" FROM "表格名" WHERE "條件"; // SELECT * FROM student WHERE science_score > 50 ``` ### AND/OR - 我們看到 WHERE 指令可以被用來由表格中有條件地選取資料。 這個條件可能是簡單的 ,也可能是複雜的。複雜條件是由二或多個簡單條件透過 AND 或是 OR 的連接而成。一個 SQL 語句中可以有無限多個簡單條件的存在。 ``` sql= SELECT "欄位名" FROM "表格名" WHERE "簡單條件" {[AND|OR] "簡單條件"}+; ``` ### ORDER BY ``` sql= SELECT "欄位名" FROM "表格名" [WHERE "條件"] ORDER BY "欄位名" [ASC, DESC]; ``` ### GROUP BY ``` sql= SELECT "欄位名" FROM "表格名" [WHERE "條件"] GROUP BY "欄位名" [ASC, DESC]; ``` ### HAVING ```sql= SELECT "欄位1", SUM("欄位2") FROM "表格名" GROUP BY "欄位1" HAVING (函數條件); ``` | Store_Name | Txn_Date | Sales | | ---------- | -------- | ----- | | Los Angeles| 1500 | 05-Jan-1999 | | San Diego | 250 | 07-Jan-1999 | | Los Angeles| 300 | 08-Jan-1999 | | Boston | 700 | 08-Jan-1999 | ```sql= SELECT Store_Name, SUM(Sales) FROM Store_Information GROUP BY Store_Name HAVING SUM(Sales) > 1500; ``` 結果如下: | Store_Name | SUM(Sales) | | ----------- | ----------- | | Los Angeles | 1800 | ## 實作挑戰: ### 新增資料表 > 新增Student,Cource,Teacher三張資料表 ```sql= CREATE TABLE Student ( SId VARCHAR ( 10 ) PRIMARY KEY, Sname VARCHAR ( 10 ), age datetime, sex VARCHAR ( 10 ), Tid VARCHAR ( 10 )); CREATE TABLE Course ( CId VARCHAR ( 10 ) PRIMARY KEY, Cname VARCHAR ( 10 ), TId VARCHAR ( 10 )); CREATE TABLE Teacher ( TId VARCHAR ( 10 ) PRIMARY KEY, Tname VARCHAR ( 10 )); create table SC( SId VARCHAR ( 10 ), CId VARCHAR ( 10 ), score decimal(18,1) ); ``` ### 新增資料欄位: ```sql= INSERT INTO Student ( SId, Sname, age, sex, Tid ) VALUES ( '01', '趙雷', '1990-01-01', '男', '01' ), ( '02', '錢電', '1990-12-21', '男', '02' ), ( '03', '孫風', '1990-12-20', '男', '03' ), ( '04', '李雲', '1990-12-06', '男', '01' ), ( '05', '周梅', '1991-12-01', '女', '02' ), ( '06', '吳蘭', '1992-01-01', '女', '03' ), ( '07', '鄭竹', '1989-01-01', '女', '01' ), ( '08', '孫七', '2014-06-01', '女', '02' ), ( '09', '張三', '2017-12-20', '女', '02' ), ( '10', '李四', '2017-12-25', '女', '01' ), ( '11', '李大人', '2012-06-06', '女', '03' ), ( '12', '趙小孩', '2013-06-13', '女', '01' ), ( '13', '趙12', '2013-06-13', '女', '01' ); INSERT INTO Course ( CId, Cname, TId ) VALUES ( '01', '語文', '02' ), ( '02', '數學', '01' ), ( '03', '英語', '03' ), ( '04', '自然', '05' ); INSERT INTO Teacher ( TId, Tname ) VALUES ( '01', '張三' ), ( '02', '李四' ), ( '03', '王五' ), ( '04', '李二' ), ( '05', '王七' ); INSERT INTO SC ( SId, CId, score ) VALUES ( '01', '01', 80 ), ( '01', '02', 90 ), ( '01', '03', 99 ), ( '01', '04', 50 ), ( '02', '01', 70 ), ( '02', '02', 60 ), ( '02', '04', 50 ), ( '03', '01', 80 ), ( '03', '02', 80 ), ( '03', '03', 80 ), ( '03', '04', 80 ), ( '04', '01', 50 ), ( '04', '02', 30 ), ( '04', '03', 20 ), ( '04', '04', 90 ), ( '05', '01', 76 ), ( '05', '02', 87 ), ( '05', '04', 45 ), ( '06', '01', 31 ), ( '06', '03', 34 ), ( '07', '02', 89 ), ( '07', '03', 98 ), ( '08', '01', 82 ), ( '08', '02', 56 ), ( '08', '03', 77 ), ( '08', '04', 70 ), ( '09', '01', 70 ), ( '09', '02', 32 ), ( '09', '03', 72 ), ( '10', '01', 80 ), ( '10', '03', 80 ), ( '11', '01', 50 ), ( '11', '02', 30 ), ( '11', '03', 20 ), ( '12', '02', 100 ), ( '13', '01', 20 ), ( '13', '03', 64 ); ``` ### 簡單題目: 1. 求每門課程的學生人數? 範例: ![](https://i.imgur.com/drDOCUD.png =230x200) 2. 查詢選修了大於等於三堂課程的學生資訊 範例: * 範例為全部課程的 ![](https://i.imgur.com/FUA2bmE.png =400x170) 3. 查詢「李」姓老師的數量 範例: ![](https://i.imgur.com/i5J1LaW.png =200x150) 4. 查詢課程編號為 01 且課程成績在 80 分以上的學生的學號和姓名 範例: ![](https://i.imgur.com/FrJ2dUn.png =400x170) --- 進階玩玩看: 5. 查詢各科成績前三名的記錄 ,取得學生年齡、性別還有成績班級、(會的話可以給老師) :::success 解答尚未公布!!! [解答連結](https://hackmd.io/gyI24hrZSz6EYNJcnS2J5A?view) :::

    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