为一个小孩写的oracle数据库简单SQL语句

2024-03-16 23:58

本文主要是介绍为一个小孩写的oracle数据库简单SQL语句,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

由于当时没有环境,然后装了个ORACLE12.1版本程序,安装过程中碰到以下问题:

oracle12c安装失败【INS-30131】执行安装程序验证所需要的初始设置失败

使用以下方法解决:

针对客户端安装,在cmd中执行命令:
前面加实际路径setup.exe -ignorePrereq -J"-Doracle.install.client.validate.clientSupportedOSCheck=false" 
针对服务端安装,在cmd中执行命令:
前面加实际路径setup.exe -ignorePrereq -J"-Doracle.install.db.validate.supportedOSCheck=false"

这个来自于:

https://blog.csdn.net/killvoon/article/details/51821928

所有的SQL语句如下:

/*=======================以下需要以DBA的身份重新建立一个用户,以新用户去执行,================*/
/*==========否则触发器和存储过程是不会以DBA的角色创建=======================================*/

drop table vcd_borrow_record cascade constraints;

drop table vcd_borrow_type cascade constraints;

drop table vcd_info cascade constraints;

drop table vcd_sale_record cascade constraints;

drop table vcd_type cascade constraints;

/*==============================================================*/
/* Table: vcd_borrow_record                                   */
/*==============================================================*/
create table vcd_borrow_record 
(
   ID                   NUMBER(20)           default NULL not null,
   vcd_id             NUMBER(20),
   record_date        DATE,
   ctype               INT                  default NULL,
   vcdnum             INT,
   constraint PK_VCD_BORROW_RECORD primary key (ID)
);

comment on table vcd_borrow_record is
'vcd借还记录表';

comment on column vcd_borrow_record.ctype is
'0:借;1还';

/*==============================================================*/
/* Table: vcd_borrow_type                                     */
/*==============================================================*/
create table vcd_borrow_type 
(
   ID                   INT                  default NULL not null,
   typename           varchar(50),
   constraint PK_VCD_BORROW_TYPE primary key (ID)
);

comment on table vcd_borrow_type is
'vcd借还类型表';

/*==============================================================*/
/* Table: vcd_info                                            */
/*==============================================================*/
create table vcd_info 
(
   ID                   NUMBER(20)           default NULL not null,
   ctype               int,
   vcdname            varchar(255),
   price              DEC(12, 2),
   saleprice          DEC(12, 2)           default NULL,
   vcdnum             NUMBER(20),
   constraint PK_VCD_INFO primary key (ID)
);

comment on table vcd_info is
'vcd信息表';

/*==============================================================*/
/* Table: vcd_sale_record                                     */
/*==============================================================*/
create table vcd_sale_record 
(
   ID                   NUMBER(20)           default NULL not null,
   vcd_id             NUMBER(20),
   record_date        DATE,
   salenum            INT,
   saleprice          DEC(12, 2),
   constraint PK_VCD_SALE_RECORD primary key (ID)
);

comment on table vcd_sale_record is
'vcd销售记录表';

/*==============================================================*/
/* Table: vcd_type                                            */
/*==============================================================*/
create table vcd_type 
(
   ID                   INT                  default NULL not null,
   typename           varchar(50),
   constraint PK_VCD_TYPE primary key (ID)
);

comment on table vcd_type is
'vcd分类表';


/*=======================以下是触发器,挨个执行=======================================*/


create sequence vcd_info_seq
increment by 1 
start with 1
nomaxvalue
nominvalue
nocache;

create or replace trigger tr_vcd_info
before insert on "vcd_info"
for each row
begin
select vcd_info_seq.nextval into :new.id from dual;
end;

create sequence vcd_borrow_seq
increment by 1 
start with 1
nomaxvalue
nominvalue
nocache;

create or replace trigger tr_vcd_borrow
before insert on "vcd_borrow_record"
for each row
begin
select vcd_borrow_seq.nextval into :new.id from dual;
end;

create sequence vcd_sale_seq
increment by 1 
start with 1
nomaxvalue
nominvalue
nocache;

create or replace trigger tr_vcd_sale
before insert on "vcd_sale_record"
for each row
begin
select vcd_sale_seq.nextval into :new.id from dual;
end;

/*=======================以下是触发器,挨个执行=======================================*/

CREATE OR REPLACE TRIGGER Trigger_borrow 
BEFORE INSERT ON vcd_borrow_record  
FOR EACH ROW 
WHEN(NEW.ctype=0) 
DECLARE
v_temp INT;
BEGIN
  SELECT COUNT(*) INTO v_temp FROM vcd_info WHERE id = :NEW.vcd_id;
 if v_temp >0 then
   update vcd_info set vcdnum=vcdnum-1 where id= :NEW.vcd_id;
  else
    raise_application_error(-20001,'error');
  end if;
