oracle操作题——试卷题

oracle操作题——试卷题

一、创建一个新用户john,密码为john123。其默认表空间为users,分配表空间配额为20m(5分)

create user c##john identified by john123 default tablespace users quote 20M on users;

二、给该用户授予connect和rescource两个角色(3分)

grant connect,resource to c##john;

三、以该用户身份连接数据库。(2分)

conn c##john/john123

四、在该用户方案下建立新表,表要求和约束要求如下:表结构中数据类型根据数据值自定义。(10分)

create table  cangku(
	ckid varchar2(10) constraint pk_id primary key,
	city varchar2(10),
	area varchar2(10) constraint ck_area check(area<=1000)
);

create table zhigong(
	ckid varchar2(10) constraint fk_id references cangku(ck_id),
	zhgid varchar2(10) constraint pk_zhgid primary key,
	sal number constraint ck_sal check(sal between 1200 and 2000)
);

五、根据要求写命令(每小题2分)

1. 将john方案下的仓库表中面积大于300的记录复制成新表仓库1

create table cangku1 as select * from cangku where area>3000;

2. 向仓库1中插入一条记录(wh5,济南,420)

insert into cnagku1 values('wh5','济南'420);

3. 删除仓库1中,仓库号为wh4的记录

delete from cangku1 where ckid='wh4';

4. 对仓库1表按面积建立索引,索引名为area_index

create index area_index on cangku1(area);

5. 根据仓库表和职工表建立一个视图view1,通过它可以查询北京和上海的仓库职工的工资总和

create or replace view view1 as select sum(sal) from zhigong where ck_id in(select ck_id from cangku where city in('北京','上海'));

create or replace view view1 as select sum(sal) from zhigong where ckid in(select ckid from cangku where city='北京' or city='上海');

6. 创建一个角色role1,通过该角色将仓库1表的增删改查权限授予所有用户。

system/root
create role role1;
grant all on cangku1 to role1;
grant role1 to public;

7. 自定义一个异常,根据职工的员工号查询该职工的工资,如果工资小于1200,则抛出异常信息:“你的工资太低了!”

declare
	myexception exception;
	vno zhigong.zhgid%type := &no;
	vsal zhigong.sal%type;
begin
	select sal into vsal from zhigong where zhgid=vno;
	if v_sal<1200 then
		raise myexception
	end if;
	exception
	when myexception then
	dbms_output.put_line('你的工资太低了');
end;

8. 查看john用户的所有表

select table_name, tablespace_name, status from user_tables;

六、用sql命令完成下面的查询(每小题3分)

1. 从职工表中检索所有工资值,要求结果中没有重复值,并按工资降序排列。

select distinct sal from zhigong order by sal desc;

2. 检索工资多于1230元的职工号。

select zhgid from zhigong where sal>1230;

3. 检索哪些仓库有工资多于1210元的职工。

select ckid,zhgid,sal from zhigong where sal>1210;

4. 给出在仓库“wh1”或“wh2”工作,并且工资少于1250元的职工号。

select zhgid from zhigong where ckid in('wh1','wh2') and sal<1250;

5. 找出工资多于1230元的职工号和他们所在的城市。

select zhgid,city from zhigong z,cangku c where z.ckid=c.ckid and sal>1230;

6. 哪些城市至少有一个仓库的职工工资为1250元。

select city from zhigong z,cangu c where z.ckid=c.ckid and sal=1250;

7. 查询所有职工的工资都多于1210元的仓库的信息。

select * from cangku where ckid in (select ckid from zhigong group by ckid having min(sal)>1210);

8. 找出和职工e4挣同样工资的所有职工。

select * from zhigong where sal=(select sal from zhigong where zhgid='e4');

9. 检索出工资在1220元到1240元范围内的职工信息。

select * from zhigong where sal between 1220 and 1240;

10. 求在wh2仓库工作的职工的最高工资值。

select max(sal) from zhigong where ckid='wh2';

11. 检索没有职工的仓库的信息

select * from cangku where ckid not in(select ckid from zhigong);

12. 求至少有两个职工的每个仓库的平均工资。

select ckid,avg(sal) from zhigong group bu ckid having count(zhgid)>=2;

七、PL/SQL编程

