Posts

Showing posts from December, 2022

資料庫設計 - 有效的使用系統資料 - 第九章 - 補充

Image
單元 1 MySQL 資料庫維護 Workbench 還有一些方便的資料統計資訊可以看,像是哪個 SQL statements 花費最多時間之類的,滿有趣的! Dashboard Network Status 網路狀態、流量 MySQL Status Select 每秒有幾筆,有系統連線就會有資料產生 insert update delete 每秒有幾筆 InnoDB Status 儲存引擎 連接十題資料的 input output 狀態 Performance Reports Memory I/O 資料庫儲存提取實體檔案時的 IO 狀況 High Cost SQL Statements 花費最多時間的前五個 Statements Users & Privileges 這邊可以新增不同 User 並且做權限設定。 例如有不同角色,User 管理 限制帳號 Query 使用額度 資料庫就限制,可能外部連線進來的系統會發生錯誤,超過額度會無法繼續服務前面使用者 Max connections: 每小時最多可連線的 Connection 數量 Concurrent connections: 瞬間可連線數量 Administrative Role DBA 可以擁有所有權限, User Admin 就只能 Create User  單元 2 NoSQL 資料庫 簡介 N ot O nly S QL 非關聯式資料庫 解決讀取海量資料的資料庫技術(FaceBook) Key-Value 資料型態(ex: id = 123) 資料量大時讀取快速 增加/減少資料效率高 擴充方便 用 API 的方式 Query 資料 資料庫軟體 MongoDB Hadoop Redis 雲端服務 Google - Google Cloud BigTable Amazone - DynamoDB Amazone: AWS Service > DataBase >  DynamoDB 單元 4 SQL 語法判斷資料的方式 判斷的語法 深入了解 procedures 的功能 Case 幫助我們對欄位做多種情況的區分 switch if else 用法: select Case sex WHEN 1 THEN "男生" WHEN 0 THEN "女生...

資料庫設計 - 有效的使用系統資料 - 第八章 - 程式語言串接資料庫的設計與實作

Image
Python 串接 MySQL 資料庫 安裝 python 與 mysql 的 connector  執行 pip3 install mysql-connector-python 建立 practice.py 檔練習串接 MySQL 資料庫 # practice.py from mysql import connector connection = connector.connect(host="localhost", database="orders", user="root", password="********") cursor = connection.cursor() cursor.execute("select database();") record = cursor.fetchone() print(record) host:資料庫的 IP 位址 database: database scheme 名稱 user: 當初設定 MySQL 資料庫時的 user 名稱 password: 當初設定 MySQL 資料庫時的 password execute("SQL 語法"): 將要執行的 SQL 語法放入 execute 的參數中 用 fetchone 取回回傳值 當輸入 "select database();" 時,可以看到取得我們匯入到 MySQL 的 orders 資料庫 與在 workbench 執行 select database(); 是一樣的 connection.close():  非常重要,一定要做!!! 資料庫不用,但在開發應用程式時,使用完 connection 要關閉,向外部存取資源時,都會有 建立資源通道 -> 關閉資源通道 的動作,如果沒有關閉, 系統會不斷佔用資源,最後當機,所以常常看到電腦重開機就會恢復正常,多是因為系統累積太多沒有在使用,或是忘記關閉的資源通道造成的,重新開啟釋放資源後,就會恢復正常。 重要觀念 連線可能會發生問題 資料庫使用完畢後要關閉連線 try except finally 用來避免發生錯誤時,系統資源佔用的問題,其結構如下 try # 執行連線,和...

資料庫設計 - 有效的使用系統資料 - 第七章 - 資料庫進階功能

