検索
ホームページデータベースmysql チュートリアルjava从零开始,学习笔记之基础入门<Oracle_函数_触发器_游标_

Oracle_函数_触发器_游标_存储过程_视图 ---PL/SQL语言部分 --PL.SQL基本格式: --declare --声明部分--一切变量和常量在此声明 --begin -- --主体,执行语句 --end; declare i number(3); begin --给变量赋值 i:=1; dbms_output.put_line(i的值是:||i); end;

Oracle_函数_触发器_游标_存储过程_视图

---PL/SQL语言部分

--PL.SQL基本格式:

--declare --声明部分--一切变量和常量在此声明

--begin

-- --主体,执行语句

--end;

declare

i number(3);

begin

--给变量赋值

i:=1;

dbms_output.put_line('i的值是:'||i);

end;

--声明常量

declare

i constant varchar2(20):='我是摩纳哥鞑子';

begin

dbms_output.put_line(i);

end;

select * from scott.emp;

--删除一条记录

declare

eno varchar2(5);

begin

eno:=7369;

delete scott.emp where empno=eno;

end;

--新增一条记录

declare

eno varchar2(5):=110;

ena varchar2(20):='周星星';

ejob varchar2(30):='影帝';

mgr number(4):=7369;

hir date:='3-1月-2011';

sals number(10):=10000;

com number(20):=100;

dep number:=10;

begin

insert into scott.emp values(eno,ena,ejob,mgr,hir,sals,com,dep);

end;

--查询数据

declare

eno number(3);

ena varchar2(10);

begin

eno:=110;

select ename into ena from scott.emp where empno=eno;

dbms_output.put_line('ena的值是:'||ena);

end;

--显示所有的记录

declare

eno varchar2(5):=110;

ena varchar2(20);

ejob varchar2(30);

mgr number(10);

hir date;

sals number(10);

com number(20);

dep number(10);

begin

select empno,ename,job,mgr,hiredate,sal,comm,deptno

into eno,ena,ejob,mgr,hir,sals,com,dep

from scott.emp

where empno=eno;

dbms_output.put_line(eno||','||ena||','||ejob||','||mgr||','||hir||','||sals||','||com||','||dep);

end;

--使用一行简化我们的查询操作

--%rowtype,返回行的数据类型

declare

emps scott.emp% rowtype;

begin

select * into emps from scott.emp where empno=110;

dbms_output.put_line(emps.ename||','||emps.job);

end;

--%type

declare

enames scott.emp.ename% type;

begin

select ename into enames from scott.emp where empno=110;

dbms_output.put_line(enames);

end;

--条件控制语言

--if--then--else

--if--then--else if--else if --else--

--有多少个if就给多少end if 结束

declare

i number:=2;

begin

if(i>4)

then

dbms_output.put_line('逻辑正确');

else

dbms_output.put_line('逻辑不正确');

endif;

end;

--查询scott.emp 表中的数据,如果工资低于3000就加2000

--如果低于4000就只加500

--如果高于5000就扣除200

declare

emp number(5):=110;

esal number(10);

begin

select sal into esal from scott.emp where empno=emp;

