青软实训 Oracle实验报告

一、创建数据库
/*创建用户*/
   创建用户 命令格式:create user username identified by password;
   用户授权  grant connect,resource to yihan;
二、创建表
/*创建tb_user表*/
      create sequence s1 start with 1000 increment by 1;
create table tb_user(
   userId number(10) not null primary key,
   name varchar2(10) unique not null,
   password varchar2(30) not null,
   email varchar2(20),
   provinceId number(10),
   cityId number(10),
   telephone char(11),
   sex char(3) default '男',
   age number(10),
   birthday date,
   money number(10) default 0,
   photo varchar2(500),
   dsp varchar2(200),
   qq varchar2(15),
   msn varchar2(100),
   loveBook varchar2(100),
   loveMusic varchar2(100),
   loveMovie varchar2(100),
   loveSport varchar2(100),
   loveGame varchar2(100),
   constraint fk_cityId foreign key(cityId) references tb_city(cityId)
   )
/*创建tb_product表*/
create sequence s2 start with 1000 increment by 1; 
   create table tb_product(
      productId number(10) not null primary key,
      userId number(10) not null,
      productName varchar2(50) not null,
      typeId number(10) not null,
      price number(10) not null,
      photo varchar2(500),
      information varchar2(500),
      constraint fk_userId foreign key(userId) references tb_user(userId),
      constraint fk_typeId foreign key(typeId) references tb_productType(productTypeId)
      )

/*创建tb_productType表*/
   -------tb_productType表(商品类型表)--
     create sequence s3 start with 1000 increment by 1;
     create table tb_productType(
         productTypeId number(10) not null primary key,
         productTypeName varchar2(100)
     );

/*创建tb_order表*/
 (orderState,orderID) 
      ----tb_order表(订单表)----
       create sequence s4 start with 1000 increment by 1;
       create table tb_order(
          orderId number(10) not null primary key,
          toId number(10) not null,
          colorId number(10),
          productId number(10),
          address varchar2(100),
          telephone char(13),
          orderState number(10),
          count number(10),
          price number(10),
          orderDate date,
          consignmentDate date,
          constraint fk_toId foreign key(toId) references tb_user(userId),
          constraint fk_colorId foreign key(colorId) references tb_productColor(productColorId),
          constraint fk_productId11 foreign key(productId) references tb_product(productId)
          );
/*创建tb_userAccount表*/
create sequence s5 start with 1000 increment by 1;
      drop table tb_userAccount;
         create table tb_userAccount(
              userAccountId number(10) primary key,
              userId number(10),
              orderId number(10),
              type number(10),
              time date,
              constraint fk_userId1 foreign key(userId) references tb_user(userId),
              constraint fk_orderId foreign key(orderId) references tb_order(orderId)
          );

/*创建tb_review表*/
    create sequence s6 start with 1000 increment by 1;
          create table tb_review (
             reviewId number(10) primary key,
             review varchar2(200),
             isReply number(1) default 0,
             isDel number(1) default 0,
             senderId number(10),
             productId number(10),
             addTime date,
             replyId  number(10),
             constraint fk_senderId foreign key(senderId) references tb_user(userId),
             constraint fk_productId1 foreign key(productId) references tb_product(productId)
             
           );

/*创建tb_province表*/
     create sequence s7 start with 1000 increment by 1;
           create table tb_province(
              provinceId number(10) not null primary key,
              provinceName varchar2(30)
           );

/*创建tb_city表*/
  create sequence s8 start with 1000 increment by 1;
           create table tb_city(
              cityId number(10) not null primary key,
              provinceId number(10),
              cityName varchar2(30),
              constraint fk_provinceId foreign key(provinceId) references tb_province(provinceId)
             );

/*创建tb_productSize表*/
    create sequence s9 start with 1000 increment by 1;    
             create table tb_productSize(
                 productSizeId number(10) not null primary key,
                 productSize varchar2(20) not null,
                 productId number(10),
                 Constraint fk_productId foreign key(productId) references tb_product(productId)
                 );

/*创建tb_productColor表*/
 create sequence s10 start with 1000 increment by 1; 
              create table tb_productColor(
                 productColorId number(10) primary key,
                 productColor varchar2(20),
                 stockPile number(10),
                 productSizeId number(10),
                 constraint fk_productSizeId foreign key(productSizeId) references tb_productSize(productSizeId)
                 )


