oracle 开发 第3章 sqlplus

2016-01-06

1.查看表结构

DESC customers;

2.编辑SQL语句

SQL> select customer_id,first_name,last_name
from customers
where customer_id = 1;

CUSTOMER_ID FIRST_NAME LAST_NAME
----------- ---------- ----------
      1 John       Brown

SQL> 1
  1* select customer_id,first_name,last_name

SQL> append , dob                                     --在行尾添加", dob"
  1* select customer_id,first_name,last_name, dob

SQL> list                                            --查看sqlplus缓存区所有行
  1  select customer_id,first_name,last_name, dob
  2  from customers
  3* where customer_id = 1

SQL> change /customer_id = 1/customer_id = 2         --将最后一行"customer_id = 1"改为"customer_id = 2"
  3* where customer_id = 2

SQL> run                                             --执行sqlplus缓存区的查询,同/
  1  select customer_id,first_name,last_name, dob
  2  from customers
  3* where customer_id = 2

CUSTOMER_ID FIRST_NAME LAST_NAME  DOB
----------- ---------- ---------- ---------
      2 Cynthia    Orange      05-FEB-68

SQL> /                                                --执行sqlplus缓存区的查询,同run

CUSTOMER_ID FIRST_NAME LAST_NAME  DOB
----------- ---------- ---------- ---------
      2 Cynthia    Orange      05-FEB-68

3.保存、检索并运行文件

SQL> select customer_id,first_name,last_name
from customers
where customer_id = 1;  

CUSTOMER_ID FIRST_NAME LAST_NAME
----------- ---------- ----------
      1 John       Brown

SQL> save /tmp/cust_query.sql                         --将sqlplus缓存区的内容保存到磁盘目录
Created file /tmp/cust_query.sql

SQL> get /tmp/cust_query.sql                         --将磁盘上的脚本读入sqlplus缓存区
  1  select customer_id,first_name,last_name
  2  from customers
  3* where customer_id = 1

SQL> /

CUSTOMER_ID FIRST_NAME LAST_NAME
----------- ---------- ----------
      1 John       Brown

SQL> start /tmp/cust_query.sql                         --执行磁盘目录上的sql脚本

CUSTOMER_ID FIRST_NAME LAST_NAME
----------- ---------- ----------
      1 John       Brown
SQL> define _editor = ‘vim‘;                        --改变默认编辑器

SQL> edit                                             --编辑sqlplus缓存区的内容
Wrote file afiedt.buf

  1  select customer_id,first_name,last_name
  2  from customers
  3* where customer_id = 2

SQL> /
CUSTOMER_ID FIRST_NAME LAST_NAME
----------- ---------- ----------
      2 Cynthia    Orange
SQL> spool /tmp/cust_results.txt                    --将sqlplus的输出结果保存到磁盘文件中
SQL> /
CUSTOMER_ID FIRST_NAME LAST_NAME
----------- ---------- ----------
      2 Cynthia    Orange
SQL> spool off

4.格式化列

column product_id format 99
column name heading product_name format a13 word_wrapped
column description format a13 word_wrapped
column price format $99.99

select product_id,name,description,price
from products
where product_id < 6;

5.设置页面大小

set pagesize 100         --设置一页显示的行数
                        --页面大小最大为50000,默认14

6.设置行大小

set linesize 50         --设置一行显示的字符数,默认80

7.清除列格式

column product_id clear
clear columns

8.使用变量

select product_id,name,price
from products
where product_id = &v_product_id;    --使用变量 &v_product_id
Enter value for v_product_id: 2
old   3: where product_id = &v_product_id
new   3: where product_id = 2

PRODUCT_ID product_name    PRICE
---------- ------------- -------
     2 Chemistry      $30.00

SQL> /
Enter value for v_product_id: 3
old   3: where product_id = &v_product_id
new   3: where product_id = 3

PRODUCT_ID product_name    PRICE
---------- ------------- -------
     3 Supernova      $25.99
SQL> set verify off                    --禁止显示旧行和新行
SQL> /
Enter value for v_product_id: 4

PRODUCT_ID product_name    PRICE
---------- ------------- -------
     4 Tank War      $13.95
