select t.name, d.name from v$tablespace t, v$datafile d;
show parameter db_block_size;
select * from user_tables;
select * from all_tables;
select * from dba_table;
select tablespace_name, status, alloction_type from dba_tablespaces;
create tablespace demospace
logging
datafile 'D:\Oracle\orsx\oradata\demospace\demospace.dbf' size 20M
autoextend on;
alter tablespace newsmgm_space
add datafile 'D:\Oracle\orsx\oradata\orcl\pdborcl\newsmgm_space_1.dbf' size 10M;
alter database datafile 'D:\Oracle\orsx\oradata\orcl\pdborcl\newsmgm_space_1.dbf' resize 20M;
select property_name, property_value from database_properties where property_name = 'DEFAULT_PERMANENT_TABLESPACE';
select username, account_status, default_tablespace from dba_users;
select con_id,dbid,name,open_mode from v$pdbs;
alter pluggable database pdborcl open;
alter session set container = pdborcl;
create user c##test01 identified by 123456 default tablespace demospace;
grant create session to c##test01;
grant select on scott.emp to test01 with grant option;
create user c##test02 identified by 123456;
select username from dba_users;
grant create session to marc;
grant create table, create sequence to marc;
alter user c##marcs quota unlimited on USERS;
grant select, insert, update on marcs.identified to test;
create role role_emp;
grant select on scott.emp to role_emp;
grant all on scott.emp to role_emp;
grant role_emp to test01;
revoke role_emp from test01;
select grantee, granted_role, admin_option, default_role
from dba_role_privs where grantee = 'C##TEST01';
grant create table, create sequence to c##test01;
alter user c##test01 quota unlimited on DEMOSPACE;
create table department(
deptno number(2)primary key,
dname varchar2(14),
loc varchar2(13)
);
desc department;
create table employee(
empno number(4)primary key,
ename varchar2(10),
job varchar2(20),
mgr number(4),
hiredate date,
sal number(7,2),
comm number(7,2),
deptno number(2)not null
constraint fk_emp_dept references department(deptno)
)
tablespace users;
alter table employee add(
sex char(3),
registdate date default sysdate not null
);
alter table employee modify job varchar2(10);
alter table employee modify sex default'男';
alter table employee rename column registdate to regist_date;
alter table employee drop column regist_date; alter table employee add registdate date;
alter table employee drop (sex, registdate);
alter table employee add registdate date;
alter table employee set unused column registdate;
comment on table employee is '员工表';
comment on column employee.empno is '工号';
comment on table employee rename to tb_employee;
drop table department cascade constraint;
select table_name, tablespace_name, status from user_tables;
select table_name, comments from user_tab_comments;
select column_name, data_type, data_length, nullable
from user_tab_columns
where table_name = 'EMPLOYEE';
create table board(
bid number(2) primary key,
bname varchar2(20) not null,
status number check(status in(0,1))
);
create table register(
rid number(10) primary key,
logname varchar2(20) unique,
password varchar2(10) not null,
age number(3) check (age>=13 and age<=80),
registboard number(2) not null references board(bid)
);
create table payroll(
empno number(4) references employee(empno),
payrolldate date not null,
deptno number(2),
sal number(7,2),
comm number(7,2),
constraint pk_payroll primary key(empno, payrolldate),
constraint fk_dept foreign key(deptno) references department(deptno)
);
select constraint_name, constraint_type, status
from user_constraints
where table_name='PAYROLL';
select constraint_name, table_name, column_name
from user_cons_columns
where table_name='PAYROLL';
create table job(
jobid number,
jobname varchar2(20),
jobdesc clob,
workplace varchar2(20),
minsalary number(6),
maxsalary number(6),
department number(2)
)tablespace users;
create table department(
deptno number(2) primary key,
dname varchar2(14),
loc varchar2(13)
);
alter table job add primary key(jobid);
alter table job add unique(jobname);
alter table job add constraint fk_job_dept foreign key(department)
references department(deptno);
alter table job add constraint ck_job_salary check( maxsalary > minsalary);
alter table job modify jobname not null;
alter table job rename constraint fk_job_dept to fk_jobdept;
alter table job drop constraint fk_jobdept;
alter table job drop primary key;
alter table job drop primary key keep index;
alter table job drop unique(jobname);
insert into department(deptno, dname, loc) values(50,'研发部','青岛');
insert into employee values(
7210,'jenny','programmer',null,to_date('20150302','yyyy-MM-dd'),
3000,null,50);
insert into department select * from scott.dept;
insert into employee select * from scott.emp;
select * from department;
select empno, ename, job, sal, comm
from employee
where comm is null;
update employee set comm = 200;
update employee set sal = sal + comm where empno = 7210;
select empno,ename,job,sal,comm from employee;
delete from employee where empno = 7210;
delete from employee;
delete from department;
alter table employee drop constraint fk_emp_dept;
truncate table department;
create table account(
account_id varchar2(16),
account_name varchar2(10),
account_balance number(16,3),
constraint pk_accountid primary key(account_id)
);
insert into account values('1001', '张三', 1000);
第一个dml语句执行,事务A开始
insert into account values('1002', '李四', 1);
select * from account;
commit;
事务提交,事务A结束
update account set account_balance = account_balance-1000
where account_id='1001';
执行dml语句,新的事务B开始
update account set account_balance = account_balance+1000
where account_id='1002';
alter table account add constraint ck_accountbalance check(account_balance>=0);
执行ddl语句,事务B自动提交,事务B结束
select * from account;
delete from account where account_id='1001';
新的事务C开始
exit;
正常退出,事务C被自动提交,事务C结束
create table department(
deptno number(2) primary key,
dname varchar2(14),
loc varchar2(13)
)tablespace users;
insert into department values(50, '研发部', '青岛');
commit;
select * from department;
insert into department values(60, '产品部', '青岛');
update department set loc = '高新区'where deptno = 60;
commit;
select * from department;
select * from department;
delete from department where deptno = 60;
rollback;
select * from department;
insert into department values(70, '市场部', '青岛');
update department set loc = '高新区'where deptno = 70;
rollback;
select * from department;
select * from department;
update department set loc = 'QING DAO'where deptno = 50;
savepoinit sp1;
delete department where deptno = 50;
savepoinit sp2;
rollback to sp1;
select * from department;
rollback;
select * from department;
set transaction isolation level serializable;
set transaction isolation level read committed;
set transaction read only;
alter session set isolation_level = serializable;
alter session set isolation_level = read committed;
会话1
create table tran_test(num number);
set transaction isolation level read committed;
insert into tran_test values(10);
select * from tran_test;
会话2
insert into tran_test values(20);
commit;
会话1
select * from tran_test;
会话1
delete from tran_test;
exit;
alter session set isolation_level = serializable;
insert into tran_test values(10);
select * from tran_test;
会话2
insert into tran_test values(20);
commit;
会话1
select * from tran_test;
会话1
delete from tran_test;
exit;
set transaction read only;
insert into tran_test values(10); ×
select * from tran_test; ×
会话2
insert into tran_test values(20);
commit;
会话1
select * from tran_test; ×
select * from emp;
select distinct * from emp;
select empno, ename, sal from emp where sal>=1500 and sal<=2900;
select empno, ename, job from emp where job='SALESMAN'
select empno, ename, hiredate
from emp where hiredate between '01-1月-81' and '31-12月-81';
select empno, ename, job from emp where job in('SALESMAN','CLEAR','MANAGER');
select empno, ename, job from emp where like 'M%';
select empno, ename, job from emp where like 'A%E_';
select * from department where dname like'IT\_%' escape' \ ';
select ename, job, sal from emp where deptno = 30 order by sal desc;
select lower('Structural|Query language')from dual;
select empno, ename, job from emp where job = upper('clerk');
select length('SQL is an english like language')from dual;
select empno, ename, initcap(ename) from emp;
select substr('SQL is an english like language', 3)from dual;
select substr('SQL is an english like language', 1, 3)from dual;
select replace('SQL Plus supports loops or if statements','supports','not supports')from dual;
select concat('hello,','world!')from dual;
select rpad(ename, 10, '*'), lpad(ename, 10, '*')from emp;
select ' QST ', ltrim(' QST '), rtrim(' QST ')from dual;
select’ QST ‘, trim(’ QST ')from dual;
select distinct job, instr(job, upper('man'))from emp;
select distinct job, instr(job, upper('man'), 2)from emp;
select sysdate from dual;
alter session set nls_format='yyyy-mm-dd hh24:mi:ss';
add_months(日期, 数字)–计算增加月数日期
last_day(日期)–计算此月最后一天
next_day(日期, 星期)–计算下一个星期几的日期
months_between(日期1, 日期2)–计算两个日期相隔月数
to_char(列|日期|数字,转换格式)–转换为字符串
to_date(字符串,转换格式)–将字符串转换为日期型(有分秒:to_timestamp(字符串,转换格式))
to_number(字符串)–将数字内容的字符串转变为字符型
select count(mgr), count(comm)from emp;
avg(列)函数:求平均值–
sum(列)函数:求和–
max(列)函数:求最大值–
min(列)函数:求最小值–
select job, avg(sal), max(sal), min(sal) from emp group by job;
select job, avg(sal)from emp group by job having avg(sal)>2000;
select empno, ename, emp.deptno, dname, loc
from emp, dept
where emp.deptno = dept.deptno;
select e.empno, e.ename, e.deptno, d.dname, d.loc
from emp e, dept d
where e.deptno = d.deptno and e.sal >= 2000
order by d.loc;
select e.empno, e.ename, e.job, e.mgr, e.ename 经理, m.job 职位
from emp e, emp m
where e.mgr = m.empno and e.job like 'sal%';
select e.ename 员工名称, e.job 职位, m.ename 经理名称, d.dname 部门名称
from emp e, emp m, dept d
where e.mgr = m.empno
and e.deptno = d.deptno
and e.job in('CLERK', 'ANALYS')
order by e.empno;
select e.ename, d.dname
from emp e inner join dept d
on e.deptno = d.deptno and d.deptno = 20;
select e.ename 员工名称, e.job 职位, m.ename 经理名称, d.dname 部门名称
from emp e
join emp m on e.mgr = e.empno
join dept d on e.deptno = d.deptno
where e.job in('CLERK', 'ANALYS')
order by e.empno;
select e.empno, e.ename, d.deptno, d.dname, d.loc
from dept d left join emp e
on d.deptno = e.deptno
order dy d.deptno desc;
select e.empno, e.ename, d.deptno, d.dname, d.loc
from dept d left join emp e
where d.deptno = e.deptno(+)
order by dy d.deptno desc;
select e.empno, e.ename, d.deptno, d.dname, d.loc
from emp e left join dept d
on e.deptno = d.deptno
order dy d.deptno desc;
select e.empno, e.ename, d.deptno, d.dname, d.loc
from emp e left join dept d
where d.deptno(+) = e.deptno
order by dy d.deptno desc;
select e.ename, e.deptno, d.deptno, d.dname, d.loc
from employee e full join dept d
on e.deptno = d.deptno
order by d.deptno;
select e.empno, e.ename, e.job, e.sal, s.grade
from emp e, salgrade s
where e.sal between s.losal and s.hisal;
select empno, ename, sal
from emp
where sal >(select avg(sal)from emp);
select a.empno, a.ename, a.sal, a.deptno
from emp a
where (select count(*)from emp where deptno = a.deptno and sal>a.sal)=0;
select e.empno, e.ename, e.job, e.sal, s.grade
from emp e, salgrade s
where e.ename != 'WARD' and (e.sal, s.grade)=(
select sal, grade from emp, salgrade
where ename = 'WARD' and sal between losal and hisal);
select empno, ename, job, sal
from emp
where sal in(select max(sal)from emp group by job);
select empno, ename, job, sal
from emp
where sal > any(select avg(sal)from emp group by job);
select deptno, dname, loc
from dept d
where exists(select * from emp e where d.deptno = e.deptno);
select d.deptno, d.dname, e.amount, a.avgsal
from dept d,
(select deptno, count(*) amount, avg(sal) avgsal from emp group by deptno) e,
where e.deptno = d.deptno;
with subdept as(
select deptno, count(*)count
from emp
group by deptno)
select deptno, dname, loc
from dept
where deptno in(
select deptno from subdept
where count = (select max(count)from subdept));
select job from emp where deptno = 30
union
select job from emp where deptno = 20;
select * from emp, dept;
select empno, ename, emp.deptno, dept.deptno, dname
from emp cross join dept;
select * from emp where rownum = 1;
select * from emp where rownum < 3;
select temp.*from(
select empno, ename, job, sal
from emp
order by sal desc)temp
where rownum <= 3;
select temp.*from(
select rownum rn, empno, ename, job, sal
from emp
where rownum < 3)temp
where temp.rn > 1;
select * from emp where rownum < 3;
minus
select * from emp where rownum < 2;
select temp.*from(
select rownum rn, empno, ename, job, sal
from emp
where rownum <= 10)temp
where temp.rn >= 6;
fetch first 3 row only :表示取得前3行记录(放在排序后)
offset 2 rows fetch next 6 row only :表示从第2行开始取得6行记录,即第3行到第8行记录(不包括边界值)
fetch next per percent row only :表示按照指定的百分比per%取得相关行数的记录
select empno, ename, mgr
from emp
start with job = 'PRESIDENT'
connect by prior empno = mgr;
grant create view to scott;
create view scott.v_emp_base
as
select empno, ename, sal, deptno from emp order by empno;
select * from scott.v_emp_base;
select ename, deptno from v_emp_base;
create view scott.v_emp_base_read
as
select empno, ename, sal, deptno from emp order by empno
with read only;
grant create view to scott;
create or replace view v_dept_sal(name, minsal, maxsal, avgsal)
as
select d.dname, min(e.sal), max(e.sal), avg(e.sal)
from dept d, emp e
where d.deptno = e.deptno
group by d.dname;
create view v_emp_salsry
as
select empno, ename, sal*12 salsay
from emp
where sal > 2000
with check option;
create or replace view v_emp_salsry
as
select empno, ename, sal*12 salsay
from emp
where sal > 2000 and deptno = 10
with check option;
select view_name, text from user_views;
drop view v_emp_salsry;
create table emplyee as select * from emp;
create or replace view v_test
as
select empno, ename, sal, deptno
from employee
where deptno = 20
with check option;
update v_test set sal = 1000 where ename = 'SMITH';
select empno, ename, sal, deptno from employee where ename = 'SMITH'
create or replace view v_test
(empno, ename, salsry, deptno)
as
select empno, enamen, sal * 12, deptno
from employee
where deptno = 20
with check option;
create or replace view v_emp_dept
as
select empno, ename, dname
from enp e, dept d
where e.deptno = d.deptno;
update v_emp_dept set sal = 800 where ename = ‘SMITH’ √
update v_emp_dept set dname = ‘XXX’ where ename = ‘SMITH’ ×
create sequence seq_emp
start with 1
increment by 1
maxvalue 1000;
create sequence seq_dept
start with 10
increment by 10
maxvalue 50
minvalue 10
cycle
cache 3;
create table department as select * from scott.dept where 1 = 2;
insert into department(deptno, dname, loc)
values(seq_dept.nextval,'RESEARCH', 'QINGDAO');
select seq_dept.currval from department;
update department set deptno = seq_dept.nextval where deptno = 10;
select sequence_name, min_value, max_value,
increment_by, cycle_flag, cache_size
from user_sequences;
drop sequence seq_dept;
grant create synonym to scott;
grant create synonym to scott;
create synonym scottemp for emp;
create or replace public synonym scottemp for scott.department;
select synonym_name, table_owner, table_name from user_synonyms
drop synonym scottemp;
select rowid, empno, ename, from emp order by empno;
create index index_employee_ename on employee(ename);
create index index_employee_ename
on pmployee(ename)
tablespce users storage(initial 20k next 10k pctincrease 65);
create table new_employee(
empno number(4) primary key using index tablespace users pactfree 0,
ename varchar2(10));
create bitmap index index_employee_deptno on employee(deptno);
select empno, ename, deptno from employee where deptno = 10 or deptno;
create index index_employee_hiredate
on employee(to_char(hiredate,'YYYY-MM-DD'));
select empno, ename, hiredate
from employee
where ton_char(hiredate,'YYYY-MM-DD')=='1981-11-17';
create index index_empno_ename on employee(empno, ename);
修改索引:alter index (合并、重建、重命名)
合并索引:alter index…coalesce
重建索引:alter index…rebuild
索引重命名:alter index…rename to
打开索引监视状态:alter index…monitoring usage
关闭索引监视状态:alter index…nomonitoring usage
视图dba_indexes:描述数据库中的所有索引信息
视图user_indexes:描述数据库中的当前用户索引信息
declare
v_empno number;
v_ename varchar2(10);
begin
dbms_output.put_line('请输入员工编号:');
v_empno:=&input_empno;
select ename into v_ename from emp where empno=v_empno;
dbms_output.put_line('编号为:'||v_empno||' 姓名为:'||v_ename);
exception
when no_data_found then
dbms_output.put_line('此编号员工不存在');
end;
set serveroutput on;
declare
v_num number;
begin
v_num:=100/0;
exception
when zero_divide then
dbms_output.put_line('除0异常');
end;
declare
v_x number:=10;
begin
declare
v_x number:=20;
begin
dbms_output.put_line('子块变量v_x='||v_x);
end;
dbms_output.put_line('外部语句块变量v_x='||v_x);
end;
declare
v_empno number;
v_deptno number;
v_sal number;
begin
begin
v_empno:=&员工编号;
select deptno into v_deptno from emp where empno = v_empno;
end;
select round(avg(sal),2)into v_sal from emp where deptno = v_deptno;
dbms_output.put_line(v_empno||'员工所在部门的平均工资为'||v_sal);
end;
declare
v_cons constant varchar2(20):='Hello,world.';
begin
dbms_output.put_line('v_cons常量值为:'||v_cons);
end;
declare
v_num1 number(3):=100;
v_num2 number(5,2):=100.99;
v_binary binary_integer:=-100;
v_float binary_float:=1000.99F;
v_double binary_double:=10000.99D;
begin
v_num1:=v_num1+v_num2;
v_binary:=v_binary+100;
v_float:=v_float+1000.99;
v_double:=v_double+10000.99;
dbms_output.put_line('number类型数据计算和:'||v_num1);
dbms_output.put_line('binary_integer类型数据计算和:'||v_binary);
dbms_output.put_line('binary_float类型数据计算和:'||v_float);
dbms_output.put_line('binary_double类型数据计算和:'||v_double);
end;
declare
v_char char(20);
v_varchar2 varchar2(20);
v_nchar nchar(20);
v_nvarchar2 nvarchar2(20);
v_dept_rowid rowid;
begin
v_char:='静水流深';
v_varchar2:='静水流深';
v_nchar:='静水流深';
v_nvarchar2:='静水流深';
select rowid into v_dept_rowid from scott.dept where deptno=10;
dbms_output.put_line('v_char内容长度:'||length(v_char));
dbms_output.put_line('v_varchar2内容长度:'||length(v_varchar2));
dbms_output.put_line('v_nchar内容长度:'||length(v_nchar));
dbms_output.put_line('v_nvarchar2内容长度:'||length(v_nvarchar2));
dbms_output.put_line('10号部门的rowid:'||v_dept_rowid);
end;
declare
v_date1 date:=sysdate;
v_date2 date:=systimestamp;
v_date3 date:='04-03月-2020';
v_timestamp1 timestamp:=systimestamp;
v_timestamp2 timestamp:=sysdate;
v_timestamp3 timestamp:='04-03月-2020 12.20.40 上午';
begin
dbms_output.put_line('v_date1:'||to_char(v_date1,'yyyy-MM-dd hh24:mi:ss'));
dbms_output.put_line('v_date2:'||v_date2);
dbms_output.put_line('v_date3:'||v_date3);
dbms_output.put_line('v_timestamp1:'||v_timestamp1);
dbms_output.put_line('v_timestamp2:'||v_timestamp2);
dbms_output.put_line('v_timestamp3:'||v_timestamp3);
end;
declare
v_flag boolean;
begin
v_flag:=true;
if v_flag then
dbms_output.put_line('条件为真');
end if;
end;
declare
v_name emp.ename%type;
v_salary emp.sal%type;
v_hiredate emp.hiredate%type;
begin
select ename, sal, hiredate, into v_name, v_salary, v_hiredate
from emp where empno=&empno;
dbms_output.put_line('雇员号:'||v_name);
dbms_output.put_line('工资:'||v_salary);
dbms_output.put_line('入职日期:'v_hiredate);
exception
when no_date_found then
dbms_output.put_line('你输入的员工号不存在');
end;
declare
emp_record emp%rowtype;
begin
select * into emp_record from emp where empno=&empno;
dbms_output.put_line('雇员号:'||emprecoord.ename);
dbms_output.put_line('工资:'||emp_record.sal);
dbms_output.put_line('入职日期:'||emp_record.hiredate);
exception
when no_data_found then
dbms_output.put_line('你输入的员工号不存在');
end;
declare
v_sal number;
begin
select sal into v_sal from emp where empno=7934;
if v_sal<1000 then
update emp set sal=sal+200 where empno=7934;
elsif v_sal>=1000 and v_sal<2000 then
update emp set sal=sal+150 where empno=7934;
else
update emp set sal=sal+100 where empno=7934;
end if;
end;
declare
v_sal number;
begin
case
when XXX then
update...where...;
when YYY then
update...where...;
when MMM then
update...where...;
when NNN then
update...where...;
end case;
end;
declare
v_i number:=1;
v_sum number:=0;
begin
loop
v_sum:=v_sum+v_i;
v_i:=v_i;
exit when v_i>100;
end loop;
dbms_output.put_line('1~100的和为:'||v_sum);
end;
declare
v_i number:=1;
v_sum number:=0;
begin
while v_i<=100 loop
v_sum:=v_sum+v_i;
v_i:=v_i+1;
end loop;
dbms_output.put_line('1~100的和为:'||v_sum);
end;
declare
v_sum number:=0;
for v_i in 1..100 loop
v_sum:=v_sum+v_i;
end loop;
dbms_output.put_line('1~100的和为:'||v_sum);
begin
for v_i reverse 1..9 loop
for v_j in 1..v_i loop
dbms_output.put_line(v_i||'*'||v_j||'='||v_i*v_j||'');
end loop;
dbms_output.put_line('');
end loop;
end;
declare
begin
for v_i in 1..5 loop
if
goto
end if;
end loop;
end;
exception
when...others then
end;
XXX exception;
pragma exception_init(XXX, -20000~-29999);
declare
v_myexp
sql%isopen
sql%found
sql%notfound
sql%rowcount
begin
update employee set deptno=20 where ename like'%s%';
if sql%rowcount = 0 then
dbms_output.put_line('数据更新失败');
else
dbms_output.put_line('数据已更新'||sql%rowcount||'行');
end if;
end;
declare
v_empno emp.empno%type;
v_emp emp%rowtype;
begin
v_empno:=&职员编号;
select * into v_emp from emp where empno=v_empno;
if sql%found then
dbms_output.put_line('职员的姓名是'||v_emp.ename);
end if;
exception
when no_data_found then
dbms_output.put_line('该编号的职员未找到');
end;
declare
cursor cursor_emp is select empno, ename from emp; --声明游标
v_empno emp.empno%type;
v_ename emp.ename%type;
begin
open cursor_emp; --打开游标
loop
fetch cursor_emp into v_empno, v_ename; --检索游标指向数据给变量
exit when cursor_emp%notfound;
dbms_output.put_line('员工号:'||v_empno||'姓名:'||v_ename);
end loop;
close cursor_emp;
end;
declare
cursor cursor_emp is select empno, ename, sal from emp;
v_emp cursor_emp%rowtype;
begin
open cursor_emp;
loop
fetch cursor_emp into v_emp;
exit when cursor_emp%notfound;
dbms_output.put_line(cursor_emp%rowcount||''||v_emp.empno||''||v_emp.ename||''||v_emp.sal);
end loop;
close cursor_emp;
end;
declare
cursor cursor_sal is
select deptno, round(avg(sal),2)avgsal from emp group by deptno;
v_sal sursor_sal%rowtype;
begin
if cursor_sal%isopen then
null;
else
open cursor_sal;
end if;
fetch cursor_sal into v_sal; --游标指向第一行
while cursor_sal%found loop
dbms_output.put_line(v_sal.deptno||' '||v_sal.avgsal);
fetch cursor_sal into v_sal; --把游标指向下一行
end loop;
close cursor_sal;
end;
declare
cursor cursor_emp is select * from emp;
begin
for v_emp in cursor_emp loop
dbms_output.put_line(cursor_emp%rowcount||' '||v_emp.ename);
end loop;
end;
declare
cursor cursor_emp(p_empno emp.empno%type)is
select * from emp where empno = p_empno;
v_emp cursor_emp%rowtype;
begin
open cursor_emp(7369);
loop
fetch cursor_emp into v_emp;
exit when cursor_emp%notfound;
dbms_output.put_line(v_emp.empno||''||v_emp.ename);
end loop;
close cursor_emp;
open cursor_emp(7499);
loop
fetch cursor_emp into v_emp;
exit when cursor_emp%notfound;
dbms_output.put_line(v_emp.empno||''||v_emp.ename);
end loop;
close cursor_emp;
end;
cousor cursor_emp is;
select * from emp for update;
cursor cursor_emp is
select e.empno, e.ename, e.sal, d.dname from emp e, dept d
where e.deptno = d.deptno for update of e.sal;
cursor cursor_emp is
select * from emp for update nowait;
declare
cursor cursor_emp is select * from employee where comm is null for update;
begin
for v_emp in cursor_emp loop
update employee set comm = 500 where current of currsor_emp;
end loop;
commit;
end;
loop
fetch 游标变量 into 变量;
exit when 游标变量%notfound;
...
end loop;
declare
type ref_cursor_type is ref cursor;
ref_cursor ref_cursor_type;
v_emp emp%rowtype;
v_dept dept%rowtype;
v_choose varchar2(1):=upper(substr('&D或E',1,1));
begin
if v_choose='E' then
open ref_cursor for select * from emp;
dbms_output.put_line('员工信息');
loop
fetch ref_cursor into v_emp;
exit when ref_cursor%notfound;
dbms_output.put_line(v_emp.empno||' '||v_emp.ename);
end loop;
close ref_cursor;
elsif v_choose='D'then
open ref_cursor for select * from dept;
dbms_output.put_line('部门信息');
loop
fetch ref_cursor into v_dept;
exit when ref_cursor%notfound;
dbms_output.put_line(v_dept.deptno||' '||v_dept.dname);
end loop;
close ref_cursor;
else
dbms_output.put_line('请输入E或D');
end if;
end;
create or replace procedure
-create any procedure 任意用户创建储存过程
create or replace procedure proc_test(p_str1 varchar2, p_str2 varchar2)
as
begin
dbms_output.put_line(p_str1||p_str2);
end;
declare
v_var1 varchar2(20):='Hello,';
v_var2 varchar2(20):='Prodcedure!';
begin
proc_test(v_var1, v_var2);
end;
create or replace procedure proc_in(p_empno in number)
as
v_ename scott.emp.ename%type;
v_sal scott.emp.sal%type;
begin
select ename, sal into v_ename, v_sal from scott.emp where empno=p_empno;
dbms_output.put_line('雇员的姓名是:'||v_ename||'工资是:'||v_sal);
exception
when no_data_found then
dbms_output.put_line('雇员编号未找到');
end;
begin
proc_in(7369);
end;
create or replace procedure proc_out(
p_deptno number,
p_num out number,
p_avgsal out number)
as
begin
select count(*)num, round(avg(sal),2)avgsal into p_num, p_avgsal
from scott.emp where deptno=p_deptno;
exception
when no_data_found then
raise_application_error(-2000,'该部门编号不存在');
end;
declare
v_num number;
v_avgsal number;
begin
proc_out(10, v_num, v_avgsal);
dbms_output.put_line('10号部门的总人数为'||v_num||'平均工资为'||v_avgsal);
end;
create or replace procedure proc_dept_dname_exist(
p_io_value in out varchar2) is v_count number;
begin
select count(*)into v_count from scott.dept where dname=p_io_value;
if(v_count>0)then
p_io_value:='已存在';
else
p_io_value:='不存在';
end if;
end;
declare
v_io_value varchar2(20):='ACCOUNTING';
begin
proc_dept_dname_exist(v_io_value);
dbms_output.put_line('部门名称ACCOUNTING'||v_io_value||'!');
end;
select object_name, object_type from user_procedures;
select name, text from user_source where type='PROCEDURE';
select name, type, referenced_name from user_dependencies
where referenced_name='EMP'or referenced_name='DEPT';
alter procedure proc_dept_insert compile;
drop procedure proc_test;
create or replace function func_hello
return varchar2
as
begin
return'hello function!';
end;
create or replace function func_get_dname(p_deptno dept.deptno%type)
return varchar2
as
v_dname dept.dname%type;
begin
select dname into v_dname from scott.dept where deptno=p_deptno;
return v_dname;
end;
select func_hello from dual;
select func_get_dname(10)from dual;
declare
v_info varchar2(100);
begin
v_info:=func_hello;
dbms_output.put_line('返回:'||v_info);
end;
declare
v_no number;
v_info varchar2(50);
begin
v_no:=&no;
v_info:=func_get_dname(v_no);
dbms_output.put_line('部门名称:'||v_info);
end;
with function fun_isnumber(param in varchar2)
return varchar2
is
begin
if(to_number(param)is not null)then
return 'Y';
else
retuen 'N';
end if;
exception
when others then
return'N';
end;
select fun_isnumber('abcd')from dual;
select name, text from user_source where type='FUNCTION';
alter function func_get_dname compile;
drop function func_hello;
before|after:前触发|后触发
for each row:行级触发器
create or replace trigger trig_emp_dml
before insert or update or delete on scott.emp
begin
if(to_char(sysdate,'DAY'))in('星期六','星期日')
or(to_char(sysdate, 'HH24:MI')not between'08:30'and'17:30')then
raise_application_error(-20002,'只要在工作时间进行操作。');
end if;
end trig_emp_dml;
create or replace trigger trig_emp_authority
before insert or update or delete on emp
begin
if user <> 'SCOTT' then
raise_application_error(-20001,'您无权操作emp表')
end if;
end trig_emp_authority;
create or replace trigger trig_emp_addsal
before update of sal on emp
for each row
declare
v_scale number;
begin
v_scale:=(:new.sal-:old.sal)/:old.sal;
if v_scale>0.1 then
:new.sal:=:old.sal*1.1;
dbms_output.put_line('加薪不能超过10%,薪水更新成:'||:new.sal);
end if;
end;
create table dept dept_log(
logid number,
type varchar2(20)not null,
logdate date,
deptno number(2),
dname varchar2(20)not null,
loc varchar2(30)not null,
constraint pk_logid primary key(logid));
create sequence seq_dept_log;
create or replace trigger trig_dept_dml
before insert or update or delete on dept
for each row
begin
when inserting then
insert into dept_log(log, type, logdate, deptno, dname, loc)
values(seq_dept_log.nextval,'INSERT',
sysdate,:new.deptno,:new,dname,:new.loc);
when updating then
insert into dept_log(log, type, logdate, deptno, dname, loc)
values(seq_dept_log.nextval,'UODATE',
sysdate,:new.deptno,:new,dname,:new.loc);
when deleting then
insert into dept_log(log, type, logdate, deptno, dname, loc)
values(seq_dept_log.nextval,'DELETE',
sysdate,:new.deptno,:new,dname,:new.loc);
end case;
end;
语句前触发器(before statement)->行级前触发器(before row)
->更新操作
->行级后触发器(after row)->语句级后触发器(after statement)
create or replace trigger dept_insert_one
before insert on dept
for each row
begin
dbms_output.put_line('执行第1个触发器');
end;
create or replace trigger dept_insert_two
before insert on dept
for each row
follows dept_insert_one
begin
dbms_output.put_line('执行第2个触发器');
end;
create table operate_log(
logid number constraint pk_logid primary key, --主键标识
operater varchar2(50), --操作者名称
operate_date date, --操作时间
object_name varchar2(50), --对象名称
object_type varchar2(50), --对象类型
object_owner varchar2(50) --对象所有者名称
);
create sequence seq_operate_log;
create or replace trigger trig_object_ddl
after ddl on database
begin
insert into operate_log(logid, operate_date, object)
end;
create table
create or replace trigger trig_userlogon
after logon
alter trigger trig_object_ddl disable;
alter trigger trig_object_ddl compile;
drop trigger trig_object_ddl;
create or replace procedure p_test(v_deptno in number)
as
cursor cursor_emp is select empno, ename, sal from emp where sal > (select avg(sal) from emp where deptno=v_deptno) and deptno = v_deptno;
v_empno emp.empno%type;
v_ename emp.ename%type;
v_sal emp.sal%type;
a_sal number;
begin
select avg(sal)into a_sal from emp where deptno=v_deptno;
dbms_output.put_line('本部门平均为:'||a_sal);
dbms_output.put_line('比本部门平均工资高的员工为');
open cursor_emp;
loop
fetch cursor_emp into v_empno, v_ename, v_sal;
exit when cursor_emp%notfound;
dbms_output.put_line('员工号:'||v_empno||' 姓名:'||v_ename||' 工资:'||v_sal);
end loop;
close cursor_emp;
end;
begin
p_test(10);
end;