当前位置:网站首页>Oracle function trigger
Oracle function trigger
2022-06-25 09:36:00 【kjshuan】
# Learning morality
When learning a new knowledge First Baidu Encyclopedia
function
# Function has return value 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;
trigger
# Stored in a database , Stored procedures that are implicitly executed . stay Oracle8i Before , Only tables or views are allowed Of DML The operation of , And from Oracle8i Start , Not only can it support DML trigger , It is also allowed to give system events and DDL The operation of # Triggers implicitly run automatically when an event occurs PL/SQL Block , Cannot receive parameter , Cannot be called . # Whenever a particular data manipulation statement (insert update delete) When issued on a specified table , Oracle Automatically execute the sequence of statements defined in the trigger .
Create table Implement triggers
# 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 Tied to the watch
create or replace trigger trig_order_mod
before update on orders
-- for each row Multiple line updates trigger
for each row
begin
dbms_output.put_line('Hello');
end;
/
Create table 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
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 Timestamp conversion date
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';
#-----------------------------------------------#
Start testing
@ /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;
######## display output
@ /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;
/
######## It's on it vs Code Delete trigger
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;Partition
# Partition data # Speed up query efficiency # The method is as follows Traditional table partition 2.1.1 Range partitioning range2.1.2 List partition list2.1.3 Hash partition hash2.1.4 Composite partition range + list or hash2.2 11g New feature partitions 2.1.1 Reference partition reference 2.1.2 Interval partition interval
Create a partition table
Range partitioning 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);# test
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);
Delete table
drop table stus;
Hash partition 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);
There are hot issues
Use the function again hash Law Add salt ? Random values do tail
Solve the code
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');
List partition list2.1.3
Tree structure
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);
cascade ( Trees ) Inquire about
Enter a name to display the associated name
The way 1
-- Create a function
create or replace function fun_linked(city addrs.name%type) return varchar
as
-- Defining variables
res varchar(100);
nm varchar(20);
suid varchar(10);
begin
--res Received incoming city
res:=city;
select name,subid into nm,suid from addrs where name=city;
-- Start the cycle ==0 Go straight back to
while (suid != 0) loop
select name,subid into nm,suid from addrs where id=suid;
-- == hinder res It was the city before nm It's the city found out later
res:= res || ',' || nm ;
end loop;
-- return res There is one or more in the data city
return res;
end;
/
#-----------------------------------------------#
@ /opt/my.sql
select fun_linked('xuzhou') from dual;
The way 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;
/
边栏推荐
- 汇付国际为跨境电商赋能:做合规的跨境支付平台!
- 高速缓冲存储器Cache的映射方式
- Explanation of assertions in JMeter
- The first techo day Tencent technology open day, 628 waiting for you!
- I put a two-dimensional code with rainbow candy
- [shared farm] smart agriculture applet, customized development and secondary development of Kaiyuan source code, which is more appropriate?
- [project part - structure and content writing of technical scheme] software system type mass entrepreneurship and innovation project plan and Xinmiao guochuang (Dachuang) application
- [zufe school competition] difficulty classification and competition suggestions of common competitions in the school (taking Zhejiang University of Finance and economics as an example)
- The problem of wirengpi program running permission
- How do dating applets make millions a year? What is the profit model?
猜你喜欢

Online notes on Mathematics for postgraduate entrance examination (9): a series of courses on probability theory and mathematical statistics

Study on correlation of pumpkin price and design of price prediction model based on BP neural network

Analysis on the bottom calling process of micro service calling component ribbon

matplotlib 简单逻辑回归可视化

备战2022年金九银十必问的1000道Android面试题及答案整理,彻底解决面试的烦恼

C语言刷题随记 —— 猴子吃桃

matplotlib matplotlib中plt.grid()

高速缓冲存储器Cache的映射方式

微服务调用组件Ribbon底层调用流程分析

Oracle-单行函数大全
随机推荐
matplotlib matplotlib中axvline()和axhline()函数
A game WP
Voiceprint Technology (I): the past and present life of voiceprint Technology
【mysql学习笔记20】mysql体系结构
在指南针上面开户好不好,安不安全?
【mysql学习笔记22】索引
[competition -kab micro entrepreneurship competition] KAB National College Students' micro entrepreneurship action participation experience sharing (including the idea of writing the application form)
[opencv] - Discrete Fourier transform
1、 Construction of single neural network
首期Techo Day腾讯技术开放日,628等你!
Voiceprint Technology (I): the past and present life of voiceprint Technology
Flutter multilingual intl: ^0.17.0 cannot be imported
Is it safe to open a stock account on the compass?
Voiceprint Technology (VI): other applications of voiceprint Technology
[competition - Rural Revitalization] experience sharing of Zhejiang Rural Revitalization creative competition
[buuctf.reverse] 117-120
[untitled] * * database course design: complete the student information management system in three days**
Oracle-单行函数大全
sklearn 高维数据集制作make_circles 和 make_moons
How to delete a blank page that cannot be deleted in word