ORACLE基础知识

ORACLE

一、ORACLE简介

什么是ORACLE

Oracle:数据库、通常应用于大型系统的数据库产品、以分布式数据库为核心的一组软件产品

mysql:免费开源、轻量级数据库产品

Oracle: 收费,重,商业级数据库产品、甲骨文(Oracle)公司

特点:

  1. 支持多用户、大事务量的事务处理(高并发)

  2. 数据安全性和完整性控制

  3. 分布式数据处理

  4. 可移植性(不同版本的下可以通用,跨平台)

二、ORACLE安装与配置

windows安装Oracle

安装教程如下:手把手教你在Windows 10安装Oracle 19c(详细图文附踩坑指南)_oracle 19c windows 安装-CSDN博客

三、项目案例(自来水公司收费系统)

1.概述

  1. 基础信息管理:

    业主类型设置

    价格设置

    区域设置

    收费员设置

    地址设置

  2. 业主信息管理:

    业主信息维护

    业主信息查询

  3. 收费管理:

    抄表登记

    收费登记

    收费记录查询

    欠费用户清单

  4. 统计分析

    收费日报单

    收费月报表

2.创建表空间

CREATE tablespace waterboss --文件名

datafile 'D:\Oracle\waterboss.dbf' --文件位置

size 100m --指定大小

autoextend on -- 自动扩充

NEXT 10m; -- 一次10m

4.创建用户

一个表空间可以创建多个用户

CREATE USER 用户名 IDENTIFIED BY 密码 DEFAULT tablespace 表空间

5.用户赋权

创建后暂时是没有权限的,需要赋予权限

赋权:GRANT dba to wateruser

四:表的创建、修改、删除

1.建表

--创建业主表 CREATE TABLE T_OWNERS( ID NUMBER PRIMARY KEY, NAME VARCHAR2(30), ADDRESSID NUMBER, HOUSENUMBER VARCHAR2(30), WATERMETER VARCHAR2(30), ADDDATE DATE, OWNERTYPEID NUMBER )

2.插入语句

insert into T_OWNERS VALUES (1,'张三丰',1,'1-1','123456',sysdate,1 );

3.修改语句

update T_OWNERS set adddate=adddate-3 where id=1;

4.追加字段

** ALTER TABLE T_OWNERS ADD** ** (** ** REMAKER VARCHAR(255)** ** )**

5.修改字段

ALTER TABLE T_OWNERS MODIFY ( REMAKER VARCHAR(20) )

6.修改字段名

ALTER TABLE 表名 RENAME COLUMN 旧列名 TO 新列名

7.删除字段名

ALTER TABLE T_OWNERS DROP COLUMN REMAKE

8.删除表

DROP TABLE 表名

9.再次建表

--建立价格区间表

 create table t_pricetable ( id number primary key, price number(10,2), ownertypeid number, minnum number, maxnum number );

--业主类型

create table t_ownertype ( id number primary key, name varchar2(30) );

--业主表

create table t_owners ( id number primary key, name varchar2(30), addressid number, housenumber varchar2(30), watermeter varchar2(30), adddate date, ownertypeid number );

--区域表

create table t_area ( id number, name varchar2(30) );

--收费员表

create table t_operator ( id number, name varchar2(30) );

--地址表

create table t_address ( id number primary key, name varchar2(100), areaid number, operatorid number );

--账务表--

create table t_account ( id number primary key, owneruuid number, ownertype number, areaid number, year char(4), month char(2), num0 number, num1 number, usenum number, meteruser number, meterdate date, money number(10,2), isfee char(1), feedate date, feeuser number );

create sequence seq_account;

--业主类型

insert into t_ownertype values(1,'居民'); insert into t_ownertype values(2,'行政事业单位'); insert into t_ownertype values(3,'商业');
--地址信息-- insert into t_address values( 1,'明兴花园',1,1); insert into t_address values( 2,'鑫源秋墅',1,1); insert into t_address values( 3,'华龙苑南里小区',2,2); insert into t_address values( 4,'河畔花园',2,2); insert into t_address values( 5,'霍营',2,2); insert into t_address values( 6,'回龙观东大街',3,2); insert into t_address values( 7,'西二旗',3,2);

