SQL文档

本文介绍了SQL中的关系型与非关系型数据库,如MySQL、Oracle、MongoDB等,并详细阐述了数据库的核心操作如增删改查,以及性能指标QPS和TPS。此外,还提到了数据库备份、查询优化和各种数据类型。
摘要由CSDN通过智能技术生成

SQL分为关系型数据库和非关系型数据库

关系型数据库主要有 MySQL、Oracle、 SQL Server、SQLite(非常轻量的数据库,只有2M、用户android)、PostgreSQL

非关系型数据库有 Redis(缓存数据库、键/值数据库)、MongoDB(数据存在硬盘中,经常读取的数据会被加载到内存中)、Elasticsearch(存储的主要目的是搜索)

QPS: QueriesPerSecond意思是“每秒查询率”,是一台服务器每秒能够相应的查询次数,是对一个特定的查询服务器在规定时间内所处理流量多少的衡量标准。

TPS: 是 TransactionsPerSecond的缩写,也就是事务数/秒。它是软件测试结果的测量单位。客户机在发送请求时开始计时,收到服务器响应后结束计时,以此来计算使用的时间和完成的事务个数。

Tips: 最好不要在主库上数据库备份,大型活动前取消这样的计划。

**数据库核心操作 就是增删改查 查询不用 尽量不要用 *查询 **

常用数据类型(典型MySQL为例)

### 整数     int    bigint                 									对应   Integer  Long 对象类型
### 字符串   varchar(n)  指n个汉字或字母       最大字符数 21801  	65535个字节			
### 长文本   text(存储 论述等长文本)      	   最大字符数 21801  	65535个字节				二者都对应 String类型 
### 时间     datetime(存储日期和时间)         timestamp  时间戳       date 只存日期       time 只存 时间        对应java.util.Date类型
### 浮点数   decimal(长度,小数位数)   DECIMAL     用于钱的表示 整数位数 为长度-小数位数      对应BigDecimal类型
### 二进制存储 blob(不常用)

查询操作(涉及到两表查询操作)

select * from user;                            ### 字符串加 '' 单引号,数字不用    字段一般不加 ''  查询字段最好加上表名或者表别名
select user.user_name from user;

as(两种形式)

###   字段 as 别名        字段 别名
select a.user_name from user a;							 ### 一张表可以当做两张表查询
select a.user_name,a.password from user as a,user as b;  ###使用过 as 别名后 不可以在用原名user了

select a.user_name from (select * from user where id>10) as a           ### 必须使用as别名才能嵌套查询

##起别名注意点
where后面不可以跟别名 (还未生成结果集)		如果where后面跟别名那么先加 ()生成别名字段 
order by可以跟别名(已经生成结果集)

IN操作符

### IN操作符允许where子句中 有多个值
select * from user where id IN(1,2)

and or(并且 或者)

select a.user_name from user a where (a.id LIKE %100% and a.birthday>'2000-01-01 15:20:00')   ### 括号优先级最高
### 优先级 ()> not > and > OR

**like(模糊查询) not like(不相似) ** regexp (正则表达式)

###   %  0个或者多个字符          _  一个字符           [a,b,c] 数组中任意一个字符          ^[a,b,c]  不包含数组中任意一个字符
select a.user_name from user a where a.user_name LIKE '%li_';    ###like后 是字符串   配合CONCAT() 字符串链接函数使用
select a.user_name from user a where a.user_name REGEXP '春'

**between and not between and > < != >= <= **

select a.birthday not between '2000-01-01 00:00:00' and '2010-12-30 23:59:59'       ### between  查询结果 包括两边边界值     not between 不包括两边边界值

order by 排序 asc(正序,默认) desc(倒序) 可多条件排序(每个排序字段后都跟desc或者asc)

select * from user order by create_time,id desc
select * from book order by price desc ,bookid desc     ### 多字段排序时 ,分开 从左向右过渡  主->次

group by 聚合 分组

select count(id),sex from book group by sex      ###必须配合聚合函数一起使用 count()  sum()  avg()

### WITH ROLLUP  在group by分组的基础上在进一步统计 (使用原来的 函数,在最后一行加上 总结果数)
select age, count(age) from  user group by age WITH ROLLUP;

left join 两表联合查询 INNER join RIGHT join(右连接)

select * from teacher left join student ON teacher.id=student.teacher_id          ###查询左表所有数据和 右表匹配到的数据  如果匹配条件值是唯一的,就是查询左表及左表 补充信息 
###注意  left join比子查询效率高一点

is null is not null 筛选条件 为null或者不为null (不能使用!=null 或者 =null)

distinct 不同的

select count(distinct age) from user            
select distinct age,phone from user        ### distinct 后可跟多个字段   必须在表的开头

with语句

with t as ( select * from teacher ) select * from t						###给某一段sql起别名,减少sql代码复用

limit 分页查询使用

select * from user limit 0,1                 ###从第0条开始查询,查询一条数据

子查询 in(在内) exists(存在) any (任何) all (所有)

### from型子查询  把()内当临时表
###  where型子查询 把内部查询结果做为外部查询条件

select * from user where age in (12,14,16,18);  ###exists  any   all均可以用这种形式  或者 not in

select * from user where age in(select max(age) user);				 ###in子查询:查询年龄最大的 用户信息 
select * from user where exists (select age from user where age>21); ###exists子查询:查询是否存在年龄大于21岁的员工的信息
select * from user where id> any (select id from user where age>21); ###查询任何年龄大于21岁的用户信息
select * from user where id> all (select id from user where age>21); 



### 根据某一相同字段查询另一字段极值 及其他数据  类似于Group By但是可以查询所有字段
select a.* from user a where not exists(select 1 from user where name = a.name and age > a.age)


exists ###用于检查子查询是否至少会返回一行数据,该子查询实际上并不返回任何数据,而是返回值True或False    可以配合case when then 使用
### 判断是sql结果行数是否大于0 exists()

case when then else end

### case when then else 语法,在 where执行后,转换结果使用     where比较还是原字段的值不是转换后的值  MySQL Oracle  SQLServer  PGSQL通用
(case when treeLevel='treeLevel-2' and treeLevel = "treeLevel-3"  then 'treeLevel-1' else 'treeLevel-2' end) as treeLevel   

(case  
    when condition then result
    when condition then result
    when condition then result
else result
end) new_column_name


### 判断是否存在  exists()返回 bool值
(case when exists(select id from table where userId='123456') then '1' else '0' end) isRead,

### case when then 用于自定义排序
select id,name from student order by (case when '小明' then '1' else name end) asc, id desc

函数(一般大写)

avg()   	###平均数  
sum()   	###求和
count()     ###求数量
max()		###返回指定列的最大值
min()		###最小值
first()		###返回指定列中第一个记录的值
last()      ###最后一个值

concat()     ###拼接字符串函数  所有SQL通用
group_concat()   ### 将一列查询结果拼接 (Mysql适用   Oracle不支持)

mid(column_name,start,length)       ###截取字符串 从字段返回结果截取子字符串
len(column_name)					###返回字符串长度   MySQL中为length()
now()             ###返回系统当前日期和时间  oracle中为sysdate
format(column_name,format)			###返回格式化的日期或者字符串
replace(column_name,' ','')       ###替换字符串函数 (删除所有空格)

nvl(column_name,0)      ###判断是否为空,如果为空则返回0

##字符串相关函数
substring(name,2,length(name)-1))
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值