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
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章