--业主信息 insert into t_owners values(1,'范冰',1,'1-1','30406',to_date('2015-04-12','yyyy-MM-dd'),1 ); insert into t_owners values(2,'王强',1,'1-2','30407',to_date('2015-02-14','yyyy-MM-dd'),1 ); insert into t_owners values(3,'马腾',1,'1-3','30408',to_date('2015-03-18','yyyy-MM-dd'),1 ); insert into t_owners values(4,'林小玲',2,'2-4','30409',to_date('2015-06-15','yyyy-MM-dd'),1 ); insert into t_owners values(5,'刘华',2,'2-5','30410',to_date('2013-09-11','yyyy-MM-dd'),1 ); insert into t_owners values(6,'刘东',2,'2-2','30411',to_date('2014-09-11','yyyy-MM-dd'),1 ); insert into t_owners values(7,'周健',3,'2-5','30433',to_date('2016-09-11','yyyy-MM-dd'),1 ); insert into t_owners values(8,'张哲',4,'2-2','30455',to_date('2016-09-11','yyyy-MM-dd'),1 ); insert into t_owners values(9,'昌平区中西医结合医院',5,'2-2','30422',to_date('2016-10-11','yyyy-MM-dd'),2 ); insert into t_owners values(10,'美廉美超市',5,'4-2','30423',to_date('2016-10-12','yyyy-MM-dd'),3 );

--操作员 insert into t_operator values(1,'马小云'); insert into t_operator values(2,'李翠花');



--地区-- insert into t_area values(1,'海淀'); insert into t_area values(2,'昌平'); insert into t_area values(3,'西城'); insert into t_area values(4,'东城'); insert into t_area values(5,'朝阳'); insert into t_area values(6,'玄武');

--价格表--

insert into t_pricetable values(1,2.45,1,0,5); insert into t_pricetable values(2,3.45,1,5,10); insert into t_pricetable values(3,4.45,1,10,null);

insert into t_pricetable values(4,3.87,2,0,5); insert into t_pricetable values(5,4.87,2,5,10); insert into t_pricetable values(6,5.87,2,10,null);

insert into t_pricetable values(7,4.36,3,0,5); insert into t_pricetable values(8,5.36,3,5,10); insert into t_pricetable values(9,6.36,3,10,null);

--账务表-- insert into t_account values( seq_account.nextval,1,1,1,'2012','01',30203,50123,0,1,sysdate,34.51,'1',to_date('2012-02-14','yyyy-MM-dd'),2 ); insert into t_account values( seq_account.nextval,1,1,1,'2012','02',50123,60303,0,1,sysdate,23.43,'1',to_date('2012-03-14','yyyy-MM-dd'),2 ); insert into t_account values( seq_account.nextval,1,1,1,'2012','03',60303,74111,0,1,sysdate,45.34,'1',to_date('2012-04-14','yyyy-MM-dd'),2 ); insert into t_account values( seq_account.nextval,1,1,1,'2012','04',74111,77012,0,1,sysdate,52.54,'1',to_date('2012-05-14','yyyy-MM-dd'),2 ); insert into t_account values( seq_account.nextval,1,1,1,'2012','05',77012,79031,0,1,sysdate,54.66,'1',to_date('2012-06-14','yyyy-MM-dd'),2 ); insert into t_account values( seq_account.nextval,1,1,1,'2012','06',79031,80201,0,1,sysdate,76.45,'1',to_date('2012-07-14','yyyy-MM-dd'),2 ); insert into t_account values( seq_account.nextval,1,1,1,'2012','07',80201,88331,0,1,sysdate,65.65,'1',to_date('2012-08-14','yyyy-MM-dd'),2 ); insert into t_account values( seq_account.nextval,1,1,1,'2012','08',88331,89123,0,1,sysdate,55.67,'1',to_date('2012-09-14','yyyy-MM-dd'),2 ); insert into t_account values( seq_account.nextval,1,1,1,'2012','09',89123,90122,0,1,sysdate,112.54,'1',to_date('2012-10-14','yyyy-MM-dd'),2 ); insert into t_account values( seq_account.nextval,1,1,1,'2012','10',90122,93911,0,1,sysdate,76.21,'1',to_date('2012-11-14','yyyy-MM-dd'),2 ); insert into t_account values( seq_account.nextval,1,1,1,'2012','11',93911,95012,0,1,sysdate,76.25,'1',to_date('2012-12-14','yyyy-MM-dd'),2 ); insert into t_account values( seq_account.nextval,1,1,1,'2012','12',95012,99081,0,1,sysdate,44.51,'1',to_date('2013-01-14','yyyy-MM-dd'),2 );