SQL> set verify on                     --重新显示新旧行
SQL> /
Enter value for v_product_id: 1
old   3: where product_id = &v_product_id
new   3: where product_id = 1

PRODUCT_ID product_name    PRICE
---------- ------------- -------
     1 Modern      $19.95
       Science
SQL> set define ‘#‘                    --修改变量定义符为‘#‘
select product_id,name,price
from products
where product_id = #v_product_id;
Enter value for v_product_id: 4
old   3: where product_id = #v_product_id
new   3: where product_id = 4

PRODUCT_ID product_name    PRICE
---------- ------------- -------
     4 Tank War      $13.95

SQL> set define ‘&‘                    --将变量定义符改回‘&‘
select name,&v_col
from &v_table
where &v_col = &v_val;                --使用变量替换表名和列名
Enter value for v_col: product_type_id
old   1: select name,&v_col
new   1: select name,product_type_id
Enter value for v_table: products
old   2: from &v_table
new   2: from products
Enter value for v_col: product_type_id
Enter value for v_val: 1
old   3: where &v_col = &v_val
new   3: where product_type_id = 1
select name,&&v_col
from &v_table
where &&v_col = &v_val;                --使用&&避免重复输入变量
Enter value for v_col: product_type_id
old   1: select name,&&v_col
new   1: select name,product_type_id
Enter value for v_table: products
old   2: from &v_table
new   2: from products
Enter value for v_val: 1
old   3: where &&v_col = &v_val
new   3: where product_type_id = 1
SQL> define v_product_id = 4        --使用define命令定义变量
SQL> define v_product_id
DEFINE V_PRODUCT_ID    = "4" (CHAR)
SQL>
select product_id,name,price
from products
  3  where product_id = &v_product_id;
old   3: where product_id = &v_product_id
new   3: where product_id = 4

PRODUCT_ID product_name    PRICE
---------- ------------- -------
     4 Tank War      $13.95

SQL> define
DEFINE _DATE           = "06-JAN-16" (CHAR)
DEFINE _CONNECT_IDENTIFIER = "unicode" (CHAR)
DEFINE _USER           = "STORE" (CHAR)
DEFINE _PRIVILEGE      = "" (CHAR)
DEFINE _SQLPLUS_RELEASE = "1102000400" (CHAR)
DEFINE _EDITOR           = "ed" (CHAR)
DEFINE _O_VERSION      = "Oracle Database 11g Enterprise Edition Release 11.2.0.4.0 - 64bit Production
With the Partitioning, OLAP, Data Mining and Real Application Testing options" (CHAR)
DEFINE _O_RELEASE      = "1102000400" (CHAR)
DEFINE V_COL           = "product_type_id" (CHAR)
DEFINE V_PRODUCT_ID    = "4" (CHAR)
SQL> accept v_customer_id number format 99 prompt ‘Customer id: ‘    --使用accept命令定义并设置变量
Customer id: 4
SQL> accept v_date date format ‘DD-MON-YYYY‘ prompt ‘Date: ‘
Date: 06-MAY-2012
SQL> accept v_password char prompt ‘Password: ‘ hide
Password:
SQL> define
DEFINE _DATE           = "06-JAN-16" (CHAR)
DEFINE _CONNECT_IDENTIFIER = "unicode" (CHAR)
DEFINE _USER           = "STORE" (CHAR)
DEFINE _PRIVILEGE      = "" (CHAR)
DEFINE _SQLPLUS_RELEASE = "1102000400" (CHAR)
DEFINE _EDITOR           = "ed" (CHAR)
DEFINE _O_VERSION      = "Oracle Database 11g Enterprise Edition Release 11.2.0.4.0 - 64bit Production
With the Partitioning, OLAP, Data Mining and Real Application Testing options" (CHAR)
DEFINE _O_RELEASE      = "1102000400" (CHAR)
DEFINE V_COL           = "product_type_id" (CHAR)
DEFINE V_PRODUCT_ID    = "4" (CHAR)
DEFINE V_CUSTOMER_ID   =      4 (NUMBER)
DEFINE V_DATE           = "06-MAY-2012" (CHAR)
DEFINE V_PASSWORD      = "1234567" (CHAR)
SQL> undefine v_col
SQL> undefine v_product_id
SQL> undefine v_customer_id
SQL> undefine v_date
SQL> undefine v_password                 --使用undefine命令删除变量

