为一个小孩写的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提供了丰富的字符串函数帮助我们高效地对字符串进行处理、转换和分析,本文我将全面且深入地介绍MySQL常用的字符串函数,并结合具体示例和场景,帮你熟练... 目录一、字符串函数概述1.1 字符串函数的作用1.2 字符串函数分类二、字符串长度与统计函数2.1

Oracle数据库定时备份脚本方式(Linux)

《Oracle数据库定时备份脚本方式(Linux)》文章介绍Oracle数据库自动备份方案,包含主机备份传输与备机解压导入流程,强调需提前全量删除原库数据避免报错,并需配置无密传输、定时任务及验证脚本... 目录说明主机脚本备机上自动导库脚本整个自动备份oracle数据库的过程(建议全程用root用户)总结

SQL Server跟踪自动统计信息更新实战指南

《SQLServer跟踪自动统计信息更新实战指南》本文详解SQLServer自动统计信息更新的跟踪方法,推荐使用扩展事件实时捕获更新操作及详细信息,同时结合系统视图快速检查统计信息状态,重点强调修... 目录SQL Server 如何跟踪自动统计信息更新:深入解析与实战指南 核心跟踪方法1️⃣ 利用系统目录

MySQL 内存使用率常用分析语句

《MySQL内存使用率常用分析语句》用户整理了MySQL内存占用过高的分析方法,涵盖操作系统层确认及数据库层bufferpool、内存模块差值、线程状态、performance_schema性能数据... 目录一、 OS层二、 DB层1. 全局情况2. 内存占js用详情最近连续遇到mysql内存占用过高导致

Mysql中设计数据表的过程解析

《Mysql中设计数据表的过程解析》数据库约束通过NOTNULL、UNIQUE、DEFAULT、主键和外键等规则保障数据完整性,自动校验数据,减少人工错误,提升数据一致性和业务逻辑严谨性,本文介绍My... 目录1.引言2.NOT NULL——制定某列不可以存储NULL值2.UNIQUE——保证某一列的每一

解密SQL查询语句执行的过程

《解密SQL查询语句执行的过程》文章讲解了SQL语句的执行流程,涵盖解析、优化、执行三个核心阶段,并介绍执行计划查看方法EXPLAIN,同时提出性能优化技巧如合理使用索引、避免SELECT*、JOIN... 目录1. SQL语句的基本结构2. SQL语句的执行过程3. SQL语句的执行计划4. 常见的性能优

SQL Server 中的 WITH (NOLOCK) 示例详解

《SQLServer中的WITH(NOLOCK)示例详解》SQLServer中的WITH(NOLOCK)是一种表提示,等同于READUNCOMMITTED隔离级别,允许查询在不获取共享锁的情... 目录SQL Server 中的 WITH (NOLOCK) 详解一、WITH (NOLOCK) 的本质二、工作

MySQL 强制使用特定索引的操作

《MySQL强制使用特定索引的操作》MySQL可通过FORCEINDEX、USEINDEX等语法强制查询使用特定索引,但优化器可能不采纳,需结合EXPLAIN分析执行计划,避免性能下降,注意版本差异... 目录1. 使用FORCE INDEX语法2. 使用USE INDEX语法3. 使用IGNORE IND

SQL Server安装时候没有中文选项的解决方法

《SQLServer安装时候没有中文选项的解决方法》用户安装SQLServer时界面全英文,无中文选项,通过修改安装设置中的国家或地区为中文中国,重启安装程序后界面恢复中文,解决了问题,对SQLSe... 你是不是在安装SQL Server时候发现安装界面和别人不同,并且无论如何都没有中文选项?这个问题也

2025版mysql8.0.41 winx64 手动安装详细教程

《2025版mysql8.0.41winx64手动安装详细教程》本文指导Windows系统下MySQL安装配置,包含解压、设置环境变量、my.ini配置、初始化密码获取、服务安装与手动启动等步骤,... 目录一、下载安装包二、配置环境变量三、安装配置四、启动 mysql 服务,修改密码一、下载安装包安装地