(一) 利用case语句,根据grade变量的值ABCD分别输出“优秀”、“良好”、“合格”、“不合格”(4分)

declare
	vgrade char(1):='&no';
begin
	case vgrade
	when 'A' then 
	dbms_output.put_line('优秀');
	when 'B' then 
	dbms_output.put_line('良好');
	when 'C' then 
	dbms_output.put_line('合格');
	when 'D' then 
	dbms_output.put_line('不合格');	
	else
       	dbms_output.put_line('输入不符合要求');
	end case;
end;

(二) 使用whie循环编程计算,输出n!,n值从键盘录入(5分)

declare
	vn number := &n;
	result vn%type :=1;
begin
	while vn>=1 loop
	result := result*vn;
	vn := vn-1;
	end loop;
	dbms_output.put_line(vn||'的阶层为:'||result);
end;

(三) 编写存储过程,输出在仓库wh1工作的职工信息,并调用该过程。过程名自定。(6分)

create or replace procedure mydure
is
	cursor mycur is select * from zhigong where ckid='wh1';
begin
	for my in mycur loop
	dbms_output.put_line(my.ckid|| '    ' ||my.zhgid|| '   ' ||my.sal);
	end loop;
end;

begin
	mydure();
end;

(四)编写存储过程,统计职工表中各部门的平均工资,并只显示平均工资超过1230元的仓库信息。调用此过程。过程名自定(6分)

create or replace procedure mydure
is
	cursor mycur is select ckid,avg(sal) from zhigong group by ckid having avg(sal)>1230;
begin
	for my in mycur loop
	dbms_output.put_line(my.ckid || '     ' || my.avg(sal));
	end loop;
end;

begin
	mydure();
end;

(五) 已有表example,要求创建一个序列myseq,再创建一个触发器,可以自动为example主键赋值,并进行测试。(7分)

表:create table example(id number(2) primary key,ename varchar2(20),address varchar2(30));
序列要求:初始值为1,增量为1,无最大值,不循环,没有缓冲区。

create sequence myseq start with 1 increment by 1 nocycle nocache;

create table example(
	id number(2) primary key,
	ename varchar2(20),
	address varchar2(30)
);


create or replace trigger mytrig before insert on example for each row
begin
	if :new.id is null then
	select myseq.nextval into :new.id from dual;
	end if;
end;

insert into example(ename,address) values('张三','北京时代胡同');

八、标准答案


--一、
CREATE user john identified by john123 DEFAULT tablespace users quota 20m on users;


--二、	
grant connect,resource to john;


--三、	
conn john/john123


--四、	
create table cangku(ckid char(3) primary key,city varchar2(10),area number(3) check (area<=1000));
create table zhigong(ckid char(3) references cangku(ckid),zhgid char(2),sal number(4) check (sal>=1200 and sal<=2000));
insert into cangku values('wh1','北京',370);
insert into cangku values('wh2','上海',500);
insert into cangku values('wh3','广州',200);
insert into cangku values('wh4','武汉',400);
insert into zhigong values('wh2','e1',1220);
insert into zhigong values('wh1','e3',1210);
insert into zhigong values('wh2','e4',1250);
insert into zhigong values('wh3','e6',1230);
insert into zhigong values('wh1','e7',1250);


--五、
--1. 
create table cangku1 as select * from cangku where area>300;
--2. 
insert into cangku1 values('wh5','济南',420);
--3. 
delete from cangku1 where ckid='wh4';
--4.
create index area_index on cangku1(area);
--5. 
create or replace view view1 as select sum(sal) from zhigong where ckid in(select ckid from cangku where city='北京' or city='上海');
--6. 连接system
create role role1;
grant all on john.cangku1 to role1;
grant role1 to public;


--7.
DECLARE
	sal_exception EXCEPTION;
	v_no zhigong.zhgid% TYPE:='&no';
	v_sal zhigong.sal%type;
begin
	select sal into v_sal from zhigong where zhgid= v_no;
	if v_sal<1200 THEN
	raise sal_exception;
	end if;
	EXCEPTION
	when sal_exception THEN
	dbms_output.put_line('你的工资太低了');
end;


--8.
select table_name, tablespace_name, status from user_tables;