END;

CREATE OR REPLACE TRIGGER Trigger_send
BEFORE INSERT  ON vcd_borrow_record 
FOR EACH ROW 
WHEN(NEW.ctype=1) 
DECLARE 
v_temp INT;
BEGIN 
 
   update vcd_info set vcdnum=vcdnum+1 where id= :NEW.vcd_id;

END;

CREATE OR REPLACE TRIGGER Trigger_sale 
BEFORE INSERT  ON vcd_sale_record 
FOR EACH ROW 
DECLARE
v_temp INT;
BEGIN
  SELECT COUNT(*) INTO v_temp FROM vcd_info WHERE id= :NEW.vcd_id;
 if v_temp >0 then
   update vcd_info set vcdnum=vcdnum-1 where id= :NEW.vcd_id;
else
raise_application_error(-20001,'error');
end if;
END;


/*=======================以下是存储过程,挨个执行=======================================*/

create or replace procedure p_sale_num(vcd_id_in    in number, --某个VCD序号
                                      dt_in_beg     in date, --开始日期
                                      dt_in_end     in date, --结束日期
                                      sale_o_num    out int --成功/销售数量
                                      )
as
i_count      int; --数
begin
SELECT sum(salenum) INTO i_count FROM vcd_sale_record WHERE vcd_id= vcd_id_in and record_date between dt_in_beg and dt_in_end ;
sale_o_num := i_count ;
end;

--借的统计数据
create or replace procedure p_borrow_num(vcd_id_in    in number, --某个VCD序号
                                      dt_in_beg     in date, --开始日期
                                      dt_in_end     in date, --结束日期
                                      o_num    out int --数量
                                      )
as
i_count      int; --数
begin
SELECT sum(vcdnum) INTO i_count FROM vcd_borrow_record WHERE vcd_id= vcd_id_in and ctype=0 and record_date between dt_in_beg and dt_in_end ;
o_num := i_count ;
end;

--还的统计数据
create or replace procedure p_send_num(vcd_id_in    in number, --某个VCD序号
                                      dt_in_beg     in date, --开始日期
                                      dt_in_end     in date, --结束日期
                                      o_num    out int --数量
                                      )
as
i_count      int; --数
begin
SELECT sum(vcdnum) INTO i_count FROM vcd_borrow_record WHERE vcd_id= vcd_id_in and ctype=1 and record_date between dt_in_beg and dt_in_end ;
o_num := i_count ;
end;

/*=======================以下是视图,挨个执行=======================================*/

CREATE OR REPLACE VIEW v_sum_vcd_as_type AS
SELECT  vt.typename,sum(vi.vcdnum) as vcdtotal from vcd_info vi, vcd_type vt where vi.ctype=vt.id
 group by vt.typename;
 
 
 create or replace function f_total_by_type(f_type in number) return int
as
total_count int;
begin
select count(1) into  total_count  from vcd_info where ctype=f_type;
return total_count;
end;

--f_type的值是:0或1
create or replace function f_total_borrow(f_type in number) return int
as
total_count int;
begin
select count(1) into  total_count  from vcd_borrow_record where ctype=f_type;
return total_count;
end;

declare
total_count int;
begin
total_count:=f_total_by_type(10);
DBMS_OUTPUT.PUT_LINE('总数:'||total_count);
end;

--时间段内销售记录数
create or replace function f_total_sale(
              dt_in_beg     in date, --开始日期
              dt_in_end     in date --结束日期
                  ) return int
as
total_count int;
begin
select count(1) into  total_count  FROM vcd_sale_record WHERE record_date between dt_in_beg and dt_in_end ;
return total_count;
end;

--时间段内销售金额数
create or replace function f_total_sale_price(vcd_id_in    in number, --某个VCD序号
              dt_in_beg     in date, --开始日期
              dt_in_end     in date --结束日期
                  ) return number
as
total_count number;
begin
select sum(salenum*saleprice) into  total_count  FROM vcd_sale_record WHERE vcd_id= vcd_id_in and record_date between dt_in_beg and dt_in_end ;
return total_count;
end;

INSERT INTO vcd_type(id,typename) VALUES(1,'教学');
INSERT INTO vcd_type(id,typename) VALUES(2,'影视');
INSERT INTO vcd_type(id,typename) VALUES(3,'娱乐');