SQL> define
DEFINE _DATE           = "06-JAN-16" (CHAR)
DEFINE _CONNECT_IDENTIFIER = "unicode" (CHAR)
DEFINE _USER           = "STORE" (CHAR)
DEFINE _PRIVILEGE      = "" (CHAR)
DEFINE _SQLPLUS_RELEASE = "1102000400" (CHAR)
DEFINE _EDITOR           = "ed" (CHAR)
DEFINE _O_VERSION      = "Oracle Database 11g Enterprise Edition Release 11.2.0.4.0 - 64bit Production
With the Partitioning, OLAP, Data Mining and Real Application Testing options" (CHAR)
DEFINE _O_RELEASE      = "1102000400" (CHAR)

9.创建简单报表

vim /tmp/report1.sql

--suppress display of the statements and verification message
set echo off                --禁止显示脚本中的SQL语句
set verify off                --禁止显示验证消息
select product_id,name,price
from products
where product_id = &v_product_id;    --使用临时变量v_product_id
SQL> @ /tmp/report1.sql
Enter value for v_product_id: 2

PRODUCT_ID product_name    PRICE
---------- ------------- -------
     2 Chemistry      $30.00

vim /tmp/report2.sql

--suppress display of the statements and verification message
set echo off
set verify off
accept v_product_id number format 99 prompt ‘Enter product id: ‘    --使用已定义变量v_product_id
select product_id,name,price
from products
where product_id = &v_product_id;
--clear up
undefine v_product_id
SQL> @ /tmp/report2.sql
Enter product id: 4

PRODUCT_ID product_name    PRICE
---------- ------------- -------
     4 Tank War      $13.95

vim /tmp/report3.sql

--suppress display of the statements and verification message
set echo off                --禁止显示脚本中的SQL语句
set verify off                --禁止显示验证消息
select product_id,name,price
from products
where product_id = &1;        --向脚本中的变量传递值
SQL> @ /tmp/report3.sql 4

PRODUCT_ID product_name    PRICE
---------- ------------- -------
     4 Tank War      $13.95

vim /tmp/report4.sql

--suppress display of the statements and verification message
set echo off                --禁止显示脚本中的SQL语句
set verify off                --禁止显示验证消息
select product_id,product_type_id,name,price
from products
where product_id = &1
and price > &2;                --向脚本中的多个变量传递值

vim /tmp/report5.sql

--添加页眉
ttitle left ‘Run date: ‘ _date center ‘Run by the‘ sql.user ‘ user‘ right ‘Page: ‘ format 999 sql.pno skip 2
--添加页脚
btitle center ‘Thanks for running the report‘ right ‘Page: ‘ format 999 sql.pno

set echo off
set verify off
set pagesize 15
set linesize 70
clear columns
column product_id heading id format 99
column name heading ‘Product Name‘ format a20 word_wrapped
column description heading Description format a30 word_wrapped
column price heading Price format $99.99

select product_id,name,description,price
from products;

clear columns
ttitle off
btitle off

vim /tmp/report6.sql

--计算小计
break on product_type_id                    --根据列值的范围分隔输出结果
compute sum of price on product_type_id        --计算一列的值
set echo off
set verify off
set pagesize 20
set linesize 70

clear columns
column price heading Price format $999.99

select product_type_id,name,price
from products
order by product_type_id;

clear columns

10.帮助信息

help
help index

11.自动生成SQL语句

select ‘drop table ‘ || table_name||‘;‘
from user_tables
order by table_name;

‘DROPTABLE‘||TABLE_NAME||‘;‘
------------------------------------------
drop table CUSTOMERS;
drop table EMPLOYEES;
drop table PRODUCTS;
drop table PRODUCT_TYPES;
drop table PURCHASES;
drop table SALARY_GRADES;

【参考资料】

[1] Jason Price.精通Oracle Database 12c SQL&PLSQL编程(第3版).[M].北京:清华大学出版社,2014

时间: 2024-10-31 03:37:50

oracle 开发 第3章 sqlplus的相关文章

转 oracle 开发 第03章 sqlplus

