-----创建序列
create sequence book_id
INCREMENT BY 1 -- 每次加几个  
 START WITH 001 -- 从1开始计数  
 NOMAXVALUE -- 不设置最大值  
 NOCYCLE -- 一直累加,不循环  
 CACHE 10;

------创建books表
create table books(
books_id varchar2(1000),
books_name varchar2(100),
price number,
qty number,
pub varchar2(200)
);

------修改books表的字段
alter table books modify(books_id number)

-------------往books表中插入数据
insert into books values(book_id.nextval,'中国文学1',39,12,'人民文学');
insert into books values(book_id.nextval,'中国文学2',30,32,'人民文学');
insert into books values(book_id.nextval,'中国文学3',59,22,'清华大学');
insert into books values(book_id.nextval,'中国文学4',33,52,'清华大学');
insert into books values(book_id.nextval,'中国文学5',99,62,'电子工业');

-----------跟新books中的信息
update books set price=100 where books_id=1

----------按出版社分组查询每个出版社金额的情况
select pub,sum(price*qty) from books group by pub

----------按出版社、书籍名称分组查询每个出版社金额的情况
select pub,books_name,sum(price*qty) from books group by pub,books_name

----------按出版社、书籍名称分组查询每个出版社金额的情况   >50
select pub,books_name,sum(price*qty) from books group by pub,books_name having sum(price)>50

----------查询相同出版社的记录数
select pub,count(pub) from books group by pub having count(pub) >1

-----标的内链接
select eid ,ename,six,name from e,d where a.id=d.id

select eid ,ename,six,name from e join d on a.id=d.id

-----做外连接
select eid ,ename,six,name from e join d on a.id=d.id(+)
----右外连接
select eid ,ename,six,name from e join d on a.id(+)=d.id

----无关子查询
select * from e where id in (select eid from d)

----相关子查询
select * from e where id in (select eid from d where id=d.id and id='003')

select * from e where id not in (select eid from d where id=d.id and id='003')

-----存在则显示
select * from e where exists(select id from d where id=d.id)

-----不存在则显示
select * from e where not exists(select id from d where id=d.id)

-----------------------PLSQL基本语法----------------------------------------------------------------------------------------------------------
set serveroutput on size 10000
declare
x varchar2(100);
begin
x:='This is ....';
DBMS_OUTPUT.PUT_LINE('x value is '||x);
end;
 
-----if  elsif   else
declare
  a number;
  b varchar2(10);
begin
  a := 2;
  if a = 1 then
    b := 'A';
  elsif a = 2 then
    b := 'B';
  else
    b := 'C';
  end if;
  DBMS_OUTPUT.put_line(b);
end;

----------------case
declare
  a number;
  b varchar2(10);
begin
  a := 2;
  case
    when a = 1 then
      b := 'A';
    when a = 2 then
      b := 'B';
  end case;
  DBMS_OUTPUT.put_line(b);
end;

-------------------------PLSQL  循环--------------------------------------------

------loop
declare
  x number;
begin
  x := 1;
  loop
    x := x + 1;
    if x > 3 then
      exit;
    end if;
    DBMS_OUTPUT.put_line(x);
  end loop;
  DBMS_OUTPUT.put_line(x);
end;

--------------while
declare
  x number;
begin
  x := 1;
  while x > 3 loop
    x := +1;
    DBMS_OUTPUT.put_line(x);
  end loop;
  DBMS_OUTPUT.put_line(x);
end;

-------for

begin
  for x in 1 .. 10 loop------从小到大
    DBMS_OUTPUT.put_line(x);
  end loop;
  DBMS_OUTPUT.put_line('end of for loop');
end;

begin
  for x in  reverse 1 .. 10 loop------从大到小
    DBMS_OUTPUT.put_line(x);
  end loop;
  DBMS_OUTPUT.put_line('end of for loop');
end;

----------------做标签
declare
  x number;
begin
  x := 0;
  <<repeat_loop>>
  x := x + 1;
  DBMS_OUTPUT.put_line(x);
  if x < 3 then
    goto repeat_loop;
  end if;
end;

----------------exception 处理-------------------------------------
declare
  test varchar2(100);
begin
  select books_name into test from books where books_id = 1;
  DBMS_OUTPUT.put_line(test);
  exception
  when no_data_found then
    DBMS_OUTPUT.put_line('没有找到数据');    
end;

-----------自定义异常
declare
  test varchar2(100);
  e exception;
