谨把这篇文章作为复习Oracle(只有相关的SQL语句部分,不涉及Oracle工具

的其他诸如回复容灾等等)之用。让各位大虾在偶然用到但却忘记的时候过来看看,正要学习的小虾们也过来看看……

     下面的SQL语句是创建序列、表,以及简单修改表结构、向表中插入数据、常用简单的SQL查询(一般的分页语句都是封装过了的,直接写句子的并不常见)。

其中,在创建sequence的时候,有一个nocache(默认)表示不创建序列缓存。大家熟知,一般情况下创建缓存是可以提高效率的,那还有什么区别呢?我们来做个实验:假设seq是start with 1 increment by 1 cache 20,你新建了15条数据,你的缓存中还有16,17,18,19,20这5个索引在缓存中未使用,突然 在这个时候宕机了,或者直接说由于某种原因数据库重启了(你的缓存就没了哈,)。但是你的索引再次使用的时候,你会发现是从21-40的,这就是是否使用缓存的区别了。nocache中能保证所有的索引都是连续的,……

--序列
create sequence teaSeq
minvalue
1
increment
by 1
start
with 1
nocache;

create sequence stuSeq
minvalue
1
increment
by 1
start
with 1
nocache;

--
create table Teacher (
teaid
int,
tname
varchar2(15),
sex
char(2) check(sex='' or sex=''),
age
int,
phonenumber
varchar2(15),
taddress
varchar2(30),
primary key(teaid)
);

create table Student(
stuid
int,
sname
varchar2(15),
sex
char(2),
age
int,
teacherid
int,
phonenumber
varchar2(12),
address
varchar2(30),
primary key(stuid),
foreign key(teacherid) references Teacher(teaid)
);

-- modify column
alter table Student modify phonenumber varchar2(15);
-- add constraint
alter table Student add constraint sex check(sex in ('',''));
-- rename column
alter table Student rename column address to saddress;

-- insert data/添加数据
insert into Teacher values(teaSeq.nextval, '张孝祥', '', 33, '13826754208', '中国河南省南阳市');
insert into Teacher values(teaSeq.nextval, '李清照', '', 43, '13426754204', '中国上海');
insert into Teacher values(teaSeq.nextval, '赵子龙', '', 28, '13626754206', '中国浙江省杭州市');
insert into Teacher values(teaSeq.nextval, '李小龙', '', 36, '13526754205', '中国湖北省武汉市');
insert into Teacher values(teaSeq.nextval, '武则天', '', 45, '13226754202', '中国广东省深圳市');
insert into Teacher values(teaSeq.nextval, '西施', '', 21, '13126754201', '中国香港九龙');
insert into Teacher values(teaSeq.nextval, '诸葛亮', '', 27, '13726754207', '中国广西省南宁市');
insert into Teacher values(teaSeq.nextval, '王昭君', '', 18, '13026754200', '中国云南省昆明市');
insert into Teacher values(teaSeq.nextval, '王永民', '', 56, '13326754203', '中国青海省西宁市');
insert into Teacher values(teaSeq.nextval, '于丹', '', 31, '13926754209', '中国陕西省西安市');

insert into Student values(stuSeq.nextval, '影影', '', 21, 1, '1234567891', '中国广西省南宁市');
insert into Student values(stuSeq.nextval, '寒梅', '', 22, 1, '1234567892', '中国广西省南宁市');
insert into Student values(stuSeq.nextval, 'Lily', '', 23, 1, '1234567893', '中国广东省深圳市');
insert into Student values(stuSeq.nextval, 'Lucy', '', 24, 2, '1234567894', '中国广东省深圳市');
insert into Student values(stuSeq.nextval, 'Tom', '', 25, 3, '1234567895', '中国西藏拉萨市');
insert into Student values(stuSeq.nextval, 'Alice', '', 26, 3, '1234567896', '中国广西省南宁市');
insert into Student values(stuSeq.nextval, 'Jack', '', 27, 2, '1234567897', '中国广西省南宁市');
insert into Student values(stuSeq.nextval, 'Jack', '', 27, 4, '1234567897', '中国广西省南宁市');
commit;

select user from dual;
select sysdate from dual;

