iT邦幫忙

第 12 屆 iThome 鐵人賽

DAY 7
1
Modern Web

ASP.NET MVC5架構說明與Q&A諮詢系統實作系列 第 7

[DAY7] SSMS 建資料庫、索引

建立資料庫

  1. 資料庫的資料夾按右鍵 - 新增資料庫
    https://ithelp.ithome.com.tw/upload/images/20200919/20128969SyYITo0fuD.png

  2. 輸入資料庫名稱:Test,按確定(其他的設定可以依自己的需求更改,這邊就先不細說)
    https://ithelp.ithome.com.tw/upload/images/20200919/20128969rbGTmJqy2Q.png

  3. 建立完成啦!是不是非常簡單ㄚ (๑•̀ㅂ•́)و✧
    https://ithelp.ithome.com.tw/upload/images/20200919/20128969tnpfZzSpuv.png


建立資料表

  1. 打開資料庫,在資料表的資料夾按右鍵 - 新增資料表
    https://ithelp.ithome.com.tw/upload/images/20200920/20128969IUCdpVQdgf.png

  2. 新增資料行名稱、屬性、是否可為 Null,儲存資料表,輸入資料表名稱
    (這邊先新增兩個資料表)
    https://ithelp.ithome.com.tw/upload/images/20200920/201289693iGaldFUM8.png
    https://ithelp.ithome.com.tw/upload/images/20200920/20128969CaPUYKZHhf.png

  3. 在左邊的物件總管按重新整理,就會看到剛剛新增的資料表了!
    (若還是沒看到,可以在 Test 資料庫按右鍵 - 重新整理)
    https://ithelp.ithome.com.tw/upload/images/20200920/20128969yBaq1pBOuD.png


欄位屬性與設定調整

這邊先介紹

  • 主鍵(primary key):確保為唯一、非空的值,常常與外鍵完整性約束連結,防止出現數據不一致
  • 外鍵(foreign key):維護兩個表之間數據的一致性,表的外鍵就是另一表的主鍵,外鍵將兩表聯系起來
  • 常見的資料屬性
    https://ithelp.ithome.com.tw/upload/images/20200920/20128969LdPUbJKoFB.jpg
    屬性括號內的單位為 byte,
    例如:char(10),為 10 bytes 大小、只裝英文或數字的資料格
    一個英文字母需要 1 byte,一個中文字需要 2 bytes
    ( bit 位元是電腦最小單位,8 bits = 1 btye)
  1. 設主鍵(主索引),不設定也行,但 EF code 會報錯
    ( EF 下一篇會解釋其意思,這邊可以先解釋成資料架構不明確,所以出錯)
    可以多設定其他索引(index):加快搜尋速度(索引可為單一欄位或多個欄位)
    這邊設定了兩個索引
    https://ithelp.ithome.com.tw/upload/images/20200920/20128969PZCC5b9D0q.png

  2. 設外鍵:目的是確保資料正確性,不誤刪等
    按右鍵 - 關聯性,將兩個資料表中的欄位進行連結
    (先前新增的兩個資料表中有相同的欄位,兩個欄位是有關聯的)
    當有多個資料表的欄位有關聯時,亦可以連接多個資料表
    https://ithelp.ithome.com.tw/upload/images/20200920/20128969FAiEdbYbZE.png
    跑出這個視窗後,先按左邊的加入,在按右邊的「…」
    https://ithelp.ithome.com.tw/upload/images/20200920/20128969kPM1eNxShD.png
    之後就將主鍵與外鍵進行連結,按確定就可以囉!
    https://ithelp.ithome.com.tw/upload/images/20200920/20128969zntYF6khB9.png


新增資料

  1. 資料表按右鍵 - 編輯前200個資料列
    https://ithelp.ithome.com.tw/upload/images/20200920/20128969yd2xxey8RO.png

  2. 新增符合資料屬性的資料
    https://ithelp.ithome.com.tw/upload/images/20200920/20128969KMmoqrjQfn.png
    https://ithelp.ithome.com.tw/upload/images/20200920/20128969qngFVioNfO.png


以上,資料庫的部分大概就完成了ㄡ ✧◝(⁰▿⁰)◜✧


上一篇
[DAY 6] SQL Server 資料庫從安裝到操作介面 - SSMS
下一篇
[DAY 8] 什麼是 Entity Framework?
系列文
ASP.NET MVC5架構說明與Q&A諮詢系統實作30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言