根據應用場景,不定時更新,歡迎大家把自己遇到的問題留言,稍后完善,
1 mySql三范式
1.1 第一范式(1NF)
(必須有主鍵,列不可分)資料庫表中的任何欄位都是單一屬性的,不可再分
1.2 第二范式(2NF)
(當一個表是復合主鍵時,非主鍵的欄位不依賴于部分主鍵(即必須依賴于全部的主鍵欄位))
資料庫表中非關鍵欄位對任一候選關鍵欄位的 都 不存在部分函式依賴
1.3 第三范式(3NF)
關系模式R(U,F)中的所有非主屬性對任何候選關鍵字都不存在傳遞依賴
2 各種場景
2.1 批量更新表資料
適用場景:將表table_a資料中欄位a,批量更新到表table_b中,適用于表遷移,
關鍵字:inner join
sql陳述句:update table_a a inner join table_b b on b.org_id=a.department_id set a.department_short_name = b.short_name
解釋:table_a的department_id和table_b的org_id中值一樣,需要將table_b 中idorg_id一樣的short_name批量更新到table_a中,
2.2 條件陳述句,給欄位重新賦值
適用場景:每個列舉,回傳對應的字串,
關鍵字:case when x=y then e when x=z then f end
sql陳述句:(case when a.status=1 then '申請中' when a.status=2 then '受理中' when a.status=3 then '已轉賬' when a.status=4 then '拒絕' end) as status
解釋:當status為1時狀態為申請中,當status為2時狀態為受理中,當status為3時狀態為已轉賬,當status為4時狀態為拒絕,
2.3 條件陳述句,給欄位拼接字符
適用場景:給欄位拼接字符,
關鍵字:concat
sql陳述句:(case when a.type=0 then concat('-',a.score) else concat('+',a.score) end) as showscore
解釋:當type為0時score增加“-”前綴,當type為其他時score增加“+”前綴,
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/56232.html
標籤:MySQL