INSERT INTO vcd_info(ctype,vcdname,price,saleprice,vcdnum) VALUES(1,'计算机教学',20,20,200);
INSERT INTO vcd_info(ctype,vcdname,price,saleprice,vcdnum) VALUES(1,'数据结构教学',25,30,250);
INSERT INTO vcd_info(ctype,vcdname,price,saleprice,vcdnum) VALUES(2,'魔游纪2:烈风峡谷',15,20,300);

SELECT * FROM vcd_info


create or replace procedure p_sale_pricenum(vcd_id_in    in number, --某个VCD序号
                                      dt_in_beg     in date, --开始日期
                                      dt_in_end     in date, --结束日期
                                      sale_o_num    out number --成功/销售数量
                                      )
as
i_count      number; --数
begin
SELECT sum(salenum*saleprice) INTO i_count FROM vcd_sale_record WHERE vcd_id= vcd_id_in and record_date between dt_in_beg and dt_in_end ;
sale_o_num := i_count ;
end;

create or replace procedure p_sale_price(vcd_id_in    in number, --某个VCD序号
                                      dt_in_beg     in date, --开始日期
                                      dt_in_end     in date, --结束日期
                                      sale_o_num    out number --成功/销售数量
                                      )
as
i_count      number; --数
begin
SELECT sum(saleprice) INTO i_count FROM vcd_sale_record WHERE vcd_id= vcd_id_in and record_date between dt_in_beg and dt_in_end ;
sale_o_num := i_count ;
end;

--时间段内销售金额数
create or replace function f_total_sale_price_num(vcd_id_in    in number, --某个VCD序号
              dt_in_beg     in date, --开始日期
              dt_in_end     in date --结束日期
                  ) return number
as
total_count number;
begin
select sum(salenum) into  total_count  FROM vcd_sale_record WHERE vcd_id= vcd_id_in and record_date between dt_in_beg and dt_in_end ;
return total_count;
end;

--f_type的值是:0或1
create or replace function f_total_borrow(f_type in number) return int
as
total_count int;
begin
select count(1) into  total_count  from vcd_borrow_record where ctype=f_type;
return total_count;
end;

----------------------------------------------
---1--p_send_num
declare 
  i_count  int;
begin
  p_send_num(1,to_date('2019-01-16 14:22:20','yyyy-mm-dd hh24:mi:ss'),to_date('2019-02-16 14:22:20','yyyy-mm-dd hh24:mi:ss'),i_count);
  dbms_output.put_line('返回数据:' || i_count);
end;
--2--p_sale_pricenum
declare 
  i_count  number;
begin
  p_sale_pricenum(1,to_date('2019-01-16 14:22:20','yyyy-mm-dd hh24:mi:ss'),to_date('2019-02-16 14:22:20','yyyy-mm-dd hh24:mi:ss'),i_count);
  dbms_output.put_line('返回数据:' || i_count);
end;
--3--p_sale_price
declare 
  i_count  number;
begin
  p_sale_price(1,to_date('2019-01-16 14:22:20','yyyy-mm-dd hh24:mi:ss'),to_date('2019-02-16 14:22:20','yyyy-mm-dd hh24:mi:ss'),i_count);
  dbms_output.put_line('返回数据:' || i_count);
end;
--4--p_borrow_num
declare 
  i_count  int;
begin
  p_borrow_num(1,to_date('2019-01-16 14:22:20','yyyy-mm-dd hh24:mi:ss'),to_date('2019-02-16 14:22:20','yyyy-mm-dd hh24:mi:ss'),i_count);
  dbms_output.put_line('返回数据:' || i_count);
end;
--5--p_send_num
declare 
  i_count  int;
begin
  p_send_num(1,to_date('2019-01-16 14:22:20','yyyy-mm-dd hh24:mi:ss'),to_date('2019-02-16 14:22:20','yyyy-mm-dd hh24:mi:ss'),i_count);
  dbms_output.put_line('返回数据:' || i_count);
end;

--1--f_total_sale
declare 
  i_count  int;
begin
  i_count :=f_total_sale(to_date('2019-01-16 14:22:20','yyyy-mm-dd hh24:mi:ss'),to_date('2019-02-16 14:22:20','yyyy-mm-dd hh24:mi:ss'));
  dbms_output.put_line('返回数据:' || i_count);
end;

--2--f_total_sale_price
declare 
  i_count  number;
begin
  i_count :=f_total_sale_price(1,to_date('2019-01-16 14:22:20','yyyy-mm-dd hh24:mi:ss'),to_date('2019-02-16 14:22:20','yyyy-mm-dd hh24:mi:ss'));
  dbms_output.put_line('返回数据:' || i_count);
end;

