DDL commands --> create user / table / view / sequence

alter

DML --> data manipulation language (insert, select, update, delete)

eg :

SELECT ename FROM emp WHERE sal = (SELECT MAX(sal) FROM EMP);

%type(single col), %rowtype(single row/record)

cursor --> manu columns/rows

eg :

DECLARE

l_emp_ename emp.ename%type;

BEGIN

SELECT ename INTO l_emp_ename FROM emp WHERE sal = (SELECT MAX(sal) FROM EMP);

DBMS_OUTPUT.put_line('Message');

END;

DBMS_OUTPUT.put('message');

DBMS_OUTPUT.put_line('message);

the implicit cursor(give us 1 row/record)(create / open / get data / close by oracle automatically)

eg : select ename from emp where empno=1111;

the explicit cursor(give us many rows/records)(create / open / get data / close by ourselves)

eg : select * from emp;

an emplicit sursor works as follows :

open the cursor

fetch data from the cursor

fetch again to check if any more rows are found

eg :

DECLARE

l_find_job varchar2(10) := 'PROGRAMMER';

BEGIN

UPDATE emp

SET job = 'DEVELOPER'

WHERE job = 'PROGRAMMER';

END;

commir : save all DML commands

rollback : undo before commit

savepoint : roll back to the savepoint; clear all data till the last commit(when there is no savepoint)

aotocommit : when you  close SQL*PLUS if autocommit is on, the data is you delete, update or insert without commit is saved.

set autocommit on/off

show autocommit

transaction control

eg :

BEGIN

update

commit

END;

commit

eg :

BEGIN

DELETE debug;

SAVEPOINT deleted_debug;

DELETE transactions;

ROLLBACK TO deleted_debug;

COMMIT;

END;

TRANSACTION starts from the last commit end with the commit.

eg :

create table test1(ename varchar2(10));

whenever you issue a SQL statement in a PL/SQL block, PL/SQL creates an implicit cursor, the implicit cursor is using number of attribute that can be selected to find the result of the SQL command.

SQL%ROWCOUNT : the number of rows processed by the SQL statement

SQL%FOUND : true if at least one row was processed by the SQL statement, otherwise false

SQL%NOTFOUND : true if no rows were processed by the SQL statement, otherwise false.

eg : select ename from emp where empno = 1111;

DBMS_OUTPUT.put_line(SQL%ROWCOUNT); (1)

eg : select * from emp;

SQL%ROWCOUNT ()

eg : select ename from emp where 1 = 2;

SQL%ROWCOUNT(0)

copy emp table to emp1 with all rec & data : create table emp1 as select * from emp;

find the largest number from three numbers

eg:

DECLARE

num1 number(10) := '&num1';

num2 number(10) := '&num2';

num3 number(10) := '&num3';

BEGIN

if num1 >= num2 then

if num2 >= num3 then

DBMS_OUTPUT.put_line(num1);

elif num1 >= num3 then

DBMS_OUTPUT.put_line(num1);

else

DBMS_OUTPUT.put_line(num3);

elif num2 >= num3 then

DBMS_OUTPUT.put_line(num2);

else

DBMS_OUTPUT.put_line(num3);

END;

eg :

DBMS_OUTPUT.new_line;

DBMS_OUTPUT.put_line();

DBMS_OUTPUT.put();

write a program to reverse the number user input : 5678 output : 8765

eg :

write a program to find the factorial (input 4 --> 4*3*2*1 ==) total as a result

eg :

declare

num number(4) := #

total number(10) := 1;

begin

for i in num

loop

total := total *i;

end loop;

end;

/

write a program to print fibonacci series(the next number is the sum of last two numbers)(0112358...)

eg :

declare

num1 number(5) := 0;

num2 number(5) := 1;

num3 number(5) := 1;

num number(5) := #

begin

for i in 1.. num

loop

num1 := num3 + num2;

num2 := num3 + num1;

num3 := num1 + num2;

DBMS_OUTPUT.put_line(num1);

DBMS_OUTPUT.put_line(num2);

DBMS_OUTPUT.put_line(num3);

end loop;

end;

/

eg :

declare

num number(6) := #

num1 number(6) := 0;

num2 number(6) := 1;

num3 number(6) := 1;

begin

DBMS_OUTPUT.put(' ' || num1);

DBMS_OUTPUT.put(' ' || num2);

for i in 3..num

loop

num3 := num1 + num2;

DBMS_OUTPUT.put(' ' || num3);

num2 := num2;

num2 := num3;

end loop;

DBMS_OUTPUT.new_line;

end;

wtite a program to display the given number is a prome number or not.(质数)(using mod() remaining=0 )

eg :

declare

num number(6) := #

begin

for i in 2..num

loop

if mod(num, i) != 0 then

end loop;

end;

eg :

declare

num number(5) := #

a number(5);

begin

for i in 2..num-1

loop

a := MOD(num, i);

if  a = 0 then

goto ABC

end if;

end loop;

<<ABC>>

if a = 1 then

DBMS_OUTPUT.put_line(num || ' is a prime number');

else

DBMS_OUTPUT.put_line(num || ' is not a prime number');

end if;

end;

.

/

%type (single column)

%rowtype (all column)

some columns from the table

composite type

eg :

declare

TYPE emp_rec_type is record ( name varchar2(10), sal number(10), hiredate date);

emp_rec emp_rec_type;

begin

select ename, sal, hiredate into emp_rec from emp where empno = &empno;

DBMS_OUTPUT.put_line(emp_rec.name || emp_rec.sal || emp_rec.hiredate);

end;

.

/

SQL with PL/SQL的更多相关文章

  1. SQL和PL/SQL的区别

    SQL和PL/SQL的区别 1. SQL是结构化查询语言,比较接近自然语言,使用SQL,只需要说干什么,不需要说怎么干.由数据定义语言.数据操纵语言.数据控制语言构成,它不面向过程,即前一条语句与后一 ...

  2. [PL/SQL]使用PL/SQL实现ORACLE的Excel格式导入导出

    注:教程所使用的PL/SQL Developer版本为10版本 1.oracle导出excel格式 第一步,在pl/sql窗口查询出你想要导出的数据. 第二步,选择你想导出的数据,然后右键" ...

  3. 动态SQL和PL/SQL的EXECUTE选项分析

    EXECUTE IMMEDIATE代替了以前Oracle8i中DBMS_SQL package包.它解析并马上执行动态的SQL语句或非运行时创建的PL/SQL块.动态创建和执行SQL语句性能超前,EX ...

  4. SQL、PL/SQL、DDL、DML、TCL介绍

    SQL:结构化查询语言(Structured Query Language) PL/SQL:过程化SQL语言(Procedural Language/SQL) DDL(Data Definition ...

  5. 【PL/SQL编程】SQL与PL/SQL的区别

    SQL概念: SQL是结构化查询语言,由数据定义语言.数据操纵语言.数据控制语言构成,它不面向过程,即前一条语句与后一条语句无关.它没有流程控制,也不存在变量. PL SQL概念:    PL/SQL ...

  6. PL/SQL不能格式化SQL:--PL/SQL Beautifier could not parse text

    PL/SQL sql语句美化器点击没有反应.查看下面提示PL/SQL Beautifier could not parse text.本人此次产生的原因是sql语句语法错误. 工具栏处(如果没有此按钮 ...

  7. Oracle 和pl/sql以及pl/sql developer

    oracle是厂家的名字,也是数据库产品的名字.比如sybase公司的sybase数据库.而微软公司的数据库产品就叫sqlserver了. pl/sql 是oracle数据库所用的sql语言的名称.微 ...

  8. PL\SQL和PL/SQL Developer 12安装与配置

    安装: (1)在已有安装包的情况下,直接将安装包解压到文件夹下,注意不要解压缩到c:\programs Files(x86)的文件夹下,不能解压缩到有中文文件夹命名的文件夹下面 (2)没有安装包的情况 ...

  9. Oracle学习笔记十 使用PL/SQL

    PL/SQL 简介 PL/SQL 是过程语言(Procedural Language)与结构化查询语言(SQL)结合而成的编程语言,是对 SQL 的扩展,它支持多种数据类型,如大对象和集合类型,可使用 ...

随机推荐

  1. J2EE 第二阶段项目之分析业务

    这次的项目为: 两金申报系统.主要是关系的练习,CRUD,统计. 这个项目没有pdf图,只有一本使用手册.(在我们出去工作后,做项目的时候,也可能只有项目需求的资料). 分析:每一张表都有分页,查看 ...

  2. Spring MVC 之类型转换(五)

    虽然SpringMVC可以自动绑定多种数据类型,但是有时候有些特殊的数据类型还是会在绑定时发生错误,需要我们自己书写类型转换完成绑定. SpringMVC中提供两种绑定方式:以时间转换为例. 1.属性 ...

  3. 车辆管理系统之搭建框架 添加必要的数据 安装svn(二)

    JAVA EE第一阶段项目——车辆管理系统.MyEclipse + MySQL +powerDesinger +tomcat +svn. 今天组长把项目的分工安排好了!这个周末两天的作业就是我的车主信 ...

  4. -bash: jps: command not found

    linux安装了jdk之后,打jps命令发现找不到这个命令: -bash: jps: command not found 查看java版本java -version,正常. java version ...

  5. VI经典插件ctags

    Vi经典插件ctags(转) (为了提高工作效率,必须学会使用一些工具) . 查看 key 相关信息说明的命令 :help keycodes ============================= ...

  6. css3属性 transition transform

    1.transition 译:过渡,转变 可以设置过渡属性 transition: property duration timing-function delay; transition-proper ...

  7. jmeter 建立一个监控测试计划

    监控 压力测试和系统管理是有用的. 使用压力 测试,服务器性能监控提供了额外的信息. 这也使得它更容易看到服务器性能之间的关系 在客户端和响应时间. 作为一个系统管理工具, 监控提供了一种简单的方法来 ...

  8. call(京基填小票拍照片)

    同事的写的函数结构,先是几个函数表达式var a = function(){},里面再是函数声明function a(){} 里面用了好多call,网上搜了一下call的用途: 1. http://w ...

  9. Flask中mongodb实现flask_login保持登录

    最近在学习Flask,使用flask-login时,一直无法完成保持登录的状态,网上的例子都是使用SQLAlchemy,但是我用的是mongodb. 网上的例子使用SQLAlchemy时,定义User ...

  10. la----3695 City Game(最大子矩阵)

    Bob is a strategy game programming specialist. In his new city building game the gaming environment ...