EddyYeh
    • 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 No publishing access yet

      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.

      Your account was recently created. Publishing will be available soon, allowing you to share notes on your public page and in search results.

      Your team account was recently created. Publishing will be available soon, allowing you to share notes on your public page and in search results.

      Explore these features while you wait
      Complete general settings
      Bookmark and like published notes
      Write a few more notes
      Complete general settings
      Write a few more notes
      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 New
    • Engagement control
    • Make a copy
    • 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 Note Insights Versions and GitHub Sync Sharing URL Create Help
Create Create new note Create a note from template
Menu
Options
Engagement control Make a copy 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 No publishing access yet

    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.

    Your account was recently created. Publishing will be available soon, allowing you to share notes on your public page and in search results.

    Your team account was recently created. Publishing will be available soon, allowing you to share notes on your public page and in search results.

    Explore these features while you wait
    Complete general settings
    Bookmark and like published notes
    Write a few more notes
    Complete general settings
    Write a few more notes
    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
    • Any changes
      Be notified of any changes
    • Mention me
      Be notified of mention me
    • Unsubscribe
    # SQL Class https://learn.microsoft.com/zh-tw/training/browse/?products=sql-server ## 資料庫基本 ### 資料庫歷史 ANSI SQL(通用SQL),在微軟加入變數、流程、函數之下變成T-SQL 其他廠商也有類似ex:oracle有plSQL SQL Server management studio(SSMS):gui介面連資料庫 - 17版不推薦用 MIS是資訊管理系統(Management Information System) 管理信息系統由決策支持系統(DSS)、工業控制系統(CCS)、辦公自動化系統(OA)以及資料庫、模型庫、方法庫、知識庫和與上級機關及外界交換信息的接口組成。 成本會計(算製造成本)、總帳會計(收多少錢) ### 資料結構: 如何組織、如何存取access queue:管子 FIFO LILO stack(堆疊區):桶子FILO、LIFO storage:RAM vs HDD 小資料系統用fileSystem,大的用資料庫 ### 資料data 1.真實世界實體:老師、學生 2.結構敘述(schema):(學生的重要特徵)學生ID,名子,生日... 3.實體(Instance):(現有的資料)10503314,Jack,1991/1/1 欄位 attribute = field = column = feature 一筆資料表的資料 = record = row = tuple 一格資料 = cell table資料表定義比較嚴謹 tabular二維表格定義較鬆(什麼五大天條的不重要) 命名重覆 C# namespace,java package ### 資料模型是什麼? 1.資料結構: unicode:外國字集 盡量用 其他字集:big5(資策會), gb, #### 定序 ![](https://i.imgur.com/7hHBlao.png) #### 字集 ci(不分大小寫) AS(全形半形的定序) utf8mb4_unicode_ci - MySQL字元編碼集中有兩套UTF-8編碼實現,「utf8」和「utf8mb4」,其中「utf8」是一個字最多占據3位元組空間的編碼實現;而「utf8mb4」則是一個字最多占據4位元組空間的編碼實現,也就是UTF-8的完整實現。 - general :沒有基於Unicode排序規則,在遇到某些特殊語言或字符是,排序結果可能不是所期望的。unicode:是基于標準的Unicode來排序和比較,能夠在各種語言之間精確排序 - ci (case insensitive),對非二進製字符串比較不區分大小寫cs (case sensitive),對非二進製字符串比較以區分大小寫的方式處理bin (binary),二進製字符串搜索 ``` 3-1-1位元(Bit)   電腦中最基本的儲存單位,一個位元有兩個狀態,可儲存一個0(沒   電)或1(有電)。也是傳輸資料的基本單位,同一時間能傳達的位元   數越多,則其資料傳輸的速度越快。 3-1-2位元組(Byte)   記憶體的容量單位,1byte=8bits=28=256種狀態,故能用來代表英文字   母、數字和特殊符號,通常電腦中使用一個位元組(一種狀態)來表示   一個字元。也是電腦用來表示資料和儲存資料的基本單位。 3-1-3字組(Word)   CPU一次所能處理的位元數,稱為「字組」。字組的長度隨電腦的不同   而有所不同。如32位元的IBM pc,其字組的長度為32個位元。亦即1word=32bits ``` - 一個中文字2byte, varchar(12)可放6個中文字 - unicode === 前面加個n,nvarchar - 可以到以下介面看資料庫的資料結構 ![](https://i.imgur.com/BDGqPL1.png) 2.整合性限制(個體整合性限制:主鍵唯一鍵,參考整合性限制:資料表之間的限制主鍵外鍵) 3.MANIPULATION操作(CRUD) ### SQL Server studio介面: master: model: msdb: 資料庫管理 tempdb:從model copy是關機會消失的 mysql兩段式:db.table,orcle:db.schema.table ex:table前面帶dbo #### 查詢內建時間函數 ![](https://i.imgur.com/RWqGedz.png) ### 匯入資料庫教學(參考readme雲端硬碟連結的檔案) 微軟有範例資料庫匯入pubs,northwind,adbenturework的資料庫 新增(mount)資料庫副檔名 .ndf .mdf .idf 1. 進入C:\Program Files\Microsoft SQL Server\MSSQL15.MSSQLSERVER\MSSQL\DATA 2. 貼檔案 3. 開sql server management 4. 資料庫右鍵>附加>加入 5. 分配到的port ![](https://i.imgur.com/kpEAwNU.png) ### SQL Server設定管理員 - 暫停:其他未登入的不能登 - 重新啟動:設定檔更新 下圖說明:SQL Server Browser先右鍵開啟>服務>已停止改自動,再右鍵開啟 ![](https://i.imgur.com/uHbYIW7.png) 下圖說明:具名管道 TCP/IP都打開 ![](https://i.imgur.com/2l6XH86.png) Null值append後還是null ## 操作資料庫 ### 建立資料庫 ```sql= -- 建立資料庫 school, DDL create-- create database school; -- 使用資料庫 use school -- 建立學生資料表 create table student( s_id int primary key, s_name varchar(20), major varchar(20), age int ) -- 新增資料 -- insert into student(s_id,s_name,major,age) values(1,'jack','cs',10); insert into student(s_id,s_name,major,age) values(2,'maria','cs',40); insert into student(s_id,s_name,major,age) values(3,'monkey','cs',30); -- 讀取 select * from student -- 更新 update student set age = 100 where s_id = 1 -- 刪除 delete from student where s_id = 3 -- 修改資料表 alter table student add tel varchar(12); -- 刪資料表 drop table student; ``` ### 資料庫鍵 - 主鍵(primary Key):唯一且非Null null 1.尚未給值 2.不知道/不清楚/有錯誤 3.null ex:空集合:查詢沒半筆資料,不是Null - 候選鍵(candidate key) ex:姓名 要複合主鍵才能當主鍵 主鍵所在的:parent table 外鍵所在的:child table - 唯一鍵 唯一不重覆 - 外鍵(Foreign key): restricted: (預設)不給動作 Cascades:同步刪除資料 ### 介面操作與Help - 可以右鍵 編輯前兩百筆資料 #### 可以用stored procedure去help找資料格式 ```sql= -- 查詢表單基本資料 sp_help student; ``` ### SQL語法筆記 ```sql= -- 填值 -- as叫做逸出字元escape select '作家' as '都是叫作家的欄位' from Table -- 另存的永久新表單 select * into {另存的永久新表單名稱} from Table -- 表格字串合併 select colA + colB as newColName from Table -- 分隔識別字 在字串中間的',第一行會故障不行,第二行才行 -- select 'the publisher's name is' as ' ' from authors; select 'the publisher"s name is' as ' ' from authors; -- 分隔識別字 "" [] ex:有空白 create table "some poeple"(c1 int,c2 varchar(10)); ``` ### sql查詢語法 #### search七大語法 /* Searching Condition: 7種 */  -- 1. Comparsion (比較運算) -- 2. Range (範圍) between但建議comparion比較快 -- 3. List (列舉) in(...) -- 4. String Matching (字串比對) -- 5. Null -- 6. Joins -- 7. Subquery ansi時間格式 '1970-01-01',SQL Server時間between不包含端點,記得select後核對一下。 #### sql時間用法 ```sql use Northwind; select * from orders order by OrderDate asc; -- 1. 1997以前有幾張訂單 select count(*) from orders where OrderDate < '1997-01-01'; select count(*) from orders where year(OrderDate) < '1997'; -- 2. 1997有幾位客戶 select * from orders where OrderDate between '1997-01-01' and '1997-12-31'; select count(distinct (CustomerId)) from orders where OrderDate between '1997-01-01' and '1997-12-31'; select count(distinct (CustomerId)) from orders where OrderDate >= '1997-01-01' and OrderDate <= '1997-12-31'; ``` ##### 題目:用in的方式(限有鍵值)與left join比較 example 2: northwind料庫中找出產品(products)的供應商(suppliers) 國籍(country)是('Canada','Australia','Germany') 的產品代號(productid),產品名稱(productname),及供應商代號(supplierid) ```sql= use Northwind go select productid,productname,supplierid from products where SupplierID in ( select SupplierID from suppliers where Country in ('Canada','Australia','Germany')) ``` ### String Pattern-Matching -- 萬用字元說明: % (萬用字元 - 相符的字元) [ ] (萬用字元 - 相符的字元) [^] (萬用字元 - 不相符的字元) _ (萬用字元 - 符合一個字元) like 1.資料庫定序中(collate)如果包含_CS --> CASE SENSITIVE 2.資料庫定序中(collate)如果包含_CI --> CASE INSENSITIVE ex: 'Broker','BROKER','broker' ### SELECT CLAUSE 執行邏輯(理論順序) table > from > join on > where > group by > with > having > select > order by ``` (8) SELECT (9) [ALL | DISTINCT] (11)[TOP n] [INTO] (1) FROM 資料來源 (3) [INNER|LEFT|RIGHT] JOIN (2) ON <join condition> (4) WHERE <condition> (5) [GROUP BY] (6) [WITH] (7) [HAVING] <condition> (10)[ORDER BY] [ASC | DESC] ``` ### 變數宣告 ![](https://i.imgur.com/033u3vZ.png) ![](https://i.imgur.com/rKjYnmm.png) ### 資料庫效能優化 - 注意 NOT IN 會降低系統執行效率,儘量少用。 - 注意 NOT BETWEEN 會降低系統執行效率,儘量少用。 ## 資料庫備份 ![](https://i.imgur.com/cA5YnPo.png) ### view的用途 權限(view看得到才能CRUD)、方便 ![](https://i.imgur.com/MopS073.png) ### ![](https://i.imgur.com/j4ZPVUS.png) ![](https://i.imgur.com/jKt2gyS.png) ## 其他建議網站連結 這個部落格不錯 有技術文章 https://medium.com/mr-efacani-teatime 建議:到微軟的官網刷教學 https://learn.microsoft.com/zh-tw/sql/t-sql/queries/select-transact-sql?view=sql-server-ver16 T-SQL 30天 https://ithelp.ithome.com.tw/articles/10007971 https://ithelp.ithome.com.tw/articles/10027653 roadmap.sh 阿共版w3school http://www.w3cmap.com/

    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
    Sign in via Facebook Sign in via X(Twitter) Sign in via GitHub Sign in via Dropbox Sign in with Wallet
    Wallet ( )
    Connect another wallet

    New to HackMD? Sign up

    By signing in, you agree to our terms of service.

    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