MSSQL-使用教學 === ###### tags: `MSSQL` part 1 ## 環境設定 1. 下載MSSQL管理工具 2. 安裝完成後,利用安裝工具下載安裝SQL Server Data Tools 3. 完成後重新開機即可 part 2 # 基礎語法 ## 查詢 ``` SQL= select * from employees -- 搜尋所有欄位在此張資料表的資料 select id,name from employees -- 搜尋所有資料的id和name select * from employees where name = "張XX" -- 搜尋 name為張XX的所有資料 select * from employees where name like "%張%" -- 搜尋 name中含有張的所有資料 select distinct * from employees -- 搜尋單筆資料間各欄位值皆不同的結果,重複資料只會顯示一次 select top 5 * from employees -- 搜尋前五筆資料 select name,count(1) as counts from employees group by name -- 搜尋所有出現過的名字,相同名字只會出現一次並在counts統計出現的次數 select * from employees order by id desc/asc -- 將搜尋結果按照id並已降序或升序顯示 select * from employees where id >= 1 and id <= 100 -- 搜尋id大於等於1且小於等於100的所有資料 select * from employees where id = 1 or id = 100 -- 搜尋id等於1或等於100的所有資料 select * from employees where id between 1 and 100 -- 搜尋id在1到100間的所有資料 select * from employees where ( 1 or 1) and 1 -- 1 代表 1個式子成立 此句翻譯的意思是 只要式子1或式子2 -- 任一個成立且式子3也成立就呈現在搜尋結果中 select * from employees where id in (1,2,3) --搜尋id在1,2,3當中的資料 select * from employees where id not in (1,2,3) --搜尋id不在1,2,3當中的資料 select * from employees where exists ( select id from employees where id = 1 or id = 2) --搜尋id存在於1或2的資料 select * from employees where not exists (select id from employees where id = 1 or id = 2) --搜尋id不存在於1或2的資料 ``` ## 新增 ``` SQL= insert into employees(id) values (newID()) insert employees (id) values (newID()) --兩種新增寫法結果相同 insert into employees values (newID(),'張xx','M') --為資料表所有欄位設定資料,須符合資料庫欄位設定否則新增會失敗 ``` ## 刪除 ``` SQL= delete from employees where id = 'xxxxxx' -- 刪除特定資料拿掉條件等同刪除所有資料 drop table employees -- 刪除整張資料表 truncate table employees -- 清除所有資料保留資料表 ``` ## 修改 ``` SQL= update employees set name='黃XXX', sex = 'F' where name = 'xxx' -- 更新單筆資料 ``` # 進階用法 ## 合併 ``` SQL= join (inner join) INNER JOIN "ON",通過連接兩個表,你可以從這兩個表中檢索出相關聯的數據。 也可以是自己。 left join 以左邊資料表為主 right join 以右邊資料表為主 full join 不論是否左右有資料都會顯示 select * from employees a join on employees_job b on a.id = b.employee_id ``` ## 子查詢 ``` SQL= select *,(select JobName from employee_job b where b.employee_id = a.id) from employees a select * from (select id,name,sex from employees) a --補充 可以將資料表或資料欄位用其他名稱取代方便更容易了解欄位或是加速撰寫join關聯的速度 --子查詢的使用會使效能變差,除非不得已盡量避免使用 ``` ## 集合 ``` SQL = UNION --聯集 顯示兩個資料表的所有資料重複的資料只顯示一次 UNION ALL --顯示兩個資料表的所有資料包含所有重複的資料,效能會比UNION好 INTERSECT --交集 同時存在兩張表的資料 EXCEPT --差集 存在於前表不存在於後表的資料 EX: select * from employee_a UNION select * from employee_b ``` part 3 ## 補充(語法能解決所有問題) ``` SQL = --取部分欄位內容 --轉換取出資料的資料型別 CharIndex ``` ## 暫存表 ``` SQL = CREATE TABLE #Employees ( id uniqueidentifier not null name varchar(20) not null ) --使用後記得刪除 Drop TABLE #Employees --一共有三種暫存表 #Table 只有創建者才可使用 ##Table 任何資料庫或使用者皆可使用 @Table 指令執行完就會自動將暫存表刪除 ``` ## 檢視表 ``` SQL = CREATE VIEW [view_name] as select a.id,b.name from employees a left join employee_job b on a.id = b.employee_id ``` ## 預存函數 ``` SQL = CREATE PROCEDURE employeeLog @creator varchar(50), @create_time DATETIME AS insert into employees_log (id,name,creator,create_time) values('00000000-0000-0000-0000-000000000000','XXX',@creator,@create_time) GO EXEC employeeLog @creator = N'me' , @create_time = GETDATE(); ``` ## 觸發程序 ``` SQL = --待重寫 CREATE TRIGGER TriggerName AFTER INSERT AS BEGIN SET NOCOUNT ON; --執行結束不回傳影響結果,減少網路傳輸量 INSERT INTO employees( id, name, sex ) SELECT i.id, i.name, i.sex, 'INS' FROM inserted i END ``` ## 索引 ``` SQL = CLUSTERED | NONCLUSTERED ``` ## 資料指標 ``` SQL = ``` ## 優化參考資料連結 1. [Mysql Join語法解析與效能分析](https://codertw.com/%E7%A8%8B%E5%BC%8F%E8%AA%9E%E8%A8%80/604531/) ## 參考資料 1. [暫存表](https://ithelp.ithome.com.tw/articles/10225120) 2. [Trigger 撰寫時要注意的小細節](https://dotblogs.com.tw/jamesfu/2014/06/20/triggersample) 4.