oracle(三)

oracle基础三

oracle常用的函数

字符函数
1. concat(str1, str2) 字符串拼接的函数
2. length(str) 返回表达式中的字符数1
3. lower(str) 将字符串转化为小写
4. upper(str) 将字符串转化为大写
5. trim(str from x) 从字符串两侧去除指定的字符串

数值函数
1. abs(value) 返回value的绝对值
2. ceil(value) 返回大于等于value的最小整数
3. floor(value) 返回小于等于value的最大整数
4. trunc(value, n) 对value进行截断
5. round(value, n) 对value进行四舍五入保存小数点右侧的n为

转换函数
1. to_char(x) 将x转化为字符串
2. to_number(x) 将x转化为数字
3. cast(x as type) 将x转换为指定的兼容的数据库类型。
4. to_date(x) 将x字符串转换为日期。

聚集函数

(1) 常用函数
	1. avg(x) 返回x的平均值
	2. count(x) 返回统计的行数
	3. max(x) 返回x的最大值
	4. min(x) 返回x的最小值
	5. sum(x) 返回x的总计值
	
分组后需要使用条件进行筛选,则使用having过滤分组后的行,不能使用where,where只能放在group by前面。

基本用法

--创建视图
create view v_stu as select * from stu;
--查询视图
select * from v_student;

--定义存储过程: 实现年龄
create or replace procedure pro_age(birthday in date, age out number)
is
begin
age := floor(to_number(to_char(sysdate,'yyyy'))-to_number(to_char(birthday,'yyyy')))
end;
--调用存储过程
DECLARE 
btime date;
age NUMBER;
begin
	select ctime into btime from t_log where id=2;
	pro_age(btime,age);
	dbms_output.put_line(age);
end;

--创建函数 实现年龄的计算
create or replace function fun_age(birthday date) return number
is
	age number := 0
	begin
		age:=floor(to_number(to_char(sysdate,'yyyy'))-to_number(to_char(birthday,'yyyy')));
		return age;
end;

select fun_age(ctime) from t_log;

-- 保留两位小数 无论是否整除
create or replace function fun_money2(money number) return varchar2
is
	m number :=0
	res varchar2;
	i number :=0
	l number :=0
begin
	m: =money/100
	res:= to_char(m);
	i:=instr(res,'.');
	l:=length(res);
	l:=l-i;
	if i=0 then res:=res||'.00';
	else if l=1 then res:=res||'0';
	end if;
	return res;
-- 创建程序包
create package pk_math
is
	 PROCEDURE pro_qh(num1 in NUMBER,num2 in NUMBER,num3 out NUMBER);
	 FUNCTION fun_qc(num1 NUMBER,num2 NUMBER) return NUMBER;
end;

--实现程序包
create package body pk_math
is
	PROCEDURE pro_qh(num1 in NUMBER,num2 in NUMBER,num3 out NUMBER)
	is
	begin
		num3:=num1+num2;
	end;

	FUNCTION fun_qc(num1 NUMBER,num2 NUMBER) return NUMBER
	is
		n NUMBER :=0;
	begin
		n:=num1-num2;
		return n;
	end;
end;
--调用程序包
DECLARE
	n1 NUMBER :=0; --定义变量
	n2 NUMBER :=0;
begin
	pk_math.pro_qh(23,44,n1);
	dbms_output.put_line(n1);
	select pk_math.fun_qc(33,11) into n2 from t_log where id=101;
	dbms_output.put_line(n2);
end;

-- 创建索引
create index i_stu on stu(sname);
-- 重新构建索引
alter index i_stu rebuild;
-- 分析索引空间
analyze index i_stu validate structure

oracle 中的索引

1. 索引: 数据库的对象,是为了加快查询的速度
2. 索引的底层实现结构:
		1. B-Tree 索引
			二叉树
		2. Bit 位图索引
        	采用Bit(0或1)
3. 索引的分类
	1.普通索引
	2.主键索引
	3.唯一索引
	4.复合索引
索引是为了提高查询速度,查询算法
最基本的查询算法:顺序查找
二分查找算法
二叉树查询算法
B-Tree/B+Tree

格式:
create [bit][unique] index 索引名称

总结

今天的就到这里了。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值