常要oracle数据库定时的自动执行一些脚本,或做数据库备份,或做数据的提炼,或做数据库的性能优化,包括重建索引等等的工作,这时
常要Oracle数据库定时的自动执行一些脚本,或做数据库备份,或做数据的提炼,或做数据库的性能优化,包括重建索引等等的工作,这时需要用到一个函数dbms_job.submit,来完成Oracle定时器Job时间的处理上。使用dbms_job.submit这个函数,我们只需要考虑两个事情:安排某一任务,和定制一个执行任务的时间点。但最重要也是最棘手的事情,我认为还是确定一个执行任务的时间点。时间点确定了,其他的事情就好办了。下面是函数dbms_job.submit使用方法:
Java代码
1. dbms_job.submit( job out binary_integer,
2. what in archar2,
3. next_date in date,
4. interval in varchar2,
5. no_parse in boolean)
其中:
●job:输出变量,是此任务在任务队列中的编号;
●what:执行的任务的名称及其输入参数;
●next_date:任务执行的时间;
●interval:任务执行的时间间隔。
其中Interval这个值是决定Job何时,被重新执行的关键;当interval设置为null时,该job执行结束后,就被从队列中删除。假如我们需要该job周期性地执行,则要用‘sysdate+m’表示。如何更好地确定执行时间的间隔需要我们掌握一个函数TRUNC。
1.TRUNC(for dates)
TRUNC函数为指定元素而截去的日期值。
其具体的语法格式如下:
TRUNC(date[,fmt])
其中:
date 一个日期值
fmt 日期格式,该日期将由指定的元素格式所截去。忽略它则由最近的日期截去
下面是该函数的使用情况:
1)按年截尾
select TRUNC(TO_DATE('2008-03-01 08:23','yyyy-mm-dd hh:mi'),'yyyy') from dual
-----------------------------------------------------------
2008-1-1
2)按月截尾
select TRUNC(TO_DATE('2008-03-01 08:23','yyyy-mm-dd hh:mi'),'mm') from dual
--------------------------------------------------------
2008-3-1
3)按日截尾
select TRUNC(TO_DATE('2008-03-01 08:23','yyyy-mm-dd hh:mi'),'dd') from dual
----------------------------------------------------------------------
2008-3-1
4)按时截尾
select TRUNC(TO_DATE('2008-03-01 08:23','yyyy-mm-dd hh:mi'),'hh') from dual
----------------------------------------------------------------------
2008-3-1 8:00:00
5)按分截尾
select TRUNC(TO_DATE('2008-03-01 08:23','yyyy-mm-dd hh:mi'),'mi') from dual
----------------------------------------------------------------------
2008-3-1 8:23:00
2.确定执行时间间隔
1) 每分钟执行
Interval => TRUNC(sysdate,'mi') + 1 / (24*60) 或Interval => sysdate+1/1440
2) 每天定时执行
例如:每天的凌晨2点执行
Interval => TRUNC(sysdate) + 1 +2 / (24)
3) 每周定时执行
例如:每周一凌晨2点执行
Interval => TRUNC(next_day(sysdate,2))+2/24 --星期一,一周的第二天
Interval => TRUNC(next_day(sysdate,'星期一'))+2/24
4) 每月定时执行
例如:每月1日凌晨2点执行
Interval =>TRUNC(LAST_DAY(SYSDATE))+1+2/24
5) 每季度定时执行
例如每季度的第一天凌晨2点执行
Interval => TRUNC(ADD_MONTHS(SYSDATE,3),'Q') + 2/24
6) 每半年定时执行
例如:每年7月1日和1月1日凌晨2点
Interval => ADD_MONTHS(trunc(sysdate,'yyyy'),6)+2/24
7) 每年定时执行
例如:每年1月1日凌晨2点执行
Interval => ADD_MONTHS(trunc(sysdate,'yyyy'),12)+2/24
3.实例
这里提供了一个简单的例子,主要是完成在每一个时间间隔内向一个表中插入一条记录
1)创建测试表
Java代码
1.
2. SQL> create table test(id number,cur_time date);
3. 表已创建。
4. ----建sequence
5. CREATE SEQUENCE test_sequence
6. INCREMENT BY 1 -- 每次加几个
7. START WITH 1 -- 从1开始计数
8. NOMAXVALUE -- 不设置最大值
9. NOCYCLE -- 一直累加,不循环
10. CACHE 10 ;
--建触发器代码为:
Java代码
1. create or replace trigger tri_test_id
2. before insert on test --test 是表名
3. for each row
4. declare
5. nextid number;
6. begin
7. IF :new.id IS NULLor :new.id=0 THEN --id是列名
8. select test_sequence.nextval --SEQ_ID正是刚才创建的
9. into nextid
10. from sys.dual;
11. :new.id:=nextid;
12. end if;
13. end tri_test_id;
14.
2)创建一个自定义过程
Java代码
1. SQL> create or replace procedure proc_test as
2. begin
3. insert into test(cur_time) values(sysdate);
4. end;
5. /
6.
过程已创建。
3)创建JOB
Java代码
1. SQL> declare job1 number;
2. begin
3. dbms_job.submit(job1,'proc_test;',sysdate,'sysdate+1/1440');--每天1440分钟,即一分钟运行test过程一次
4. end;
PL/SQL JOB已成功完成。
----------------------------------------------
--1 、建立一个存储过程,转历史并删除,假设表名名:test,历史表:test_his(两表结构一样):如
CREATE OR REPLACE PROCEDURE delhisdata AS
BEGIN
INSERT INTO test_his
SELECT * FROM test WHERE ins_date
DELETE FROM test t WHERE ins_date
COMMIT;
EXCEPTION
WHEN OTHERS THEN
ROLLBACK;
END;
/
--1、数据库中建立一个JOB对存储过程进行调用,并且每月执行一次,,
DECLARE
jobno NUMBER;
BEGIN
DBMS_JOB.SUBMIT(JOB => jobno, /*自动生成JOB_ID*/
WHAT => 'delhisdata;', /*需要执行的过程或SQL语句*/
NEXT_DATE => TRUNC(SYSDATE + 1) + 2 / 24, /*初次执行时间*/
INTERVAL => 'TRUNC(add_months(SYSDATE,1))+2/24'); /*执行周期*/
COMMIT;
END;
/

