整理的Oracle精辟问答题(5)
1.pl/sql是一种什么语言?
-- 是结合了Oracle过程语言和结构化查询语言的一种扩展语言
2.构成pl/sql程序的基本单元是什么?
--逻辑块
3.pl/sql块中的匿名块是什么意思?
是一个未在数据块里命名的
4.pl/sql块由哪几部分组成?
--过程,函数,匿名块
5.pl/sql块中能不能使用ddl语句?
--不能 只能用DML 和 TCL
6.<<>>在pl/sql块中是用来作什么的?
--标签分隔符
7.在pl/sql中如何给一个变量赋值?
--declare 变量名 类型(值)
8.请定义vqrchar2类型变量,长度20,初始值为china?
--declare china varchar2(20)
9.数据类型positive,positiven,signtype,real,float,integer,pls_integer分别是什么意思?可用在什么地方?
positive 可以限制变量存储正整数
positiven 可以限制变量存储在正整数,且非空
signtype 可以限制变量只存储值 -1,0和,1三个值
real 用于声明最高精度为38位的十进制数字的整数
float 用于声明最高精度为126位的二进制数字的浮点数
integer 用于声明最高精度为38位的十进制数字的整数
pls_integer 用于存储带符号的整数
10.raw用来存储什么类型的数据,它的最大长度是多少?可以将raw类型的变量插入到数据库表的raw类型字段中吗?
用于存储二进制数据或字节串。最大长度是2000个字节
不能向RAW列中插入长度大于2000个字节的RAW的值。
但可以向LONG RAW数据库中插入任何RAW的值
11.pl/sql下的long是什么类型,长度是多少?
是可变的长度字符串 2GB
12.long raw变量和long raw列有什么区别?
long raw变量
long raw列 可以向long列中插入任何long变量值
13.lob数据类型的数据库列存储的内容是什么?
文本 , 图像图形,视频剪辑,声音剪辑
14.空Lob指的是一个什么样的lob?它是什么函数来完成的?
空LOB是一个具有定位器且长度为零的LOB, 可以通过(EMPTY_BLOB()或EMPTY_CLOB())
15.如何向BFILE类型的列中插入数据?如有表books(chapter varchar2(10),content bfile),
如何向表中插入一条数据chapter='chapter 1',content指向c:"bookdata"book01.doc文件呢.写出完成步骤?
(1.)创建表 create table books
(
chapter varchar2(10),
content clob
)
drop table books
select * from books
(2.)在system用户下创建目录
create or replace directory bookdata as 'J;"my project"Oracle"work'
(3.)在system用户下给lee授权
grant read on directory bookdata to lee
(4.)
declare
l_bfile bfile;
l_clob clob;
begin
insert into books(chapter,content) values ('01',empty_clob()) return content into l_clob;
l_bfile := bfilename('BOOKDATA','"001.txt');
dbms_lob.open(l_bfile,dbms_lob.file_readonly);
dbms_lob.loadfromfile(l_clob,l_bfile,
dbms_lob.getlength(l_bfile));
dbms_lob.close(l_bfile);
commit;
end;
--
declare
l_bfile bfile;
l_clob clob;
begin
insert into student(stuid,memo) values('01',empty_clob())
return memo into l_clob;
l_bfile := bfilename('STUCLOB','"0001.txt');
dbms_lob.open(l_bfile,dbms_lob.file_readonly);
dbms_lob.loadfromfile(l_clob,l_bfile,
dbms_lob.getlength(l_bfile));
dbms_lob.close(l_bfile);
commit;
end;
empty
declare
clob_var clob;
amount integer;
offset integer;
output_var varchar2(1000);
begin
select content into clob_var
from books where chapter = '01';
amount := 24;
offset := 100;
dbms_lob.read(clob_var,amount,offset,output_var);
dbms_output.put_line(output_var);
end;
16.有表student(stuid varchar(2),pic blob),如何向表中插入一条数据stuid=0001,pic的内容为c:"bookdata"image"0001.jpg呢?写入完整步骤?
create table student1(stuid varchar2(2),pic blob)
select * from student1
declare
pic_file bfile;
pic_blob blob;
begin
insert into student1 values ('1',empty_blob())
return pic into pic_blob;
pic_file := bfilename('STUDENT','"photo.jpg');
dbms_lob.open(pic_file,dbms_lob.file_readonly);
dbms_lob.loadfromfile(pic_blob,pic_file,dbms_lob.getlength(pic_file));
dbms_lob.close(pic_file);
commit;
end;
select * from student1
17.有表student(stuid varchar(2),memo clob),如何向表中插入一条数据stuid=0001,memo的内容为c:"bookdata"memo"0001.dat呢?写入完整步骤?
declare
myfile bfile;
mylob blob;
begin
insert into student1 values ('2',empty_blob())
return pic into mylob;
myfile := bfilename('STUDENT','"my.dat');
dbms_lob.open(myfile,dbms_lob.file_readonly);
dbms_lob.loadfromfile(mylob,myfile,dbms_lob.getlength(myfile));
dbms_lob.close(myfile);
commit;
end;
select * from student1
18.如何来定义一个变量,使它与表student中的mark字段同类型,以致将来在修改mark字段的类型时不用再修改变量的类型。
mark_ajh student.mark%type;
declare
temp student.stuid%type;
begin
select stuid into temp from student1 where stuid = 1;
commit;
end
19.如何来定义一个变量,使它与先前定义的变量nowMoney同类型?
money_ajh nowMoney%type;
declare
变量 表名.列名%type;
begin
select 列名 into 变量 from 表名 where 条件;
commit;
end;
20.在学习下一章时,我们将用到游标,游标为我们提供了依次访问行的机制,
在SQL SERVER中我们是分别依据每个字段定义变量,
再将当前游标的字段值取到我们的变量中,
此时,当我们在游标中增加字段时,我们还需增加变量的定义,
同时还要修改fetch操作中的变量,在oracle中有什么好的方法吗?
现有表student,如何来定义变量呢?
--declare 变量 用户名.表名%rowtype
21.if ... then ...end if
22.if ...then
....
elseif ... then
...
end if
正确吗?
--if ...then ... else ... end if...
23.declare
a varchar2(20);
begin
a:='&a';
if a=1 then
else
dbms_output.put_line(a);
end if;
end;
以上语句正确吗?
--不对
declare
a varchar2(20);
begin
a:='&a';
if a=1 then dbms_output.put_line(2);
else
dbms_output.put_line(a);
end if;
end;
24.set serveroutput on 有什么含义?
--set serveroutput on = dbms_output.put_line(25)
--可將打印的結果顯示在屏幕上
25.以下语句正确吗?
case a
when '1' then ....;
when '2' then ....;
default .....;
end case;
错在哪儿?
-- case 选择器('&selector')
--when '1' then ....;
--when '2' then ....;
--else .....;
-- end case;
26.如果写一个case语句来判断成绩信息mark,当大于90时输出'优秀',大于80时输出‘良好’,大于70时输出一般,大于60输出及格,小于60时输出未通过?
select (case when s_mark < 100 and s_mark > 80 then '优异'
when s_mark <= 80 and s_mark >= 60 then '优秀'
when s_mark <= 59 and s_mark >= 40 then '良好'
when s_mark < 40 and s_mark >= 20 then '一般'
when s_mark <= 19 and s_mark > 0 then '较差'
end ) as 成绩 from stumark
27.case和decode (P61)语句有什么区别?分别在什么场合使用?
SELECT CASE SIGN(5 - 5) WHEN 1 THEN 'Is Positive' WHEN -1 THEN 'Is Negative' ELSE 'Is Zero' END FROM DUAL;
select decode (条件,结果1,输出1,结果2,输出2)
SELECT DECODE(SIGN(5-9), 1, 'Is Positive', -1, 'Is Negative', 'Is Zero')FROM DUAL
28.基本的循环语句格式是什么?如何退出循环?
set serveroutput on
loop
end
29.oracle下的循环语句有哪几种,有什么共同的地方?
loop
while
for
用于重复执行一系列的语句
30.请建表student(stuid varchar2(5),stuname varchar2(20),clsid varchar2(6),mark number(4,1)),
写一个循环语句向表中插入1000行数据,
stuid,stuname使用dbms_random包的string函数来生成值,mark用value来生成值,clsid要求是vb,sql,c#,oracle,c内容。
drop table xunhuan
create table xunhuan
(stuid varchar2(200),stuname varchar2(2000),clsid varchar2(6),mark number(4,1))
select dbms_random.string('~' ,1000) from dual
select * from xunhuan
declare i int
for i in 1..1000
loop
case i := i%5
when 1 then insert into xunhuan values(i,dbms_random.string('~' ,i/1000),'vb',i);
when 2 then insert into xunhuan values(i,dbms_random.string('1' ,i/1000),'sql',i*5);
when 3 then insert into xunhuan values(i,dbms_random.string('a' ,i/1000),'c#',i*5);
when 4 then insert into xunhuan values(i,dbms_random.string('d' ,i/1000),'oracle',i*5);
else insert into xunhuan values(i,dbms_random.string('a' ,i/1000),'ava',i*5);
end case;
end loop;
end;
select * from xunhuan
31.oracle的标签语句是什么?如何来标识一个标签?标签后可以没有语句吗?
<<>>
<<标签体>>
declare
不可以
select * from stumark
declare
smark stumark.s_mark%type;
sname stumark.s_name%type;
begin
select s_mark,s_name into smark ,sname from stumark where s_id = 5;
if smark > 60 then goto up1;
else goto quit;
end if;
<<up1>>
dbms_output.put_line('123456');
<<quit>>
null;
end;
32.
begin
a:=1
if a=1 then
goto ta;
else
goto tb;
end if;
<<ta>>
dbms_output.put_line('aaaaa');
goto quit;
<<tb>>
dbms_output.put_line('bbbbb');
<<quit>>
null;
end;
请问上面语句会输出什么?有什么改进方法吗?
begin
declare
if a=1 then goto ta;
else goto tb;
end if;
<<ta>>
dbms_output.put_line('aaaaa');
goto quit;
<<tb>>
dbms_output.put_line('bbbbb');
<<quit>>
null;
end;
33.如何来运行动态的SQL语句?动态的SQL语句是指什么?运行时有什么限制?
34.如何在运行动态的SQL语句时指定参数?
35.请写一个动态的SQL程序来实现查询题30中的学生信息,要求输入学生号,然后根据动态查询输出学生的成绩?输出学号9999时退出。
36.当查询不到记录时会触发何种异常?
37.在使用select into语句时,如果记录数大于1条,会出现哪种异常?
38.如何来定义异常,如何来触发异常?
39.从38题中我们看到,自定义异常其实完全不必要通过异常来进行?那为什么还要这样做呢?有什么好处?
-- 是结合了Oracle过程语言和结构化查询语言的一种扩展语言
2.构成pl/sql程序的基本单元是什么?
--逻辑块
3.pl/sql块中的匿名块是什么意思?
是一个未在数据块里命名的
4.pl/sql块由哪几部分组成?
--过程,函数,匿名块
5.pl/sql块中能不能使用ddl语句?
--不能 只能用DML 和 TCL
6.<<>>在pl/sql块中是用来作什么的?
--标签分隔符
7.在pl/sql中如何给一个变量赋值?
--declare 变量名 类型(值)
8.请定义vqrchar2类型变量,长度20,初始值为china?
--declare china varchar2(20)
9.数据类型positive,positiven,signtype,real,float,integer,pls_integer分别是什么意思?可用在什么地方?
positive 可以限制变量存储正整数
positiven 可以限制变量存储在正整数,且非空
signtype 可以限制变量只存储值 -1,0和,1三个值
real 用于声明最高精度为38位的十进制数字的整数
float 用于声明最高精度为126位的二进制数字的浮点数
integer 用于声明最高精度为38位的十进制数字的整数
pls_integer 用于存储带符号的整数
10.raw用来存储什么类型的数据,它的最大长度是多少?可以将raw类型的变量插入到数据库表的raw类型字段中吗?
用于存储二进制数据或字节串。最大长度是2000个字节
不能向RAW列中插入长度大于2000个字节的RAW的值。
但可以向LONG RAW数据库中插入任何RAW的值
11.pl/sql下的long是什么类型,长度是多少?
是可变的长度字符串 2GB
12.long raw变量和long raw列有什么区别?
long raw变量
long raw列 可以向long列中插入任何long变量值
13.lob数据类型的数据库列存储的内容是什么?
文本 , 图像图形,视频剪辑,声音剪辑
14.空Lob指的是一个什么样的lob?它是什么函数来完成的?
空LOB是一个具有定位器且长度为零的LOB, 可以通过(EMPTY_BLOB()或EMPTY_CLOB())
15.如何向BFILE类型的列中插入数据?如有表books(chapter varchar2(10),content bfile),
如何向表中插入一条数据chapter='chapter 1',content指向c:"bookdata"book01.doc文件呢.写出完成步骤?
(1.)创建表 create table books
(
chapter varchar2(10),
content clob
)
drop table books
select * from books
(2.)在system用户下创建目录
create or replace directory bookdata as 'J;"my project"Oracle"work'
(3.)在system用户下给lee授权
grant read on directory bookdata to lee
(4.)
declare
l_bfile bfile;
l_clob clob;
begin
insert into books(chapter,content) values ('01',empty_clob()) return content into l_clob;
l_bfile := bfilename('BOOKDATA','"001.txt');
dbms_lob.open(l_bfile,dbms_lob.file_readonly);
dbms_lob.loadfromfile(l_clob,l_bfile,
dbms_lob.getlength(l_bfile));
dbms_lob.close(l_bfile);
commit;
end;
--
declare
l_bfile bfile;
l_clob clob;
begin
insert into student(stuid,memo) values('01',empty_clob())
return memo into l_clob;
l_bfile := bfilename('STUCLOB','"0001.txt');
dbms_lob.open(l_bfile,dbms_lob.file_readonly);
dbms_lob.loadfromfile(l_clob,l_bfile,
dbms_lob.getlength(l_bfile));
dbms_lob.close(l_bfile);
commit;
end;
empty
declare
clob_var clob;
amount integer;
offset integer;
output_var varchar2(1000);
begin
select content into clob_var
from books where chapter = '01';
amount := 24;
offset := 100;
dbms_lob.read(clob_var,amount,offset,output_var);
dbms_output.put_line(output_var);
end;
16.有表student(stuid varchar(2),pic blob),如何向表中插入一条数据stuid=0001,pic的内容为c:"bookdata"image"0001.jpg呢?写入完整步骤?
create table student1(stuid varchar2(2),pic blob)
select * from student1
declare
pic_file bfile;
pic_blob blob;
begin
insert into student1 values ('1',empty_blob())
return pic into pic_blob;
pic_file := bfilename('STUDENT','"photo.jpg');
dbms_lob.open(pic_file,dbms_lob.file_readonly);
dbms_lob.loadfromfile(pic_blob,pic_file,dbms_lob.getlength(pic_file));
dbms_lob.close(pic_file);
commit;
end;
select * from student1
17.有表student(stuid varchar(2),memo clob),如何向表中插入一条数据stuid=0001,memo的内容为c:"bookdata"memo"0001.dat呢?写入完整步骤?
declare
myfile bfile;
mylob blob;
begin
insert into student1 values ('2',empty_blob())
return pic into mylob;
myfile := bfilename('STUDENT','"my.dat');
dbms_lob.open(myfile,dbms_lob.file_readonly);
dbms_lob.loadfromfile(mylob,myfile,dbms_lob.getlength(myfile));
dbms_lob.close(myfile);
commit;
end;
select * from student1
18.如何来定义一个变量,使它与表student中的mark字段同类型,以致将来在修改mark字段的类型时不用再修改变量的类型。
mark_ajh student.mark%type;
declare
temp student.stuid%type;
begin
select stuid into temp from student1 where stuid = 1;
commit;
end
19.如何来定义一个变量,使它与先前定义的变量nowMoney同类型?
money_ajh nowMoney%type;
declare
变量 表名.列名%type;
begin
select 列名 into 变量 from 表名 where 条件;
commit;
end;
20.在学习下一章时,我们将用到游标,游标为我们提供了依次访问行的机制,
在SQL SERVER中我们是分别依据每个字段定义变量,
再将当前游标的字段值取到我们的变量中,
此时,当我们在游标中增加字段时,我们还需增加变量的定义,
同时还要修改fetch操作中的变量,在oracle中有什么好的方法吗?
现有表student,如何来定义变量呢?
--declare 变量 用户名.表名%rowtype
21.if ... then ...end if
22.if ...then
....
elseif ... then
...
end if
正确吗?
--if ...then ... else ... end if...
23.declare
a varchar2(20);
begin
a:='&a';
if a=1 then
else
dbms_output.put_line(a);
end if;
end;
以上语句正确吗?
--不对
declare
a varchar2(20);
begin
a:='&a';
if a=1 then dbms_output.put_line(2);
else
dbms_output.put_line(a);
end if;
end;
24.set serveroutput on 有什么含义?
--set serveroutput on = dbms_output.put_line(25)
--可將打印的結果顯示在屏幕上
25.以下语句正确吗?
case a
when '1' then ....;
when '2' then ....;
default .....;
end case;
错在哪儿?
-- case 选择器('&selector')
--when '1' then ....;
--when '2' then ....;
--else .....;
-- end case;
26.如果写一个case语句来判断成绩信息mark,当大于90时输出'优秀',大于80时输出‘良好’,大于70时输出一般,大于60输出及格,小于60时输出未通过?
select (case when s_mark < 100 and s_mark > 80 then '优异'
when s_mark <= 80 and s_mark >= 60 then '优秀'
when s_mark <= 59 and s_mark >= 40 then '良好'
when s_mark < 40 and s_mark >= 20 then '一般'
when s_mark <= 19 and s_mark > 0 then '较差'
end ) as 成绩 from stumark
27.case和decode (P61)语句有什么区别?分别在什么场合使用?
SELECT CASE SIGN(5 - 5) WHEN 1 THEN 'Is Positive' WHEN -1 THEN 'Is Negative' ELSE 'Is Zero' END FROM DUAL;
select decode (条件,结果1,输出1,结果2,输出2)
SELECT DECODE(SIGN(5-9), 1, 'Is Positive', -1, 'Is Negative', 'Is Zero')FROM DUAL
28.基本的循环语句格式是什么?如何退出循环?
set serveroutput on
loop
end
29.oracle下的循环语句有哪几种,有什么共同的地方?
loop
while
for
用于重复执行一系列的语句
30.请建表student(stuid varchar2(5),stuname varchar2(20),clsid varchar2(6),mark number(4,1)),
写一个循环语句向表中插入1000行数据,
stuid,stuname使用dbms_random包的string函数来生成值,mark用value来生成值,clsid要求是vb,sql,c#,oracle,c内容。
drop table xunhuan
create table xunhuan
(stuid varchar2(200),stuname varchar2(2000),clsid varchar2(6),mark number(4,1))
select dbms_random.string('~' ,1000) from dual
select * from xunhuan
declare i int
for i in 1..1000
loop
case i := i%5
when 1 then insert into xunhuan values(i,dbms_random.string('~' ,i/1000),'vb',i);
when 2 then insert into xunhuan values(i,dbms_random.string('1' ,i/1000),'sql',i*5);
when 3 then insert into xunhuan values(i,dbms_random.string('a' ,i/1000),'c#',i*5);
when 4 then insert into xunhuan values(i,dbms_random.string('d' ,i/1000),'oracle',i*5);
else insert into xunhuan values(i,dbms_random.string('a' ,i/1000),'ava',i*5);
end case;
end loop;
end;
select * from xunhuan
31.oracle的标签语句是什么?如何来标识一个标签?标签后可以没有语句吗?
<<>>
<<标签体>>
declare
不可以
select * from stumark
declare
smark stumark.s_mark%type;
sname stumark.s_name%type;
begin
select s_mark,s_name into smark ,sname from stumark where s_id = 5;
if smark > 60 then goto up1;
else goto quit;
end if;
<<up1>>
dbms_output.put_line('123456');
<<quit>>
null;
end;
32.
begin
a:=1
if a=1 then
goto ta;
else
goto tb;
end if;
<<ta>>
dbms_output.put_line('aaaaa');
goto quit;
<<tb>>
dbms_output.put_line('bbbbb');
<<quit>>
null;
end;
请问上面语句会输出什么?有什么改进方法吗?
begin
declare
if a=1 then goto ta;
else goto tb;
end if;
<<ta>>
dbms_output.put_line('aaaaa');
goto quit;
<<tb>>
dbms_output.put_line('bbbbb');
<<quit>>
null;
end;
33.如何来运行动态的SQL语句?动态的SQL语句是指什么?运行时有什么限制?
34.如何在运行动态的SQL语句时指定参数?
35.请写一个动态的SQL程序来实现查询题30中的学生信息,要求输入学生号,然后根据动态查询输出学生的成绩?输出学号9999时退出。
36.当查询不到记录时会触发何种异常?
37.在使用select into语句时,如果记录数大于1条,会出现哪种异常?
38.如何来定义异常,如何来触发异常?
39.从38题中我们看到,自定义异常其实完全不必要通过异常来进行?那为什么还要这样做呢?有什么好处?