-- 最简单的关连查询
select * from Student stu, Teacher tea where teacherid = teaid; -- 等效于全连接
select * from Student stu left join Teacher tea on teacherid=teaid; -- 左连接
select * from Student stu right join Teacher tea on teacherid=teaid; -- 右连接
select * from Student stu join Teacher tea on teacherid=teaid; -- 全连接
--
很奇怪,当查询id的时候distinct就无效了(各路神仙,求解)……
select distinct sname, sex, age, saddress from student stu;
select * from Teacher order by age desc; -- asc(default)/desc

     PROCEDURE and EXCEPTION/存储过程和异常

存储过程的结构:

CREATE OR REPLACE PROCEDURE 存储过程名字(参数1 IN NUMBER,  参数2 OUT NUMBER,  ...)
IS|AS
变量1 INTEGER :=0;
变量2 DATE;
BEGIN
     ...
     exception
         when VALUE_ERROR/NO_DATA_FOUND/TOO_MANY_ROWS then
              dbms_output.put_line('...');
         when OTHERS then
              dbms_output.put_line('...');
END 存储过程名字;

-- no parameter
create or replace procedure Proc1
is
begin
Dbms_Output.put_line(
'Hello world!'||chr(10)||'I am a happy DBA secooler');
end;
-- parameter: in(default)
create or replace procedure proc2(stname student.sname%type)
is
address student.saddress
%type;
begin
select saddress into address from student where sname=stname;
dbms_output.put_line(
'Name:'||stname||' Address:'||address);
exception
when VALUE_ERROR then
dbms_output.put_line(
'赋值错误/parameter error!');
when NO_DATA_FOUND then
dbms_output.put_line(
'No data!');
when TOO_MANY_ROWS then
dbms_output.put_line(
'Too many rows!');
when OTHERS then
dbms_output.put_line(
'Other error!');
end;
-- parameter: in out out out
create or replace procedure proc3(stid in student.stuid%type, stname out student.sname%type, stphone out student.phonenumber%type, stuadd out student.saddress%type)
as countnumber number;
begin
select count(*) into countnumber from student where stuid=stid;
if countnumber=1 then
select phonenumber into stphone from student where stuid=stid;
select saddress into stuadd from student where stuid=stid;
select sname into stname from student where stuid=stid;
else
dbms_output.put_line(
'返回值过多');
end if;
end;
-- use these procedures/调用存储过程.
declare
stuid student.stuid
%type;
stuname student.sname
%type;
stuphone student.phonenumber
%type;
stuadd student.saddress
%type;
begin
proc1;
-- 调用存储过程proc1
stuname :='Lucy'; -- 调用输入参数前必须先赋值,输出参数不必要
proc2(stuname); -- use proc2
stuname :='Jack';
proc2(stuname);
-- Too many rows error.
stuid := 1;
proc3(stuid,stuname,stuphone,stuadd);
-- use proc3
dbms_output.put_line('Name:'||stuname||' Phone:'||stuphone||' Address'||stuadd);
proc3(stid
=>2,stphone=>stuphone,stname=>stuname,stuadd=>stuadd); -- 此种写法参数顺序可变
dbms_output.put_line('Name:'||stuname||' Phone:'||stuphone||' Address'||stuadd);
end;

-- VALUE_ERROR
declare
stuid student.stuid
%type;
begin
stuid :
='ss'; -- value_error
exception
when VALUE_ERROR then
dbms_output.put_line(
'赋值错误');
when OTHERS then
dbms_output.put_line(
'其它错误');
end;
-- user-defined exception/自定义异常
declare
stuname student.sname
%type;
VALUE_NOT_EXIST exception;
begin
stuname :
= 'd';
if stuname not in('a','b','c') then
raise VALUE_NOT_EXIST;
end if;
exception
when VALUE_NOT_EXIST then
dbms_output.put_line(
'该值不存在');
when OTHERS then
dbms_output.put_line(
'其它错误');
end;

作者: 雨之殇 发表于 2011-08-24 15:38 原文链接

推荐.NET配套的通用数据层ORM框架:CYQ.Data 通用数据层框架
新浪微博粉丝精灵,刷粉丝、刷评论、刷转发、企业商家微博营销必备工具"