数据库sql脚本

合集下载
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

//多次回滚

select * from emp;

update emp set sal=1where ename='SMITH'; commit;

savepoint a;

update emp set sal=2where ename='SMITH'; commit;

savepoint b;

update emp set sal=3where ename='SMITH'; commit;

savepoint c;

update emp set sal=400where ename='SMITH'; rollback to b;

PL/SQL

-----例1

declare

pl_grade number(5);

begin

select grade into pl_grade

from Enrollment where cno='c2';

if pl_grade>=90then

insert into Students values('888888','better','M',20,'Art'); end if;

commit;

EXCEPTION

WHEN OTHERS THEN

DBMS_OUTPUT.PUT_LINE('出错'||SQLERRM);

END;

----例2(if判断语句)

declare

pl_grade number(5);

comment1 constant varchar(30):='你的分数是';

comment2 constant varchar(30):=',还需要努力';

begin

select grade into pl_grade

from Enrollment where cno='c3';

/*大于90输出成绩*/

if pl_grade>=90then

Dbms_Output.put_line(pl_grade);

/*小于70输出仍需努力*/

elsif pl_grade<70then

Dbms_Output.put_line(comment1||pl_grade||comment2);

end if;

commit;

EXCEPTION

WHEN OTHERS THEN

DBMS_OUTPUT.PUT_LINE('出错'||SQLERRM);

END;

----例3(嵌套循环)

declare

/*声明计数变量*/

pl_count number(3) :=0;

begin

/*While循环遍历*/

while (pl_count<3) loop

/*for循环遍历*/

for i in1..2 loop

dbms_output.put_line('For循环:'||i);/*输出*/ end loop;

dbms_output.put_line('where循环:'||pl_count);/*输出*/ pl_count := pl_count+1;/*count计数+1*/

end loop;

end;

----例4(for)

/*请打印出15~25之间的所有数*/

declare

begin

/*15-25的for循环*/

for i in15..25 loop

dbms_output.put_line(' '||i);/*打印输出*/

end loop;

end;

----例5

/*请打印出1~10之间的偶数(While loop的语法实现)*/

declare

pl_count number(20) := 1;

num number(2) := 2;

begin

while pl_count<=10loop

if MOD(pl_count,2) = 0then/*如果能被2取余等于0说明是偶数*/ dbms_output.put_line(pl_count); /*打印输出*/

end if;

pl_count := pl_count+1; /*+1操作*/

end loop;

end;

----例6(case语句)

declare

light_color varchar(5) := ('&p_light'); /*手动输入*/

behavie varchar(5);

begin

behavie:=

case light_color

when'红灯'then'停'/*如果light_color为红灯,为停*/

when'绿灯'then'行'/*如果light_color为绿灯,为行*/

when'黄灯'then'等'/*如果light_color为黄灯,为等*/

else'不执行'/**否则不执行*/

end;

dbms_output.put_line(light_color||' '||behavie);

end;

新老师课程

【建表】

create table teachers(

tno number(3) primary key,

tname varchar(10)

);

create table students(

sno number primary key,

sname varchar(10),

sage number(3),

gender varchar(2),

tno number(3),

constraint fpk_t foreign key(tno) references teachers(tno) );

drop table teachers;

drop table students;

select * from teachers;

select * from students;

insert into teachers values(1,'Tom');

insert into teachers values(2,'Jone');

insert into students values(1,'Jeck',20,'M',1);

insert into students values(2,'Smith',21,'W',1);

insert into students values(3,'Jery',25,'M',2);

insert into students values(4,'Lik',22,'W',2);

相关文档
最新文档