一、Pl sql使用
表:Student
declare
v_a student.sage%type; --变量
c_b constant varchar2(20) := '常量'; --常量
v_c student.sname%type;
begin
-- 直接给变量赋值
v_a := 10;
select sage,sname into v_a,v_c from student where sid = 6;
Dbms_Output.put_line('变量的值是'||chr(10)||v_a||c_b);
dbms_output.put_line('名字是'||v_c);
end;
小练习 计算三门成绩的平均分
declare
v_chinese number(3);
v_math number(3);
v_english number(3);
begin
v_chinese := 85;
v_math := 90;
v_english := 100;
dbms_output.put_line('我的平均分是'||(v_chinese+v_math+v_english)/3);
end;
-- if判断
declare
v_age student.sage%type;
begin
select sage into v_age from student where sid = 6;
if v_age<18 then
dbms_output.put_line('未成年!');
else
dbms_output.put_line('成年人!');
end if;
end;
-- 循环
declare
v_a number(4) := 15;
begin
loop
dbms_output.put_line('');
v_a:=v_a+1;
insert into student values(v_a,'小白',100,'女');
if v_a when
continue;
exit when v_a = 30;
end loop;
end;
-- switch
declare
v_a number(11) := 11;
begin
case
when v_a<10 then dbms_output.put_line('小于10');
when v_a<20 then dbms_output.put_line('小于20');
when v_a<30 then dbms_output.put_line('小于30');
end case;
end;
-- 游标 cursor
declare
cursor cursor_a is
select * from student;
v_sid student.sid%type;
v_sname student.sname%TYPE;
v_sage student.sage%type;
v_ssex student.ssex%type;
begin
--定义游标
--打开游标
open cursor_a;
--提取游标
loop
fetch cursor_a into v_sid,v_sname,v_sage,v_ssex;
exit when cursor_a%notfound;
dbms_output.put_line('iD'||v_sid);
end loop;
--关闭游标
close cursor_a;
end;
-- 存储过程
-- 存储过程相当于封装了一个方法
-- 先声明
create procedure add_student(
sid student.sid%TYPE,
sname student.sname%TYPE,
sage student.sage%TYPE,
sex student.ssex%TYPE,
out_flag out number
)
is
begin
insert into student values(sid,sname,sage,sex);
commit;
out_flag:=1;
end;
declare
v_a number;
begin
add_student(17,'小李子',19,'女',v_a);
dbms_output.put_line('返回值为'||v_a);
end;
二、创建表空间、序列、同义词、索引
-- 表空间是真实存在的 所以要指定路径、分配存储大小
create tablespace tp_first
datafile 'E:\sql\tablespaceFile\tp_first.dbf' size 60M;
-- 删除
drop tablespace tp_first including contents;
-- 赋予新建用户登录权限
grant connect,resource to test1;
-- 赋予具体实体的操作权限
grant all on scott.student to test1;
-- 创建序列 是独立存在的
select seq_first.currval from dual;
insert into student values(seq_first.nextval,'啊舔',18,'女');
-- Create sequence
create sequence SEQ_FIRST
minvalue 1
maxvalue 1000
start with 1
increment by 1
cache 20
cycle;
-- 同义词 synonym 解锁 for update
select * from stu for update;
-- 索引 一定要建立在大数据量的基础上才有用 index
-- 唯一索引
create unique index index_u age on student(sage)
-- 反向键索引
create index index_u_age on student(sage) reverse;
-- 位图索引
create bitmap index_u_age on student(sage);
-- 针对函数建立索引 大写函数索引
create index index_u_age on student(UPPER(sid));
希望这些能够给大家带来帮助!