--六、用sql命令完成下面的查询
--1.	从职工表中检索所有工资值,要求结果中没有重复值,并按工资降序排列。
select distinct sal from zhigong order by sal desc;
--2.	检索工资多于1230元的职工号。
select zhgid from zhigong where sal>1230;
--3.	检索哪些仓库有工资多于1210元的职工。
select ckid, zhgid,sal from zhigong where sal>1210;
--4.	给出在仓库“wh1”或“wh2”工作,并且工资少于1250元的职工号。
select zhgid from zhigong where sal<1250 and ckid in('wh1','wh2');
--5.	找出工资多于1230元的职工号和他们所在的城市。
select zhgid,city from cangku c, zhigong z where c.ckid= z.ckid and sal>1230; 
--6.	哪些城市至少有一个仓库的职工工资为1250元。
select c.ckid,city from cangku c, zhigong z where c.ckid= z.ckid and sal=1250;
--7.	查询所有职工的工资都多于1210元的仓库的信息。
select * from cangku c where 1210<all(select sal from zhigong where c.ckid=ckid) and ckid in (select ckid from zhigong);
--8.	找出和职工e4挣同样工资的所有职工。
select * from zhigong where sal=(select sal from zhigong where zhgid='e4');
--9.	检索出工资在1220元到1240元范围内的职工信息。 
select * from zhigong where sal between 1220 and 1240;
--10.	求在wh2仓库工作的职工的最高工资值。
select zhgid,sal from zhigong where ckid='wh2' and sal=(select max(sal) from zhigong where ckid='wh2');
--11.	检索没有职工的仓库的信息
select * from cangku where ckid not in(select ckid from zhigong);
--12.	求至少有两个职工的每个仓库的平均工资。
select ckid,avg(sal) from zhigong group by ckid having count(zhgid)>=2;


--七、PL/SQL编程
--(一)
set serveroutput on
declare
	grade char:='&no';
BEGIN
  case grade
    WHEN 'A' THEN
      dbms_output.put_line('优秀');
    WHEN 'B' THEN
      dbms_output.put_line('良好');
    WHEN 'C' THEN
      dbms_output.put_line('合格');
    WHEN 'D' THEN
      dbms_output.put_line('不合格');
    ELSE
       dbms_output.put_line('输入不符合要求');
  END CASE;
end;


--(二)
declare
	v_no int:=&no;
	v_jch int:=1;
	i int:=1;
begin
  	while i<=v_no loop
    v_jch:= v_jch*i;
    i:=i+1;
  	end LOOP;
  	dbms_output.put_line(v_no||'的阶乘是:'||v_jch);
end;


--(三)
--创建过程
create or REPLACE procedure zhg_proc
as
	CURSOR mycursor is select * from zhigong where ckid='wh1';
	rec_zhigong zhigong%rowtype;
begin
 	open mycursor;
 	loop
  	FETCH mycursor into rec_zhigong;
  	exit when mycursor%notfound;
  	dbms_output.put_line(rec_zhigong.ckid||' '||rec_zhigong.zhgid||' '|| rec_zhigong.sal);
 	END LOOP;
 	close mycursor;
end;
--调用过程
set serveroutput on
begin
  	zhg_proc();
end;


--(四)
create or replace procedure zhig_proc
as
	cursor ckcur is select ckid,avg(sal) avgsal from zhigong group by ckid having avg(sal)>1230;
	rec_zhig ckcur%rowtype;
begin
  	open ckcur;
  	loop 
    FETCH ckcur into rec_zhig;
    exit when ckcur%notfound;
    dbms_output.put_line(rec_zhig.ckid||' ' || rec_zhig.avgsal);
    END LOOP;
    close ckcur;
end;
--调用过程
set serveroutput on
begin
  	zhig_proc();
end;


--(五)
--创建表:
create table example(id number(2) primary key,ename varchar2(20),address varchar2(30));
--创建序列:
create sequence myseq start with 1 increment by 1 nomaxvalue nocycle nocache;
--创建触发器:
create or REPLACE trigger exam_trig
before insert on example
for each row
begin
	if new.id is null
  	select myseq.nextval into :new.id from dual;
  	end if;
end;
--测试:
insert into example(ename, address) values('aaa','nanjing');
select * from example;


猜你喜欢

转载自blog.csdn.net/qq_45696288/article/details/122244255