insert into t_account values( seq_account.nextval,2,1,3,'2012','01',30334,50433,0,1,sysdate,34.51,'1',to_date('2013-02-14','yyyy-MM-dd'),2 ); insert into t_account values( seq_account.nextval,2,1,3,'2012','02',50433,60765,0,1,sysdate,23.43,'1',to_date('2013-03-14','yyyy-MM-dd'),2 ); insert into t_account values( seq_account.nextval,2,1,3,'2012','03',60765,74155,0,1,sysdate,45.34,'1',to_date('2013-04-14','yyyy-MM-dd'),2 ); insert into t_account values( seq_account.nextval,2,1,3,'2012','04',74155,77099,0,1,sysdate,52.54,'1',to_date('2013-05-14','yyyy-MM-dd'),2 ); insert into t_account values( seq_account.nextval,2,1,3,'2012','05',77099,79076,0,1,sysdate,54.66,'1',to_date('2013-06-14','yyyy-MM-dd'),2 ); insert into t_account values( seq_account.nextval,2,1,3,'2012','06',79076,80287,0,1,sysdate,76.45,'1',to_date('2013-07-14','yyyy-MM-dd'),2 ); insert into t_account values( seq_account.nextval,2,1,3,'2012','07',80287,88432,0,1,sysdate,65.65,'1',to_date('2013-08-14','yyyy-MM-dd'),2 ); insert into t_account values( seq_account.nextval,2,1,3,'2012','08',88432,89765,0,1,sysdate,55.67,'1',to_date('2013-09-14','yyyy-MM-dd'),2 ); insert into t_account values( seq_account.nextval,2,1,3,'2012','09',89765,90567,0,1,sysdate,112.54,'1',to_date('2013-10-14','yyyy-MM-dd'),2 ); insert into t_account values( seq_account.nextval,2,1,3,'2012','10',90567,93932,0,1,sysdate,76.21,'1',to_date('2013-11-14','yyyy-MM-dd'),2 ); insert into t_account values( seq_account.nextval,2,1,3,'2012','11',93932,95076,0,1,sysdate,76.25,'1',to_date('2013-12-14','yyyy-MM-dd'),2 ); insert into t_account values( seq_account.nextval,2,1,3,'2012','12',95076,99324,0,1,sysdate,44.51,'1',to_date('2014-01-14','yyyy-MM-dd'),2 );

insert into t_account values( seq_account.nextval,100,1,3,'2012','12',95076,99324,0,1,sysdate,44.51,'1',to_date('2014-01-01','yyyy-MM-dd'),2 ); insert into t_account values( seq_account.nextval,101,1,3,'2012','12',95076,99324,0,1,sysdate,44.51,'1',to_date('2015-01-01','yyyy-MM-dd'),2 );

update t_account set usenum=num1-num0; update t_account set money=usenum*2.45;
10.插入数据语句
insert into 表名 [(列名1,列名2,.....)] values (值1,值2....)

测试:insert into T_OWNERTYPE (ID,NAME) VALUES (1,'居民')

测试:insert into T_OWNERS  VALUES (1,'张三丰',1,'1-1','123456',sysdate,1)
11.修改数据
update 表名 set 列名1=值1,列名2=值2,.... where 修改条件

测试:update T_OWNERS set adddate = adddate-3 where id = 3
12.删除数据
delete from 表名 where 删除条件 --- 支持回滚

