一.    了解数据库
1.1数据库
数据库是以某种方式有组织的方式存储的数据集合

数据相当于文件柜
而表相当于文件


1.2. 表
表是一种结构化文件,可用来存储某种特定类型的数据

1.3.列
列是存储表中的某部分数据
列是表中的一个字段

1.4 行
表中的数据是按行存储的,所保存的每条数据存储在自己的行内。
行是表中的一条记录




1.1.5 主键
表中每一行都应该有一列可以唯一标识自己

表中每一行都应该有一列可以唯一标识自己

///////////////////////////////////////////////////////////////////////////////////////////
2.检索数据
检索单个列
1.select prod_name from Products;


检索多个列
1.select prod_id,prod_name,prod_price from Products;

检索所有列
1.select * from Products;

检索不同的值,办法就是使用DISTINCT关键字,它指示数据库只返回不同的值
1.select distinct vend_id from Products;

限制结果:
1.select prod_name from Products LIMIT 5; (mysql)
2.select prod_name from Products where ROWNUM <=5; (ORACle 语句  ROWNUM 行计数器)

3.SELECT prod_name FROM Products LIMIT 5 OFFSET 5; (从哪开始以及其所行数 offset从哪开始)

////////////////////////////////////////////////////////////////////////////////////////////

三: 排序检索数据

排序数据
order by子句 使用的列 是 select后 要显示的列
1.select prod_name from Products order by prod_name;(在指定一条order by子句时,应该保证select语句最后一条子句)

按多个列排序

要按多个列排序,简单指明列名,列名之间用逗号分开即可
1.select prod_id,prod_price,prod_name from Products order by prod_price,prod_name;

按列的位置排序
order by还支持按列位置进行排序
select prod_id,prod_price,prod_name from Products order by 2,3;

按指定顺序排序;
升序排序是默认的排序方式;可以指定ASC
使用order by 子句进行降序排序 必须指定desc
select prod_id,prod_price,prod_name from Products order by prod_price DESC;

///////////////////////////////////////////////////////////////////////////////////////////////////////////

四。过滤数据

使用where子句
根据where子句中指定的搜索条件进行过滤
1.select prod_name,prod_price from Products where prod_price = 3.49;

检查单个值      (<>不等于   is null  是NULL值)
1.select prod_name,prod_price from Products where prod_price < 10;

不匹配检查
1.select vend_id,prod_name from Products where vend_id <> 'DLL01'

范围值检查
要检查某个范围的值,可以使用BETWEEN操作符,因为它需要两个值,即范围的开始值和结束值
select prod_name,prod_price from Products where prod_price between 5 and 10;

空值检查
select prod_name from Products where prod_price IS NULL;


////////////////////////////////////////////////////////////////////////////////////////////////////

五高级过滤查询

组合where子句查询
AND 操作符
select prod_id,prod_price,prod_name from Products where vend_id = 'DLL01' AND prod_price <=4;

or操作符
or操作符检索匹配一条的行
select prod_name,prod_price from Products where vend_id = 'DLL01' or vend_id = 'BRS01';

求值顺序
需要列出价格为10美元以上,且由DLL01或BRS01制造的所有产品
select prod_name,prod_price from Products where vend_id = 'DLL01' or vend_id = 'BRS01' AND prod_price >=10


select prod_name,prod_price from Products where (vend_id = 'DLL01' or vend_id = 'BRS01') AND prod_price >=10

IN操作符
IN操作符用来指定条件范围
select prod_name,prod_price from Products where vend_id in ('DLL01','BRS01') order by prod_name;
(in操作符完成了or相同的功能,in操作符比or操作符执行快)


NOT操作符
where子句中的NOT 操作符有且只有一个功能,那就是否定其后所跟的任何条件
列出除DLL01之外的所有供应商制造的产品
select prod_name from Products where NOT vend_id = 'DLL01' order by prod_name;

////////////////////////////////////////////////////////////////////////////////////////////////////////


六。用通配符进行过滤
利用通配符,可以创建比较特定数据的搜索模式

百分比%通配符
&表示任何字符出现的任意次数,不管有多少个字符

1.select prod_id,prod_name from Products where prod_name LIKE 'Fish%'

2.select prod_id,prod_name from Products where prod_name LIKE '%bean bag%';

3.select prod_name from Products where prod_name LIKE 'F%Y';

