数据库课本习题及笔记

sqlplus  sys/123@222.16.65.250/orcl  as  sysdba
conn L13209010003/lxh12345@222.16.65.250/orcl

set serveroutput on;

数据库命名:
           rename medicine to m_medi;

1)sqlplus  kk/12345@localhost/orcl  as  sysdba
    conn lxh/lxh12345@localhost/orcl
2)SQL> create tablespace xinhui
    datafile 'c:\app\lxh\oradata\orcl\xinhui.dbf' size 100m;
3) create temporary tablespace xinhui1 tempfile
   'c:\app\lxh\oradata\orcl\xinhui1.dbf'size 100m;

3.5
Create table medicine(
Medicinecode char(10)primary key,
Medicinename varchar(50),
Pycode char(10),
Dosagefrom char(10),standard char(15),
Batchnumber char(20) unique,
Productiondate date,
Expirationdate date,
Category char(10),
Yb char(10))
;

Create table provider(
Providercode char(4) not null,
Providername char(60) not null,
Pycode char(10),address char(50),
Tel char(15),zip char(6),
Email char(30),relation char(8),
Primary key(providercode)
)
;

3.8
Create table pm(
Medicinecode char(10) not null,
Providercode char(4) not null,
Pmdate date not null,
Price  number,  qyt int,
Primary key(medicinecode,providercode,pmdate),
Foreign key(medicinecode) references medicine(medicinecode),
Foreign key(providercode)references provider(providercode)
);

查看表信息:
select *
from medicine
select *
from provider
select *
from pm
3.17
create cluster index idx_medicine_batchnumber
on medicine(batchnumber)
;




5月14
3.28
Select *
From medicine 
Where to_char(productiondate,’yy’,’mm’,’dd’)not between 10-06-06 and 12-01-06    
7.23
Create or replace trigger trg_insert_pm
Before insert on pm
For each row
Declare
 Mprovidername provider.providername%type;
Bigin
  Select providername into m_providername
From provider
Where providercode=:new.provider;
If m_providername='哈药制药' then
Raise_application_error(-2000,'不能插入');
End if;
End;
/
3.35
select*
from provider
where email like 'ttm*_%'escape'*'
;
3.52
select medicine.*,pm.*
from pm right join medicine
on medicine.medicinecode=pm.medicinecode;

3.56
select medicine.*
from medicine
where dosagefrom=
(select dosagefrom
 from medicine
 where medicinename='小儿颗粒'
)
3.57
select medicinecode,providercode
from pm x
where price>(select avg(price)
from pm y
 where y.medicinecode=x.medicinecode
)
3.58
select medicinename
from medicine
where exists
(select *
 from pm
where pm.medicinecode=medicine.medicinecode
and providercode='s001'
)
3.59
select distinct providercode from pm pm_a
where not exists
(select *
 from pm pm_b
 where(medicinecode='10001'or medicinecode='10002')
and not exists
  (select *from pm pm_c
    where pm_c.providercode=pm_a.providercode
   and pm_c.medicinecode=pm_b.medicinecode
   )
)
;
3.60
select distinct providercode
from pm pm_a
where not exists
 ( select*
    from medicine
   where category='中成药'
  )  
3.61
select *
from medicine
where dosagefrom='颗粒'
union (all)
select *
from medicine 
where category='中药'
;
3.62
select*
from medicine
where dosagefrom='颗粒'
intersect
select*
from medicine
where category='中药'
;
3.63
select*
from medicine
where dosagefrom='颗粒'
except
select*
from medicine
where category='中药'
;


3.73
Create VIEW ISYB_Medicine
As 
Select *
From Medicine
Where YB=’是’;
3.74
Create VIEW ISYB_Medicine1
As 
Select *
From Medicine
Where YB=’是’
With check option; 
3.75
Create view ZJKEB_Medicine
(Medicinecode,Medicinename,Category,Providername)
As 
Select Medicine.Medicinecode, Medicine. Medicinename,Category,Providername
From Medicine,PM,Provider
Where Medicine.Medicinecode=PM.Medicinecode
And Provider.Providercode=PM.Providercode
And Provider.Providername=’浙江康恩贝’
With check option;
3.77
Create view ZJKEB_ZCY_Medicine
AS
Select *
From ZJKEB_Medicine
Where Category=’中成药’;
3.77
Create view Proyear_Medicine(Medicinecode,Medicinename,Proyear)
As 
Select Medicinecode,Medicinename,to_date(Productiondate)
From Medicine;
3.78
Create view Count_Medicine(Providername,CountM)
As 
Select Providername,Count(Medicinecode)
From PM,Provider
Where PM.Providercode=Provider.Providercode
Group by Provider.Providername; 

