为一个小孩写的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 分区与分库分表策略应用小结

《MySQL分区与分库分表策略应用小结》在大数据量、复杂查询和高并发的应用场景下,单一数据库往往难以满足性能和扩展性的要求,本文将详细介绍这两种策略的基本概念、实现方法及优缺点,并通过实际案例展示如... 目录mysql 分区与分库分表策略1. 数据库水平拆分的背景2. MySQL 分区策略2.1 分区概念

MySQL高级查询之JOIN、子查询、窗口函数实际案例

《MySQL高级查询之JOIN、子查询、窗口函数实际案例》:本文主要介绍MySQL高级查询之JOIN、子查询、窗口函数实际案例的相关资料,JOIN用于多表关联查询,子查询用于数据筛选和过滤,窗口函... 目录前言1. JOIN(连接查询)1.1 内连接(INNER JOIN)1.2 左连接(LEFT JOI

MySQL 中查询 VARCHAR 类型 JSON 数据的问题记录

《MySQL中查询VARCHAR类型JSON数据的问题记录》在数据库设计中,有时我们会将JSON数据存储在VARCHAR或TEXT类型字段中,本文将详细介绍如何在MySQL中有效查询存储为V... 目录一、问题背景二、mysql jsON 函数2.1 常用 JSON 函数三、查询示例3.1 基本查询3.2

MySQL中动态生成SQL语句去掉所有字段的空格的操作方法

《MySQL中动态生成SQL语句去掉所有字段的空格的操作方法》在数据库管理过程中,我们常常会遇到需要对表中字段进行清洗和整理的情况,本文将详细介绍如何在MySQL中动态生成SQL语句来去掉所有字段的空... 目录在mysql中动态生成SQL语句去掉所有字段的空格准备工作原理分析动态生成SQL语句在MySQL

MySQL中FIND_IN_SET函数与INSTR函数用法解析

《MySQL中FIND_IN_SET函数与INSTR函数用法解析》:本文主要介绍MySQL中FIND_IN_SET函数与INSTR函数用法解析,本文通过实例代码给大家介绍的非常详细,感兴趣的朋友一... 目录一、功能定义与语法1、FIND_IN_SET函数2、INSTR函数二、本质区别对比三、实际场景案例分

MySQL中的交叉连接、自然连接和内连接查询详解

《MySQL中的交叉连接、自然连接和内连接查询详解》:本文主要介绍MySQL中的交叉连接、自然连接和内连接查询,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录一、引入二、交php叉连接(cross join)三、自然连接(naturalandroid join)四

Mysql如何将数据按照年月分组的统计

《Mysql如何将数据按照年月分组的统计》:本文主要介绍Mysql如何将数据按照年月分组的统计方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录mysql将数据按照年月分组的统计要的效果方案总结Mysql将数据按照年月分组的统计要的效果方案① 使用 DA

Mysql表如何按照日期字段的年月分区

《Mysql表如何按照日期字段的年月分区》:本文主要介绍Mysql表如何按照日期字段的年月分区的实现方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录一、创键表时直接设置分区二、已有表分区1、分区的前置条件2、分区操作三、验证四、注意总结一、创键表时直接设置分区

Go 语言中的select语句详解及工作原理

《Go语言中的select语句详解及工作原理》在Go语言中,select语句是用于处理多个通道(channel)操作的一种控制结构,它类似于switch语句,本文给大家介绍Go语言中的select语... 目录Go 语言中的 select 是做什么的基本功能语法工作原理示例示例 1:监听多个通道示例 2:带

mysql的基础语句和外键查询及其语句详解(推荐)

《mysql的基础语句和外键查询及其语句详解(推荐)》:本文主要介绍mysql的基础语句和外键查询及其语句详解(推荐),本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋... 目录一、mysql 基础语句1. 数据库操作 创建数据库2. 表操作 创建表3. CRUD 操作二、外键