下划线_通配符
下划线_,它只能匹配单个字符,只能匹配一个
1.select prod_id,prod_name from Products where prod_name LIKE '_ inch teddy bear'

方括号[] 通配符,[JM]匹配任何以方括号中字母开头的联系人名,它也只能匹配单个字符
1.select cust_contact from Customers where cust_contact LIKE '[JM]%' order by cust_contact;

[] 方括号通配符表否定 用[^JM]
2.select cust_contact from Customers where cust_contact LIKE '[^JM]%' order by cust_contact;


////////////////////////////////////////////////////////////////////////////////////////////////////////

七 创建计算字段
拼接字段
Venders表包含供应商名和地址信息
可以用一个特殊的字符拼接两个列,用+或者||,,支持Oracle
1.select vend_name +'('+vend_country+')' from Venders order by vend_name;
2.select vend_name || '(' || vend_country || ')' from vendors order by vend_name;

MySQL 用Concat 拼接,CONCAT函数会将所有参数按照参数的顺序拼接成一个字符串做为
返回值
1.select Concat(vend_name,'(',vend_country,')') from Vendors order by vend_name;

许多数据库保存填充为列宽的文本值,而市级上你要的结果不需要这些空格
为了正确返回格式化的数据,必须去掉这些空格
select RTRIM(vend_name)+'('+RTRIM(vend_country)+')' from Vendors order by vend_name;
select CONCAT(RTRIM(vend_name),'(',RTRIM(vend_country),')') from Vendors order by vend_name; mysql

RTRIM去掉右边空格,LTRIM()去掉左边空格,TRIM()去掉中间空格


使用别名
别名是一个字段或者值的替换名,别名用AS关键字赋予
1、select RIRIM(vend_name) || '('||RIRIM(vend_ccuntry)||')' AS vend_title from Vendors order by vend_name;(ORACLE)
2.select RIRIM(vend_name) + '('+RIRIM(vend_ccuntry)+')' AS vend_title from Vendors order by vend_name;(ORACLE)
3.select Concat(vend_name,'(',vend_country,')') AS vend_title from Vendors order by vend_name;(mysql)


执行算术计算 + - * /
1.select prod_id,quantity,item_price,quantity*item_price AS expand_price from OrderItems where order_num = 20008;


/////////////////////////////////////////////////////////////////////////////////////////////////////////////////

八,使用数据处理函数

函数,SQL也可以用函数处理数据

提取字符串的组成部分
substr() oracle
substring() mysql

数据类型转换
CAST() oracle
CONVERT() MYSQL;

取出当前日期
CURDATE() mysql
SYSDATE() oracle

使用函数
文本处理函数
1.select vend_name,UPPER(vend_name) AS vend_name_upcase from Vendors order by vend_name;

upper()
lower()
LTRIM()
RTRIM()
TRIM()
RIGHT()
LEFT();


日期和时间处理函数
1.select order_num from Orders where to_number(to_char(order_date,'YYYY')) = 2012 (ORACLE语句)

to_char() 用来提取日期的成分  用于字符串
to_number() 用于转换成数字

2.select order_num from Orders where order_date BETWEEN to_date('01-01-2012') AND to_date('12-31-2012') (oracle语句)
to_date()函数用来将字符串转换成日期

3.select order_num from Orders  where date_format(order_date,'YYYY') = 2012 (MYSQL语句)
date_format(date,'%Y-%m-%d')     将日期转换成字符串
str_to_date(date,'%Y-%m-%d')     将字符串转换成日期

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////

九 汇总数据

聚集函数

AVG函数
AVG对表中行数计数并计算列值之和,该列的平均值
1.select AVG(prod_price) AS avg_price from Products;

COUNT()函数
count()确定表中行的数目或符合特定的条件的行的数目
1.select COUNT(*) AS num_cast from Customers;

MAX函数 返回列中的最大值
1.select MAX(prod_price) AS max_price from Products;

MIN函数 返回列中的最小值
1.select MIN(prod_price) AS max_price from Products;

SUM函数 返回指定列的总和
1.select SUM(quantity) AS items_ordered FROM OrderItems where order_num = 20005;

聚集不同的值  DISTINCT 不能用于count(*)
select AVG(DISTINCT prod_price) AS avg_price from products where vend_id = 'DLL01';

