Pl/sql基础知识:
(1)pl/sql的块结构
(2)变量的声明
(3)字符值,字符和保留字
(4)pl/sql语言使用的数据类型
(5)隐藏代码的Wrpper实用程序
了解:
DDL:data definition language的缩写 包括:alter,create,drop,truncate,grant,revoke
DML:data manipulation language的缩写 包括:insert update delete
目前:oracle10g存储的数据量可以达到EB级,1EB=1024PB,1PB=1024TB,1TB=1024G,1G=1024M,1M=1024KB,1KB=1024B 1B=8b
pl/sql的数据类型:
标量类型:字符/字符串类型,数字类型,布尔类型,日期/时间类型
引用类型:ref cursor(游标变量),ref(可以理解为一个指针,只是该指针指向的内容是对象表,或对象视图里的一个对象实例)
复合类型:记录,嵌套表,index-by表,varrays
LOB大对象类型:oracle 10前可以存储小于4G的二进制或字符数据,oracle 10可以存储最大长度是:8T-128T
代码块的基本结构:
begin
null;--如果没有null会出现异常
end
----------------------
--匿名块的实例:
CREATE TABLE authors (
id NUMBER PRIMARY KEY,
first_name VARCHAR2(50),
last_name VARCHAR2(50)
);
CREATE TABLE books (
isbn CHAR(10) PRIMARY KEY,
category VARCHAR2(20),
title VARCHAR2(100),
num_pages NUMBER,
price NUMBER,
copyright NUMBER(4),
author1 NUMBER CONSTRAINT books_author1
REFERENCES authors(id),
author2 NUMBER CONSTRAINT books_author2
REFERENCES authors(id),
author3 NUMBER CONSTRAINT books_author3
REFERENCES authors(id)
);
CREATE TABLE inventory (
isbn CHAR(10) CONSTRAINT fk_isbn REFERENCES books (isbn),
status VARCHAR2(25) CHECK (status IN ('IN STOCK', 'BACKORDERED', 'FUTURE')),
status_date DATE,
amount NUMBER
);
INSERT INTO authors (id, first_name, last_name)
VALUES (1, 'Marlene', 'Theriault');
INSERT INTO authors (id, first_name, last_name)
VALUES (2, 'Rachel', 'Carmichael');
INSERT INTO authors (id, first_name, last_name)
VALUES (3, 'James', 'Viscusi');
INSERT INTO books (isbn, category, title, num_pages, price, copyright, author1, author2, author3)
VALUES ('72121203', 'Oracle Basics', 'Oracle DBA 101', 563, 39.99, 1999, 1, 2, 3);
INSERT INTO inventory (isbn, status, status_date, amount)
VALUES ('72121203', 'BACKORDERED', TO_DATE('06-JUN-2004', 'DD-MON-YYYY'), 1000);
SET SERVEROUTPUT ON ESCAPE OFF--当退出该匿名块时就关闭SERVEROUTPUT
DECLARE
v_first_name authors.first_name%TYPE; --声明和定义变量
v_last_name authors.last_name%TYPE;
v_row_count PLS_INTEGER := 0;
CURSOR auth_cur IS --声明游标
SELECT a.first_name, a.last_name, count(b.title)
FROM authors a, books b
WHERE a.id = b.author1
OR a.id = b.author2
OR a.id = b.author3
GROUP BY a.first_name, a.last_name
HAVING count(b.title) > 0
ORDER BY a.last_name;
BEGIN
DBMS_OUTPUT.ENABLE(1000000);
OPEN auth_cur; --打开游标
LOOP
FETCH auth_cur INTO v_first_name, v_last_name, v_row_count;--推进游标
EXIT WHEN auth_cur%NOTFOUND; --当没有发现数据时,推出
DBMS_OUTPUT.PUT_LINE(v_last_name
||', '
||v_first_name
||' wrote '
||v_row_count
||' book(s).');
END LOOP;
CLOSE auth_cur; --关闭游标
EXCEPTION --异常处理部分
WHEN OTHERS --others表示其它别的异常
THEN
DBMS_OUTPUT.PUT_LINE(SQLERRM);
END;