create table tablename(
id number not null primary key,
name varchar(20),
create_time date,
total_consumption number(4,2)
);
insert into tablename values(222,'shuifenglin','01-dec-95',4756.50);
update tablename
set total_consumption=8888.56
where id=222;
delete from tablename
where id=222;
select distinct name from tablename
where name like '%lin%'; // '%lin'查詢以lin結尾,'lin%'查詢以lin開頭
select查詢語句以後單獨寫
* 大寫為語法部分,小寫是自己定義的部分 *
create or replace procedure factorial (num in number)
return number
asbegin
if num=0
then return 1;
else return num*factorial(num-1);
end if;
end;
游標是sql的乙個記憶體工作區,由系統或使用者以變數的形式定義。游標的作用就是用於臨時儲存從資料庫中提取的資料塊。在某些情況下,需要把資料從存放在磁碟的表中調到計算機記憶體中進行處理,最後將處理結果顯示出來或最終寫回資料庫。這樣資料處理的速度才會提高,否則頻繁的磁碟資料交換會降低效率。顯式游標主要是用於對查詢語句的處理,尤其是在查詢結果為多條記錄的情況下;游標有兩種型別:顯式游標和隱式游標。我們常用到的select…into…查詢語句,一次只能從資料庫中提取一行資料,對於這種形式的查詢和dml操作,系統都會使用乙個隱式游標。但是如果要提取多行資料,就要由程式設計師定義乙個顯式游標,並通過與游標有關的語句進行處理。顯式游標對應乙個返回結果為多行多列的select語句。 游標一旦開啟,資料就從資料庫中傳送到游標變數中,然後應用程式再從游標變數中分解出需要的資料,並進行處理。
而對於非查詢語句,如修改、刪除操作,則一般使用隱式游標。隱式游標由oracle 系統自動地為這些操作設定游標並建立其工作區,隱式游標的名字為sql,這是由oracle 系統定義的。
對於隱式游標的操作,如定義、開啟、取值及關閉操作,都由oracle 系統自動地完成,無需使用者進行處理。使用者只能通過隱式游標的相關屬性,來完成相應的操作。在隱式游標的工作區中,所存放的資料是與使用者自定義的顯示游標無關的、最新處理的一條sql 語句所包含的資料。
格式呼叫為: sql%
declare
v_rows number;
begin
--更新資料
update employees set salary = 30000
where department_id = 90 and job_id = 'ad_vp';
--獲取預設游標的屬性值
v_rows := sql%rowcount;
dbms_output.put_line('更新了'||v_rows||'個雇員的工資');
``````
--刪除指定雇員;如果部門中沒有雇員,則刪除部門
delete from employees where department_id=v_deptno;
if sql%notfound then
delete from departments where department_id=v_deptno;
end if;
end;
顯式是相對與隱式cursor而言的,就是有乙個明確的宣告的cursor。顯式游標的宣告類似如下:
cursor cursor_name (parameter list) is select ...
游標從declare、open、fetch、close是乙個完整的生命旅程。當然了乙個這樣的游標是可以被多次open進行使用的,顯式cursor是靜態cursor,它的作用域是全域性的,但也必須明白,靜態cursor也只有pl/sql**才可以使用它。
1.只取一行:
declare
id employees.id%type;
name employees.name%type;
cursor cur is select *from employees;
begin
open cur;
fetch cur into id,name;
close cur;
dbms_output.put_line(id||name);
end;
2.手動loop迴圈取多行declare
id number(4)
name varchar(20)
cursor cur is select *from employees;
begin
open cur;
loop
fetch cur into id,name;
exit when cur%notfound;
dbms_output.put_line(id||name);
end loop;
close cur;
end;
3.游標for迴圈取多行
pl/sql語言提供了游標for迴圈語句,自動執行游標的open、fetch、close語句和迴圈語句的功能;
當進入迴圈時,游標for迴圈語句自動開啟游標,並提取第一行游標資料;
當程式處理完當前所提取的資料而進入下一次迴圈時,游標for迴圈語句自動提取下一行資料供程式處理;
當提取完結果集合中的所有資料行後結束迴圈,並自動關閉游標。
格式:for index_variable in cursor_name[(value[, value]…)] loop
– 游標資料處理**
end loop;
其中:index_variable為游標for 迴圈語句隱含宣告的索引變數,該變數為記錄變數,其結構與游標查詢語句返回的結構集合的結構相同。在程式中可以通過引用該索引記錄變數元素來讀取所提取的游標資料,index_variable中各元素的名稱與游標查詢語句選擇列表中所制定的列名相同。如果在游標查詢語句的選擇列表中存在計算列,則必須為這些計算列指定別名後才能通過游標for 迴圈語句中的索引變數來訪問這些列資料。
declare
cursor cur is select *from employees;
begin
for counter in cur
loop
dbms_output.put_line(counter.id||counter.name);
end loop;
end;
4.帶引數的游標for迴圈:例:
declare
cursor c_cursor(dept_no number default 10)
is select department_name, location_id from departments where department_id <= dept_no;
begin
--當dept_no引數值為30
for c1_rec in c_cursor(30) loop
dbms_output.put_line(c1_rec.department_name||'---'||c1_rec.location_id);
end loop;
--使用預設的dept_no引數值10
for c1_rec in c_cursor loop
dbms_output.put_line(c1_rec.department_name||'---'||c1_rec.location_id);
end loop;
end;
或者可以在游標for迴圈語句中使用子查詢
begin
for c1_rec in(select department_name, location_id from departments) loop
dbms_output.put_line(c1_rec.department_name||'---'||c1_rec.location_id);
end loop;
end;
Oracle資料庫入門
oracle database,又名oracle rdbms,或簡稱oracle。是甲骨文公司的一款關聯式資料庫管理系統。它是在資料庫領域一直處於領先地位的產品。可以說oracle資料庫系統是目前世界上流行的關聯式資料庫管理系統,系統可移植性好 使用方便 功能強,適用於各類大 中 小微機環境。它是一...
Oracle資料庫入門 二
oracle sql plus常用命令 一 sys使用者和system使用者 oracle安裝會自動的生成sys使用者和system使用者。1 sys使用者時超級使用者,具有最高許可權,具有sysdba角色,有create database的許可權,該使用者預設的密碼是change on insta...
Oracle資料庫入門 一
資料庫分類 一 資料庫分類 1 小型資料庫 access foxbase 2 中型資料庫 informix sql server mysql 3 大型資料庫 sybase db2 oracle。二 專案中如何合理地使用資料庫,可以依據如下三個方面入手 1 負載量有多大,即使用者數有多大 2 成本 3...