组合聚集函数
1.select COUNT(*) AS num_items,MIN(prod_price) AS price_min,MAX(prod_price) AS price_max,AVG(prod_price) AS price_avg from Products;

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

十 分组数据

创建分组
select  vend_id ,count(*) AS num_prods from Products GROUP BY vend_id;

在select语句的表达式必须在group by子句中指出
select语句中每一列必须在group by子句中指出
group by子句必须在where子句之后,order  by子句之前



过滤分组
where子句没有分组的概念
where过滤行,HAVING过滤分组

1.select cust_id,count(*) AS orders from Orders GROUP BY cust_id HAVING count(*)>=2;

2.列出具有两个以上产品其价格大于等于4的供应商
select vend_id,COUNT(*) AS num_prods from Products where prod_price >=4 group by vend_id having COUNT(*)>=2

分组和排序
select order_num,COUNT(*) AS items FROM OrderItems GROUP BY order_num HAVING COUNT(*)>=3 ORDER BY items,order_num;



select 子句顺序
SELECT
FROM
WHERE
GROUP BY
HAVING
ORDER BY


////////////////////////////////////////////////////////////////////////////////////////////

十一 使用子查询
利用子查询进行过滤

检索包含物品RGAN01的所有订单的编号
select order_num from OrderItems where prod_id = 'RGAN01';

检索订单编号的所有顾客的ID
select cust_id from Orders where order_num IN(20007,20008);

组合两个查询
select cust_id from Orders where order_num IN(select order_num from orderitems where prod_id = 'RGAN01');


SQL子句从里向外查询 ,用某个查询结果作为另一个查询的条件

检索这些顾客id的顾客信息
select cust_name,cust_contact from Customers where cust_id in(select cust_id from Orders where order_num IN(select order_num from orderitems where prod_id = 'RGAN01'));
子查询的SELECT语句只能查询单个列

检索出每个顾客,在Order表中的订单数目和 每个顾客信息
select cust_name,cust_state,(select count(*) from Orders where Orders.cust_id = Customers.cust_id) AS Orders from Customers order by cust_name;


////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

十二 联结表

1.关系表
关系表的设计就是要把信息分解成多个表,一类数据一个表。
各表通过某些共同的值互相关联(所以叫关系数据库)

创建联结
指定要联结的所有表以及关联它们的方式即可

select vend_name,prod_name,prod_price from Vendors,Products where Vendors.vend_id = Products.vend_id;

内联结
目前使用的联结称为等值联结,它基于两个表之间相等的测试
这种连接也称内联结

select vend_name,prod_name,prod_price from Vendors Inner join Products on Venders.vend_id = Products.vend_id;
指定条件用on指出

联结多个表
select prod_name ,vend_name, prod_price ,quantityy from OrderItems,Products,Vendors
where Products.vend_id = Vendors.vend_id And OrderItems.prod_id = Products.prod_id And order_num = 20007

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////


十三 创建高级联结
使用表别名
select cust_name ,cust_contact from Customers as c ,Orders as o ,OrderItems as oi
where c.cust_id = o.cust_id and oi.order_num = o.order_num and prod_id = 'RGAN01';

使用不同类型的联结
我们使用的只是内联结或等值联结的简单联结。
其他联结:自联结,自然联结,和外联结


自联结:
使用表别名的一个主要原因是能在一条select语句中不止一次引用相同的表

要给与jim jones同一公司的所有顾客发送一封信件
这个查询首先要找出jim jones工作的公司,然后找出在该公司工作的顾客

select cust_id ,cust_name,cust_contact from Customers
where cust_name = (select cust_name from Customers where cust_contact = 'Jim jones');

使用联结的相同的查询
select c1.cust_id ,c1.cust_name , c1.cust_contact from Customers as c1,Customers as c2
where c1.cust_name = c2.cust_name and c2.cust_contact = 'Jim jones';

自然联结:
自然联结排除多次出现,使每一列只返回一次。
自然联结要求 每列显示不许出现重复 一次显示出现一次
select c.* ,o.order_num,o.order_date, oi.prod_id,oi.quantity,oi.item_price FROM
customers as c, orders as o,orderitems as oi
where c.cust_id = o.cust_id
and oi.order_num =o.order_num
and prod_id = 'RGAN01';


外联结:
联结将一个表中的行与另一个表中的行相关联,
但有时候需要包含没有关联的那些行