truncate table 表名 -- 不支持回滚 表销毁后重建表

五、使用ruoyi框架连接ORACLE数据库进行数据CURD测试(方便)

配置文件设置多数据源
spring:
    datasource:
        type: com.alibaba.druid.pool.DruidDataSource
#        driverClassName: com.mysql.cj.jdbc.Driver
        druid:
            # 主库数据源
            master:
                url: jdbc:mysql://localhost:3306/tony-flowable?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=true&serverTimezone=GMT%2B8&nullCatalogMeansCurrent=true
                username: root
                password: 111111
            # 从库数据源
            slave:
                # 从数据源开关/默认关闭
                enabled: true
                url: jdbc:oracle:thin:@localhost:1521:orcl
                username: wateruser
                password: Zxw991116
                driverClassName: oracle.jdbc.driver.OracleDriver
            # 初始连接数
            initialSize: 5
            # 最小连接池数量
            minIdle: 10
            # 最大连接池数量
            maxActive: 20
            # 配置获取连接等待超时的时间
            maxWait: 60000
            # 配置连接超时时间
            connectTimeout: 30000
            # 配置网络超时时间
            socketTimeout: 60000
            # 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒
            timeBetweenEvictionRunsMillis: 60000
            # 配置一个连接在池中最小生存的时间,单位是毫秒
            minEvictableIdleTimeMillis: 300000
            # 配置一个连接在池中最大生存的时间,单位是毫秒
            maxEvictableIdleTimeMillis: 900000
            # 配置检测连接是否有效
            validationQuery: SELECT 1 FROM DUAL
            testWhileIdle: true
            testOnBorrow: false
            testOnReturn: false
            webStatFilter:
                enabled: true
            statViewServlet:
                enabled: true
                # 设置白名单,不填则允许所有访问
                allow:
                url-pattern: /druid/*
                # 控制台管理用户名和密码
                login-username: ruoyi
                login-password: 123456
            filter:
                stat:
                    enabled: true
                    # 慢SQL记录
                    log-slow-sql: true
                    slow-sql-millis: 1000
                    merge-sql: true
                wall:
                    config:
                        multi-statement-allow: true

 在编写的方法上或者service或者mapper层加上@DataSource注解,指定数据源

编写insert和update方法进行swagger测试
<insert id="insertTest">
 insert into t_owners values (12,'张三',1,'123456','1',to_date('2015-04-12','yyyy-MM-dd'),12)
</insert>
<update id="updateTest">
 update t_owners set ADDRESSID = 2,HOUSENUMBER='12' where ID = 12
</update>

六、整库的导入导出

命令:

导出:exp 用户名/密码ull=y file = 文件名 f--指定文件名称

导出:exp 用户名/密码 full=y

导入:imp 用户名/密码 full=y

指定用户导出: exp system/Zxw991116 owner=wateruser file=wateruser.DMP

指定用户导入: imp system/Zxw991116 file=wateruser.DMP fromuser=wateruser

按表导出: exp wateruser/Zxw991116 file=a.dmp tables=t_owners,t_ownertype

按表导入:imp wateruser/Zxw991116 file=a.dmp tables=t_owners(多个表用逗号分隔)

七、基于伪列的查询

1、ROWID(相当于唯一标识)

ROWID:物理地址,直接定位,效率比根据主键查询更高

SELECT 
    ROWID,
    t.* 
FROM
    T_OWNERS t

2.ROWNUM(每一行的行号---结果集的序号)

SELECT ROWID
    ,
    ROWNUM,
    t.* 
FROM
    T_OWNERS t

八、聚合统计

求和 sum,

平均avg,

最大值max,

最小值min,

求条数count

分组聚合(查询的字段一定是分组聚合的条件或者是聚合函数):group by

分组后的条件查询 having,分组后的条件筛选

SELECT
	AREAID,
	SUM( MONEY ) 
FROM
	"T_ACCOUNT" 
GROUP BY
	AREAID 
HAVING
	SUM( MONEY ) > 169000
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值