if(esal

update scott.emp set sal=sal+2000;

else if(esal>5000) then

update scott.emp set sal=sal-200;

end if;

end if;

select sal into esal from scott.emp where empno=emp;

dbms_output.put_line(esal);

end;

--case

declare

i number(2):=1;

begin

case i

when1 then

dbms_output.put_line('i的值是1');

when 2 then

dbms_output.put_line('i的值是2');

when 3 then

dbms_output.put_line('i的值是3');

else

dbms_output.put_line('没有匹配的值');

end case;

end;

--循环语句

--loop,for.while

--简单的loop循环

declare

i number(2):=1;

begin

loop

if(i>10) then

exit; --终止程序

end if;

dbms_output.put_line(i);

i:=i+1;

end loop;

end;

--for循环

declare

j number(2):=10;

begin

for i in1..j loop --for循环,不需要声明此处的变量i,范围采用".."

dbms_output.put_line(i);

end loop;

end;

--while 循环

declare

k number(2):=1;

begin

while (k

dbms_output.put_line(k);

k:=k+1;

end loop;

end;

--异常的处理

--预定义异常

--用户自定义异常

declare

invalied EXCEPTION;

categroy varchar2(10);

begin

categroy :='tt';

if categroy not in('沈水','林下','石小孟') then

raise invalied;

else

dbms_output.put_line('你是:'||categroy);

end if;

exception

when invalied then

dbms_output.put_line('你输入的不匹配');

end;

declare

j number(2):=10;

begin

for i in1..j loop --for循环,不需要声明此处的变量i,范围采用".."

dbms_output.put_line(i);

end loop;

end;

/*

*

**

***

****

*****

*/

 

declare

begin

for i in0..5 loop

for j in0..i loop

dbms_output.put('*');

end loop;

dbms_output.new_line;

end loop;

end;

select * from scott.emp

存储过程

--存储过程

create or replace procedure test_pro

as

begin

declare

i number(3):=1;

begin

dbms_output.put_line(i);

end;

end;

execute test_pro;--在sqlplus里执行

--在sql里执行存储过程

begin

test_pro;

end;

--带参数的存储过程

create or replace procedure get_par(i innumber)

as

begin

dbms_output.put_line(i);

end;

begin

get_par(2012);

end;

--根据学生id查询某学生名

create or replace procedure get_stu(stu_id innumber)

is

stuname varchar2(28);

begin

select s_name into stuname from student where s_id =stu_id;

dbms_output.put_line(stuname);

exception

when no_data_found then

dbms_output.put_line('查无此人');

end;

begin

get_stu(1);

end;

--带有输入输出参数的存储过程

--根据学生id来查询学生姓名

create or replace procedure get_stu(stu_id in number,stuname in out varchar2)

as

begin

select username into stuname from user_tb where userid =stu_id;

dbms_output.put_line(stuname);

exception

when no_data_found then

dbms_output.put_line('查无此人');

end;

declare

stuname varchar2(30);

begin

get_stu(1,stuname);

end;

*******************************************************

--模拟登录与注册

create table usertb(

userid number primary key,

username varchar2(30),

userpwd varchar2(30)

)

create or replace procedure login_pros(uname in out varchar2,pwd in out varchar2,islogin in out boolean)

as

begin

select username,userpwd into uname,pwd from usertb where username=uname and userpwd=pwd;

islogin:=true;

exception

when no_data_found then

dbms_output.put_line('用户没有注册');

islogin:=false;

end;

--执行登录的存储过程

declare

uname varchar2(30):='李冰冰';

pwd varchar2(30):='abc';

islogin boolean;

begin

login_pros(uname,pwd,islogin);

if(islogin) then

dbms_output.put_line('登录成功,'||'登录的用户是'||uname);

else

dbms_output.put_line('登录失败'||'请重新注册');

end if;

end;

--注册

create or replace procedure regist_pros(uname in out varchar2,pwd in out varchar2,userid in number,isregist out boolean)

as

begin

insert into usertb values(userid,uname,pwd);

isregist:=true;

dbms_output.put_line('注册成功'||'注册用户是:'||uname);

exception

when no_data_found then

dbms_output.put_line('您输入的用户信息是否正确');

isregist:=false;

end;

--注册的存储过程的调用

declare

uname varchar2(30):='李冰冰';

pwd varchar2(30):='abc';

userid number(10):=3;

isregist boolean;

begin

regist_pros(uname,pwd,userid,isregist);

if(isregist) then

dbms_output.put_line('注册的用户是:'||uname);

else

dbms_output.put_line('是否重新注册');

end if;

end;

视图

--视图

create view emp_view

as

select * from usertb;

--删除视图中的字段,会影响到住表中的数据

delete from emp_view where userid=2;

--对视图进行更新

update emp_view set username='李斯' where userid=2;

触发器

--触发器

--触发器对select不起作用

create or replace trigger delete_tir after delete on scott.emp

begin

dbms_output.put_line('删除一条语句');

end;

alter trigger delete_tir disable;

delete from scott.emp where empno=110;

create or replace trigger update_tri before update on scott.emp

begin

dbms_output.put_line('更新一条语句');

end;

update scott.emp set sal=9000 where empno=7499;

--对scott.emp表进行插入数据的时候,也同时将此数据插入到

create table emptest(

eno number(20) primary key,

enames varchar2(30),

jobs varchar2(30),

mgrs number(10),

hiretime date,

sals number(10),

comms number(10),

dept_no number(10)

)

--在删除之前执行,在删除之前打印即将删除学生的信息

--删除之前执行用":old",更新之前执行用":new";

create or replace trigger stu_delete_prinStu

before deleteon tb_stu for each row

begin

dbms_output.put_line('即将删除的学生学号是:'||:old.stu_no);

dbms_output.put_line('即将删除的学生姓名是:'||:old.stu_name);

end;

delete from tb_stu where stu_no=6;

create or replace trigger new_tri before insert on scott.emp

for each row

begin

insert into emptest values(:new.empno,:new.ename,:new.job,:new.mgr,:new.hiredate,:new.sal,:new.comm,:new.deptno);

end;

insert into scott.emp values(11,'OFFICELADY','SALE',7698,'1-5月-2011',29000,3000,20);

--对表进行删除操作后的记录

create or replace trigger old_tri after delete on scott.emp

for each row

begin

dbms_output.put_line('删除的用户是:'||:old.ename);

end;

delete from scott.emp where empno=11;

--before和after

--行级触发器,语句级触发器 (for each row)

--行级触发器对DML语句影响每一行的操作,例如update语句,有多少条语句,触发器就会被执行多少次

--语句级触发器对我们的DML语句只执行一次操作,例如insert语句,即使有多条,触发器只被执行一次

--before表示在语句执行之前出发

--after表示在语句执行之后进行出发

--实际看到的效果没有什么区别

--禁用触发器,启用触发器

alter trigger new_tri disable;

alter trigger old_tri enable;

--禁用一个表中所有触发器

alter table tb_stu disable all triggers;

--删除触发器

drop trigger new_tri;

select * from scott.emp;

select * from lu.emptest

函数

--定义一个函数实现加法运算

create or replace function myAdd(num1 number,num2 number)

return number--在规则说明中需要return

--只能够返回一个值(和声明的返回类型匹配)

--需要return关键字来返回值

--函数不能单独的被调用,只能作为sql代码的一部分来执行

as

num3 number;

begin

num3:=num1+num2;

return num3;

end;

--调用函数:函数不能单独的被调用,需要作为sql一部分来调用

declare

n number;

begin

n:=myAdd(12,34);

dbms_output.put_line(n);

end;

--定义一个函数,查询emp 将所有的信息打印到output

--并且计算出所有员工的工资总和,返回工资总和

--函数和存储过程一样,在定义的时候如果没有参数就不需要"()"

create or replace function getMsg

return number

as

cursor emp_msg is select * from scott.emp;

totalSal number;

begin

for e in emp_msg

loop

dbms_output.put_line(e.ename||'-'||e.sal);

end loop;

select sum(sal) into totalSal from scott.emp;

return totalSal;

end;

--调用函数

select getMsg() from dual;

--定义一个包的说明部分 (类似接口)

create or replace package my_package is

procedure myProc;

function myAdd(m number,n number)return number;

end my_package;

--定义一个人包的主题部分(类似程序体DAO)

create or replace package body my_package is

procedure myProc

is

cursor emp_msg isselect * from scott.emp;

begin

for e in emp_msg

loop

dbms_output.put_line(e.ename);

endloop;

end;

function myAdd(m number,n number)

return number

as

num number;

begin

num:=n+m;

return num;

end;

end my_package;

call my_package.myProc();

select my_package.myAdd(1,2) 结果 from dual;

声明
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。
MySQL:世界で最も人気のあるデータベースの紹介MySQL:世界で最も人気のあるデータベースの紹介Apr 12, 2025 am 12:18 AM

MySQLはオープンソースのリレーショナルデータベース管理システムであり、主にデータを迅速かつ確実に保存および取得するために使用されます。その実用的な原則には、クライアントリクエスト、クエリ解像度、クエリの実行、返品結果が含まれます。使用法の例には、テーブルの作成、データの挿入とクエリ、および参加操作などの高度な機能が含まれます。一般的なエラーには、SQL構文、データ型、およびアクセス許可、および最適化の提案には、インデックスの使用、最適化されたクエリ、およびテーブルの分割が含まれます。

MySQLの重要性:データストレージと管理MySQLの重要性:データストレージと管理Apr 12, 2025 am 12:18 AM

MySQLは、データストレージ、管理、クエリ、セキュリティに適したオープンソースのリレーショナルデータベース管理システムです。 1.さまざまなオペレーティングシステムをサポートし、Webアプリケーションやその他のフィールドで広く使用されています。 2。クライアントサーバーアーキテクチャとさまざまなストレージエンジンを通じて、MySQLはデータを効率的に処理します。 3.基本的な使用には、データベースとテーブルの作成、挿入、クエリ、データの更新が含まれます。 4.高度な使用には、複雑なクエリとストアドプロシージャが含まれます。 5.一般的なエラーは、説明ステートメントを介してデバッグできます。 6.パフォーマンスの最適化には、インデックスの合理的な使用と最適化されたクエリステートメントが含まれます。

なぜMySQLを使用するのですか?利点と利点なぜMySQLを使用するのですか?利点と利点Apr 12, 2025 am 12:17 AM

MySQLは、そのパフォーマンス、信頼性、使いやすさ、コミュニティサポートに選択されています。 1.MYSQLは、複数のデータ型と高度なクエリ操作をサポートし、効率的なデータストレージおよび検索機能を提供します。 2.クライアントサーバーアーキテクチャと複数のストレージエンジンを採用して、トランザクションとクエリの最適化をサポートします。 3.使いやすく、さまざまなオペレーティングシステムとプログラミング言語をサポートしています。 4.強力なコミュニティサポートを提供し、豊富なリソースとソリューションを提供します。

InnoDBロックメカニズム(共有ロック、排他的ロック、意図ロック、レコードロック、ギャップロック、次のキーロック)を説明します。InnoDBロックメカニズム(共有ロック、排他的ロック、意図ロック、レコードロック、ギャップロック、次のキーロック)を説明します。Apr 12, 2025 am 12:16 AM

INNODBのロックメカニズムには、共有ロック、排他的ロック、意図ロック、レコードロック、ギャップロック、次のキーロックが含まれます。 1.共有ロックにより、トランザクションは他のトランザクションが読み取らないようにデータを読み取ることができます。 2.排他的ロックは、他のトランザクションがデータの読み取りと変更を防ぎます。 3.意図ロックは、ロック効率を最適化します。 4。ロックロックインデックスのレコードを記録します。 5。ギャップロックロックインデックス記録ギャップ。 6.次のキーロックは、データの一貫性を確保するためのレコードロックとギャップロックの組み合わせです。

貧弱なMySQLクエリパフォーマンスの一般的な原因は何ですか?貧弱なMySQLクエリパフォーマンスの一般的な原因は何ですか?Apr 12, 2025 am 12:11 AM

MySQLクエリのパフォーマンスが低いことの主な理由には、インデックスの使用、クエリオプティマイザーによる誤った実行計画の選択、不合理なテーブルデザイン、過剰なデータボリューム、ロック競争などがあります。 1.インデックスがゆっくりとクエリを引き起こし、インデックスを追加するとパフォーマンスが大幅に向上する可能性があります。 2。説明コマンドを使用してクエリ計画を分析し、オプティマイザーエラーを見つけます。 3.テーブル構造の再構築と結合条件を最適化すると、テーブルの設計上の問題が改善されます。 4.データボリュームが大きい場合、パーティション化とテーブル分割戦略が採用されます。 5.高い並行性環境では、トランザクションの最適化とロック戦略は、ロック競争を減らすことができます。

複数の単一列インデックスに対して複合インデックスをいつ使用する必要がありますか?複数の単一列インデックスに対して複合インデックスをいつ使用する必要がありますか?Apr 11, 2025 am 12:06 AM

データベースの最適化では、クエリ要件に従ってインデックス作成戦略を選択する必要があります。1。クエリに複数の列が含まれ、条件の順序が固定されている場合、複合インデックスを使用します。 2。クエリに複数の列が含まれているが、条件の順序が修正されていない場合、複数の単一列インデックスを使用します。複合インデックスは、マルチコラムクエリの最適化に適していますが、単一列インデックスは単一列クエリに適しています。

MySQLでスロークエリを識別して最適化する方法は? (スロークエリログ、Performance_schema)MySQLでスロークエリを識別して最適化する方法は? (スロークエリログ、Performance_schema)Apr 10, 2025 am 09:36 AM

MySQLスロークエリを最適化するには、slowquerylogとperformance_schemaを使用する必要があります。1。LowerQueryLogを有効にし、しきい値を設定して、スロークエリを記録します。 2。performance_schemaを使用してクエリの実行の詳細を分析し、パフォーマンスのボトルネックを見つけて最適化します。

MySQLおよびSQL:開発者にとって不可欠なスキルMySQLおよびSQL:開発者にとって不可欠なスキルApr 10, 2025 am 09:30 AM

MySQLとSQLは、開発者にとって不可欠なスキルです。 1.MYSQLはオープンソースのリレーショナルデータベース管理システムであり、SQLはデータベースの管理と操作に使用される標準言語です。 2.MYSQLは、効率的なデータストレージと検索機能を介して複数のストレージエンジンをサポートし、SQLは簡単なステートメントを通じて複雑なデータ操作を完了します。 3.使用の例には、条件によるフィルタリングやソートなどの基本的なクエリと高度なクエリが含まれます。 4.一般的なエラーには、SQLステートメントをチェックして説明コマンドを使用することで最適化できる構文エラーとパフォーマンスの問題が含まれます。 5.パフォーマンス最適化手法には、インデックスの使用、フルテーブルスキャンの回避、参加操作の最適化、コードの読み取り可能性の向上が含まれます。

See all articles

ホットAIツール

Undresser.AI Undress

Undresser.AI Undress

リアルなヌード写真を作成する AI 搭載アプリ

AI Clothes Remover

AI Clothes Remover

写真から衣服を削除するオンライン AI ツール。

Undress AI Tool

Undress AI Tool

脱衣画像を無料で

Clothoff.io

Clothoff.io

AI衣類リムーバー

AI Hentai Generator

AI Hentai Generator

AIヘンタイを無料で生成します。

ホットツール

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

このプロジェクトは osdn.net/projects/mingw に移行中です。引き続きそこでフォローしていただけます。 MinGW: GNU Compiler Collection (GCC) のネイティブ Windows ポートであり、ネイティブ Windows アプリケーションを構築するための自由に配布可能なインポート ライブラリとヘッダー ファイルであり、C99 機能をサポートする MSVC ランタイムの拡張機能が含まれています。すべての MinGW ソフトウェアは 64 ビット Windows プラットフォームで実行できます。

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Eclipse を SAP NetWeaver アプリケーション サーバーと統合します。

メモ帳++7.3.1

メモ帳++7.3.1

使いやすく無料のコードエディター

Dreamweaver Mac版

Dreamweaver Mac版

ビジュアル Web 開発ツール

SublimeText3 Linux 新バージョン

SublimeText3 Linux 新バージョン

SublimeText3 Linux 最新バージョン