2021最熱門的20個數據庫學習總結,你會用哪幾個?

{"type":"doc","content":[{"type":"heading","attrs":{"align":null,"level":1},"content":[{"type":"text","text":"前言","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"數據庫有多重要就不用我說了吧,懂的都懂!","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"今天就來看看2021最熱門的數據庫有哪些,該怎麼學怎麼用,文章開始之前先來看看這張圖","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/b7/b71f740115f1aab110a7f268c02609a4.jpeg","alt":null,"title":null,"style":[{"key":"width","value":"75%"},{"key":"bordertype","value":"none"}],"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"從圖中數據可以看到,穩居前三的 Oracle、MySQL 和 Microsoft SQL Server 分數出現了較大幅度的下跌,分別減少 46.82、34.14 和 7.33 分。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"其中 SQL Server 分數已經連續下跌了兩個月。若與去年同期的數據相比,三者下跌的分數平均已達到 64 分。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"後起之秀 PostgreSQL 和 MongoDB 依舊保持着穩步上升的趨勢,分數與上個月相比有小幅度增加,與去年同期相比也平均增加了 40 分左右。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"這個系列主要講一講常年混跡前列的三大數據庫——即Oracle、MySQL 和 Microsoft SQL Server該怎麼學習,學習資料也給大夥整理好了,需要的可以直接點擊領取。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"bulletedlist","content":[{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"link","attrs":{"href":"https://jq.qq.com/?_wv=1027&k=PjEVS3qt","title":"","type":null},"content":[{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"MySQL、Oracle and Microsoft SQL Server學習筆記","attrs":{}}]}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"link","attrs":{"href":"https://jq.qq.com/?_wv=1027&k=PjEVS3qt","title":"","type":null},"content":[{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"數據庫學習相關書籍","attrs":{}}]}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"link","attrs":{"href":"https://jq.qq.com/?_wv=1027&k=PjEVS3qt","title":"","type":null},"content":[{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"2021金三銀四數據庫面試真題總結","attrs":{}}]}]}]}],"attrs":{}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"篇幅所限,本文就先講一下MySQL,有時間再發文講講其他的數據庫,大夥感興趣的話可以關注一波我","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"好了,話不多說,坐穩扶好,發車嘍!","attrs":{}}]},{"type":"heading","attrs":{"align":null,"level":1},"content":[{"type":"text","text":"MySQL","attrs":{}}]},{"type":"heading","attrs":{"align":null,"level":2},"content":[{"type":"text","text":"概述","attrs":{}}]},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"爲什麼要學數據庫","attrs":{}}]},{"type":"bulletedlist","content":[{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"大數據時代,所有最後落地最普遍的便是數據庫","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"數據庫是所有軟件體系中最核心的存在","attrs":{}}]}]}],"attrs":{}},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"什麼是數據庫","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"數據庫——DataBase——DB,數據倉庫,用於存儲和管理數據。","attrs":{}}]},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"數據庫分類","attrs":{}}]},{"type":"heading","attrs":{"align":null,"level":4},"content":[{"type":"text","text":"關係型數據庫——SQL","attrs":{}}]},{"type":"bulletedlist","content":[{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"MySQL,Oracle,Sql Server,DB2,SQLlite","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"通過表和表、行與列之間的關係進行數據存儲","attrs":{}}]}]}],"attrs":{}},{"type":"heading","attrs":{"align":null,"level":4},"content":[{"type":"text","text":"非關係型數據庫——NoSQL","attrs":{}}]},{"type":"bulletedlist","content":[{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"Redis,MongoDB","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"存儲的是鍵值對。","attrs":{}}]}]}],"attrs":{}},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"DBMS——數據庫管理系統——Database Management System","attrs":{}}]},{"type":"bulletedlist","content":[{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"數據庫管理軟件","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"MySQL是數據庫管理系統","attrs":{}}]}]}],"attrs":{}},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"MySQL簡介","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"穩定版本:5.7 ,8.0","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"體積小、速度快,成本底、招人成本底","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"安裝建議:儘量不使用exe安裝,因爲刪除麻煩,且會進註冊表,盡","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"可能使用壓縮包安裝","attrs":{}},{"type":"text","text":"。","attrs":{}}]},{"type":"heading","attrs":{"align":null,"level":4},"content":[{"type":"text","text":"壓縮包安裝步驟","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"link","attrs":{"href":"https://www.cnblogs.com/xuqp/p/9172254.html","title":"","type":null},"content":[{"type":"text","text":"MySQL 8.0 壓縮包版安裝方法","attrs":{}}]}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"numberedlist","attrs":{"start":1,"normalizeStart":1},"content":[{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":1,"align":null,"origin":null},"content":[{"type":"text","text":"解壓","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":2,"align":null,"origin":null},"content":[{"type":"text","text":"在環境變量path上配置解壓後bin所在的目錄地址","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":3,"align":null,"origin":null},"content":[{"type":"text","text":"在mysql解壓後目錄下新建my.ini文件——配置文件","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":4,"align":null,"origin":null},"content":[{"type":"text","text":"管理員模式運行cmd,用於註冊註冊表","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":5,"align":null,"origin":null},"content":[{"type":"text","text":"初始化MySQL數據庫 ","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"mysqld --initialize-insecure","attrs":{}}],"attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":6,"align":null,"origin":null},"content":[{"type":"text","text":"安裝MySQL服務 ","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"mysqld install","attrs":{}}],"attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":7,"align":null,"origin":null},"content":[{"type":"text","text":"啓動MySQL服務 ","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"net start mysql","attrs":{}}],"attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":8,"align":null,"origin":null},"content":[{"type":"text","text":"跳過登錄密碼驗證註釋掉 ","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"#skip-grant-tables","attrs":{}}],"attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":9,"align":null,"origin":null},"content":[{"type":"text","text":"停止MySQL服務 ","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"net stop mysql","attrs":{}}],"attrs":{}}]}]}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"配置文件如下:","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"codeblock","attrs":{"lang":"text"},"content":[{"type":"text","text":"[mysql]\n# 設置mysql客戶端默認字符集\ndefault-character-set=utf8\n[mysqld]\n# 設置3306端口\nport = 3306\n# 設置mysql的安裝目錄\nbasedir=D:\\Program Files (x86)\\mysql\\mysql-8.0.19-winx64 \n# 設置mysql數據庫的數據的存放目錄,數據庫會自動生成,不需要我們創建文件夾\ndatadir= D:\\Program Files (x86)\\mysql\\mysql-8.0.19-winx64\\data\n# 允許最大連接數\nmax_connections=20\n# 服務端使用的字符集默認爲8比特編碼的latin1字符集\ncharacter-set-server=utf8\n# 創建新表時將使用的默認存儲引擎\ndefault-storage-engine=INNODB\n# 跳過登錄密碼驗證 在修改完密碼後要把這句註釋掉\n#skip-grant-tables\nskip-grant-tables\n","attrs":{}}]},{"type":"heading","attrs":{"align":null,"level":4},"content":[{"type":"text","text":"修改root密碼","attrs":{}}]},{"type":"heading","attrs":{"align":null,"level":5},"content":[{"type":"text","text":"mysql 5.7","attrs":{}}]},{"type":"codeblock","attrs":{"lang":"text"},"content":[{"type":"text","text":"update mysql.user set password=PASSWORD('123456')where User='root';\nflush privileges;\n","attrs":{}}]},{"type":"heading","attrs":{"align":null,"level":5},"content":[{"type":"text","text":"mysql 8.0","attrs":{}}]},{"type":"codeblock","attrs":{"lang":"text"},"content":[{"type":"text","text":"alter user 'root'@'localhost' identified by '123456'; \n","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"codeblock","attrs":{"lang":"text"},"content":[{"type":"text","text":"#注意-p後不能加空格直接輸入密碼\nmysql -u root -p123456\nmysql -u root -p\n#再輸入密碼\n","attrs":{}}]},{"type":"heading","attrs":{"align":null,"level":4},"content":[{"type":"text","text":"SQLyog","attrs":{}}]},{"type":"heading","attrs":{"align":null,"level":5},"content":[{"type":"text","text":"SQLyog驗證碼","attrs":{}}]},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/3d/3d0397904c41cc65bb958e7736a83488.png","alt":null,"title":null,"style":[{"key":"width","value":"75%"},{"key":"bordertype","value":"none"}],"href":null,"fromPaste":true,"pastePass":true}},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"SQLyog/Navicat","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"可以查看歷史執行記錄,包括創建表,數據庫等記錄,比navicat詳細。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"blockquote","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"sc delete mysql 清空服務,即刪除mysql","attrs":{}}]}],"attrs":{}},{"type":"heading","attrs":{"align":null,"level":2},"content":[{"type":"text","text":"Mysql定義結構","attrs":{}}]},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"創建數據庫","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"默認創建數據庫字符集編碼和排序規則如下","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/dd/dd9901f440a71e4ec88c5ce146efea3f.png","alt":null,"title":null,"style":[{"key":"width","value":"75%"},{"key":"bordertype","value":"none"}],"href":null,"fromPaste":true,"pastePass":true}},{"type":"heading","attrs":{"align":null,"level":4},"content":[{"type":"text","text":"創建表","attrs":{}}]},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/48/48c865920452986e360ddc7f596abf6c.png","alt":null,"title":null,"style":[{"key":"width","value":"75%"},{"key":"bordertype","value":"none"}],"href":null,"fromPaste":true,"pastePass":true}},{"type":"heading","attrs":{"align":null,"level":4},"content":[{"type":"text","text":"連接數據庫","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"當執行命令行連接數據庫報錯","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"'mysql' 不是內部或外部命令,也不是可運行的程序","attrs":{}}],"attrs":{}},{"type":"text","text":"時,是因爲我們沒有配置好mysql的環境變量,這時候就需要我們往path上添加mysql的安裝地址到bin文件夾路徑到path上。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"codeblock","attrs":{"lang":"text"},"content":[{"type":"text","text":"-- 在客戶端查看mysql安裝目錄的語句\nshow variables like \"%char%\";\n","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/89/89ba27507d3a4953b2d05fbe36a6df4f.png","alt":null,"title":null,"style":[{"key":"width","value":"75%"},{"key":"bordertype","value":"none"}],"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"codeblock","attrs":{"lang":"text"},"content":[{"type":"text","text":"#有以下兩種命令行連接數據庫的方法,第一種需要我們回車後輸入密碼\nmysql -u root -p\nmysql -u root -p123456\n","attrs":{}}]},{"type":"heading","attrs":{"align":null,"level":5},"content":[{"type":"text","text":"命令行執行sql語句注意事項","attrs":{}}]},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/27/27d210ab7472562628ff930ee40d42d4.png","alt":null,"title":null,"style":[{"key":"width","value":"75%"},{"key":"bordertype","value":"none"}],"href":null,"fromPaste":true,"pastePass":true}},{"type":"heading","attrs":{"align":null,"level":4},"content":[{"type":"text","text":"基本數據庫連接操作","attrs":{}}]},{"type":"codeblock","attrs":{"lang":"text"},"content":[{"type":"text","text":"-- 5.7.29-log 查看mysql版本\nSELECT VERSION();\n\n-- 創建數據庫\nCREATE DATABASE `my_test` CHARACTER SET utf8 COLLATE utf8_general_ci;\n-- 第二種\nCREATE DATABASE `my_test` CHARSET= utf8 COLLATE utf8_general_ci;\n\n-- 展示所有的數據庫\nSHOW DATABASES;\n\n-- 切換數據庫\nUSE school;\n\n-- 創建表sql 我們一般通過navicat等定義創建表,上生產時再執行創建表的sql語句\nCREATE TABLE `t_student` (\n `id` bigint(10) NOT NULL AUTO_INCREMENT COMMENT 'id',\n `name` varchar(30) NOT NULL COMMENT '學生姓名',\n `age` int(3) NOT NULL COMMENT '學生年齡',\n `create_user` varchar(30) DEFAULT NULL COMMENT '創建人',\n `crreate_tim` datetime DEFAULT NULL COMMENT '創建時間',\n `update_user` varchar(30) DEFAULT NULL COMMENT '修改人',\n `update_time` datetime DEFAULT NULL COMMENT '修改時間',\n `delete_flag` tinyint(1) DEFAULT NULL COMMENT '是否刪除 0-否 1-是',\n PRIMARY KEY (`id`)\n) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='學生表';\n\n-- 修改用戶密碼\nUSE mysql;\nUPDATE user SET password=PASSWORD('123456') WHERE user='root';\n-- 刷新權限\nFLUSH PRIVILEGES;\n\nshow variables like \"%char%\";\n\n-- 查看數據庫所有的表\nSHOW TABLES;\n\n-- 查看數據庫中指定表的表結構信息\nDESCRIBE t_student;\n\n-- 退出數據庫連接\nexit;\n\n-- 單行註釋\n\n/*\n多行\n註釋\n*/\n","attrs":{}}]},{"type":"heading","attrs":{"align":null,"level":4},"content":[{"type":"text","text":"utf8和utf8mb4的區別","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"utf8mb4的編碼,mb4就是most bytes 4的意思,專門用來兼容四字節的unicode。 爲了節省空間,一般情況下使用utf8也就夠了。 但是爲了獲取更好的兼容性,應該總是使用 utf8mb4 而非 utf8. 對於 CHAR 類型數據,utf8mb4 會多消耗一些空間,根據 Mysql 官方建議,使用 VARCHAR 替代 CHAR。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"link","attrs":{"href":"https://zhidao.baidu.com/question/1949879706369876188.html","title":"","type":null},"content":[{"type":"text","text":"參考","attrs":{}}]}]},{"type":"heading","attrs":{"align":null,"level":4},"content":[{"type":"text","text":"Mysql中的排序規則utf8_unicode_ci、utf8_general_ci的區別總結","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"首先,ci是 case insensitive, 即 \"大小寫不敏感\", a 和 A 會在字符判斷中會被當做一樣的; bin 是二進制, a 和 A 會別區別對待。所以我們在用sql進行匹配查找的時候,","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"如果是ci,則無論大小寫都可以匹配出來,而如果是utf8_bin中的bin是二進制,則會區分大小寫,這時候就只能唯一匹配到精確的那個值。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"link","attrs":{"href":"https://blog.csdn.net/m0_37754003/article/details/82770252","title":"","type":null},"content":[{"type":"text","text":"參考","attrs":{}}]}]},{"type":"heading","attrs":{"align":null,"level":4},"content":[{"type":"text","text":"SQL語言分類","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"link","attrs":{"href":"https://www.cnblogs.com/adamgq/p/12532567.html","title":"","type":null},"content":[{"type":"text","text":"參考","attrs":{}}]}]},{"type":"heading","attrs":{"align":null,"level":5},"content":[{"type":"text","text":"DDL—— ","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"數據定義語言DDL,(data definition language)","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"用來創建數據庫中的各種對象-----表、視圖,DDL操作是隱性提交的!不能rollback 。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"codeblock","attrs":{"lang":"text"},"content":[{"type":"text","text":"CREATE,ALTER,DROP,TRUNCATE,COMMENT ,RENAME\n","attrs":{}}]},{"type":"heading","attrs":{"align":null,"level":5},"content":[{"type":"text","text":"DML—— ","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"數據操縱語言(DML:Data Manipulation Language)","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"數據的增刪改。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"codeblock","attrs":{"lang":"text"},"content":[{"type":"text","text":"INSERT,UPDATE,DELETE,EXPLAIN, PLAN\n","attrs":{}}]},{"type":"heading","attrs":{"align":null,"level":5},"content":[{"type":"text","text":"DQL——數據查詢語言(DQL: Data Query Language)","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"數據的查詢","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"codeblock","attrs":{"lang":"text"},"content":[{"type":"text","text":"SELECT\n","attrs":{}}]},{"type":"heading","attrs":{"align":null,"level":5},"content":[{"type":"text","text":"DCL—— ","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"數據庫控制語言(Database Control Language)","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"用來設置或更改數據庫用戶或角色權限的語句,包括(grant,deny,revoke等)語句 。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"codeblock","attrs":{"lang":"text"},"content":[{"type":"text","text":"GRANT,REVOKE\n","attrs":{}}]},{"type":"heading","attrs":{"align":null,"level":5},"content":[{"type":"text","text":"TCL——","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"存儲過程控制預言(Transaction Control Language)","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"數據庫事務的控制語言。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"codeblock","attrs":{"lang":"text"},"content":[{"type":"text","text":"COMMIT,SAVEPOINT,ROLLBACK,SET TRANSACTION\n","attrs":{}}]},{"type":"heading","attrs":{"align":null,"level":2},"content":[{"type":"text","text":"操作數據庫","attrs":{}}]},{"type":"blockquote","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"操作數據庫> 操作數據庫中的表> 操作數據庫中的表的數據","attrs":{}}]}],"attrs":{}},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"數據庫操作示例","attrs":{}}]},{"type":"codeblock","attrs":{"lang":"text"},"content":[{"type":"text","text":"-- 創建數據庫 IF NOT EXISTS 表示不存在則創建,存在則跳過\nCREATE DATABASE IF NOT EXISTS `my_test` CHARACTER SET utf8 COLLATE utf8_general_ci;\n\n-- 刪除數據庫\nDROP DATABASE IF EXISTS `my_test`;\n\n-- 使用數據庫\nUSE school;\n\n-- 查看數據庫\nSHOW DATABASES;\n","attrs":{}}]},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"數據庫中表的列類型","attrs":{}}]},{"type":"blockquote","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"數值類型","attrs":{}}]}],"attrs":{}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"embedcomp","attrs":{"type":"table","data":{"content":"
數據庫數值類型描述大小(字節)對應Java中的類型
tinyint特別小的數據1Bytebyte
smallint較小的整數2Byteshort
mediumint中等大小的整數3Byte
int標準的整數4Byteint
bigint較大的整數8Bytelong
float浮點數(單精度)4Bytefloat
double浮點數(雙精度)8Bytedouble
decimal字符串形式的浮點數BigDecimal
"}}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"注:在金融等金額計算時,一般使用decimal來表示金額大小,因爲使用double會有精度問題","attrs":{}},{"type":"text","text":",而使用字符串形式的浮點數則可以完整得表示出金額的大小。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"blockquote","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"字符串","attrs":{}}]}],"attrs":{}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"embedcomp","attrs":{"type":"table","data":{"content":"
數據庫數值類型描述大小(字節)對應Java中的類型
char固定大小的字符串0-255String
varchar可變字符串0-65535String
tinytext微型文本2^8 -1 =255String
text文本串2^16 -1 =65535String
"}}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"blockquote","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"日期時間","attrs":{}}]}],"attrs":{}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"embedcomp","attrs":{"type":"table","data":{"content":"
數據庫數值類型描述對應Java中的類型
date日期格式Date,LocalDate
time時間格式LocalTime
datetime日期時間格式Date,LocalDateTime
timestamp時間戳,1970.1.1到現在的毫秒數Timestamp
year年份Year
"}}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"注:","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"yyyy-MM-dd HH:mm:ss","attrs":{}}],"attrs":{}},{"type":"text","text":" 表示年月日時分秒。月:","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"用MM大寫表示是與時間單位的mm作爲區別,而時:HH用大寫表示24小時制,用小寫hh則是12小時制。","attrs":{}}]},{"type":"heading","attrs":{"align":null,"level":4},"content":[{"type":"text","text":"VARCHAR 和 TEXT 長度問題","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"VARCHAR :varchar在mysql中滿足最大行限制,也就是 ","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"65535(16k)字節","attrs":{}},{"type":"text","text":",在mysql中使用 uft-8(mysql中的 utf-8 和我們正 常的編碼utf-8不同)字符集一個字符佔用三個字節。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"①使用 utf-8 字符編碼集  varchar 最大長度是   (65535-1)/3=21844  個字符(由於會有1字節的額外佔用空間開銷, 所以減1)。②","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"使用 utf-8mb4 字符集","attrs":{}},{"type":"text","text":"(mysql中 utf-8mb4 字符集也就是我們通常使用的 utf-8 字符集),mysql中使用 ","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"utf8mb4 字符集一個字符佔用4個字節,所以 varchar 最大長度是(65535-1)/4=16383","attrs":{}},{"type":"text","text":" 個字符(由於1字節額外佔用空間開銷,所以減1)。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"TEXT :","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"最大限制是64k","attrs":{}},{"type":"text","text":", 採用 utf-8 字符集,(262144-1)/3=87381 個字符。採用 utf-8mb4字符集,(262144-1)/4=65535 個字符。","attrs":{}},{"type":"link","attrs":{"href":"https://blog.csdn.net/Mark_Chao/article/details/78992049","title":"","type":null},"content":[{"type":"text","text":"參考","attrs":{}}]}]},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"數據庫中表的字段屬性——重要","attrs":{}}]},{"type":"blockquote","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"無符號——Unsigned","attrs":{}}]}],"attrs":{}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"bulletedlist","content":[{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"無符號整數","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"聲明瞭該列不能聲明爲負數,比如主鍵id這個列","attrs":{}}]}]}],"attrs":{}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"blockquote","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"0填充——zerofill","attrs":{}}]}],"attrs":{}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"bulletedlist","content":[{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"0填充","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"列聲明的位數,沉澱的值如果不足聲明的位數則用0來填充,比如:int(3),沉澱3則在數據庫裏爲003","attrs":{}}]}]}],"attrs":{}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"blockquote","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"自增——AUTO_INCREMENT","attrs":{}}]}],"attrs":{}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"bulletedlist","content":[{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"即數據庫幫我們做自增,自動在上一條記錄的基礎上+1(默認)","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"通常用來設置唯一的主鍵,必須爲整數類型,效率高","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"可以自定義設置主鍵自增的起始值和步長,比如AUTO_INCREMENT=11表示從11開始自增。","attrs":{}}]}]}],"attrs":{}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"blockquote","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"非空 NULL NOT NULL","attrs":{}}]}],"attrs":{}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"bulletedlist","content":[{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"設置爲 NOT NULL表示非空,如果不設置值則會報錯","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"設置爲NULL表示可以爲空,不填寫值則默認爲NULL","attrs":{}}]}]}],"attrs":{}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"blockquote","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"默認——DEFAULT","attrs":{}}]}],"attrs":{}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"bulletedlist","content":[{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"設置默認值","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"當不指定該列的值時,則會有默認值填充","attrs":{}}]}]}],"attrs":{}},{"type":"heading","attrs":{"align":null,"level":4},"content":[{"type":"text","text":"表設計時必須存在的字段","attrs":{}}]},{"type":"codeblock","attrs":{"lang":"text"},"content":[{"type":"text","text":"id 主鍵\nversion 版本號,用於樂觀鎖\ndelete_flag 刪除狀態,用於僞刪除\ngmt_create 創建時間\ngmt_update 修改時間\n","attrs":{}}]},{"type":"heading","attrs":{"align":null,"level":4},"content":[{"type":"text","text":"MySQL中數據類型的長度問題總結","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"Q:int(5)以及varchar(20)長度表示的是什麼?","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"A:int數據類型是固定的4個字節;","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"但是int(5)和int(11)區別在於,顯示的數據位數一個是5位一個是11位,","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"在開啓zerofill(填充零)情況下,若int(5)存儲的數字長度是小於5的則會在不足位數的前面補充0","attrs":{}},{"type":"text","text":",但是如果int(5)中存儲的數字長度大於5位的話,則按照實際存儲的顯示(數據大小在int類型的4個字節範圍內即可),也就是說int(M)的M不代表數據的長度;varchar(20)中的20表示的是varchar數據的數據長度最大是20,超過則數據庫不會存儲,直接報錯;","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"總結:","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"int(M) M表示的不是數據的最大長度,只是數據寬度,跟0填充有關","attrs":{}},{"type":"text","text":",並不影響存儲多少位長度的數據;","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"varchar(M) M表示的是varchar類型數據在數據庫中存儲的最大長度","attrs":{}},{"type":"text","text":",超過則不存;","attrs":{}},{"type":"link","attrs":{"href":"https://blog.csdn.net/YAruli/article/details/79187814","title":"","type":null},"content":[{"type":"text","text":"參考","attrs":{}}]}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"DECIMAL(M,D)","attrs":{}},{"type":"text","text":" 中,M就是總長度,D就是小數點後面的長度,","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"超出範圍或者長度不夠會被截斷或補位","attrs":{}},{"type":"text","text":" 。比如: DECIMAL(5,4)=>總長度不超過5位數字,並且小數點後頭必須要4位數字:1.2345 DECIMAL(14,9)=>總長度5位數字,整數5位,小數點後9位:12345.123456789","attrs":{}}]},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"創建數據庫表——重要","attrs":{}}]},{"type":"heading","attrs":{"align":null,"level":4},"content":[{"type":"text","text":"創建表示例","attrs":{}}]},{"type":"codeblock","attrs":{"lang":"text"},"content":[{"type":"text","text":"-- 注意點\n-- 表內容在英文()裏,表名、字段名和索引名儘量用``括起來\n-- AUTO_INCREMENT 自增\n-- 字符串使用時用 ''括起來\n-- 所有語句後面都要加英文逗號 ,最後一句不用加\n-- PRIMARY KEY 主鍵,一張表一般一個唯一主鍵\nCREATE TABLE IF NOT EXISTS `t_student` (\n `id` INT(10) UNSIGNED NOT NULL AUTO_INCREMENT COMMENT 'id',\n `name` VARCHAR(20) NOT NULL DEFAULT '佚名' COMMENT '姓名',\n `age` INT(3) DEFAULT NULL COMMENT '年齡',\n `gender` TINYINT(2) DEFAULT NULL COMMENT '性別',\n `birthday` DATETIME DEFAULT NULL COMMENT '出生日期', \n PRIMARY KEY(`id`),\n KEY `idx_name`(`name`)\n) ENGINE= INNODB AUTO_INCREMENT=1000 DEFAULT CHARACTER SET utf8mb4 COLLATE = utf8mb4_general_ci COMMENT '學生表';\n\nCREATE TABLE IF NOT EXISTS `t_student` (\n `id` INT(10) UNSIGNED NOT NULL AUTO_INCREMENT COMMENT 'id',\n `name` VARCHAR(20) NOT NULL DEFAULT '佚名' COMMENT '姓名',\n `age` INT(3) DEFAULT NULL COMMENT '年齡',\n `gender` TINYINT(2) DEFAULT NULL COMMENT '性別',\n `birthday` DATETIME DEFAULT NULL COMMENT '出生日期', \n PRIMARY KEY(`id`),\n KEY `idx_name`(`name`)\n) ENGINE= INNODB AUTO_INCREMENT=1000 DEFAULT charset=utf8mb4 COLLATE=utf8mb4_general_ci COMMENT='學生表';\n","attrs":{}}]},{"type":"heading","attrs":{"align":null,"level":4},"content":[{"type":"text","text":"格式","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"注:","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"[表類型][字符集設置] [表名註釋]","attrs":{}}],"attrs":{}},{"type":"text","text":"通常不寫默認是數據庫的默認配置","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"codeblock","attrs":{"lang":"text"},"content":[{"type":"text","text":"CREATE TABLE IF NOT EXISTS `表名` (\n`字段名` 列類型 [屬性] [註釋],\n `字段名` 列類型 [屬性] [註釋]\n) [表類型] [字符集設置] [表名註釋]\n","attrs":{}}]},{"type":"heading","attrs":{"align":null,"level":4},"content":[{"type":"text","text":"查看建表","attrs":{}}]},{"type":"codeblock","attrs":{"lang":"text"},"content":[{"type":"text","text":"-- 查看t_student表的創建語句\nSHOW CREATE TABLE t_student;\n\n-- 查看創建數據庫的語句\nSHOW CREATE DATABASE school;\n\n-- 顯示錶結構\nDESC t_student;\n","attrs":{}}]},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"數據庫數據表的存儲引擎","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"存儲引擎是針對表的,每張表都可以設置自己的存儲引擎。我們對數據庫會設置默認的存儲引擎,當我們建表時沒有明確指定表的存儲引擎時默認就是數據庫設置的存儲引擎。比較常見的存儲引擎有:","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"bulletedlist","content":[{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"InnoDB——一般默認選擇使用","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"MyISAM","attrs":{}}]}]}],"attrs":{}},{"type":"heading","attrs":{"align":null,"level":4},"content":[{"type":"text","text":"InnoDB與MyISAM的區別","attrs":{}}]},{"type":"embedcomp","attrs":{"type":"table","data":{"content":"
InnoDBMyISAM
事務支持支持不支持
數據行鎖定(行鎖)支持不支持(表鎖)
外鍵約束支持(一般不喜歡在數據庫使用外鍵)不支持
全文索引不支持(5.7版本後支持)支持
表空間大小較大,約是MyISAM表空間的2倍較小
"}}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"MyISAM——節約空間,速度較快","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"InnoDB——安全性高,支持事務和多表多用戶操作","attrs":{}}]},{"type":"heading","attrs":{"align":null,"level":4},"content":[{"type":"text","text":"在物理空間的位置","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"Mysql數據庫的文件存儲在裝目錄下的data文件夾下,","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"一個數據庫對應一個文件夾","attrs":{}},{"type":"text","text":",如數據庫school對應data目錄下的school文件夾,因此","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"數據庫本質還是文件的存儲","attrs":{}},{"type":"text","text":"。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"codeblock","attrs":{"lang":"text"},"content":[{"type":"text","text":"#mysql數據存儲位置,安裝目錄下的data文件夾下\nC:\\ProgramData\\MySQL\\MySQL Server 5.7\\Data\n","attrs":{}}]},{"type":"heading","attrs":{"align":null,"level":4},"content":[{"type":"text","text":"Mysql存儲引擎在物理文件下的區別","attrs":{}}]},{"type":"heading","attrs":{"align":null,"level":5},"content":[{"type":"text","text":"InnoDB","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"一張表對應兩個文件分別是:","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"bulletedlist","content":[{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"*.frm——表結構定義文件","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"*.ibd——表數據和索引的存儲文件","attrs":{}}]}]}],"attrs":{}},{"type":"heading","attrs":{"align":null,"level":5},"content":[{"type":"text","text":"MyISAM","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"一張表對應兩個文件分別是:","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"bulletedlist","content":[{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"*.frm——表結構定義文件","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"*.myd——表數據文件data","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"*.myi——表索引文件index","attrs":{}}]}]}],"attrs":{}},{"type":"heading","attrs":{"align":null,"level":4},"content":[{"type":"text","text":"設置數據庫表的字符集編碼","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"在創建表結構時設置如下,建議使用這個:","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"codeblock","attrs":{"lang":"text"},"content":[{"type":"text","text":"DEFAULT CHARSET=utf8mb4\n","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"不設置會是mysql默認的字符集編碼,Mysql默認字符集編碼是Latin1,不支持中文。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"第二種設置字符集編碼是在mysql的配置文件my.ini中設置默認的字符集編碼爲utf8mb4。建議使用第一種在表結構文件中設置,這樣在其他服務器的mysql中就一定是我們設置的字符集編碼,因爲別人不一定在配置文件中設置了默認的字符集編碼爲utf8mb4。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"codeblock","attrs":{"lang":"text"},"content":[{"type":"text","text":"character-set-server=utf8mb4\n","attrs":{}}]},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"修改刪除表","attrs":{}}]},{"type":"blockquote","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"修改","attrs":{}}]}],"attrs":{}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"codeblock","attrs":{"lang":"text"},"content":[{"type":"text","text":"-- 修改表名 ALTER TABLE 舊錶名 RENAME AS 新表名;\nALTER TABLE t_student RENAME AS student;\n\n-- 增加表字段 ALTER TABLE 表名 ADD 字段名 列屬性\nALTER TABLE student ADD address VARCHAR(30) DEFAULT NULL COMMENT '地址';\n\n-- 修改表字段 ALTER TABLE 表名 MODIFY 字段名 [列屬性];\nALTER TABLE student MODIFY address TIMESTAMP ;\n-- CHANGE 字段可以重命名和修改約束, MODIFY 只能修改約束\nALTER TABLE student CHANGE update_time update_date date ;\n-- 刪除表字段 ALTER TABLE 表名 DROP 字段名;\nALTER TABLE student DROP address;\n","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"blockquote","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"刪除","attrs":{}}]}],"attrs":{}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"codeblock","attrs":{"lang":"text"},"content":[{"type":"text","text":"--刪除表 如果存在則刪除\nDROP TABLE IF EXISTS student;\n","attrs":{}}]},{"type":"heading","attrs":{"align":null,"level":2},"content":[{"type":"text","text":"MySQL的數據管理","attrs":{}}]},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"外鍵","attrs":{}}]},{"type":"codeblock","attrs":{"lang":"text"},"content":[{"type":"text","text":"\nCREATE TABLE IF NOT EXISTS `t_grade` (\n id BIGINT(10) NOT NULL AUTO_INCREMENT COMMENT '年級id',\n grade_name VARCHAR(30) NOT NULL COMMENT '年級名',\n PRIMARY KEY (`id`)\n) ENGINE=INNODB CHARSET=utf8mb4 COMMENT '年級表';\n\n-- 創建外鍵方式1 \n-- 1、學生表的grade_id字段要添加年級表的外鍵索引\n-- 2、給這個外鍵添加約束(執行引用)\nCREATE TABLE IF NOT EXISTS `t_student` (\n `id` BIGINT ( 10 ) NOT NULL AUTO_INCREMENT COMMENT 'id',\n `name` VARCHAR ( 30 ) NOT NULL COMMENT '學生姓名',\n `age` INT ( 3 ) NOT NULL COMMENT '學生年齡',\n `grade_id` BIGINT ( 10 ) NOT NULL COMMENT '年級id',\n `create_user` VARCHAR ( 30 ) DEFAULT NULL COMMENT '創建人',\n `crreate_time` datetime DEFAULT NULL COMMENT '創建時間',\n `update_user` VARCHAR ( 30 ) DEFAULT NULL COMMENT '修改人',\n `update_time` datetime DEFAULT NULL COMMENT '修改時間',\n `delete_flag` TINYINT ( 1 ) DEFAULT NULL COMMENT '是否刪除 0-否 1-是',\n `address` VARCHAR ( 30 ) DEFAULT NULL COMMENT '地址',\n PRIMARY KEY ( `id` ) ,\n KEY (`fk_grade_id`) ,\n CONSTRAINT `fk_grade_id` FOREIGN KEY `grade_id` REFERENCES `t_grade`(`id`)\n) ENGINE = INNODB DEFAULT CHARSET = utf8mb4 COMMENT = '學生表';\n\n-- 創建外鍵方式2\nALTER TABLE t_student ADD CONSTRAINT `fk_grade_id` FOREIGN KEY(`grade_id`) REFERENCES `t_grade`(`id`);\n","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"注意:外鍵是物理外鍵,屬於數據庫級別的外鍵,必須管理關係導致的問題。每次做delete或update時都要考慮外檢約束,導致開發和測試都不方便。因此強制不得使用外鍵和級聯,一切外鍵概念必須在應用層解決。","attrs":{}}]},{"type":"heading","attrs":{"align":null,"level":4},"content":[{"type":"text","text":"最佳實踐","attrs":{}}]},{"type":"bulletedlist","content":[{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"數據庫只是單純的表,只存放數據和字段","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"通過代碼層面實現外鍵功能","attrs":{}}]}]}],"attrs":{}},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"Insert——插入語句","attrs":{}}]},{"type":"heading","attrs":{"align":null,"level":4},"content":[{"type":"text","text":"示例","attrs":{}}]},{"type":"codeblock","attrs":{"lang":"text"},"content":[{"type":"text","text":"-- 插入語句 \n-- 格式 insert into 表名([字段名1,字段名2,字段名3...]) values (值1,值2,值3...), (值1,值2,值3...);\n-- 插入表的字段如果省略不寫則需要在傳入值時要所以字段值都填上並且與字段順序一一對應匹配\n-- Column count doesn't match value count at row 1 \nINSERT INTO t_student VALUES (null,'艾米');\nINSERT INTO t_student VALUES (6,'艾米2',18,1,'2002-10-10');\n-- 對於自增主鍵我們在插入時可以使用null代替,這樣數據庫會自增幫我們插入\nINSERT INTO t_student VALUES (null,'艾米2',18,1,'2002-10-10');\n\ninsert into t_student(`name`,age) values('大青山',19);\n-- 插入多條數據用(),()隔開\ninsert into t_student(`name`,age) values('池傲天',20),('霍恩斯',200);\n","attrs":{}}]},{"type":"heading","attrs":{"align":null,"level":4},"content":[{"type":"text","text":"注意","attrs":{}}]},{"type":"bulletedlist","content":[{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"語法都是英文的,包括英文標的符號","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"插入字段省略則後面valus值必須一一對應","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"插入多條語句用括號和英文逗號隔開。","attrs":{}}]}]}],"attrs":{}},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"Update——修改語句","attrs":{}}]},{"type":"heading","attrs":{"align":null,"level":4},"content":[{"type":"text","text":"示例","attrs":{}}]},{"type":"codeblock","attrs":{"lang":"text"},"content":[{"type":"text","text":"-- 修改\n-- 語法 UPDATE 表名 SET column_name=value,[column_name=value] where [條件]\nUPDATE t_student SET `name`= '年輕的艾米',`age`=6 WHERE id =1;\nUPDATE t_student SET `name`= '年輕的艾米',`age`=6 WHERE id = null;\n-- 不加where條件的話則更新全部數據,所以要注意\nUPDATE t_student SET `name`= '年輕的艾米',`age`=6 ;\n-- 修改id=2到5之間的數據,是閉區間\nupdate t_student set grade_id = '7' where id between 2 and 5;\n-- or 或\nupdate t_student set grade_id='8' where id=1 or id =6;\n-- and 和\nupdate t_student set grade_id='9' where id=1 and age=20;\n-- 設置的value值可以是一個具體的值,也可以是變量\nupdate t_student set crreate_time = CURRENT_TIME where id=1 and age=20;\nupdate t_student set update_time = t_student.crreate_time where id=1 and age=20;\n","attrs":{}}]},{"type":"heading","attrs":{"align":null,"level":4},"content":[{"type":"text","text":"注意","attrs":{}}]},{"type":"bulletedlist","content":[{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"column表的列名最好都帶上``,防止數據庫保留的關鍵字問題","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"where條件沒有加入則會修改表全部的數據","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"設置的value值可以是一個具體的值,也可以是變量,如","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"now()","attrs":{}}],"attrs":{}},{"type":"text","text":",","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"current_time","attrs":{}}],"attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"set=value設置值之間用逗號隔開","attrs":{}}]}]}],"attrs":{}},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"Delete、TRUNCATE——刪除語句","attrs":{}}]},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"示例","attrs":{}}]},{"type":"codeblock","attrs":{"lang":"text"},"content":[{"type":"text","text":"-- 語法 DELETE FROM 表名 [WHERE 條件]\n-- 刪除指定條件下的數據\nDELETE FROM t_student WHERE id =2;\n-- 刪除所有數據,避免使用\nDELETE FROM t_student ;\n\n-- 更好的表數據刪除使用TRUNCATE語句\n-- TRUNCATE 表名 \n-- TRUNCATE 完全清空數據庫表\ntruncate t_student;\n","attrs":{}}]},{"type":"heading","attrs":{"align":null,"level":4},"content":[{"type":"text","text":"Delete和TRUNCATE的區別","attrs":{}}]},{"type":"bulletedlist","content":[{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"相同點:都可以刪除數據,不會影響表結構","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"不同點:","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":1,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"TRUNCATE會重新設置自增列,自增計數器會歸零,而Delete自增計數器不會變","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":1,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"TRUNCATE不受事務影響,而Delete可以在事務中回滾rollback","attrs":{}}]}]}],"attrs":{}},{"type":"heading","attrs":{"align":null,"level":4},"content":[{"type":"text","text":"Delete刪除的問題","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"當重啓數據庫時,InnoDB的自增列會歸零,因爲計數器存在內存中,斷電即失;而MyISAM計數器存放在文件中,不會斷電或者重啓丟失。","attrs":{}}]},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"Select——查詢(DQL)","attrs":{}}]},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/05/051827351f8a35bbb46964bbbaef91ad.png","alt":null,"title":null,"style":[{"key":"width","value":"75%"},{"key":"bordertype","value":"none"}],"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/19/19c11e878d7455a0871cae3a68abd369.png","alt":null,"title":null,"style":[{"key":"width","value":"75%"},{"key":"bordertype","value":"none"}],"href":null,"fromPaste":true,"pastePass":true}},{"type":"heading","attrs":{"align":null,"level":4},"content":[{"type":"text","text":"簡單查詢示例","attrs":{}}]},{"type":"codeblock","attrs":{"lang":"text"},"content":[{"type":"text","text":"-- 查詢表全部字段全部數據 \nSELECT * from student;\n-- 查詢指定字段\nSELECT studentno,studentname FROM student;\n-- 字段和表都可以起別名,可以用as 或者空格\nSELECT studentno as 學號,studentname 姓名 FROM student s;\n-- 拼接函數 concat\n-- 姓名:張偉前來報到,聯繫號碼:13800001234\nSELECT CONCAT('姓名:',studentname,'前來報到,聯繫號碼:',phone) FROM student;\n-- DISTINCT去重\nSELECT DISTINCT r.studentno FROM result r;\n","attrs":{}}]},{"type":"heading","attrs":{"align":null,"level":4},"content":[{"type":"text","text":"通過SELECT進行函數、表達式、變量使用示例","attrs":{}}]},{"type":"codeblock","attrs":{"lang":"text"},"content":[{"type":"text","text":"-- 查看系統版本,(函數)形式\n-- 5.7.17-log\nSELECT VERSION();\n-- 用於計算,(表達式) \n-- 300\nSELECT 100*5-200 AS 結果;\nSELECT 100*5-200 AS total;\n\n-- 查看自增的步長,即每次自增多少,(變量)\n-- 1\nSELECT @@auto_increment_increment;\n-- 篩選出成績加1分查看 \nSELECT s.studentno `no` , s.studentresult + 1 score FROM result s;\nSELECT s.studentno , s.studentresult/10 as score FROM result s;\n","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"link","attrs":{"href":"https://dev.mysql.com/doc/refman/5.7/en/sql-function-reference.html","title":"","type":null},"content":[{"type":"text","text":"MySQL5.7官網方法與函數操作符地址","attrs":{}}]}]},{"type":"heading","attrs":{"align":null,"level":4},"content":[{"type":"text","text":"where條件子句示例","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"where條件結果問布爾值,由一個或多個表達式組成。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"邏輯運算符 與或非可以使用下面兩種語句都行,儘量使用第一種英文。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"bulletedlist","content":[{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"and or not","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"&& || !","attrs":{}}]}]}],"attrs":{}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"codeblock","attrs":{"lang":"text"},"content":[{"type":"text","text":"-- 與或非的其他寫法展示\n-- and 與\nSELECT * FROM result s where s.studentresult >1 && s.studentresult <80;\n-- or 或\nSELECT * FROM result s where s.studentresult =70 || s.studentresult =68;\n-- not 非\nSELECT * FROM result s where not s.studentresult =70;\n","attrs":{}}]},{"type":"heading","attrs":{"align":null,"level":4},"content":[{"type":"text","text":"模糊查詢示例","attrs":{}}]},{"type":"embedcomp","attrs":{"type":"table","data":{"content":"
運算符語法描述
IS NULLage is nullage爲null則結果爲true
IS NOT NULLage is not nullage不爲null則結果爲true
BETWEEN ANDage between a and bage在a和b之間則結果爲true
Likename like '%a%'模糊匹配,name包含a則爲真
INage in(a,b,c)age在其中某一個值則結果爲真
"}}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"codeblock","attrs":{"lang":"text"},"content":[{"type":"text","text":"-- 模糊查詢 like %表示0到任意個字符, _表示一個字符\n-- 查詢姓張,名字爲2個字的學生\nSELECT * FROM student s where s.studentname like '張_';\n-- 查詢名字中帶偉的學生\nSELECT * FROM student s where s.studentname like '%偉%';\n-- in 查詢\nSELECT * FROM student s where s.studentno in(1000,1001);\n","attrs":{}}]},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"聯表查詢","attrs":{}}]},{"type":"heading","attrs":{"align":null,"level":4},"content":[{"type":"text","text":"聯表示例","attrs":{}}]},{"type":"codeblock","attrs":{"lang":"text"},"content":[{"type":"text","text":"-- 左聯、右聯等外聯是要用on來關聯否則報錯,on確定主表,無論條件如何主表數據都會返回然後進行篩選。\nSELECT * FROM student s LEFT JOIN result r on s.studentno = r.studentno where s.studentname='張偉';\n-- 內聯時用on或者where進行關聯都可以\nSELECT * FROM student s INNER JOIN result r where s.studentno = r.studentno and s.studentname='張偉';\nSELECT * FROM student s JOIN result r where s.studentno = r.studentno and s.studentname='張偉';\n\n-- 聯表查詢思路\n/*\n1、分析需求要的字段來自哪些表\n2、確定使用哪種連接查詢\n3、確定交叉點,比如訂單號id\n4、判斷的條件\n*/\n-- 學生年級聯表查詢\nselect s.studentno,s.studentname,g.gradename,r.studentresult,sub.subjectname from student s \nINNER JOIN grade g ON s.gradeid = g.gradeid \nINNER JOIN result r ON s.studentno = r.studentno \nleft JOIN `subject` sub ON sub.subjectno = r.subjectno;\n\n-- 自連接,設計表時把有父子關係的兩種層級表合併成一張表\n-- 進行查詢時自己和自己關聯查詢\nSELECT a.categoryname 父類別,b.categoryname 子類別 from category a,category b where a.categoryid= b.pid;\n","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"codeblock","attrs":{"lang":"text"},"content":[{"type":"text","text":"1000 張偉 大二 85 高等數學-1\n1000 張偉 大二 70 高等數學-2\n1000 張偉 大二 68 高等數學-3\n1000 張偉 大二 98 高等數學-4\n1000 張偉 大二 58 C語言-1\n","attrs":{}}]},{"type":"heading","attrs":{"align":null,"level":4},"content":[{"type":"text","text":"七種join理論","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"link","attrs":{"href":"https://blog.csdn.net/huang__2/article/details/83688001","title":"","type":null},"content":[{"type":"text","text":"參考","attrs":{}}]}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/eb/ebb630e905ee0ba4e4e0d56d53261f19.png","alt":null,"title":null,"style":[{"key":"width","value":"75%"},{"key":"bordertype","value":"none"}],"href":null,"fromPaste":true,"pastePass":true}},{"type":"heading","attrs":{"align":null,"level":4},"content":[{"type":"text","text":"mysql進行關聯Join時where和on的區別","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"Join時where和on的區別關鍵在於left join,right join,full join(inner join)的特殊性,不管","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"on上的條件是否爲真都會返回left或right表中的記錄","attrs":{}},{"type":"text","text":",full則具有left和right的特性的並集。 而","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"inner jion沒這個特殊性,則條件放在on中和where中,返回的結果集是相同的。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"在使用mysql數據庫時,使用left join或者right join都必須要使用關鍵字on,否則就sql執行就報錯。可以這樣理解:on是建立外連的橋,兩張表如何連接就靠on後面的條件。因爲外連分主次表,數據以主表爲基礎,次表對應連接,如果沒有on來建立連接,那麼次表的數據就不知道如何對應上主表。而內連,又稱爲直連,不存在主次表之分,取得是兩者的交集,因此不需要on。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"外連接時,on條件是在生成臨時表時使用的條件,它不管on中的條件是否爲真,都會返回左邊表中的記錄。而where條件是在臨時表生成好後,再對臨時表進行過濾的條件。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"link","attrs":{"href":"https://www.dazhuanlan.com/2019/10/16/5da68cfdddbf2/","title":"","type":null},"content":[{"type":"text","text":"參考","attrs":{}}]}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"link","attrs":{"href":"https://blog.csdn.net/tayngh/article/details/99684035","title":"","type":null},"content":[{"type":"text","text":"參考2","attrs":{}}]}]},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"分頁和排序","attrs":{}}]},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"分頁示例","attrs":{}}]},{"type":"heading","attrs":{"align":null,"level":5},"content":[{"type":"text","text":"分頁的意義","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"緩解數據庫壓力,體驗更好,返回數據更快,瀑布流方式分頁","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"codeblock","attrs":{"lang":"text"},"content":[{"type":"text","text":"-- 排序 ORDER BY 字段名 DESC \n-- 升序 ASC ,降序 DESC\nSELECT * FROM result r ORDER BY r.studentresult DESC;\n\n-- 分頁 limit offset,pagesize 參數描述:(起始值,從0開始),(頁面大小) \nSELECT * FROM result r ORDER BY r.studentresult DESC LIMIT 0,2;\nSELECT * FROM result r ORDER BY r.studentresult DESC LIMIT 2,2;\n","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/83/83677c136bca2d01ff3f002f373a5735.png","alt":null,"title":null,"style":[{"key":"width","value":"75%"},{"key":"bordertype","value":"none"}],"href":null,"fromPaste":true,"pastePass":true}},{"type":"heading","attrs":{"align":null,"level":4},"content":[{"type":"text","text":"Java分頁實現","attrs":{}}]},{"type":"codeblock","attrs":{"lang":"text"},"content":[{"type":"text","text":" /**\n * 當前分頁總頁數\n */\n default long getPages() {\n if (getSize() == 0) {\n return 0L;\n }\n long pages = getTotal() / getSize();\n if (getTotal() % getSize() != 0) {\n pages++;\n }\n return pages;\n }\n\n /**\n * 計算當前分頁偏移量\n */\n default long offset() {\n long current = getCurrent();\n if (current <= 1L) {\n return 0L;\n }\n return (current - 1) * getSize();\n }\n","attrs":{}}]},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"子查詢和嵌套查詢","attrs":{}}]},{"type":"heading","attrs":{"align":null,"level":4},"content":[{"type":"text","text":"示例","attrs":{}}]},{"type":"codeblock","attrs":{"lang":"text"},"content":[{"type":"text","text":"-- 子查詢關聯查詢\nSELECT DISTINCT s.studentno,s.studentname FROM student s INNER JOIN result r on s.studentno=r.studentno\nWHERE r.studentresult >=80 and r.subjectno=(SELECT subjectno from `subject` sub where sub.subjectname='高等數學-4');\n\n-- 多張表聯表查詢 \nSELECT DISTINCT s.studentno,s.studentname FROM student s INNER JOIN result r on s.studentno=r.studentno\nINNER JOIN `subject` sub \nON sub.subjectno=r.subjectno\nWHERE r.studentresult >=80 and sub.subjectname='高等數學-4';\n\n-- 嵌套查詢\nSELECT DISTINCT s.studentno,s.studentname FROM student s where s.studentno IN(\nSELECT r.studentno FROM result r WHERE r.studentresult >=80 and r.subjectno =(\nSELECT subjectno from `subject` sub where sub.subjectname='高等數學-4'\n)\n)\n","attrs":{}}]},{"type":"heading","attrs":{"align":null,"level":2},"content":[{"type":"text","text":"函數","attrs":{}}]},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"常用函數","attrs":{}}]},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/e2/e23c4dc0801bdebe7d7f0557fae57889.png","alt":null,"title":null,"style":[{"key":"width","value":"75%"},{"key":"bordertype","value":"none"}],"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/1d/1d3af8cd151c50e060c899102030aa14.png","alt":null,"title":null,"style":[{"key":"width","value":"75%"},{"key":"bordertype","value":"none"}],"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/49/495e0386f266b61dab5fec62c736a18f.png","alt":null,"title":null,"style":[{"key":"width","value":"75%"},{"key":"bordertype","value":"none"}],"href":null,"fromPaste":true,"pastePass":true}},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"聚合函數","attrs":{}}]},{"type":"embedcomp","attrs":{"type":"table","data":{"content":"
函數描述
count()計數
sum()求和
avg()平均值
max()最大值
min()最小值
"}}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"where的條件不能用聚合函數,要改用having過濾。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/e8/e80533ffff80fa780823fc72477d7b07.png","alt":null,"title":null,"style":[{"key":"width","value":"75%"},{"key":"bordertype","value":"none"}],"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"codeblock","attrs":{"lang":"text"},"content":[{"type":"text","text":"-- 通過分組查詢各個科目的最高分平均分等聚合統計\n-- GROUP BY r.subjectno 通過什麼字段來分組,該字段 不需要是在展示的列名上\nSELECT s.subjectname , MAX(r.studentresult) max, min(r.studentresult) min, avg(r.studentresult) avg FROM result r INNER JOIN\n`subject` s ON r.subjectno = s.subjectno \nGROUP BY r.subjectno \nHAVING avg >=80;\n","attrs":{}}]},{"type":"heading","attrs":{"align":null,"level":5},"content":[{"type":"text","text":"執行count(1)、count(*) 與 count(列名) 的區別","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"count(列名)會忽略字段中數據爲null的值,這時候不會計入總數,而count(1)、count(*) 不會忽略null值,只要這一行有數據就會計數,本質是計算行數。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"執行效果上:","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"bulletedlist","content":[{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"count(*)包括了所有的列,相當於行數,統計結果時不會忽略列值爲null的記錄。","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"count(1)會忽略所有的列,用1代表一行數據,統計結果時不會忽略列值爲null的記錄。","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"count(列名)值包括列名那一列,統計結果時當列值數據爲null時會忽略計數,不統計。","attrs":{}}]}]}],"attrs":{}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"執行效率上:","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"bulletedlist","content":[{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"列名爲主鍵","attrs":{}},{"type":"text","text":",count(列名)會比count(1)快","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"列名不爲主鍵,count(1)會比count(列名)快","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"如果表多個列並且沒有主鍵,則 count(1 的執行效率優於 count(*)","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"如果有主鍵,則 select count(主鍵)的執行效率是最優的","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"如果表只有一個字段,則 select count(*)最優。","attrs":{}}]}]}],"attrs":{}},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"MD5加密","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"什麼是MD5?","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"MD5信息摘要算法(英語:MD5 Message-Digest Algorithm),一種被廣泛使用的密碼散列函數,可以產生出一個128位(16字節)的散列值(hash value),用於確保信息傳輸完整一致。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"MD5不可逆,相同的密碼值MD5是一樣的,因此MD5破解網站的原理是後臺存儲了MD5的字典,用於匹配MD5加密後的值和加密前的值。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"MD5加鹽:鹽被稱作“Salt值”,這個值是由系統隨機生成的,並且只有系統知道。即便","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"兩個用戶使用了同一個密碼,由於系統爲它們生成的salt值不同,散列值也是不同的。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"codeblock","attrs":{"lang":"text"},"content":[{"type":"text","text":"-- 創建用戶表,測試md5加密\nCREATE table `user`(\n`id` BIGINT(8) UNSIGNED auto_increment NOT null COMMENT 'id',\n`name` varchar(20) NOT null COMMENT '姓名',\n`pwd` VARCHAR(50) NOT null COMMENT '密碼',\nPRIMARY KEY(`id`)\n)ENGINE=INNODB DEFAULT charset=utf8mb4;\n-- 明文密碼\ninsert into `user`(`name`,`pwd`)VALUES('艾米','123456'),('大青山','123456'),('霍恩斯','123456'),('雷葛','123456');\n-- 使用MD5加密\n-- 指定用戶加密\nupdate `user` set pwd=MD5(pwd) where `name`='艾米';\n\n-- 更新所有用戶加密 \n-- 注意加密後的MD5值是一樣的,之所以艾米用戶的值不一樣是因爲更新所有導致二次加密了\nupdate `user` set pwd=MD5(pwd) ;\n\n-- 插入時加密\ninsert into `user`(`name`,`pwd`)VALUES('綠兒',MD5('123456'));\n\n-- 校驗加密,相當於用戶登錄\n-- 將用戶登錄的密碼用md5加密後與數據庫的值比對\nSELECT * FROM `user` u where u.`name`='綠兒' and u.pwd = MD5('123456');\n","attrs":{}}]},{"type":"heading","attrs":{"align":null,"level":2},"content":[{"type":"text","text":"事務","attrs":{}}]},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"ACID","attrs":{}}]},{"type":"heading","attrs":{"align":null,"level":4},"content":[{"type":"text","text":"原子性(Atomicity)","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"原子性是指事務是一個不可分割的工作單位,事務中的操作要麼都發生,要麼都不發生。","attrs":{}}]},{"type":"heading","attrs":{"align":null,"level":4},"content":[{"type":"text","text":"一致性(Consistency)","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"事務前後數據的完整性必須保持一致。","attrs":{}}]},{"type":"heading","attrs":{"align":null,"level":4},"content":[{"type":"text","text":"隔離性(Isolation)","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"事務的隔離性是多個用戶併發訪問數據庫時,數據庫爲每一個用戶開啓的事務,不能被其他事務的操作數據所幹擾,多個併發事務之間要相互隔離。","attrs":{}}]},{"type":"heading","attrs":{"align":null,"level":4},"content":[{"type":"text","text":"持久性(Durability)","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"持久性是指一個事務一旦被提交,它對數據庫中數據的改變就是永久性的,接下來即使數據庫發生故障也不應該對其有任何影響","attrs":{}}]},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"事務的隔離級別","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"因爲隔離性導致的髒讀、幻讀、不可重複讀。","attrs":{}}]},{"type":"heading","attrs":{"align":null,"level":4},"content":[{"type":"text","text":"髒讀:","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"指一個事務讀取了另外一個事務未提交的數據。","attrs":{}}]},{"type":"heading","attrs":{"align":null,"level":4},"content":[{"type":"text","text":"虛讀(幻讀)","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"是指在一個事務內讀取到了別的事務插入的數據,導致前後讀取不一致。(一般是行影響,多了一行)","attrs":{}}]},{"type":"heading","attrs":{"align":null,"level":4},"content":[{"type":"text","text":"不可重複讀:","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"在一個事務內讀取表中的某一行數據,多次讀取結果不同。(這個不一定是錯誤,只是某些場合不對)","attrs":{}}]},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"數據庫隔離級別設置","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"set transaction isolation level 設置事務隔離級別select @@tx_isolation 查詢當前事務隔離級別","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"設置描述:Serializable 可避免髒讀、不可重複讀、虛讀情況的發生。(串行化)Repeatable read 可避免髒讀、不可重複讀情況的發生。(可重複讀)Read committed 可避免髒讀情況發生(讀已提交)。Read uncommitted 最低級別,以上情況均無法保證。(讀未提交)","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"link","attrs":{"href":"https://blog.csdn.net/qq_42337368/article/details/102660338","title":"","type":null},"content":[{"type":"text","text":"數據庫ACID參考","attrs":{}}]}]},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"數據庫操作事務示例","attrs":{}}]},{"type":"codeblock","attrs":{"lang":"text"},"content":[{"type":"text","text":"-- 關閉事務自動提交\nSET autocommit =0;\n-- mysql默認開啓事務自動提交 \nSET autocommit =1;\n\nSTART TRANSACTION;\ninsert into `user`(`name`,`pwd`)VALUES('艾米','123456'),('大青山','123456'),('霍恩斯','123456'),('雷葛','123456');\n-- 成功提交事務持久化\nCOMMIT;\n-- 回滾,一般用於失敗後回滾數據\nROLLBACK;\n\n-- 保存點\nSAVEPOINT a;\n-- 回滾到一個事務的保存點\nROLLBACK TO SAVEPOINT a;\n-- 釋放保存點\nRELEASE SAVEPOINT a;\n","attrs":{}}]},{"type":"heading","attrs":{"align":null,"level":2},"content":[{"type":"text","text":"索引","attrs":{}}]},{"type":"blockquote","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"MySQL官方對索引的定義爲:索引(Index)是幫助MySQL高效獲取數據的數據結構。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"索引的本質:索引是數據結構。","attrs":{}}]}],"attrs":{}},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"索引的分類","attrs":{}}]},{"type":"bulletedlist","content":[{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"主鍵索引——PRIMARY KEY","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"唯一索引——UNIQUE KEY","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"普通索引——INDEX 、KEY","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"全文索引——FULLTEXT","attrs":{}}]}]}],"attrs":{}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/91/91d45f428a5dcf2e08b0cd9841702ed7.png","alt":null,"title":null,"style":[{"key":"width","value":"75%"},{"key":"bordertype","value":"none"}],"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"link","attrs":{"href":"http://blog.codinglabs.org/articles/theory-of-mysql-index.html","title":"","type":null},"content":[{"type":"text","text":"MySQL索引背後的數據結構及算法原理","attrs":{}}]}]},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"如何用EXPLAIN優化sql查詢效率","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"link","attrs":{"href":"https://segmentfault.com/a/1190000021458117","title":"","type":null},"content":[{"type":"text","text":"一張圖徹底搞懂MySQL的 explain","attrs":{}}]}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/a7/a7968c4cc1b12588e2a92d7fa45a96f6.png","alt":null,"title":null,"style":[{"key":"width","value":"75%"},{"key":"bordertype","value":"none"}],"href":null,"fromPaste":true,"pastePass":true}},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"索引示例——測試百萬數據的索引效果","attrs":{}}]},{"type":"codeblock","attrs":{"lang":"text"},"content":[{"type":"text","text":"-- 測試表 \nCREATE TABLE `t_user` (\n `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT COMMENT 'id',\n `name` varchar(20) NOT NULL COMMENT '姓名',\n `email` varchar(50) DEFAULT NULL COMMENT '郵箱',\n `phone` varchar(20) DEFAULT NULL COMMENT '手機號碼',\n `pwd` varchar(50) DEFAULT NULL COMMENT '密碼',\n `age` TINYINT(3) DEFAULT NULL COMMENT '年齡',\n `create_time` datetime DEFAULT CURRENT_TIMESTAMP COMMENT '創建時間',\n `update_time` datetime DEFAULT CURRENT_TIMESTAMP COMMENT '修改時間',\n PRIMARY KEY (`id`)\n) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHAstudent.gradeidRSET=utf8mb4 COMMENT='測試用戶表';\n\n-- 插入100w數據函數\n-- 寫函數前先更改mysql分隔符爲$$ ,寫函數標記\nDELIMITER $$\n CREATE FUNCTION 100w_data()\n RETURNS INT\n BEGIN\n DECLARE num INT DEFAULT 1000000;\n DECLARE i INT DEFAULT 1;\n WHILE i物理磁盤位置/文件名\nmysqldump -hlocalhost -uroot -p123456 school student >D:a.sql\n#導出整個數據庫\nmysqldump -hlocalhost -uroot -p123456 school >D:a.sql\n#多張表導出,用空格隔開,命令行是用空格\n#mysqldump -h主機 -u用戶名 -p密碼 數據庫 表1 表2 表3 >物理磁盤位置/文件名\nmysqldump -hlocalhost -uroot -p123456 school student >D:a.sql\n","attrs":{}}]},{"type":"heading","attrs":{"align":null,"level":4},"content":[{"type":"text","text":"命令行導入——linux中使用","attrs":{}}]},{"type":"codeblock","attrs":{"lang":"text"},"content":[{"type":"text","text":"#登錄的情況下導入,先切換到指定的數據庫\n#source 備份文件\nsource d:a.sql\n\n#未登錄狀態 表示導出\n#mysql -u用戶名 -p密碼 庫名< 備份文件\nmysql -uroot -p123456 school student
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章