Image
單元 1 建立索引 Indexes 加快搜尋資料的速度, 提升資料庫搜尋資料的效率 建立 indexes 以 Employees Table 為例,一般查找員工會用「名字」去找: Last Name,不太會用 id 當資料量大時,可以在 LastName 建立 index 加速索引。 打開 Employee Table 點選板手 找到 Indexes Tab 將 LastName 加入 index Apply 觀念 用 LastName 搜尋的時候,可以加速 employee 資訊搜尋的效率 單元 2 建立 Triggers 當事件發生時,建立一個觸發程序,能幫你啟動自動化的程式 Triggers 可以根據每一個 Table 去設定 Before/After Insert Before/After Update Before/After Delete 以 employees table 為例,按下板手後,到 triggers tab 在新增一筆 employee 的資料後,我們希望可以把這筆資料加到 employee_logs 裡面,紀錄這個員工是在什麼時候輸入這筆資料的 建立 Triggers 點選 AFTER INSERT 旁邊的 + 會出現 CREATE DEFINER = CURRENT_USER TRIGGER `orders`.`employees_AFTER_INSERT` AFTER INSERT ON `employees` FOR EACH ROW BEGIN END 用 insert into 這個方法,將資料塞入 log 中 CREATE DEFINER = CURRENT_USER TRIGGER `orders`.`employees_AFTER_INSERT` AFTER INSERT ON `employees` FOR EACH ROW BEGIN insert into employees_log(name, date) values(new.lastname, sysdate()); END new.lastname 可以使用 lastname 欄位輸入的值 sysdate() 目前現在時間,日期-時:分:秒 按右下角 Apply 嘗試新增一筆資料到 employee table 查看 employees_log table ...

資料庫設計 - 有效的使用系統資料 - 第六章 - MySQL 函式庫與運算子

Image
單元 1 數學運算子 運算 加 + 減 - 乘 * 除 / 除法取餘數 mod 除法取商 div 練習 算出 products table 中所有產品價格的平均值 select sum(price) / count(productid) average_price from products; 單元 2 比較運算子 比較運算子 > < = != select * from Table1 where column1 > 50; 練習 找出 productid 不等於 1 的所有資料 select * from products where productid != 1; 單元 3 邏輯運算子 比較運算子 and or not select * from TABLE1 where (name = "value1" or name = "value2"); select * from TABLE1 where not name = "value1"; 練習 找出 supplier table 中 supplierid = 1 或 2 的資料 select * from products where supplierid = 1 or SupplierID = 2; 作業 找出 supplier table 中 category = 1 或 2 ,且 price 介於 10 與 20 之間的資料 用 and select * from products where (CategoryID = 1 or categoryid = 2) and (price >= 10 and price <= 20); 用 between select * from products where (price between 10 and 20) and (CategoryID = 1 or categoryid = 2); 單元 4 計算資料的數量 count 計算資料的數量 select count(column1) from TABLE1; distinct 呈現不重複的資料 select distinct column1 from TABLE1; 練習 1. 計算 product Table...

資料庫設計 - 有效的使用系統資料 - 第五章 SQL 語法

Image
單元 1 Select 撈取資料庫資料的關鍵字 select <欄位名稱> from <資料庫.table 名稱> where <條件>; select * from orders where costomerid = 3; 在 workbench 要先指定 orders 資料庫 orders 變成粗體 如果指定到 sys 資料庫會發生 error 補充: 單純寫 select 就是列印 資料量大時避免使用 *,要寫出欄位名稱 搜尋資料時,加入條件增加搜尋效率,條件語法 where select * from orders where customerid = 10; Django 語法 Orders.objects.get(customer_id = 10) limit select * from orders limit 10; Django 語法 returns the first 5 objects ( LIMIT 10 ): ** >>>** Orders**.**objects**.**all()[:10] This returns the sixth through tenth objects ( OFFSET 5 LIMIT 10 ): ** >>>** Orders**.**objects**.**all()[5:15] Generally, slicing a  [QuerySet] returns a new  [QuerySet] – it doesn’t evaluate the query. An exception is if you use the “step” parameter of Python slice syntax. For example, this would actually execute the query in order to return a list of every  second  object of the first 10: ** >>>** Order...