三、添加约束
/*给每个表添加约束*/
   建表时已添加约束

四、基础数据(商品类型+省份+城市)添加
/*基础数据(商品类型+省份+城市)添加*/
/*tb_productType表添加商品类型*/
  insert into tb_productType values(s3.nextval,'饼干');
  insert into tb_productType values(s3.nextval,'雪糕');      
  select * from tb_productType;    

/*tb_province表添加省份名称*/
     insert into tb_province values(s7.nextval,'山东省');
     insert into tb_province values(s7.nextval,'浙江省);
     select * from tb_province;
     
  

/*tb_city表给山东省添加城市名称*/
   insert into tb_city values(s8.nextval,1001,'青岛市');
           insert into tb_city values(s8.nextval,1001,'烟台市');
           select * from tb_city;


五、用户数据(注册+信息完善)添加
/*用户数据(注册+信息完善)添加*/
/*tb_user表添加用户信息*/
  insert into tb_user values(s1.nextval,'yiyi','123456','123456789',1001,1003,15455556666,
     '男',20,to_date('2014-02-14','yyyy-mm-dd'),10000,'1005','5656','1566','44545','5555','5666','5555','5555','55666');
     insert into tb_user values(s1.nextval,'yi','123456','123456789',1002,1004,15455556666,
     '男',20,to_date('2014-02-14','yyyy-mm-dd'),10000,'1005','5656','1566','44545','5555','5666','5555','5555','55666');
  

六、商品信息(商品信息+型号+颜色)添加
/*商品信息(商品信息+型号+颜色)添加*/
/*tb_product表添加商品信息*/
     insert into tb_product values(s2.nextval,1002,'小苹果',1001,1000,'s55s5','zixi');
       insert into tb_product values(s2.nextval,1004,'小苹果',1001,1000,'s55s5','zixi');
       
       select * from tb_product;

/*tb_productSize表添加商品型号*/
    insert into tb_productSize values(s9.nextval,'XXX',1001);
    insert into tb_productSize values(s9.nextval,'XXL',1002);
            

/*tb_productColor表添加商品型号*/
  insert into tb_productColor values(s10.nextval,'red',1000,1001);
  insert into tb_productColor values(s10.nextval,'yellow',1000,1002);
七、删除用户信息(使用plsql)
/*删除用户为XX的用户信息*/
八、商品信息查询
/*商品信息查询*/
/*单表查询*/
/*查询tb_product表中商品id为的商品信息*/
   

/*查询tb_productSize表中商品型号id为的型号信息*/

   
/*查询tb_productColor表中商品色号id为的颜色与库存信息*/
 

/*多表查询*/
/*用户在商城以商品类型查询一件商品的名称*/
 --------/*用户在商城以商品类型查询一件商品的名称*/-----
        select * from tb_product tp join tb_productType tp1 on tp.typeId=tp1.productTypeId; 
    

/*用户在商城以商品名称查询一件商品的所有信息*/
select * from tb_product where productName='小苹果';
     

九、用户信息查询
/*用户信息查询*/
/*用户在商城可以查询已知用户(或卖家)的基本信息*/
select * from tb_product tp join tb_user tu on tp.userId=tu.userId;


十、用户下订单并确认付款
/*用户下订单并确认付款*/
/*用户***想要购买卖家***的产品颜色-大小-产品名*/
 select productColor,productSize,productName from tb_productColor tpc,tb_productSize tps,
                tb_product tp,tb_user tu 
                where tpc.productSizeId=tps.productSizeId and tps.productId=tp.productId 
                and tp.userId=tu.userId and tu.name='yi'; 

/*定义变量,用于存储本次订单的物品单价*/
declare 
             this_price number(10);
           begin
             select price into this_price from tb_order where orderId=1002;
             dbms_output.put_line(this_price);
           end;
    
/*定义变量,用于存储本次订单的物品ID*/
    declare
              this_product varchar2(20);
           begin
              select productName into this_product from tb_order tbo join tb_product tp on  
              tbo.productId=tp.productId where orderId=1002;
              dbms_output.put_line(this_product);
           end;
   

/*定义变量,用于存储本次订单的颜色ID*/
          declare
             this_productColor varchar2(20);
          begin
             select productColor into this_productColor from tb_order tbo join tb_productColor tp on  tbo.colorId=tp.productcolorid where orderId=1002;
             dbms_output.put_line(this_productColor);
          end;
    

/*定义变量,用于存储本次订单的买家ID*/
           declare
             this_userId number(10);
           begin
             select userId into this_userId from tb_order tbo join tb_user tu on 
             tbo.toId=tu.userId where orderId=1002;
             dbms_output.put_line(this_userId);
           end;
  

/*定义变量,用于存储流水表的用户ID*/
        declare
              this_userAccountId number(10);
           begin
               select userAccountId into this_userAccountId from tb_userAccount where orderId=1002;
            dbms_output.put_line(this_userAccountId);
           end;
           
    

/*用户****下订单*/
/*用户****将订单信息填入到tb_order表中*/
		--orderState订单状态为用户选择填入下订单(买家付款后自动转为已付款,买家付款后卖家可选择修改为延后,卖家发货后选择修改为发货,买家未付款时卖家可选择修改为撤销,买家收到货物后可选择修改为完成)
/*下订单后减少商品库存*/
/*确认付款后减少用户余额*/
declare
  this_userId number;
  this_productColorId number;
  this_productId number;
  this_count number;
  this_stockPile number;
  this_price number;
  this_money number;
  this_phone char(11);
  Ordernum number;------------当前订单编号---
begin
  this_userid:='&买家id';
  this_productColorId:='&颜色Id';
  this_productId:='&商品id';
  this_count:='&数量';
  select telephone into this_phone from tb_user where userId=this_userId;
  select price into this_price from tb_product where productId=this_productId;
  ----------/*用户****将订单信息填入到tb_order表中*/
  insert into tb_order values(s4.nextval,this_userid,this_productcolorid,this_productid,'默认地址',this_phone,0,this_count,(this_count*this_price),sysdate,(sysdate+3));
  -------/*下订单后减少商品库存*/---
  select stockpile into this_stockPile from tb_productColor where productColorId=this_productColorId;
  if this_stockPile>this_count then
     begin
       update tb_productColor set stockPile=stockPile-this_count where productColorId=this_productColorId;
     end;
  else
      raise_application_error(-20002, '库存不足');  
  end if;
  ---------/*确认付款后减少用户余额*/
  
  select money into this_money from tb_user where userId=this_userId;
  if this_money>this_count*this_price then
    begin
      update tb_user set money=this_money-this_count*this_price where userId=this_userid;
    end;
  else
    raise_application_error(-20002, '账户余额不足');
  end if;
  
  select s4.currval into ordernum from dual;------获取当前订单编号---
  
  ------/*将用户付款的订单状态改为已付款  1代表"已付款"*/
  update tb_order set orderState=1 where orderId=orderNum;
  
  ------/*用户账户上的钱转账完成后建立用户资金流水表一条买家购买商品支出记录*/
  
  insert into tb_useraccount values(s5.nextval,this_userId,ordernum,this_productColorId,sysdate);
      dbms_output.put_line('操作成功');
  commit;
  
  exception 
  when no_data_found then
 -- begin
      dbms_output.put_line('数据未找到,操作失败');
  rollback;
 -- end; 
end;
 
十一、买家对订单信息进行修改(包括发货后的订单状态修改及修改商品颜色)
/*卖家对订单信息进行修改*/
/*假设买家***想修改商品的颜色,则需要对相应颜色的库存进行修改*/
/*修改信息是各项信息均为可选择项,如不想修改某项信息,则选择以前的选项,但是会将数据重复的修改一次,即用相同的数据来覆盖之前的数据*/
/*除此之外卖家发货后可将订单状态改为发货*/
/*定义变量,用于存储本次订单的物品单价*
/*修改tb_order表中的送货地址*/
/*修改tb_order表中的联系方式*/
/*修改tb_order表中的状态*/
	--若卖家因故不能发货,但已付款则卖家可修改订单状态为'延后'
/*修改tb_order表中的商品颜色*/
/*修改tb_order表中的商品总价*/
	--若和买家商议后买家同意降价,但商品信息上还是不修改的,就可以在订单总价上修改,付款后卖家修改总价,差价打回买家账户余额内
/*将与之前的差价加回买家账户余额内*/
/*修改tb_user表中买家的账户余额*/
/*修改颜色后要将之前选择的颜色库存加一再将修改后的颜色库存减一*/

declare
   this_orderId number;
   this_address varchar2(20);
   this_telephone char(11);
   this_orderState number;
   this_colorId number;
   this_price number;
   this_count number;
   this_bef_price number;
   this_old_productColorId number;
begin
  
  ------ 根据订单编号来修改----
  this_orderId:=&订单编号Id;
  ----/*修改tb_order表中的送货地址*/--
  this_address:='&送货地址';
  update tb_order set address=this_address where  orderId=this_orderId;
  dbms_output.put_line('送货地址已修改');
  
  -----------/*修改tb_order表中的联系方式*/----;
  this_telephone:='&联系方式';
  update tb_order set telephone=this_telephone where orderId=this_orderId;
  dbms_output.put_line('联系方式已修改');
  
  -----------/*修改tb_order表中的状态*/----
  this_orderState:=&修改状态;
  ------若卖家因故不能发货,但已付款则卖家可修改订单状态为'延后'
  update tb_order set orderState=this_orderState where orderId=this_orderId;
  dbms_output.put_line('订单状态已修改');
  -----------/*修改tb_order表中的商品颜色*/----
  ----先获取修改之前的订单商品的颜色----
  
  select colorId into this_old_productColorId from tb_order where orderId=this_orderId;
  ------获取此订单颜色的数量---
  
  select count into this_count from tb_order where orderId=this_orderId; 
  -----------新的颜色----
  
  this_colorId:='&颜色id';
  update tb_order set colorId=this_colorId where orderId=this_orderId;
  
  --------------  /*修改颜色后要将之前选择的颜色库存加一再将修改后的颜色库存减一*/---
  update tb_productcolor set stockPile =stockPile-this_count where productColorId=this_orderId;  
  update tb_productColor set stockPile=stockPile+this_count where productColorId=this_old_productColorId;
  
  dbms_output.put_line('商品颜色已修改');
  -------------/*修改tb_order表中的商品总价*/-----
  
  select price into this_bef_price from tb_order where orderId=this_orderId;
  this_price:='&当前价格';
  
  update tb_order set price=this_price where orderId=this_orderId;
  
  -----------若和买家商议后买家同意降价,但商品信息上还是不修改的,就可以在订单总价上修改,付款后卖家修改总价,差价打回买家账户余额内
  -------将之前的差价补回原账户----
  
  update tb_user set money=money+this_bef_price-this_price where userId=(select toId from tb_order where orderId=this_orderId);
  dbms_output.put_line('商品总价已修改');
  dbms_output.put_line('操作成功');
  commit;
  
  exception 
  when no_data_found then
   dbms_output.put_line('数据未找到,操作失败');
  rollback;
end;

十二、订单完成进行转账
/*订单完成进行转账*/
/*定义变量,用于存储流水表的用户ID*/
/*定义变量,用于存储本次订单的物品单价*/
/*买家收到商品后,修改tb_order表中的状态*/
/*订单状态为'完成'后建立用户资金流水表,一条卖家出售商品收入记录*/
/*交易成功后卖家账户余额增加*/
declare
  this_userId number;
  this_sellerId number;
  this_price number;
  this_orderId number;
begin
  this_orderId:='&订单号';
  ----------/*买家收到商品后,修改tb_order表中的状态*/---
  update tb_order set orderState=3 where orderId=this_orderId;
  dbms_output.put_line('订单状态已完成');
  
  ---------- /*订单状态为'完成'后建立用户资金流水表一条卖家出售商品收入记录*/
  
  select userId into this_sellerId from tb_product where productId=(select productId from tb_order where 
    orderId=this_orderId);
  insert into tb_useraccount values(s5.nextval,this_sellerid,this_orderId,1,sysdate);
  dbms_output.put_line('用户资金流水表插入一条卖家出售商品收入记录');
  
    --------- /*交易成功后卖家账户余额增加*/
    select price into this_price from tb_order where orderId=this_orderId;
    
    update tb_user set money=money+this_price where userId=this_sellerId;
    dbms_output.put_line('卖家账户余额增加');
    dbms_output.put_line('操作成功');
    COMMIT;
exception
when no_data_found then
  dbms_output.put_line('数据未找到,操作失败');
rollback;
  
end;



十三、货物丢件进行退款
/*货物丢件进行退款*/
/*前提条件:订单为发货状态,但物流出现丢件情况,由物流赔偿卖家,卖家确认丢件情况发生后,可进行退款操作*/
/*定义变量,用于存储流水表的用户ID*/


/*卖家确认商品丢件后,修改tb_order表中的状态*/
/*订单状态为'撤销'后建立用户资金流水表一条买家商品退款收入记录*/
/*撤销成功后买家账户余额恢复*/
declare
  this_sellerId number;
  this_userId number;
  this_orderId number;
  this_price number;
begin
  this_orderId:='&订单编号';
  -------------/*卖家确认商品丢件后,修改tb_order表中的状态*/---
  update tb_order set orderState=5 where orderId=this_orderId;
  dbms_output.put_line('订单状态:撤销');
  ------- /*订单状态为'撤销'后建立用户资金流水表一条买家商品退款收入记录*/--
  select toid into this_userId from tb_order where orderId=this_orderId;
  insert into tb_useraccount values(s5.nextval,this_userId,this_orderId,1,sysdate);
  dbms_output.put_line('用户资金流水表插入一条买家商品退款收入记录');
  -------------/*撤销成功后买家账户余额恢复*/---
  select price into this_price from tb_order where orderId=this_orderId;
  update tb_user set money=money+this_price where userId=this_userId;
  dbms_output.put_line('买家账户余额恢复');
  dbms_output.put_line('操作成功');
  commit;
  
exception
when no_data_found then
    dbms_output.put_line('数据未找到,操作失败');
    rollback;
end;
  



十四、视图
--/*多表查询*/
--/*用户在商城以商品类型查询一件商品的名称*/
create view view_Name
as
   select * from tb_product tp ,tb_productType tpt where tp.typeid=tpt.producttypeid;
declare
   name varchar2(20);
   dname varchar2(20);
   cursor cur(xname varchar2) 
   is 
     select productName from view_Name where productTypeName=xname;
begin
  dname:='&商品类型';
  open cur(dname);
    loop
      fetch cur into name;
      exit when cur%notfound;
      dbms_output.put_line('商品名称: '||name);
    end loop;
 close cur;
end;

--/*用户在商城以商品名称查询一件商品的所有信息*/
create view this_view
as 
   select productcolorid,productcolor,stockpile,
   c.productsizeid,productsize,s.productid,userid,productname,typeid,price,photo,information 
   from tb_productcolor c 
   join tb_productsize s on c.productsizeid=s.productsizeid
   join tb_product p on p.productid=s.productid;
   
declare
dm  this_view%rowtype;
pname varchar2(50);
cursor cur_t (product_name in varchar2)
is
 select *  from this_view
   where productname=product_name;
begin
   pname:='&商品名称';
   open cur_t(pname);
   loop
   fetch cur_t into dm;
   exit when cur_t%notfound;
    dbms_output.put_line(
    '商品id:'||dm.productid||
    '  商品名字:'||dm.productname||
    '  商品颜色id:'||dm.productcolorid||
    '  商品颜色:'||dm.productcolor||
    '  商品库存:'||dm.stockpile||
    '  商品型号id:'||dm.productsizeid||
    '  商品型号:'||dm.productsize||
    '  商品卖家id:'||dm.userid||
    '  商品类型id:'||dm.typeid||
    '  商品价格:'||dm.price||
    '  商品图片路径:'||dm.photo||
    '  商品简介:'||dm.information
   );
    end loop;
   close cur_t;
exception
   when no_data_found then
   dbms_output.put_line('没有发现您要找的数据!');
end;
 

--/*用户信息查询*/
--/*用户在商城可以查询已知用户(或卖家)的基本信息*/
declare
   yonghuId tb_user.userId%type;
   yonghu tb_user%rowtype;
begin
  yonghuId:='&用户ID';
  select * into yonghu from tb_user where userId=yonghuId;
  dbms_output.put_line(
   '用户id:'||yonghu.userid||
    '  用户名:'||yonghu.name||
    '  邮箱:'||yonghu.email||
    '  所在省份id:'||yonghu.provinceid||
    '  所在城市id:'||yonghu.cityid||
    '  手机号:'||yonghu.telephone||
    '  性别:'||yonghu.sex||
    '  年龄:'||yonghu.age||
    '  生日:'||yonghu.birthday
   );
exception
   when no_data_found then
   dbms_output.put_line('没有发现您要找的数据!');
end;


十五、用户登录
/*用户登录(应用存储过程)*/
create or replace procedure login(login_name in varchar2,login_password in varchar2)
as
  huoqu_password varchar2(30);
begin
  select password into huoqu_password from tb_user where name=login_name;
  if huoqu_password=login_password then 
    dbms_output.put_line('欢迎登陆!');
  else 
    dbms_output.put_line('密码错误!');
  end if;
  EXCEPTION
     when no_data_found then
       dbms_output.put_line('该用户尚未注册!');
end login;

begin
  login('yiyi','123456');
end;

begin
  login('yi','123456');
end;



 

实验一 SQL*PLUS练习 【实验目的】 (1) 了解Oracle的工作环境和基本使用方法。 (2) 练习标准SQL的数据操作,查询命令及其查询优化。 (3) 学会使用高级SQL命令,排序、分组、自连接查询等。 (4) 学会使用SQL*PLUS命令显示报表,存储到文件等。 【实验内容】 一、 准备使用SQL*PLUS 1. 进入SQL*PLUS 2. 退出SQL*PLUS 3. 显示表结构命令DESCRIBE SQL>DESCRIBE emp 使用DESCRIBE(缩写DESC)可以列出指定表的基本结构,包括各字段的字段名以及类型、长度、是否非空等信息。 4. 使用SQL*PLUS显示数据库中EMP表的内容 输入下面的查询语句: SQL>SELECT * FROM emp; 按下回车键执行查询 5. 执行命令文件 START或@命令将指定文件调入SQL缓冲区中,并执行文件内容。 SQL>@ 文件名(文件后缀缺省为.SQL)或 SQL>START 文件名 文件中每条SQL语句顺序装入缓冲区并执行。 二、 数据库命令——有关表、视图等的操作 1. 创建表employee 例1 定义一个人事信息管理系统中存放职工基本信息的一张表。可输入如下命令: SQL>CREATE TABLE employee (empno number(6) PRIMARY KEY, /* 职工编号 name varchar2(10) NOT NULL, /* 姓名 deptno number(2) DEFAULT 10, /* 部门号 salary number(7,2) CHECK(salarycreate table emp2 as select * from emp where 1=2; 在命令的where子句中给出1=2,表示条件不可能成立,因而只能复制表结构,而不能复制任何数据到新表中去。另外,还可以复制一个表的部分列定义或部分列定义及其数据。 三、 Oracle数据库数据查询 1、单表查询 2、多表查询 四、 SQL*PLUS常用命令 表1 常用报表格式化名命令 命令 定义 Btitle 为报表的每一页设置底端标题 Column 设置列的标题和格式 Compute 让SQL*PLUS计算各种值 Remark 将某些字标记为注释 Set linesize 设置报表的行宽字符数 Set newpage 设置报表各页之间的行数 Spool 使SQL*PLUS将输出写入文件中 Start 使SQL*PLUS执行一个sql文件 Ttitle 设置报表每页的头标题 Break 让SQL*PLUS进行分组操作 例3 建立一个批命令文件对查询到的数据以报表的形式输出并将其保存到指定的文件中。 处理方法:利用SQL*PLUS语言工具(也可以使用其他文本编辑器)建立批命令的.SQL文件。在“SQL>”提示符下,使用EDIT命令在”E:\”中建立SCGB.SQL文件。 SCGB.SQL文件中的命令组如下: SQL>EDIT E:\ SCGB.SQL SET echo off SET pagesize 30 SET linesize 75 TTITLE’2008年4月10号’CE’公司职员基本情况登记表’R’Page:’ FORMAT 99- >SQL.PNO SKIP 1 CE’===========================’ BTITLE COL 60 ’制标单位’ TAB 3 ‘人事部’ COLUMN empno heading ‘职工|编号’ COLUMN ename format a10 heading ‘姓 名’ COLUMN job heading ‘工 种’ COLUMN sal format $99,990 heading 工 资’ COLUMN comm Like sal heading ‘奖 金’ COLUMN deptno format 9999 heading ‘部门|编号’ COLUMN hiredate heading ‘参加工作时间’ SPOOL e:\sjbb /*在E盘中建立格式报表输出文件,默认属性为LST BREAK on deptno skip 1 COMPUTE sum of sal comm on deptno SELECT empno,ename,job,hiredate,sal,comm,deptno from emp ORDER BY deptno,sal; SPOOL off /*终止SPOOL功能,关闭其文件。注意,此命令不可省,否则将建立空文件。 五、 实验内容 1、以cs+学号为用户名创建用户,并授予用户创建数据对象的权限。 2、复制emp表,复制表名为emp_学号,然后将emp表中工资低于$2000 的职工插入到复制的表中。 3、对复制的emp表插入一行只包含有职工号,职工名,工资与部门号四个数据 项值的记录。 4、在复制的emp表中将雇员ALLEN提升为经理,工资增至$2500, 奖(佣 )金增加40%。 5、删除复制的emp表中工资低于500的记录行。 6、列出10号部门中既不是经理,也不是秘书的职工的所有信息。 7、查找出部门所在地是CHICAGO的部门的职工姓名、工资和工种。 8、统计各部门中各工种的人数、工资总和及奖金总和。 9、查找出工资比其所在部门平均工资高的职工姓名、工种与工资情况。 实验3 Oracle数据库开发环境下PL/SQL编程 【实验目的】 (1)掌握 PL/SQL 的基本使用方法。 (2)在SQL*PLUS环境下运行PL/SQL的简单程序。 (3)应用 PL/SQL 解决实际问题 【实验内容与步骤】 PL/SQL块中的可执行部分是由一系列语句组成的(包括对数据库进行操作的SQL语句,PL/SQL语言的各种流程控制语句等)。在块中对数据库查询,增、删、改等对数据的操作是由SQL命令完成的。在PL/SQL块中,可以使用SQL的数据查询命令,数据操纵命令和事务控制命令。可使用全部SQL函数。PL/SQL中的SQL语句,可使用SQL的比较操作等运算符。但不能使用数据定义语句。 在PL/SQL块中使用SELECT语句时注意几点: (1)SELECT语句必须含有INTO子句。 (2)INTO子句后的变量个数和位置及数据类型必须和SELECT命令后的字段名表相同。 (3)INTO子句后可以是简单类型变量或组合类型变量。 (4)SELECT语句中的WHERE条件可以包含PL/SQL块中定义的变量及表达式,但变量名不要同数据库表列名相同。 (5)在未使用显式游标的情况下,使用SELECT语句必须保证只有一条记录返回,否则会产生异常情况。 [例3-1] 问题:编写一个过程,求和运算。 SET SERVEROUTPUT ON; DECLARE a number:=1; BEGIN a:=a+5; DBMS_OUTPUT.PUT_LINE('和为:'||TO_CHAR(a)); END; / 【例3-2】:使用%TYPE声明变量,输出制定表中的相关信息。 DECLARE my_name student.sname%TYPE; BEGIN SELECT sname INTO my_name FROM student WHERE no=’01203001’; DBMS_OUTPUT.PUT_LINE(my_name); END; / 【例3-3】问题:编写一个过程,可以输入一个雇员名,如果该雇员的工资低于2000,就给该员工工资增加10%。 declare v_sal emp.sal%type; begin select sal into v_sal from emp where ename=spName; if v_sal :NEW.sal THEN DBMS_OUTPUT.PUT_LINE('工资减少'); ELSIF :OLD.sal < :NEW.sal THEN DBMS_OUTPUT.PUT_LINE('工资增加'); ELSE DBMS_OUTPUT.PUT_LINE('工资未作任何变动'); END IF; DBMS_OUTPUT.PUT_LINE('更新前工资 :' || :OLD.sal); DBMS_OUTPUT.PUT_LINE('更新后工资 :' || :NEW.sal); END; / --执行UPDATE查看效果 UPDATE emp SET sal = 3000 WHERE empno = '7788'; 6、需要对在表上进行DML操作的用户进行安全检查,看是否具有合适的特权。 Create table foo(a number); Create trigger biud_foo Before insert or update or delete On foo Begin If user not in (‘DONNY’) then Raise_application_error(-20001, ‘You don’t have access to modify this table.’); End if; End; / 即使SYS,SYSTEM用户也不能修改foo表。 2、 利用PL/SQL编写程序实现下列触发器 1)、编写一个数据库触发器,当任何时候某个部门从dept表中删除时,该触发器将从emp表中删除该部门的所有雇员。(要求:emp表、dept表均为复制后的表) 2)、创建一个触发器,当客户下完订单后,自动统计该订单的所有图书的价格总额。 3)、创建一个触发器,禁止客户在非工作时间(早上8:00前,晚上17:00后)下订单。 五、实验心得
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值