--3--f_total_by_type
declare 
  i_count  int;
begin
  i_count :=f_total_by_type(1);
  dbms_output.put_line('返回数据:' || i_count);
end;

--4--f_total_sale_price_num
declare 
  i_count  number;
begin
  i_count :=f_total_sale_price_num(1,to_date('2019-01-16 14:22:20','yyyy-mm-dd hh24:mi:ss'),to_date('2019-02-16 14:22:20','yyyy-mm-dd hh24:mi:ss'));
  dbms_output.put_line('返回数据:' || i_count);
end;

--5--f_total_borrow
declare 
  i_count  int;
begin
  i_count :=f_total_borrow(1);
  dbms_output.put_line('返回数据:' || i_count);
end;
 

这篇关于为一个小孩写的oracle数据库简单SQL语句的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/817167

相关文章

MySQL MCP 服务器安装配置最佳实践

《MySQLMCP服务器安装配置最佳实践》本文介绍MySQLMCP服务器的安装配置方法,本文结合实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下... 目录mysql MCP 服务器安装配置指南简介功能特点安装方法数据库配置使用MCP Inspector进行调试开发指

mysql中insert into的基本用法和一些示例

《mysql中insertinto的基本用法和一些示例》INSERTINTO用于向MySQL表插入新行,支持单行/多行及部分列插入,下面给大家介绍mysql中insertinto的基本用法和一些示例... 目录基本语法插入单行数据插入多行数据插入部分列的数据插入默认值注意事项在mysql中,INSERT I

一文详解MySQL如何设置自动备份任务

《一文详解MySQL如何设置自动备份任务》设置自动备份任务可以确保你的数据库定期备份,防止数据丢失,下面我们就来详细介绍一下如何使用Bash脚本和Cron任务在Linux系统上设置MySQL数据库的自... 目录1. 编写备份脚本1.1 创建并编辑备份脚本1.2 给予脚本执行权限2. 设置 Cron 任务2

SQL Server修改数据库名及物理数据文件名操作步骤

《SQLServer修改数据库名及物理数据文件名操作步骤》在SQLServer中重命名数据库是一个常见的操作,但需要确保用户具有足够的权限来执行此操作,:本文主要介绍SQLServer修改数据... 目录一、背景介绍二、操作步骤2.1 设置为单用户模式(断开连接)2.2 修改数据库名称2.3 查找逻辑文件名

SQL Server数据库死锁处理超详细攻略

《SQLServer数据库死锁处理超详细攻略》SQLServer作为主流数据库管理系统,在高并发场景下可能面临死锁问题,影响系统性能和稳定性,这篇文章主要给大家介绍了关于SQLServer数据库死... 目录一、引言二、查询 Sqlserver 中造成死锁的 SPID三、用内置函数查询执行信息1. sp_w

canal实现mysql数据同步的详细过程

《canal实现mysql数据同步的详细过程》:本文主要介绍canal实现mysql数据同步的详细过程,本文通过实例图文相结合给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的... 目录1、canal下载2、mysql同步用户创建和授权3、canal admin安装和启动4、canal

SQL中JOIN操作的条件使用总结与实践

《SQL中JOIN操作的条件使用总结与实践》在SQL查询中,JOIN操作是多表关联的核心工具,本文将从原理,场景和最佳实践三个方面总结JOIN条件的使用规则,希望可以帮助开发者精准控制查询逻辑... 目录一、ON与WHERE的本质区别二、场景化条件使用规则三、最佳实践建议1.优先使用ON条件2.WHERE用

MySQL存储过程之循环遍历查询的结果集详解

《MySQL存储过程之循环遍历查询的结果集详解》:本文主要介绍MySQL存储过程之循环遍历查询的结果集,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录前言1. 表结构2. 存储过程3. 关于存储过程的SQL补充总结前言近来碰到这样一个问题:在生产上导入的数据发现

MySQL 衍生表(Derived Tables)的使用

《MySQL衍生表(DerivedTables)的使用》本文主要介绍了MySQL衍生表(DerivedTables)的使用,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学... 目录一、衍生表简介1.1 衍生表基本用法1.2 自定义列名1.3 衍生表的局限在SQL的查询语句select

MySQL 横向衍生表(Lateral Derived Tables)的实现

《MySQL横向衍生表(LateralDerivedTables)的实现》横向衍生表适用于在需要通过子查询获取中间结果集的场景,相对于普通衍生表,横向衍生表可以引用在其之前出现过的表名,本文就来... 目录一、横向衍生表用法示例1.1 用法示例1.2 使用建议前面我们介绍过mysql中的衍生表(From子句