7.17
declare
ex_var1 varchar2(10) default 'pl/sql';
ex_var2 constant number:=16;
med_name medicine.medicinename%type;
m_md medicine%rowtype;
type m_mdt is table of medicine.medicinename%type index by binary_integer;
m_mdt1 m_mdt;
begin
dbms_output.put_line('ex_var1='||ex_var1);
dbms_output.put_line('ex_var2='||ex_var2);
select medicinename
into med_name
from medicine
where medicinecode='10001';
dbms_output.put_line('药品名称='||med_name);
select*
into m_md
from medicine
where medicinecode='10001';
dbms_output.put_line('药品名称:'||m_md.medicinename);
dbms_output.put_line('拼音简码:'||m_md.pycode);
dbms_output.put_line('剂型:'||m_md.dosagefrom);
dbms_output.put_line('规格:'||m_md.standard);
m_mdt1(0):='健儿清解液';
m_mdt1(1):='小儿感冒灵';
dbms_output.put_line(m_mdt1(0));
dbms_output.put_line(m_mdt1(1));
end;
/
set serveroutput on;
7.18
declare
n int;
info varchar2(30);
m_mdatee medicine.expirationdate%type;
begin
select expirationdate
into m_mdatee
from medicine
where medicinename='小儿颗粒';
n:=m_mdatee-sysdate;
case sign(n)
when -1 then info:='已过期';
when 1 then info:='未过期';
else
info:='今天过期';
end case;
dbms_output.put_line('状态::'||info);
for n in 3..6
loop
insert into medicine(medicinecode) values('9000'||n);
end loop;
commit;
end;
/
select medicinecode from medicine where medicinecode like '9000%';

update medicine
set expirationdate='31-12月-16'
where medicinecode='10001';

7.19
declare
m_md medicine%rowtype;
cursor cur_m_md is select* into m_md from medicine;
bigin
if not cur_m_md%isopen then open cur_m_md;
end if;
dbms_output.put_line('提取行数:'||to_char(cur_m_md%rowcount));
loop
 fetch cut_m_md into m_md;
 dbms_output.put_line('行:'||to_char(cur_m_md%rowcount));
 exit when cur_m_md%notfound or cur_m_md%rowcount>=3;
end loop;
close cur_m_md;
end;
/

7.20
declare 
cursor cur_medi_name
is
select medicinename from m_medi;
medi_name m_medi.medicinename%type;
result varchar2(200);
begin 
open cur_medi_name;
loop
fetch cur_medi_name into medi_name;
exit when cur_medi_name%notfoun;
result:=result||','||medi_name;
end loop;
dbms_output.put_line(result);
close cur_medi_name;
end;

7.21
create or replace produre prc_query_name
(m_code m_medi.medicinecode%type,
m_name out m_medi.medicinename%type)
as
begin
select medicinename into m_name
from m_medi where medicinecode=m_code;
end;
/
declare
m_mdname char(20);
begin
prc_query_mname('10001',m_mdname);
end;
/
7.22
create or replace package pkg_m_medi
as
m_mediname char(20);
procedure prc_query_mname;
end;
/
create or replace package body pkg_m_medi
as
begin
select medicinename into m_medicine from m_medi
where medicinecode='10001';
dbms_output.put_line('药品名称'||m_mediname);
end;
end;
/
begin 
pkg_m_medi.prc_query_name;
end;
/
7.23
create or replace trigger trg_insert_pm
before insert on pm
for each row
declare
m_providername provider.providername%type;
begin
select providername into m_providername
from provider
where providercode=:new.providercode;
if m_providername='哈'then
raise_application_error(-20000,'不能插入');
end if;
end;
/
7.25
create or replace trigger trg_update_pm
before update on pm
for each row
declare
m_providername provider.providername%type;
begin
select providername into m_providername
from provider
where providercode=:new.providercode;
if m_providername='海南'and:old.price>=20 and :new.qyt>=300 then
:new.price:=20;
end if;
end;
/
 update pm set qyt=300 where providercode='s001'and medicinecode='10003';
7.26
create table record_pm(
medicinecode char(10) not null,
providercode char(4) not null,
pmdate date not null,
qyt int,
primary key(medicinecode,providercode),
foreign key(medicinecode)references m_medi(medicinecode),
foreign key(providercode)references provider(providercode)
)
/

create or replace trigger trg_record_pm
after update on pm
for each row
begin
if :new.qyt<>:old.qyt then insert into record_pm
values(:old.medicinecode,:new.providercode,sysdate,:new.qyt);
end if;
end;
/

9.1
insert into provider(providercode,providername,pycode,address,tel,zip,email,relation)
values('s005','海灵','hlzy','海南','570206','hailing@sina.com','李艳');
1 row inserted
commit;
commit complete

9.2
insert into provider(providercode,providername,pycode,address,tel,zip,email,relation)
增加一列:
alter table T_score add score float;
alter table T_course drop column score;
alter table T_score rename column socre to score;

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值