联结包括了那些在相关表中没有关联行的行。
这种联结称为外联结

检索所有顾客及订单
select Customers.cust_id ,Orders.order_num from Customers Inner join Orders on Customers,cust_id = Orders.cust_id;

检索包括没有订单顾客在内的所有顾客
select Customers.cust_id ,Orders.order_num from Customers  Left outer join Orders on Customers.cust_id = Orders.cust_id;

与内联结关联两个表中的行不同的是,外联结还包括没有关联行的行。
right指出的是outer join 右边的表,而 left 指出的是outer join左边的表

使用带聚集函数的联结
要检索所有顾客及每个顾客所下的订单数,下面的代码使用COUNT()函数完成
select Customers.cust_id ,count(Orders.order_num) as num_ord from Customers Inner join Orders
on Customers.cust_id =Orders.cust_id  group by Customers.cust_id;

查询所有顾客及没有下订单的每个顾客的订单数
select Customers.cust_id,count(Orders.order_num) as num_ord from Customers left outer join Orders
on Customers.cust_id = Orders.cust_id group by Customers.cust_id

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

组合查询
多数SQL查询只包含从一个或多个表中返回数据的单条SELECT语句。
但是,SQL也允许执行多个查询(多条SELECT语句),并将结果作为一个查询结果集返回。
这种组合查询称为union或者符合查询语句

组合查询情况
在一个查询中从不同的表返回结构数据
对一个表执行多个查询,按一个查询返回数据


创建组合查询
可用UNION操作符数条SQL查询,
利用UNION,可给出多条SELECT语句,将它们的结果组合成一个结果集

需要Illinois、Indiana和Michigan等美国几个州的所有顾客报表,
还想包括不管位于哪个州的所有的Fun4All

首先 先 单条查询
select cust_name,cust_contact,cust_email
from Customers where cust_state in('IN','IL','MI');

select cust_name,cust_contact,cust_email
from Customers where cust_name = 'Fun4All';

组合两条语句
select cust_name,cust_contact,cust_email
from Customers where cust_state in('IN','IL','MI');
union
select cust_name,cust_contact,cust_email
from Customers where cust_name = 'Fun4All';


使用UNION的规则
UNION必须由两条或两条以上的SELECT 语句组成
UNION中每个查询必须包含相同的列,表达式或者聚集函数
UNION会自动去除了重复的行

使用UNION ALL 不取消重复行
select cust_name,cust_contact,cust_email
from Customers where cust_state in('IN','IL','MI');
union ALL
select cust_name,cust_contact,cust_email
from Customers where cust_name = 'Fun4All';

select语句的输出排序用ORDER BY 子句排序 ,它必须放在最后一条SELECT语句之后

////////////////////////////////////////////////////////////////////////////////////

插入数据
用INSERT将行插入或添加到数据
插入方式:
插入完整的行
插入行的一部分
插入某些查询的结果

insert into Customers values('100000006','Toy land','123 any Street','New York','NY','11111','USA',NULL,NULL);
必须给每一列提供一个值,某列没有值,应该使用null值,以它们在表定义中出现的次序填充

编写INSERT语句的更安全的方法如下
insert into Customers(cust_id,cust_name,cust_address,cust_city,cust_state,cust_zip,cust_country,cust_contact,cust_email)
values('10000000006','Toy land','123 any Street','New York','NY','111111','USA',NULL,NULL);


插入检索出的数据:
可以将select语句的结果插入表中(insert select 用一条insert语句 插入select查出的所有行数据)
insert into Customers(cust_id,cust_contact,cust_email,cust_name,cust_address,cust_city,cust_state,cust_zip,cust_country)
select cust_id,cust_contact,cust_email,cust_name,cust_address,cust_city,cust_state,cust_zip,cust_country from CustNew;


从一个表赋值到另一个表(mysql  oracle语法)
create table CustCopy AS
select * from customers;

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////


十六 更新和删除数据
更改表中的数据,可以使用UPDATE语句
update更新表名开始
set用来将新值赋予给被更更新的列

update Customers set cust_email = 'kim@thetoystore.com' where cust_id = '10000000005';

更新更多列:
update Customers set cust_contact = 'sam roberts',cust_email='sam@toyland.com' where cust_id ='10000000006';

要删除某个列的值,可以设置为null
update Customers set cust_email = NULL where cust_id = '10000000005';


