当前位置:网站首页>MySQL - based
MySQL - based
2022-08-02 07:54:00 【kk_lina】
目录
3.1、Create multiple pieces of data at once
四、Some common statements for query
一、SQL分类
- DDL(数据定义语言):对数据库、表、视图、Addition, deletion and modification of objects such as indexes,关键字:CREAT、DROP、ALTER、RENAME、TRUNCATE等,It cannot be rolled back after execution;
- DML(数据操作语言):Add, delete, modify, and query operations on database records,关键字:INSERT、DELETE、UPDATE、SELECT等,By default, it cannot be rolled back after execution,可以修改参数autocommit =false则可以回滚.
- DCL(数据控制语言):对数据库、表、字段、用户的访问权限、Definition of security level,关键字:GRANT、REVOKE、COMMIT、ROLLBACK、SAVEPOINT
- DQL(数据查询语言):Because query operations are frequent,Therefore, many people single out the query as a category,SELECT;
- TCL(事务控制语言):关键字:COMMIT、ROLLBACK.
二、命名规范
- 数据库名、表名、表别名、字段名、字段别名等都是小写;
- SQL关键字、函数名、Bind variable names, etc. are all capitalized
三、导入数据
source 文件存储地址
3.1、Create multiple pieces of data at once
- 创建表
- 设置参数 log_bin_trust_function_creators
#不加global只对当前窗口有效 set global log_bin_trust_function_creators=1;
- 创建函数 随机产生字符串
DELIMITER // CREATE FUNCTION rand_string(n INT) RETURNS VARCHAR(255) #该函数会返回一个字符串 BEGIN
四、Some common statements for query
- 去除重复行
select distinct 字段 from 表; select distinct 字段,字段 from 表;
- IFNULL(字段,字段值,默认值),If it is empty, take the field value,否则取默认值
- 显示表结构
describe 表名; desc 表名;
- The difference between safe equals and equals is that,NULL=NULL为NULL,NULL<=>NULL为1;
- least()、greatest()Query the minimum value for the field、最大值;
select least(字段1,字段2) from 表名; select greatest(字段1,字段2) from 表名;
- between ... and 查询x-y的信息,包含边界值;
select * from table_name where id between 10 and 20;
- inin certain ranges;
select * from table_name where id in(1,2,3);
- ESCA或者\:转义字符;
- 正则表达式:regexp、rlike;^以...开始,$以...结尾,.匹配任何一个字符
- 逻辑运算符:or 、&&、!、xor.and优先级高于or
- 在一定范围内,Shifting one bit to the left is equivalent to multiplying2,向右移动一位相当于除以2
- The data returned by the query without the use of a sort operation is by defaultThe order in which data is added;
- order by对查询数据进行排序默认从低到高,DESC降序,ASC升序;
select * from table_name order by 字段; select * from table_name order by 字段 DESC; select * from table_name order by 字段 ASC; select * from table_name where id in(1,2,3) order by 字段 ASC;
- limit分页,8.0版本中limit x offset y;oracle不支持
select * from table_name limit 0,20
- 多表查询
分类:等值链接、非等值链接;自连接、非自连接;内连接、外连接
union:满外连接,去除重复部分;
union all:满外连接,Duplicates are not removed;
4.1、函数
From an implementation perspective:数值函数、字符函数、加密解密函数、时间日期函数、流程控制函数、聚合函数等;
Built-in functions are reclassified:单行函数、聚合函数(或分组函数).
4.1.1基本函数
函数 | 用法 |
ABS(x) | 返回x的绝对值 |
SIGN(X) | 返回x的符号.正数返回1,负数返回-1,0返回0 |
PI() | 返回圆周率的值 |
CEIL(X),CEILING(X) | 返回大于或等于某个值的最小整数,向上取整 |
FLOOR(X) | Returns the smallest integer less than or equal to a value,向下取整 |
LEAST(e1,e2,e3...) | Returns the minimum value in the table |
GREATEST(e1,e2,e3...) | Returns the maximum value in the table |
MOD(X,Y) | 返回X除以Y后的余数 |
RAND() | 返回0-1的随机数 |
RAND(X) | 返回0-1的随机数,其中X的值用作种子值,相同的X值会产生相同的随机数 |
ROUND(x) | 返回一个对x的值进行四舍五入后,最接近于x的整数 |
ROUND(X,Y) | 返回一个对x的值进行四舍五入后最接近x的值,and keep after the decimal pointy位 |
TRUNCATE(X,Y) | 返回数字x截断y位小数的结果.取y位的x值,不四舍五入 |
SQRT(X) | 返回x的平方根.当x的值为负数时,返回null |
4.1.2三角函数
函数 | 用法 |
SIN(x) | 返回x的正弦值,其中,参数x为弧度值 |
ASIN(x) | 返回x的反正弦值,即获取正弦为x的值.如果x的值不在-1到1之间,则返回NULL |
COS(x) | 返回x的余弦值,其中,参数x为弧度值 |
ACOS(x) | 返回x的反余弦值,即获取余弦为x的值.如果x的值不在-1到1之间,则返回NULL |
TAN(x) | 返回x的正切值,其中,参数x为弧度值 |
ATAN(x) | 返回x的反正切值,即返回正切值为x的值 |
ATAN2(m,n) | 返回两个参数的反正切值 |
COT(x) | 返回x的余切值,其中,x为弧度值 |
4.1.3指数与对数
函数 | 用法 |
POW(x,y),POWER(X,Y) | 返回x,y的次方 |
EXP(X) | 返回e的X次方,其中e是一个常数,2.718281828459045 |
LN(X),LOG(X) | 返回以e为底的X的对数,当X<=0时,返回的结果为NULL |
LOG10(X) | 返回以10为底的X的对数,当X<=时,返回的结果为NULL |
LOG2(X) | 返回以2为底的X的对数,当X<=0时,返回NULL |
4.1.4进制间的转换
函数 | 用法 |
BIN(X) | 将X转换为二进制 |
HEX(X) | 将X转换为十六进制 |
OCT(X) | 将X转换为八进制 |
CONV(X,F1,F2) | 返回F1进制数变成 F2进制数 |
4.1.5字符串函数
函数 | 用法 |
ASCII(S) | 返回字符串S中的第一个字符 |
CHAR_LENGTH(S) | 返回字符串s的字符数.作用于CHARACTER_LENGTH(s)相同 |
LENGTH(S) | 返回字符串s的字节数,Regardless of character set |
CONCAT(S1,S2,S3...) | 连接s1,s2,s3......sn,为一个字符串 |
CONCAT_WS(X,S1,S2,S3...) | 同CONCAT(S1,S2,S3...)函数,但是每个字符串之间要加上x |
INSERT(str,idx,len,replacestr) | 将字符串str从第idx位置开始,len个字符长的子串替换为字符串replacestr |
REPLACE(str,a,b) | 用字符串b代替字符串strin all occurrences of the stringa |
UPPER(S)或UCASE(S) | 将字符串s的所有字母转换成大写字母 |
LOWER(S)或LCASE(S) | 将字符串s的所有字母转换成小写字母 |
LEFT(str,n) | 返回字符串str最左边的n个字符 |
RIGHT(str,n) | 返回字符串str最右边的n个字符 |
LPAD(str,len,pad) | 用字符串pad对str最左边进行填充,知道str的长度为len个字符 |
RPAD(str,len,pad) | 用字符串pad对str最右边进行填充,知道str的长度为len个字符 |
LTRIM(s) | 去掉字符串s左侧的空格 |
RTRIM(s) | 去掉字符串s右侧的空格 |
TRIM(s) | 去掉字符串s首位空格 |
TRIM(s1 FROM s) | 去掉字符串s开始与结尾的s1 |
TRIM(LEADING s1 FROM s) | 去掉字符串s开始处的s1 |
TRIM(TRAILING s1 FROM s) | 去掉字符串s结尾处的s1 |
REPEAT(str,n) | 返回str重复n次的结果 |
SPACE(n) | 返回n个空格 |
STRCMP(s1,s2) | 比较字符串s1,s2的ASCII码值的大小 |
SUBSTR(s,index,len) | 返回从字符串s的index位置其len个字符,作用与SUBSTRING(s,n,len)、MID(s,n,len)相同 |
LOCATE(substr,str) | 返回字符串substr在字符串 str中首次出现的位置,作用于POSITION(substr IN str)、INSTR(str,substr)相同.未找到,返回0 |
ELT(m,s1,s2,s3...sn) | 返回指定位置的字符串,如果m=1,则返回s1 |
FIELD(s1,s2,s3...) | 返回字符串s在字符串列表中第一次出现的位置 |
FIND_IN_SET(s1,s2) | 返回字符串s1在字符串s2中出现的位置.其中,字符串s2is a comma-separated string |
REVERSE(s) | 返回s反转后的字符串 |
NULLIF(value2,value2) | 比较两个字符串,如果value1与value2相等,则返回null,否则返回value1 |
4.1.6获取日期、时间
函数 | 用法 |
CURDATE(),CURRENT_DATE() | 返回当前日期,只包含年、月、日 |
CURTIME(),CURRENT_TIME() | 返回当前时间,只包含时、分、秒 |
NOW/SYSDATE()/CURRENT_TIMESTAMP()/LOCALTIME()/LOCALTIMESTAMP() | 返回当前系统日期和时间 |
UTC_DATE() | 返回UTC(世界标准时间)日期 |
UTC_TIME() | 返回UTC(世界标准时间)时间 |
UNIX_TIMESTAMP() | 以UNIX时间戳的形式返回当前时间.SELECT UNIX_TIMESTAMP() >1634348884 |
UNIX_TIMESTAMP(data) | 将时间data以UNIX时间戳的形式返回 |
FROM_UNIXTIME(timestamp) | 将UNIX时间戳的时间转换为普通格式的时间 |
4.1.7 聚合函数
函数 | 用法 |
AVG | 平均数 |
SUM | 求和 |
MAX | 最大值 |
MIN | 最小值 |
COUNT | 个数总和 |
SELECT AVG(字段名) FROM TABLE_NAME;
SELECT SUM(字段名) FROM TABLE_NAME;
SELECT MAX(字段名) FROM TABLE_NAME;
SELECT MIN(字段名) FROM TABLE_NAME;
SELECT COUNT(1) FROM TABLE_NAME;
SELECT COUNT(*) FROM TABLE_NAME;
#When counting the number of occurrences of the specified field, it is not countedNULL值的
SELECT COUNT(字段名) FROM TABLE_NAME;
countEfficiency issues in use:
If the storage engine is usedMYISAM,The efficiency of the three is the same;
如果使用InnoDB存储引擎:count(*)=count(1)>count(字段),Create a secondary index on the data table,系统会自动采用key_len小的二级索引进行扫描.
4.2、分组 group by使用
注意事项:
- The fields in the query must be written ingroup by中,反之不一定,在oracle中会报错.
- group by声明在from、where后面,order by、limit、having前面.
- group by中使用with rollupAn overall average is calculated,要慎重使用order by.
4.3、过滤数据 having 的使用
注意事项:
- If used in filter conditions聚合函数,则必须使用having来替代where.否则,报错.
- having必须声明group by后面.
- 开发中使用having的前提是使用了group by.
- When a non-aggregate function filter condition occurs,where比having效率高.通过使用whereWhen grouping, you can filter some unnecessary data,提高效率.
4.4、select 语法的使用总结
- sql92语法
SELECT ...,...,...(存在聚合函数) FROM ...,...,... WHERE Link conditions for multiple tables AND 不包含聚合函数的过滤条件 GROUP BY ...,... HAVING 包含聚合函数的过滤条件 ORDER BY ...,...(ASC / DESC) LIMIT ...,...
- sql99语法
SELECT ...,...,...(存在聚合函数) FROM ...,...,... (LEEF / RIGHT) JOIN ... ON Link conditions for multiple tables (RIGHT / LEEF) JOIN ... ON JOIN ... ON ... WHERE Link conditions for multiple tables GROUP BY ...,...(ASC / DESC) HAVING 包含聚合函数的过滤条件 ORDER BY ...,...(ASC / DESC) LIMIT ...,...
4.5、sql语句执行过程
根据FROMFind a specific table -> 根据ONFilter join conditions ->Whether it is a left outer right outer link -> 行过滤:where过滤数据 -> group by 分组 -> HAVING 过滤 -> 过滤列:SELECT 过滤字段 -> DISTINCT去除 -> ORDER BY -> LIMIT
4.6、子查询
select中可以使用
case 字段名 when 条件 then 取值1 else 取值2 end 显示字段名
Used to set a value to return different field names when the conditional statement is met.
- 多行子查询:IN ANY ALL SOME;
- 相关子查询:The inner query needs to use the conditions of the outer query to query,例如:Find the employees of the company whose salary is greater than the average salary of the departmentlast_name,salary,department_id;
- EXISTS的使用:Send the outer query into a subquery
select employee_id,last_name,job_id,department_id from employees el where exists(select * from employees e2 where el.employee_id = e2.manager_id ;
五、数据类型的选择
- 无符号数
对象 | 年龄区间 | 类型 | 字节 | 表示范围 |
人 | 150岁之间 | tinyinta unsigned | 1 | 无符号值:0到255 |
龟 | 数百岁 | smallint unsigned | 2 | 无符号值:0到65535 |
恐龙化石 | 数千万年 | int unsigned | 4 | 无符号值:0到约43亿 |
太阳 | 约50亿年 | bigint unsigned | 8 | 无符号值:约0到约10的19次方 |
- 选择建议
整数用int;小数用decimal(m,d);时间用datetime;
If the storage range exceedsdecimal范围,Split data into integers and decimals and store them separately;
如果存储的字符串长度几乎相等,使用char定长字符串类型;
varchar是可变长字符串,不预先分配存储空间,长度不要超过5000.如果存储长度大于此值,定义字段类型为text,独立出来一张表,用主键来对应,避免影响其他字段索引效率.
六、约束
约束的作用:
- not null(非空约束)
- unique(唯一约束)
create table table_name( 字段1; 字段2; 字段3 unique; 字段4 unique key; constraint 约束名称 unique(字段1,字段2) ); alter table 表名称 add unique key(字段列表); alter table 表名称 drop index 索引名称;
- primary key(主键约束)
create table table_name(
字段1;
字段2;
字段3 primary key;
constraint 约束名称 primary key(字段1,字段2)
);
alter table 表名称 add primary key(字段列表);
alter table 表名称 drop primary key;
- foreign key(外键约束)
- check(检查约束)8.0版本支持
create table table_name( 字段1; 字段2; 字段3 check('男'or'女'); constraint 约束名称 primary key(字段1,字段2) );
- default(默认值约束)
查看表约束:
select * from information_schema.table_constraints where table_name='表名';
七、视图
- 创建视图
create view 视图名称 as 查询语句
- 利用视图对数据进行格式化
create view 视图名称 as select concat(e.last_name,'(',d.department_name,')') emp_info from emps e join depts d on e.department_id = d.department_id;
边栏推荐
- 根据一个字段的内容去更新另一个字段的数据,这样的sql语句该怎么样书写
- Ask a question, my Flinkcdc has run through, I can monitor the binlog of msql, and I can also send kafk
- 查找最大的n个文件
- redis-高级篇
- pnpm install出现:ERR_PNPM_PEER_DEP_ISSUES Unmet peer dependencies
- LeetCode刷题(7)
- Neural network
- (2022牛客多校五)D-Birds in the tree(树形DP)
- gdalinfo: error while loading shared libraries: libgdal.so.30: cannot open shared object file: No su
- 吃透Chisel语言.30.Chisel进阶之通信状态机(二)——FSMD:以Popcount为例
猜你喜欢
论文理解:“Cross-Scale Residual Network: A GeneralFramework for Image Super-Resolution,Denoising, and “
自然语言处理 文本预处理(下)(张量表示、文本数据分析、文本特征处理等)
pnpm install出现:ERR_PNPM_PEER_DEP_ISSUES Unmet peer dependencies
Xilinx约束学习笔记—— 时序约束
【心电信号】基于matlab心率检测【含Matlab源码 1993期】
Chain Of Responsibility
吃透Chisel语言.31.Chisel进阶之通信状态机(三)——Ready-Valid接口:定义、时序和Chisel中的实现
pnpm + workspace + changesets 构建你的 monorepo 工程
MySQL - Index Optimization and Query Optimization
WebGPU 导入[2] - 核心概念与重要机制解读
随机推荐
从云计算到函数计算
Conditional constructor ~wapper
吃透Chisel语言.30.Chisel进阶之通信状态机(二)——FSMD:以Popcount为例
spark读取文件夹数据
暑假第五周总结
LeetCode刷题(7)
spark架构
Thesis understanding: "Cross-Scale Residual Network: A GeneralFramework for Image Super-Resolution, Denoising, and "
埋点开发流程
【故障诊断分析】基于matlab FFT轴承故障诊断(包络谱)【含Matlab源码 2002期】
mysql操作入门(四)-----数据排序(升序、降序、多字段排序)
【机器学习】实验6布置:基于集成学习的Amazon用户评论质量预测
MySQL database design specification
逆变器锁相原理及DSP实现
【机器学习】实验2布置:基于回归分析的大学综合得分预测
MySQL-多版本并发控制
Splunk Filed extraction field interception
使用hutool做本地缓存的工具类
Azure Synapse Analytics上创建用户并赋予权限
【机器学习】实验1布置:基于决策树的英雄联盟游戏胜负预测