iT邦幫忙

0

第37天~兩張資料表之間

Tzu 2022-02-08 01:35:171036 瀏覽
  • 分享至 

  • xImage
  •  

這篇的上一篇:https://ithelp.ithome.com.tw/articles/10283916

超鍵、候選鍵、主鍵、外來鍵、聯合主鍵、複合主鍵

/images/emoticon/emoticon04.gif

https://www.itread01.com/content/1545892442.html

寫得很好~

https://blog.kkbruce.net/2010/10/normalization-key.html#.YgGcN7pBxPY


https://ithelp.ithome.com.tw/upload/images/20220208/20119035laZKsJeocm.png

主鍵的挑選順序:超鍵、候選鍵、主鍵、替代鍵

超鍵-唯一性.不允許兩個值相同.表格看不到

n 一律不會定義可儲存的字元數。 這類似於 CHAR (n) 和 VARCHAR (n) 的定義。

固定大小 nchar 或變動大小 nvarchar 的字元資料類型

在資料庫語法前面要加[表格名]=N'字串'

除了數字之外.'字串' 都要加' '


候選鍵-唯一性

主鍵-唯一性/不允許空值/-單一資料表-PK-Primary Key

外來鍵(外部索引鍵)-Foreign Key,FK-多重資料表


TSQL開發圖表工具-兩個檔案直接放C槽(不要放桌面)
https://ithelp.ithome.com.tw/upload/images/20220208/201190358m74Y5TnAu.png

https://ithelp.ithome.com.tw/upload/images/20220208/20119035MvCONsiLfk.png

畫圖~ERD

https://ithelp.ithome.com.tw/upload/images/20220208/20119035E8TNln15Nh.png

打開長這樣

https://ithelp.ithome.com.tw/upload/images/20220208/20119035BEqtItmyR6.png

選-->其他工作表-->ER

https://ithelp.ithome.com.tw/upload/images/20220208/20119035CpTNJigoXO.png

主要選這三個圖-

https://ithelp.ithome.com.tw/upload/images/20220208/20119035SeQdkYMjMD.png

傳統ERD SOP:

  1. 實體--資料表
  2. 關係(聯)-->FK-->PK
  3. 屬性-->資料行(欄位)
    4.資料庫設計步驟

https://ithelp.ithome.com.tw/upload/images/20220208/20119035TYWcuBopv2.png

開始~

  1. 實體--資料表

https://ithelp.ithome.com.tw/upload/images/20220208/20119035QS4DaUaBHu.png

編輯名稱-確定

https://ithelp.ithome.com.tw/upload/images/20220208/20119035RHoZePH84c.png

長這樣
https://ithelp.ithome.com.tw/upload/images/20220208/20119035WoNFO5PNtu.png

  1. 關係(聯)-->FK-->PK

https://ithelp.ithome.com.tw/upload/images/20220208/20119035AP2G8sdMYt.png

修改內容+1+N代表很多(從以前大家都這樣做)-確定-->按CTRL+滾輪就是放大

https://ithelp.ithome.com.tw/upload/images/20220208/20119035F67D2iUFUn.png

準備畫線

https://ithelp.ithome.com.tw/upload/images/20220208/20119035Hi0Y2J95VF.png
把下面3個都變成-直線
https://ithelp.ithome.com.tw/upload/images/20220208/20119035k3pCN4cDYt.png

按住開始畫直線
https://ithelp.ithome.com.tw/upload/images/20220208/20119035uU8yUEzQJh.png
結果長這樣
https://ithelp.ithome.com.tw/upload/images/20220208/20119035KIAq2qihDP.png

  1. 屬性-->資料行(欄位)

https://ithelp.ithome.com.tw/upload/images/20220208/20119035AJmGxQqfvC.png

放好(用ctrl+C-->ctrl+V)

https://ithelp.ithome.com.tw/upload/images/20220208/20119035pqU3GRacck.png
-畫好線

https://ithelp.ithome.com.tw/upload/images/20220208/2011903563IUwKxruZ.png

點開--鍵值選"是"

https://ithelp.ithome.com.tw/upload/images/20220208/20119035HEeAtd7HZT.png

出現底線- 沒有底線=鍵值選"否"

https://ithelp.ithome.com.tw/upload/images/20220208/201190350TrJXIKDgv.png

畫好-存檔--取名稱--存檔

https://ithelp.ithome.com.tw/upload/images/20220208/201190356cls5WdP7f.png


成果-https://ithelp.ithome.com.tw/upload/images/20220208/20119035KrlWQ80XvY.png


換工具來做-下載

https://ithelp.ithome.com.tw/upload/images/20220208/20119035Ve2HZqEIfd.png
從開始來打開
https://ithelp.ithome.com.tw/upload/images/20220208/20119035eNHcjUQz5I.png

按CREAT

https://ithelp.ithome.com.tw/upload/images/20220208/20119035z8eTRGkcxr.png

按X
https://ithelp.ithome.com.tw/upload/images/20220208/20119035wuAQEuQH8V.png

準備開始畫-

現代ERD SOP:

  1. 實體--資料表=table
  2. 屬性-->資料行(欄位)=Column
  3. 關係(聯)-->FK-->PK

  1. 實體--資料表=table

右鍵-table--改第一格變"課程"

https://ithelp.ithome.com.tw/upload/images/20220208/20119035BTKs2GSLnM.png

改第一格變"課程"
https://ithelp.ithome.com.tw/upload/images/20220208/20119035njE79fAqA1.png

長這樣
https://ithelp.ithome.com.tw/upload/images/20220208/20119035h4x945IDM3.png

右上+放大鏡放大

新增欄位-
https://ithelp.ithome.com.tw/upload/images/20220208/20119035bqMWE8vhm4.png
主鍵V
https://ithelp.ithome.com.tw/upload/images/20220208/20119035qL3xqnVhNJ.png

選擇TYPE-數字用INT
--不可以空值
https://ithelp.ithome.com.tw/upload/images/20220208/20119035euicc88Orq.png
長這樣
https://ithelp.ithome.com.tw/upload/images/20220208/20119035Xj00SQPPT1.png

繼續上面步驟--只是是要放中文字--要選VARCHAR--20

https://ithelp.ithome.com.tw/upload/images/20220208/201190356y1Ff54pqb.png

錯了~點2下可以再進去改

下面依序做

https://ithelp.ithome.com.tw/upload/images/20220208/20119035LCU89dd4XV.png

現在長這樣

https://ithelp.ithome.com.tw/upload/images/20220208/20119035RsOt6cSeDh.png

再把另外一個表也蓋好

https://ithelp.ithome.com.tw/upload/images/20220208/20119035CUeCf8HRG4.png

畫關係線:

https://ithelp.ithome.com.tw/upload/images/20220208/20119035MPAMZyzhai.png

按虛線-要先選"課程"--再選"測驗"-->就是"一對多"

https://ithelp.ithome.com.tw/upload/images/20220208/20119035XEq69TSaRW.png

然後存檔

https://ithelp.ithome.com.tw/upload/images/20220208/20119035M6XzukshBA.png

最後長這樣

https://ithelp.ithome.com.tw/upload/images/20220208/20119035auZEzqaYv6.png


產生語法:
按SQL圖案-

https://ithelp.ithome.com.tw/upload/images/20220208/20119035B0eMk7or6k.png

  1. 屬性-->資料行(欄位)=Column
  2. 關係(聯)-->FK-->PK

這篇的下一篇:https://ithelp.ithome.com.tw/articles/10283923


圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言