InnoDBBufferPool reduces disk I/O by caching data and indexing pages, improving database performance. Its working principle includes: 1. Data reading: Read data from BufferPool; 2. Data writing: After modifying the data, write to BufferPool and refresh it to disk regularly; 3. Cache management: Use the LRU algorithm to manage cache pages; 4. Reading mechanism: Load adjacent data pages in advance. By sizing the BufferPool and using multiple instances, database performance can be optimized.

Compared with other programming languages, MySQL is mainly used to store and manage data, while other languages such as Python, Java, and C are used for logical processing and application development. MySQL is known for its high performance, scalability and cross-platform support, suitable for data management needs, while other languages have advantages in their respective fields such as data analytics, enterprise applications, and system programming.

MySQL is worth learning because it is a powerful open source database management system suitable for data storage, management and analysis. 1) MySQL is a relational database that uses SQL to operate data and is suitable for structured data management. 2) The SQL language is the key to interacting with MySQL and supports CRUD operations. 3) The working principle of MySQL includes client/server architecture, storage engine and query optimizer. 4) Basic usage includes creating databases and tables, and advanced usage involves joining tables using JOIN. 5) Common errors include syntax errors and permission issues, and debugging skills include checking syntax and using EXPLAIN commands. 6) Performance optimization involves the use of indexes, optimization of SQL statements and regular maintenance of databases.

MySQL is suitable for beginners to learn database skills. 1. Install MySQL server and client tools. 2. Understand basic SQL queries, such as SELECT. 3. Master data operations: create tables, insert, update, and delete data. 4. Learn advanced skills: subquery and window functions. 5. Debugging and optimization: Check syntax, use indexes, avoid SELECT*, and use LIMIT.

MySQL efficiently manages structured data through table structure and SQL query, and implements inter-table relationships through foreign keys. 1. Define the data format and type when creating a table. 2. Use foreign keys to establish relationships between tables. 3. Improve performance through indexing and query optimization. 4. Regularly backup and monitor databases to ensure data security and performance optimization.

MySQL is an open source relational database management system that is widely used in Web development. Its key features include: 1. Supports multiple storage engines, such as InnoDB and MyISAM, suitable for different scenarios; 2. Provides master-slave replication functions to facilitate load balancing and data backup; 3. Improve query efficiency through query optimization and index use.

SQL is used to interact with MySQL database to realize data addition, deletion, modification, inspection and database design. 1) SQL performs data operations through SELECT, INSERT, UPDATE, DELETE statements; 2) Use CREATE, ALTER, DROP statements for database design and management; 3) Complex queries and data analysis are implemented through SQL to improve business decision-making efficiency.

The basic operations of MySQL include creating databases, tables, and using SQL to perform CRUD operations on data. 1. Create a database: CREATEDATABASEmy_first_db; 2. Create a table: CREATETABLEbooks(idINTAUTO_INCREMENTPRIMARYKEY, titleVARCHAR(100)NOTNULL, authorVARCHAR(100)NOTNULL, published_yearINT); 3. Insert data: INSERTINTObooks(title, author, published_year)VA


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SublimeText3 Chinese version
Chinese version, very easy to use

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

WebStorm Mac version
Useful JavaScript development tools

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.