begin
  select books_name into test from books where books_id = 1;
  if test <> '中国文学1' then
    raise e;
  end if;
  DBMS_OUTPUT.put_line(test);
exception
  when e then
    DBMS_OUTPUT.put_line('不是需要的书籍名称');
end;

-----------------------记录的声明-------------------------------
declare
type myrecord is record(
bname varchar2(100),
bpub varchar2(100)
);
real_record myrecord;
begin
select books_name,pub into real_record from books where books_id=1;
DBMS_OUTPUT.put_line(real_record.bname || real_record.bpub);
end;

declare
  type myrecord is record(
    bname books.books_id%type, ---------------声明的字段和表中的字段类型一样
    bpub  varchar2(100));
  real_record myrecord;
begin
  select books_name, pub into real_record from books where books_id = 1;
  DBMS_OUTPUT.put_line(real_record.bname || real_record.bpub);
end;

declare
  myrecord books%rowtype;
begin
  select * into myrecord from books where books_id = 1;
  DBMS_OUTPUT.put_line(myrecord.books_name || myrecord.pub);
end;

-----------------------游标--------------------

----显示游标的使用方法
declare
  cursor mycursor is
    select * from books;
  myrecord books%rowtype;
begin
  open mycursor;
  fetch mycursor
    into myrecord;
  while mycursor%found loop
    DBMS_OUTPUT.put_line(myrecord.books_name || myrecord.pub);
    fetch mycursor
      into myrecord;
  end loop;
  close mycursor;
end;

------带参数的游标
declare
  cursor mycursor(bookid number) is
    select * from books where books.books_id = bookid;
  myrecord books%rowtype;
begin
  open mycursor(1);
  fetch mycursor
    into myrecord;
  while mycursor%found loop
    DBMS_OUTPUT.put_line(myrecord.books_name || myrecord.pub);
    fetch mycursor
      into myrecord;
  end loop;
  close mycursor;
end;

------使用for做游标的循环
declare
cursor mycursor(bookid number) is
select books_name from books where books.books_id=bookid;
begin
for cur in mycursor(1) loop
DBMS_OUTPUT.put_line(cur.books_name);
end loop;
end;

----is open
declare
  bookname books.books_name%type;
  cursor mycursor(booksid number) is
    select books_name from books where books_id = booksid;
begin
  if mycursor%isopen then
    DBMS_OUTPUT.put_line('cursor is opened');
  else
    open mycursor(1);
  end if;
  fetch mycursor
    into bookname;
  close mycursor;
  dbms_output.put_line(bookname);
end;

-------rowcount
declare
  bookname books.books_name%type;
  cursor mycursor is
    select books_name from books;
begin
  open mycursor;
  loop
    fetch mycursor
      into bookname;
    exit when mycursor%notfound or mycursor%notfound is null;
    DBMS_OUTPUT.put_line(mycursor%rowcount);
  end loop;
  close mycursor;
end;
   
-----游标跟新数据
declare
  cursor mycursor is
    select books_name from books for update;
  text varchar2(100);
begin
  open mycursor;
  fetch mycursor
    into text;
  while mycursor%found loop
    update books
       set books_name = books_name || '_t'
     where current of mycursor;
    fetch mycursor
      into text;
  end loop;
  close mycursor;
end;

----------------隐式游标   不需要声明
begin
  for cur in (select books_name from books) loop
    DBMS_OUTPUT.put_line(cur.books_name);
  end loop;
end;

--------------------存储过程------------------------------------------------------------------
create or replace procedure getBookNameById(bookid in number) is
  bookname varchar2(100);
begin
  select books_name into bookname from books where books_id = bookid;
  --select books_name               from books where books_id=1;
  dbms_output.put_line(ookbname);
end;
end getBookNameById;

-----
begin调用过程
test.getBookNameById(1);
end;
----调用过程
execute test.getBookNameById(1);

-----显示某个存储过程的错误信息
show errors procedure getBookNameById;

分页:

SELECT * FROM
(
SELECT A*, ROWNUM RN
FROM (SELECT * FROM books) A
)
WHERE RN BETWEEN 1 AND 5

