iT邦幫忙

2021 iThome 鐵人賽

DAY 9
0
Modern Web

小白的從零開始食譜搜尋系統系列 第 9

MySQL學習_Day2

學習內容
資料儲存、約束資料、修改&刪除資料

  • 儲存資料:
    https://ithelp.ithome.com.tw/upload/images/20210918/201404970lmO5uVNQA.png

    • 在輸入字串資料時,若資料型態是VARCHAR時,可以使用雙引號「“ “」或單引號「‘ ’」表示。
    • 輸入資料時,若該欄位為空值,可以用’NULL’表示
      https://ithelp.ithome.com.tw/upload/images/20210918/20140497JqQyE66juQ.png
    • PRIMARY KEY是唯一表示每筆資料的屬性,因此在student_id中的資料絕對不能重複 !
    • 存入資料另一種寫法 _自行決定屬性順序,如果name欄位為空值,可以直接不填
      https://ithelp.ithome.com.tw/upload/images/20210918/201404979MIDapdZzh.png
      https://ithelp.ithome.com.tw/upload/images/20210918/20140497ZJvE6gFZ8N.png
  • 約束資料 :

    • 在屬性中加入特定條件來限制或約束。
    • EX :
      • NOT NULL :該屬性不可為空值
      • UNIQUE :該屬性的值皆不可以重複DEFAULT :設定預設值,例如5號學生丹娜沒有輸入major屬性的資料,執行時會自動填入預設值「德文」
        https://ithelp.ithome.com.tw/upload/images/20210918/20140497YBBKTJ64o6.png
      • AUTO_INCREMENT :可以不需要輸入student_id,在執行時自動填入
        https://ithelp.ithome.com.tw/upload/images/20210918/20140497MXqU0vfCIu.png
  • 修改資料&刪除 :

    • 要先把Workbench的預設模式關閉
      https://ithelp.ithome.com.tw/upload/images/20210918/20140497M311hylBqO.png
    • 情境描述1 : 要把成績低於60分的學生改為60分
      https://ithelp.ithome.com.tw/upload/images/20210918/20140497bw8ymAO57B.png
      • UPDATE : 更新資料表
      • WHERE : 設定更新條件,多個條件用OR/AND做區分
      • SET : 設定新的資料內容,多個內容用「,」做區分
    • 情境描述2 : 要把成績低於60分的學生刪除
      https://ithelp.ithome.com.tw/upload/images/20210918/20140497ZJpoGwoEe4.png

上一篇
MySQL學習_Day1
下一篇
MySQL學習_Day3
系列文
小白的從零開始食譜搜尋系統30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言