目录 1.查看表结构 desc 2.编辑SQL语句 append.list.change.run 3.保存.检索并运行文件 save.get.start.edit.spool 4.格式化列 column 5.设置页面大小 pagesize 6.设置行大小 linesize 7.清除列格式 clear 8.使用变量 define 9.创建简单报表 10.帮助信息 help 1.查看表结构 desc DESC customers; 2.编辑SQL语句 append.list.change.run S

oracle 开发 第1章 简介

2016-01-04 一.DCL语句1.创建用户store create user store identified by store_password; 2.授予权限connect.resource grant connect,resource to store; 二.DDL语句1.创建customers表 create table customers ( customer_id integer constraint customers_pk primary key, first_name v

oracle 开发 第4章 函数

2016-01-07 目录 一.字符函数 二.数值函数 三.转换函数 四.正则表达式函数 五.聚合函数 六.分组语句 一.字符函数1.ASCII(x)和CHR(x) --获得字符x的ASCII码 select ascii('a'),ascii('A'),ascii('z'),ascii(0),ascii(9) from dual; --获得ASCII码为x的字符 select chr(97),chr(65),chr(122),chr(48),chr(57) from dual; 2.CONCAT

oracle 开发 第2章 查询

2016-01-05 1.选择列 select customer_id,first_name,last_name,dob,phone from customers; select * from customers; 2.选择行 select * from customers where customer_id =2; 3.行标识符 select rowid,customer_id from customers; 4.行号 select rownum,customer_id,first_name,

oracle学习 第三章 常用的SQL*PLUS命令 ——02

今天接着昨天的RUN命令继续讲. 3.5 n(设置当前行)命令和A(PPEND)(附加)命令 设想,你输入了例3-10的查询语句 例 3-10 SQL> SELECT ename 2 FROM emp; 例 3-10 结果 看到以上输出时,您发现在SELECT子句中忘了job,sal.这时您又如何修改您的SELECT子句呢?首先您应该使用SQL*PLUS的L(LIST)命令来显示SQL缓冲中的内容. 例 3-11 SQL> L 例 3-11 结果 在例3-11显示的结果中,2后面的"

Asp.Net MVC4 + Oracle + EasyUI + Bootstrap 序章

Asp.Net MVC4 + Oracle + EasyUI + Bootstrap 序章 -- 新建微软实例 1.  简介 为学习MVC4而写的文章,以项目开发的角度书写.循序渐进,同时也是自己学习的过程,博文编写过程中,我会参考相关专业书籍和网站,尽量编写和描述规范的编码和原理.若是过程中有错误,请各位看官不吝啬指出! 2.  博文定位 新手教程,所以适合刚刚准备学习MVC4的博友阅读.不过会讲述很多原理,个人认为,目的是学会如何操作,但是原理也同样重要.授人以鱼不如授人以渔. 3.  初次

oracle开发系列(三)exists&amp;not exists用法(10g)

注:以下内容适合 初学oracle开发或者java等开发者,高手略过 一 exists&in 以下三个语句  功能都是从 iodso.qos_hisentry_sheet_jtext_td 里面找到  sheet_no在  iodso.qos_hisentry_sheet_td表 arch_time 1天时间里面的单子. iodso.qos_hisentry_sheet_jtext_td 有个普通的联合索引                   iodso.qos_hisentry_sheet_t

Android深度探索HAL与驱动开发 第四章

Android深度探索HAL与驱动开发 第四章 源代码的下载和编译 读书笔记 一.下载编译和测试Android源代码 1.配置Android源代码下载环境 创建一个用于存放下载脚本文件的目录(可将该文件放到任何一个目录在这里使用-/bin) # mkdir ~/bin # PATH=~/bin:$PATH 2.下载repo脚本文件(用于下载Android源代码) # curl htttps://dl-ssl.google.com/dl/googlesource/git-repo/repo> ~/

Android深度探索HAL与驱动开发 第三章

Android深度探索HAL与驱动开发 第三章 Git使用入门 读书笔记 Git是对源代码进行管理的软件. 一.安装Git # apt-get install git # apt-get install git-doc git-emall git-gui gitk 用以下命令控制Git: # apt-get install git-core # apt-get install git-doc git-svn git-email git-gui gitk 二.查看Git文档 查看git-check