当前位置:网站首页>oracle 函数 触发器
oracle 函数 触发器
2022-06-25 09:32:00 【kjshuan】
#学习心德
学一个新知识时 先百度百科
函数
# 函数有返回值 return # create or replace function func_calsale(gdid orders.gdid%type) return number as salenum number(20); begin select sum(buynum) into salenum from orders o where o.gdid=gdid; return salenum; end; # @ /opt/my.sql # select func_calsale(2) from dual;
触发器
#存放在数据库中,并被隐含执行的存储过程。在 Oracle8i 之前,只允许给予表或者视图的 的 DML 的操作,而从 Oracle8i 开始,不仅可以支持 DML 触发器,也允许给予系统事件和 DDL 的操作 #触发器是在事件发生时隐式地自动运行的PL/SQL程序块,不能接收参数,不能被调用。 #每当一个特定的数据操作语句(insert update delete)在指定的表上发出时, Oracle自动执行触发器中定义的语句序列。
创建表 实现触发器
# create table orderhistory(oid int primary key,sid int,modifydate date, buynum int);
# create sequence seq_oh;
# @ /opt/my.sql
# set serveroutput on
# update orders set buynum=2 where oid=4;
# update orders set buynum=2;
# rollback;
#-- on 绑在表身上
create or replace trigger trig_order_mod
before update on orders
-- for each row 多行更新都触发
for each row
begin
dbms_output.put_line('Hello');
end;
/
创建表2
create table custs(id int primary key,name varchar(20) not null);
create table cards(id int primary key,trantype int,money
number(10,2));
alter table cards add custid int not null;
alter table cards add constraint FK_cust_card foreign key(custid) references custs(id)
#-----------------------------------------------#
#创建序列
create sequence seq_cust;
create sequence seq_card;
@ /opt/my.sql
insert into custs values(seq_cust.nextval,'zs');
commit;
select * from custs;
#-----------------------------------------------#
create or replace trigger trig_bank
after insert on custs
for each row
begin
insert into cards values(seq_card.nextval,1,1,:new.id);
end;
/
#-----------------------------------------------#
desc orderhistory;
create sequence seq_his;
@ /opt/my.sql
select * from orderhistory;
#-----------------------------------------------#
create table myorders(id int primary key, ordid int, status int,starttime datetime, overtime datetime);
create table xyz(id int primary key, dt timestamp );
insert into xyz values(1,sysdate);
commit;
select * from xyz;
alter table xyz modify dt timestamp(2);
delete from xyz;
commit;
#-----------------------------------------------#
oracle 时间戳转换日期
select to_date(dt,'yyyy-MM-dd hh24:mi:ss') from xyz;
select to_char(dt,'YYYY-MM-DD hh24:mi:ss') from xyz;
select to_date(to_char(dt,'YYYY-MM-DD hh24:mi:ss')) from xyz;
select to_timestamp('9999-12-31 23:59:59','YYYY-MM-DD hh24:mi:ss') from dual;
#-----------------------------------------------#
create table myorders(id int primary key,orderid int, status int ,starttime timestamp(0),overtime timestamp(0));
desc myorders;
#-----------------------------------------------#
select * from xyz where to_char(dt,'YYYY-MM-DD')='2022-06-24';
#-----------------------------------------------#
开始测试
@ /opt/my.sql
desc myorders;
insert into myorders values(1,1,1,sysdate,to_timestamp('9999-12-31 1:0:0','YYYY-MM-DD hh:mi:ss'));
rollback;
######## 显示输出
@ /opt/my.sql
select * from myorders;
insert into myorders values(1,1,1,sysdate,to_timestamp('9999-12-31 1:0:0','YYYY-MM-DD hh:mi:ss'));
insert into myorders values(2,1,2,sysdate,to_timestamp('9999-12-31 1:0:0','YYYY-MM-DD hh:mi:ss'));
select * from myorders;
#-----------------------------------------------#
create or replace trigger trig_myorder
before insert on myorders
for each row
begin
update myorders set overtime=sysdate where ORDERID=:new.orderid
and to_char(overtime,'YYYY-MM-DD')='9999-12-31';
-- dbms_output.put_line(:new)
end;
/
######## 上面是vs代码删除触发器
create or replace procedure proc_myorder
(ordid myorders.orderid%type,ordstatu myorders.status%type)
as
begin
update myorders set ovetime=sysdate where ORDERID=ordid
and to_char(ovetime,'YYYY-MM-DD')='9999-12-31';
insert into myorders values(seq_mo.nextval,ordid,ordstatu
,sysdate,to_timestamp('9999-12-31 1:0:0','YYYY-MM-DD hh:mi:ss'));
end;
/
#-----------------------------------------------#
drop trigger trig_myorder;
delete from myorders;分区
#给数据分区 #加快查询效率 #方法如下 传统表分区 2.1.1范围分区range2.1.2 列表分区list2.1.3 哈希分区hash2.1.4 复合分区 range + list or hash2.2 11g 新特性分区 2.1.1引用分区 reference 2.1.2 间隔分区interval
创建分区表
范围分区list2.1.3
drop table stus; create table stus(stid int primary key,name varchar(20),age int) partition by range(age)( partition p1 values less than(20) tablespace mydemo, partition p2 values less than (50) tablespace mydemo, partition p3 values less than (maxvalue) tablespace mydemo);#测试
insert into stus values(1,'zs',18); insert into stus values(2,'ls',19); select * from stus partition(p1); select * from stus partition(p2); select * from stus partition(p1,p2);
删除表
drop table stus;
哈希分区hash2.1.4
create table stus (id int primary key,name varchar(20),age number) partition by hash(name)( partition p1 tablespace mydemo, partition p2 tablespace mydemo, partition p3 tablespace mydemo ); insert into stus values(1,'zs',18); insert into stus values(2,'zsf',100); select * from stus partition(p1); select * from stus partition(p2); select * from stus partition(p3); insert into stus values(3,'lisi',22); insert into stus values(4,'lisiguang',1221); select * from stus partition(p3);
存在热点问题
用函数再次hash法 加盐? 随机值做尾巴
解决代码
create table stus1(id int primary key,name varchar(20),age int,hashname varchar(30)) partition by hash(hashname)( partition p1 tablespace mydemo, partition p2 tablespace mydemo, partition p3 tablespace mydemo ); insert into stus1 values(1,'zs',10,trunc(dbms_random.value(1,1000))||'zs');
列表分区list2.1.3
树状结构
create table addrs(id int primary key,name varchar(20),subid int); insert into addrs values(1,'chian',0); insert into addrs values(2,'jiangsu',1); insert into addrs values(3,'zhejiang',1); insert into addrs values(4,'nanjing',2); insert into addrs values(5,'xuzhou',2); insert into addrs values(6,'hangzhou',3); insert into addrs values(7,'jiaxing',3);
级联(树状)查询
输一个名字显示关联的名字
方式1
--创建函数
create or replace function fun_linked(city addrs.name%type) return varchar
as
--定义变量
res varchar(100);
nm varchar(20);
suid varchar(10);
begin
--res接收到传入的city
res:=city;
select name,subid into nm,suid from addrs where name=city;
-- 开始循环 ==0直接返回
while (suid != 0) loop
select name,subid into nm,suid from addrs where id=suid;
-- ==后面的res是之前的城市 nm是后面查出的城市
res:= res || ',' || nm ;
end loop;
--返回res数据里面有一个或者多个city
return res;
end;
/
#-----------------------------------------------#
@ /opt/my.sql
select fun_linked('xuzhou') from dual;
方式2
select * from addrs start with id=1 connect by id=prior subid; select * from addrs start with id=2 connect by prior id=subid;
create or replace function fucs(city addrs.name%type)return
varchar
as
flag varchar(100);
aname varchar(20);
asubid varchar(20);
begin
flag:=city;
select name,subid into aname,asubid
from addrs
where name=city;
while(asubid !=0) loop
select name,subid into aname,asubid
from addrs
where id=asubid;
flag:=flag || ' +++++ ' || aname
end loop;
return flag;
end;
/
边栏推荐
- [zero foundation understanding innovation and entrepreneurship competition] overall cognition and introduction of mass entrepreneurship and innovation competition (including FAQs and integration of bl
- matplotlib matplotlib中axvline()和axhline()函数
- Wallys/MULTI-FUNCTION IPQ6010 (IPQ6018 FAMILY) EMBEDDED BOARD WITH ON-BOARD WIFI DUAL BAND DUAL
- Register the jar package as a service to realize automatic startup after startup
- 【mysql学习笔记21】存储引擎
- Wechat official account can reply messages normally, but it still prompts that the service provided by the official account has failed. Please try again later
- [matlab] image binarization (imbinarize function)
- socket编程——epoll模型
- [project part - structure and content writing of technical scheme] software system type mass entrepreneurship and innovation project plan and Xinmiao guochuang (Dachuang) application
- The meshgrid() function in numpy
猜你喜欢

首期Techo Day腾讯技术开放日,628等你!

Voiceprint Technology (VI): other applications of voiceprint Technology

matplotlib matplotlib中决策边界绘制函数plot_decision_boundary和plt.contourf函数详解

行业春寒回暖,持续承压的酒店企业于何处破局?
![[learn C from me and master the key to programming] insertion sort of eight sorts](/img/2c/22e065390464ed5cd1056346d46573.jpg)
[learn C from me and master the key to programming] insertion sort of eight sorts

Matplotlib axvline() and axhline() functions in Matplotlib

SQL高级

Solution to the problem of repeated startup of esp8266

Analysis on the thinking of 2022 meisai C question

Wallys/MULTI-FUNCTION IPQ6010 (IPQ6018 FAMILY) EMBEDDED BOARD WITH ON-BOARD WIFI DUAL BAND DUAL
随机推荐
Title B of the certification cup of the pistar cluster in the Ibagu catalog
首期Techo Day腾讯技术开放日,628等你!
3 big questions! Redis cache exceptions and handling scheme summary
[opencv] - Discrete Fourier transform
Level 6 easy to mix words
8、智慧交通项目(1)
The first techo day Tencent technology open day, 628 waiting for you!
Is it safe to open a stock account through the account opening QR code of the account manager?
Matplotlib decision boundary drawing function plot in Matplotlib_ decision_ Boundary and plt Detailed explanation of contour function
股票在线开户安全吗?找谁可以办理?
35 websites ICER must know
Voiceprint Technology (I): the past and present life of voiceprint Technology
请问在手机上开户股票,还是去证券公司开户安全?
【mysql学习笔记22】索引
Prediction of pumpkin price based on BP neural network
Webgl Google prompt memory out of bounds (runtimeerror:memory access out of bounds, Firefox prompt index out of bounds)
Is the security account opening risky and safe?
x86的编码格式
通过客户经理的开户二维码开股票账户安全吗?
2021mathorcupc topic optimal design of heat dissipation for submarine data center