oracle基础语法大全的更多相关文章

  1. ORACLE| ORACLE基础语法汇总

    创 ORACLE| ORACLE基础语法汇总 2018-07-18 16:47:34 YvesHe 阅读数 9141更多 分类专栏: [数据库]   版权声明:本文为博主原创文章,遵循CC 4.0 B ...

  2. [SQL] Oracle基础语法

    1.安装: oracle11g server 这里的口令为sys和system的密码.(10版本以前默认用户会有系统默认密码.) Oracle 11g 默认用户名和密码 oracle11g clien ...

  3. MarkDown基础语法大全

    一.MarkDown是什么? Markdown是一种轻量级的「标记语言」,创始人为约翰·格鲁伯,用简洁的语法代替排版,目前被越来越多的知识工作者.写作爱好者.程序员或研究员广泛使用.其常用的标记符号不 ...

  4. sql基础语法大全 转载过来的,出处忘了!

    一.基础 1.说明:创建数据库CREATE DATABASE database-name 2.说明:删除数据库drop database dbname3.说明:备份sql server--- 创建 备 ...

  5. oracle 基础语法(二)

    一.实现分页 说明以下tablename是同一表.这些操作是对同一表(tablename)的操作 ======================================= 如何实现分页提取记录 ...

  6. Oracle基础语法

    --表create table tb_myTable( mname vardhar2(30), pwd varchar2(30)); --存储过程create or replace procedure ...

  7. Oracle基础语法 一

    表空间操作 1.创建一个表空间 create tablespace emp(空间名) datafile 'emp.dbf' size 10M autoextend on ;   →表空间名字 /  物 ...

  8. Oracle常用基础语法(未完待补和操作)

    这篇博客主要是Oracle常用基础语法, 另外,存储过程和存储函数很重要,这个后期看视频学习,还有DB优化,另外,还有plsql develop和navicat的使用,重点是数据的导入导出: ---- ...

  9. SQL操作数据——SQL组成,查询基础语法,where,Oracle常用函数等

    SQL组成 DML数据操作语言 DCL数据控制语言 DQL数据查询语言 DDL数据定义语言 查询基础语法 记录筛选 where 子句 记录筛选 where 子句 实例练习 实例练习 Select语句中 ...

随机推荐

  1. 用JS控制video暂停再播放

    模块就是用来进行封装,进行高内聚 低耦合的功能.其实各人认为ng2 的模块和.net的工程类似,如果要使用模块中定义的功能,第一步就是必须要引用它,ng2 中叫import 导入.那么我们看模块是否有 ...

  2. 排序算法练习--JAVA(插入、直接选择、冒泡、快速排序、非递归快速排序)

    排序算法是数据结构中的经典算法知识点,也是笔试面试中经常考察的问题,平常学的不扎实笔试时候容易出洋相,回来恶补,尤其是碰到递归很可能被问到怎么用非递归实现... package sort; impor ...

  3. Servlet页面注册用户的小程序(一)

    本实例实现用userreg.jsp页面中的表单提交注册请求,把注册信息提交给regservlet写入数据库并且查询新用户显示出来. 一.准备工作. 1.jdbc数据驱动开发包mysql-connect ...

  4. #uwp# XMAL

    类型转换 在xaml中对属性赋值时,会将填入的字符串转换成对应的属性类型.比如: <Button Visibility="Visible" /> 会将Visible这个 ...

  5. java web的开发 知识要点

    近期闲下来时写的一个有关 java web的开发 的 常用架构 的总结,用于初 学 者或团队新人培训. Java开发初步.ppt SSH  为 struts+spring+hibernate 的一个集 ...

  6. Linux uniq常用命令

    -u 只显示不重复行.-d 只显示有重复数据行,每种重复行只显示其中一行-c 打印每一重复行出现次数.-f n为数字,前n个域被忽略.一些系统不识别- f选项,这时替代使用- n.

  7. NULL指针、零指针、野指针

    1.1.空指针 如果 p 是一个指针变量,则 p = 0; p = 0L; p = '\0'; p = 3 - 3; p = 0 * 17;p=(void*)0; 中的任何一种赋值操作之后, p 都成 ...

  8. <<Differential Geometry of Curves and Surfaces>>笔记

    <Differential Geometry of Curves and Surfaces> by Manfredo P. do Carmo real line Rinterval I== ...

  9. sip协议注册时response值的计算方法

    sip注册时有四个步骤, 1.客户端向服务端发送不带Authorization字段的注册请求 2.服务端回401,在回复消息头中带WWW_Authorization 3.客户端向服务端发送带Autho ...

  10. JAVA 取得当前目录的路径/Servlet/class/文件路径/web路径/url地址

    在写java程序时不可避免要获取文件的路径...总结一下,遗漏的随时补上 1.可以在servlet的init方法里 String path = getServletContext().getRealP ...