數據庫課本習題及筆記

sqlplus  sys/[email protected]/orcl  as  sysdba
conn L13209010003/[email protected]/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','[email protected]','李豔');
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;

發佈了94 篇原創文章 · 獲贊 24 · 訪問量 12萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章