为一个小孩写的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 中的 CAST 函数详解及常见用法

《MySQL中的CAST函数详解及常见用法》CAST函数是MySQL中用于数据类型转换的重要函数,它允许你将一个值从一种数据类型转换为另一种数据类型,本文给大家介绍MySQL中的CAST... 目录mysql 中的 CAST 函数详解一、基本语法二、支持的数据类型三、常见用法示例1. 字符串转数字2. 数字

Mysql实现范围分区表(新增、删除、重组、查看)

《Mysql实现范围分区表(新增、删除、重组、查看)》MySQL分区表的四种类型(范围、哈希、列表、键值),主要介绍了范围分区的创建、查询、添加、删除及重组织操作,具有一定的参考价值,感兴趣的可以了解... 目录一、mysql分区表分类二、范围分区(Range Partitioning1、新建分区表:2、分

MySQL 定时新增分区的实现示例

《MySQL定时新增分区的实现示例》本文主要介绍了通过存储过程和定时任务实现MySQL分区的自动创建,解决大数据量下手动维护的繁琐问题,具有一定的参考价值,感兴趣的可以了解一下... mysql创建好分区之后,有时候会需要自动创建分区。比如,一些表数据量非常大,有些数据是热点数据,按照日期分区MululbU

SQL Server配置管理器无法打开的四种解决方法

《SQLServer配置管理器无法打开的四种解决方法》本文总结了SQLServer配置管理器无法打开的四种解决方法,文中通过图文示例介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的... 目录方法一:桌面图标进入方法二:运行窗口进入检查版本号对照表php方法三:查找文件路径方法四:检查 S

MySQL 删除数据详解(最新整理)

《MySQL删除数据详解(最新整理)》:本文主要介绍MySQL删除数据的相关知识,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧... 目录一、前言二、mysql 中的三种删除方式1.DELETE语句✅ 基本语法: 示例:2.TRUNCATE语句✅ 基本语

MySQL中查找重复值的实现

《MySQL中查找重复值的实现》查找重复值是一项常见需求,比如在数据清理、数据分析、数据质量检查等场景下,我们常常需要找出表中某列或多列的重复值,具有一定的参考价值,感兴趣的可以了解一下... 目录技术背景实现步骤方法一:使用GROUP BY和HAVING子句方法二:仅返回重复值方法三:返回完整记录方法四:

从入门到精通MySQL联合查询

《从入门到精通MySQL联合查询》:本文主要介绍从入门到精通MySQL联合查询,本文通过实例代码给大家介绍的非常详细,需要的朋友可以参考下... 目录摘要1. 多表联合查询时mysql内部原理2. 内连接3. 外连接4. 自连接5. 子查询6. 合并查询7. 插入查询结果摘要前面我们学习了数据库设计时要满

MySQL查询JSON数组字段包含特定字符串的方法

《MySQL查询JSON数组字段包含特定字符串的方法》在MySQL数据库中,当某个字段存储的是JSON数组,需要查询数组中包含特定字符串的记录时传统的LIKE语句无法直接使用,下面小编就为大家介绍两种... 目录问题背景解决方案对比1. 精确匹配方案(推荐)2. 模糊匹配方案参数化查询示例使用场景建议性能优

mysql表操作与查询功能详解

《mysql表操作与查询功能详解》本文系统讲解MySQL表操作与查询,涵盖创建、修改、复制表语法,基本查询结构及WHERE、GROUPBY等子句,本文结合实例代码给大家介绍的非常详细,感兴趣的朋友跟随... 目录01.表的操作1.1表操作概览1.2创建表1.3修改表1.4复制表02.基本查询操作2.1 SE

MySQL中的锁机制详解之全局锁,表级锁,行级锁

《MySQL中的锁机制详解之全局锁,表级锁,行级锁》MySQL锁机制通过全局、表级、行级锁控制并发,保障数据一致性与隔离性,全局锁适用于全库备份,表级锁适合读多写少场景,行级锁(InnoDB)实现高并... 目录一、锁机制基础:从并发问题到锁分类1.1 并发访问的三大问题1.2 锁的核心作用1.3 锁粒度分