删除数据
要删除一个表的数据,用DELETE语句
delete from Customers where cust_id = '10000000006';
delete from要求指定从中删除数据的表名,where子句过滤要删除的行

delete语句删除的是表中的数据而不是删除表
要删除表中所有行数据,可以用truncate table

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

十七 创建和操纵表

创建表可以用数据库表工具 和 SQL语句操作

表创建基础
利用create table创建表
新表的名字在create table之后指出
表列的名字和定义用 逗号分隔

create table Products(
 prod_id char(10) not null,
 vend_id char(10) not null,
 prod_name char(10) not null,
 prod_price decimal(8,2) not null,
 prod_desc varchar(1000) null
);

每列定义以列名开始,后跟列的数据类型
整条语句以圆括号的分号结束

使用NULL值
create table Products(
 prod_id char(10) not null,
 vend_id char(10) not null,
 prod_name char(10) ,
 prod_price decimal(8,2) ,
 prod_desc varchar(1000)
);
not null插入值
null没有的 可以插入null值

指定默认值
create table Products(
 prod_id char(10) not null,
 vend_id char(10) not null,
 prod_name char(10) not null,
 prod_price decimal(8,2) not null,
 quantity integer not null default 1,
 prod_desc varchar(1000) null
);
default 1如果有default 1,如果不指出数量则使用数量1



更新表
更新表定义,使用 alter table
针对已有表增加列
alter table Vendors add vend_phone char(20);

alter table Vendors drop column vend_phone;

删除表
删除表使用DROP TABLE语句
drop table CustCopy;  永久删除该表

///////////////////////////////////////////////////////////////////////////////////

使用视图

视图是虚拟的表,视图只包含使用时动态检索数据的查询

为什么使用视图?
简化复杂的SQL查询

视图仅仅是用来查看存储在别处数据的一种设施,
视图本身不包含数据,因此返回的数据是从其他表中检索出来的。
在添加或者更改这些表中的数据时,视图将返回改变过的数据


视图的规则和限制
1.与表一样,视图必须唯一命名
2.对于可以创建的视图没有限制
3.创建视图,必须具有足够的访问权限。
4.视图可以嵌套,即可以利用从其他视图中检索数据的查询来构造视图
5.禁止在视图中使用ORDER BY子句
6.要求返回所有列进行命名。如果列是计算字段,则需要使用别名
7.视图不能索引,也不能有关联的触发器或者默认值
8.视图作为只读的查询,这表示可以从视图检索数据,但不能将数据写回底层表
9.允许创建这样的视图,它不能进行导致行不再属于视图的插入或更新



创建视图
视图用create view语句来创建
视图从命名使用 drop view viewname


利用视图简化复杂的联结
视图应用是隐藏复杂的SQL,这通常涉及到联结
create view ProductCustomers AS
select cust_name,cust_contact,prod_id
from Customers,Orders,OrderItems
where Customers.cust_id = Orders.cust_id
and OrderItems.order_num = Orders.order_num;

创建一个名为ProductCustomers的视图,返回联结三个表的数据

检索订购了产品RGAN01的顾客
select cust_name,cust_contact from ProductCustomers where prod_id = 'RGAN01';
视图简化了复杂SQL语句的使用

用视图重新格式化检索出的数据
视图的另一常见用途是重新格式化检索出的数据
create view VendorLocations AS
select RIRIM(vend_name)+ '('+RIRIM(vend_country)+')' as vend_title from Vendors

select * from VendorLocations;

用视图过滤不想要的数据
create view CustomersEMailList AS
select cust_id,cust_name,cust_email
from Customers
where cust_email is not null;

select * from CustomersEMailList;

使用视图与计算字段
create viem OrderItemsExpanded AS
select order_num,prod_id,quantity,item_price,quantity*item_price as expand_price
from OrderItems

select * from OrderItemsExpanded where order_num = 20008;

视图提供了一种封装select语句的层次,可用来简化数据处理,重新格式化或保护基础数据

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

十九 使用存储过程

存储过程就是为以后使用而保存的一条或多条SQL语句
将其视为批文件

