(5)数据库--sql

在这里插入图片描述
字符串数据类型:
char(n):定长
varchar(n):按照实际长度存储

数据存储:
numeric(p,d):
p:整数和小数部分的数据长度
d:小数部分的精度

当定义约束条件为primary key 之后,不需要额外定义 unique 和 not null

alter table tableName add
alter table tableName drop A

属性不区分大小写

distinct + 属性 去重
all + 属性 保留重复
不写distinct && all 默认为all

between and 为闭区间

from borrower, loan 为笛卡尔乘积的运算

重命名:
当在select语句中使用as相当于为rename,但是在from子句中使用as, 相当于将原来的表重新拷贝了一份新的表在临时表空间中,当查询语句执行完时,此表将会被释放

string 模式匹配
%:任意长度字串
_:任意一个字符

转义字符
‘Main#%’ escape ‘#’

字符串的拼接:
||
e.g.
select name||male from student
为name 和 male 拼接在一起

排序:
order by name asc||desc
asc 可以省略
排序时注意排序额外空间的利用, 不能让结果集很大

并集
union all

交集
intersect

差集
except
e.g.
(select name from a)
except || union || intersect all
(select name form b)

聚集函数:忽略null值
avg()
min()
max()
sum()
count()
group by : group by 子句后面的列必须出现在select子句中

Having子句
类似于where子句,但是having对于使用聚集函数的列进行判断,也可以对于自然属性进行判断,但是where子句用于group by子句前,having子句在group by子句后判断

Null 值
e.g.
select number from loan where number is null
5+null return null
null>null
5<null
null = null
return null
在这里插入图片描述
在这里插入图片描述
not unkown = unknown
聚集函数中忽略空值,处理count(*)
在逻辑运算中,null视为false

子查询:

select distinct customer_name 
from borrower, loan
where borrower.loan_number = loan.loan_number
and branch_name = 'P' and
(branch_name, customer_name) in
(select branch_name, customer_name
from depositer, account
where depositor.account_number = account.account.number)

在这里插入图片描述
因为一个表中的指针只有一个,所以可以通过as复制表结构到临时表中,从而在一个表中进行比较

还可以:
在这里插入图片描述

some:
在这里插入图片描述
= some和 in 等价
在这里插入图片描述

select branch_name from branch where assets > all (select assets from branch where branch_name = 'Brooklyn')

当比较的时候最好加上some||all,因为只能一对一,除非用没有group by 的聚集函数

Exists:
判断子查询的返回值是否空值

select customer_name from branch where customer_name exists 
(select customer_name from branch where branch_name = 'Brooklyn')

在这里插入图片描述
外层查询的时候对于里层查询相当于常量、
except相当于两个集合做了差集

unique:
判断子查询返回的值是否有重复的值

在这里插入图片描述

select T.customer_name from depositor as T 
where unique(
select R.customer_name from account, depositor as R 
where T.customer_name = R.customer_name
and R.account_number = account.account_number
and account.branch_name = 'Perryridge'
)

with 子句:
相当于创建了一个新的临时表 简化查询

with max_balance(value) as 
select max(balance) from account
select account_number from account, max_balance
where account.balance = max_balance.value

在这里插入图片描述

with branch_total(branch_name, value) as
	select branch_name, sum(balance) from account
	group by branch_name
with branch_total_avg(value) as 
	select avg(value)
	from branch_total
select branch_name
from branch_total, branch_total_avg
where branch_total.value >= branch_total_avg.value

聚集函数不能嵌套

update
在这里插入图片描述

update account set balance = balance * 1.06 where balance >= 10000 

update account set balance = balance * 1.05 where balance < 10000 

或者

update account 
set balance = case 
	when balance > 10000 then
		balance*1.06
	else
		balance * 1.05
	end

delete:
在这里插入图片描述

delete from depositor where account_number in
(select account_number from account where balance = 0)

select account_number from account where balance = 0

view
将一些常用的临时表存储在虚表中,实际存储的时查询语句
e.g.
在这里插入图片描述

create view all_customer as (
select branch_name, customer_name 
from depositor, account
where depositor.account_number = account.account_number)
union
(select branch_name, customer_name 
from borrower, account
where borrower.account_number = account.account_number)

# using view
select customer_name from all_customer

view insert

insert into all_acutomer values(
'John', '123')

可更新的视图:
哪些视图可以更新:
单表视图
视图中包含主键,不能使主键非空
不能包含聚集函数

insert

insert into account(branch_name)
values('P')

在这里插入图片描述
在这里插入图片描述

insert into depositor
select customer_name, loan_number 
from loan, borrower
where branch_name = 'p'
and loan.account_number = borrower.account_number

内连接外连接
natural 相当于去掉了on 条件
在这里插入图片描述

发布了89 篇原创文章 · 获赞 0 · 访问量 1595

猜你喜欢

转载自blog.csdn.net/qq_43410618/article/details/104649432