为什么使用存储过程?
1.通过把处理封装在以一个易用的单元中,可以简化复杂的操作
2.由于不要求反复建立一系列处理步骤,因而保证了数据的一致性,
如果所有开发人员和应用程序都使用同一存储过程,则所使用的代码都是相同的
这一点的延伸就是防止失误
3.简化对变动的管理
4.因为存储过程通常以编译过的形式存储,提高了性能
5.存在一些只能用单个请求中的SQL元素和特性,存储过程可以使用它们来编写功能更强更灵活的代码
6.存在一些只能用在单个请求中的SQL元素的特性,存储过程可以使用它们编写更强更灵活的代码

执行存储过程:
执行存储过程的SQL语句很简单,即EXECUTE
EXECUTE接受存储过程名和需要传递给它的任何参数

EXECUTE AddNewProduct('jts01',
                      'Stuffed eiffel tower',
                      '6.39',
                      'Plush stuffed toy with the text laTour Eiffl in red white and blue');
                      
这里执行一个存储过程,将新产品添加到Product中,并将传入的属性赋值给响应的列

创建存储
例子:它对邮件发送清单中具有邮件地址的顾客进行计数

Oracle版本
create Procedure MailingListCount( ListCount out INTEGER) IS v_rows INTEGER;
BEGIN
 SELECT COUNT(*) INTO v_rows
 from Customers
 where NOT cust_email is null
 ListCount := v_rows;
END;
此存储过程有一个名为ListCount的参数,
此参数从存储过程返回一个值不是传递一个值给存储过程
关键字out 用来指示这种行为 IN传递值给存储过程
OUT从存储过程返回值
INOUT即传递也可以返回值
存储过程的代码从BEGIN和END语句中
执行SELECT语句 检索具有邮件地址的顾客 然后检索出设置ListCount

调用ORacle的例子
var ReturnValue NUMBER
EXEC MailingListCount(:ReturnValue);
SELECT ReturnValue;

这段代码声明一个变量来保存存储过程返回的值
然后执行存储过程,再使用select语句显示返回的值

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////

二十 管理事务处理
事物处理是一种机制,用来管理必须成批执行的SQL操作,
保证数据库不包含不完整的操作结果,利用事物处理,可以保证一组操作不会中途停止,
它们要么完全执行,要么完全不执行。如果没有错误发生,整组语句提交给数据库表
如果错误发生,则进行回退,将数据库恢复到某个已知且安全的状态


事物处理的几个关键字
事务transaction  指一组SQL语句
回退rollback     指撤销指定SQL语句的过程
提交commit       指将未存储的SQL语句结果写入数据库表
保留点savepoint  指事物处理中设置临时占位符

控制事务
事务处理块的开始和结束
MYSQL:start transaction

Oracle:set transaction


使用ROLLBACK
SQL的ROLLBACK命令用来回退
DELETE FROM Orders;
ROLLBACK

使用COMMIT
一般SQL语句都是针对数据库表直接执行和编写的,
这就是所谓的隐士提交,即提交自动操作
在事物处理块中,提交不会隐式进行

oracle:
set transaction
delete FROM OrderItems where order_num = 12345;
commit;

使用保留点
要支持回退部分事务,必须在事务处理块中的合适位置放置占位符
这样,如果需要回退,可以回退到某个占位符
在SQL中这些占位符称为保留点

SAVEPOINT delete1;

mysql,oracle:
ROLLBACK TO delete1;

//////////////////////////////////////////////////////////////////////////////////////////////////

二十一 使用游标

有时,需要在检索出来的行中前进或后退一行或多行,
这就是游标的用途所在

能够标记游标为只读,使数据能读取,但不能删除和更新
能控制可以执行的定向操作
能标记某些列为可编辑的,某些列为不可编辑的
规定范围,使游标对创建它的特定请求或对所有请求可访问
对检索出的数据进行赋值,使数据在游标打开或访问期间不变化


使用游标
OPEN CURSOR CustCurosr
OPEN CURSOR语句,执行查询 存储检索出的数据以供浏览和滚动
现在可以用FETCH语句访问游标数据。
FETCH指出要检索哪些行,从何处检索它们以及将它们放于何处

DECLARE TYPE CustCursor IS REF CURSOR
RETURN Ccustomers%ROWTYPE;
DECLARE CustRecord customers%ROWTYPE
BEGIN
  OPEN CUstCursor;
  FETCH CustCursor INTO CustRecord;
  CLOSE CustCursor;
END;

关闭游标
CLOSE CustCusor







posted on 2016-02-24 21:21  Sharpest  阅读(316)  评论(0编辑  收藏  举报