Hydropower management oracel study notes

installation

Insert picture description here
Mumu0407
Insert picture description here

Mumu0407
Insert picture description here

Oracel

problem:

1. Deadlock
Reason: Deadlock occurred.
Insert picture description here
Solution: https://blog.csdn.net/qq_34664202/article/details/80462254

2. Table relationship
Insert picture description here
Reason: not found
Solution: use sql statement

alter table 要添加外键的表
add constraint 外键别名(给这个外键取个名字 FK_表名)
foreign key (外键字段)
references 要连接的表名(父表)(父表的字段);

Oracle statement

1. Query

select <列名> from <表名> [where <查询条件表达试>] [order by <排序的列名>[asc或desc]]
select  POWER_MAKE from ST_WATPOW_REP_DAILY where DAILY_ID=1;

1.1 Nested query in insert

insert into the table name to be inserted (field 1, field 2)
select field 1, field 2 from table name;

2. Grouping function

1. AVG-average
2, COUNT-count
3, MAX-maximum
4, MIN-minimum
5, SUM-sum

SELECT AVG(POWER_MAKE)*/SUM(POWER_MAKE)/MAX(POWER_MAKE)/MIN(POWER_MAKE)/COUNT(POWER_MAKE)*
FROM   ST_WATPOW_REP_DAILY;

2.1 Total monthly

select SUM(POWER_MAKE) 
from ST_WATPOW_REP_DAILY 
where DAILY_DATE between to_date('2020-10-01','yy-mm-dd') and to_date('2020-10-31','yy-mm-dd');

3. Add foreign keys

alter table to add a foreign key table
add constraint foreign key alias (take a name for the foreign key FK_ table name)
foreign key (foreign key field)
references the name of the table to be connected (parent table) (the field of the parent table);

4. View

4.1. Role

Based on a multi-table predefined query, these checked tables are called base tables
. DML (Data Management Language) operations can be performed on the base table through
the view. The method of retrieving information in the view is exactly the same as retrieving information from the base table.

Note: the view does not have data, it is considered a virtual table, the view will only access the rows of the base table

Applies to: Check retrieved data

Advantages:
Restrict users from only retrieving data through views, so that users cannot see the underlying base table.
Compile complex queries as views, reduce the complexity of SQL.
Restrict users to only access part of the data in the base table to achieve security

4.2, the use of views

create:

create view shitu
as select  DAILY_ID,DAILY_DATE,SUM(POWER_MAKE) from ST_WATPOW_REP_DAILY;

An error occurred: the grouping function sum was used Insert picture description here
correctly: create view shitu2 as select DAILY_ID,DAILY_DATE,POWER_MAKE from ST_WATPOW_REP_DAILY where DAILY_ID<10;

Delete: drop view view name;

5. Unfamiliar functions

5.1 NVL function

nvl(e1, e2)
If the calculation result of e1 is a null value, NVL() returns e2. If the calculation result of e1 is not a null value, return e1. e1 and e2 can be any data type. If the results of e1 and e2 are both null values, NVL() returns .NULL..

5.2 group by rollup

The function of the rollup clause after group by can be understood as: first generate multiple groups according to certain rules, and then statistical data according to various groups, in addition, the result set returned by the rollup clause after group by can be understood as each group The union of the result sets produced without removing duplicate data.

5.3 Date function

Date conversion: to_date('date to be converted','yy-mm-dd hh-mm-ss')

select DAILY_ID,sum(POWER_MAKE),MACHINE_INFO 
from ST_WATPOW_REP_DAILY where DAILY_DATE 
between to_date('2020-10-01','yy-mm-dd') and to_date('2020-10-31','yy-mm-dd') group by POWER_MAKE; 

6, bag, bag body

6.1 Relationship

1. Package (Package): The package in Oracle is used to classify functions and procedures in the database (similar to the interface in JAVA). The functions and procedures in the package only need to be declared, without giving specific Operation statement (similar to the abstract method in JAVA).

2. Package body (Package body): Only the package, we can not operate the functions and procedures in the package, it should be because they did not give specific implementation statements, so if you want to use the functions and procedures in the package, you need Instantiate it into a package body, and reify the functions and procedures inside (equivalent to creating a class in JAVA to implement an interface and rewriting the methods).

6.2 Creation

create or replace package bao is

  -- Author  : MUMU
  -- Created : 2020/10/29 16:42:24
  -- Purpose : 
  
  -- Public type declarations 公共类型申明
  type <TypeName> is <Datatype>;
  
  -- Public constant declarations
  <ConstantName> constant <Datatype> := <Value>;

  -- Public variable declarations 公共变量申明
  <VariableName> <Datatype>;

  -- Public function and procedure declarations
  function <FunctionName>(<Parameter> <Datatype>) return <Datatype>;

end bao;
create or replace package body baoti is

  -- Private type declarations
  type <TypeName> is <Datatype>;
  
  -- Private constant declarations
  <ConstantName> constant <Datatype> := <Value>;

  -- Private variable declarations
  <VariableName> <Datatype>;

  -- Function and procedure implementations
  function <FunctionName>(<Parameter> <Datatype>) return <Datatype> is
    <LocalVariable> <Datatype>;
  begin
    <Statement>;
    return(<Result>);
  end;

begin
  -- Initialization
  <Statement>;
end baoti;

7 trigger

7.1 Role

The trigger is executed through this "trigger event" (and the call or execution of the stored procedure is performed by the user or application). The operation that can cause the trigger to run is called "trigger event", such as executing DML statements (using INSERT, UPDATE, DELETE statements to perform data processing operations on tables or views); executing DDL statements (CREATE, ALTER, DROP statements in the database Create, modify, and delete schema objects); trigger database system events (such as system startup or exit, abnormal errors, etc.); trigger user events (such as login or exit database operations).

7.2 Syntax

create or replace trigger INSERT_Test
  after insert
  on onetest 
  for each row
declare
  -- local variables here定义参数
  
begin
  --触发语句
  ;
end INSERT_Test;

7.3 Type

Statement level, line level, replacement, user event, system event

8. Process

8.1 Cursor

1. Declare the cursor
In the DECLEAR section, declare the cursor in the following format:
CURSOR cursor name [(parameter 1 data type[, parameter 2 data type...])]
IS SELECT statement;
parameters are optional parts, and the defined parameters can appear in the SELECT statement WHERE clause. If parameters are defined, the corresponding actual parameters must be passed when opening the cursor.
The SELECT statement is a query statement on a table or view, or even a joint query. Clauses such as WHERE conditions, ORDER BY, or GROUP BY can be included, but INTO clauses cannot be used. Variables defined before the cursor is defined can be used in the SELECT statement.
2. Open the cursor
In the executable section, open the cursor in the following format:
OPEN cursor name [(actual parameter 1[, actual parameter 2...])]; When the
cursor is opened, the query result of the SELECT statement is transferred to the cursor work area.
3. Extracting data
In the executable part, the data in the cursor work area is taken to variables in the following format. The fetch operation must be performed after opening the cursor.
FETCH cursor name INTO variable name 1[, variable name 2…];
or
FETCH cursor name INTO record variable;
after the cursor is opened, there is a pointer to the data area, the FETCH statement returns one row of data pointed by the pointer at a time, and multiple rows need to be repeated Execution can be achieved using loop statements. The control loop can be carried out by judging the properties of the cursor.
The two formats are described below:
The variable name in the first format is the variable used to receive data from the cursor and needs to be defined in advance. The number and type of variables should be consistent with the number and type of field variables in the SELECT statement.
The second format fetches one row of data into the record variable at a time. It is necessary to use %ROWTYPE to define the record variable in advance. This form is more convenient to use, and there is no need to define and use multiple variables separately.
The method of defining record variables is as follows:
variable name table name|cursor name %ROWTYPE;
the table must exist, and the cursor name must be defined first.
4. Close cursor
CLOSE cursor name;
after an explicit cursor is opened, it must be explicitly closed. Once the cursor is closed, the resources occupied by the cursor are released, the cursor becomes invalid and must be reopened before it can be used.

8.2 Creation process

create or replace procedure P_getDates(S_RQ date,RE out sys_refcursor)
as --输入输出存储过程
begin
  open RE for
    select f.站点名称,g.当日,f.月累
from 
 (select nvl(s.DEPARTMENT,'油田发电量合计') as 站点名称, /* e.attr_set_id,*/sum(e.input_value)  as 月累
from ST_WATPOW_REP_ELEC_ATTR_SET s,ST_WATPOW_REP_DAILY d,ST_WATPOW_REP_ART_INP_ACT_ELEC e 
where d.daily_id=e.daily_id and d.daily_date=S_RQ and s.attr_set_id=e.attr_set_id  and s.SET_ATTR='发电'  --or  */replace(s.SET_ATTR,chr(9),'')='外购'
group by  rollup(s.DEPARTMENT)--, e.attr_set_id
)  f  left join
(select  c.department as department,a.attr_set_id  as attr_set_id,a.input_value  as 当日
from ST_WATPOW_REP_ART_INP_ACT_ELEC a,ST_WATPOW_REP_DAILY b ,ST_WATPOW_REP_ELEC_ATTR_SET c
where a.daily_id=b.daily_id and b.daily_date=S_RQ and c.set_attr='发电' and c.attr_set_id=a.attr_set_id
order by a.attr_set_id)  g  on  f.站点名称 =  g.department;
close RE;
end P_getDates;

9, synonyms

Similar to an alias, it is a mapping relationship. Multiple users can use one table.
Insert picture description here

10. Database backup and recovery

Backup

在命令行中输入命令:

EXP username/userpassword@orcl file=D:\201806041148.dmp tables=(tableName1,tableName2)

命令详细如下:

username:数据库用户名(必须)

userpassword:数据库用户密码(必须)

orcl:需备份的数据库的服务标识名(必须)

file:备份文件的全路径名称,可根据需要修改(必须)

tables:可填写需备份的表名,多个以‘,’分隔(可选)

restore

在命令行中输入命令

imp username/userpassword@orcl  file=D:\201806041148.dmp tables=(tableName1,tableName2) FULL=Y

命令详细如下:

username:数据库用户名(必须)

userpassword:数据库用户密码(必须)

orcl:需恢复的数据库的服务标识名(必须)

file:备份文件的全路径名称,可根据需要修改(必须)

tables:可填写需备份的表名,多个以‘,’分隔(可选)

FULL:FULL=Y,表示恢复备份中的所有数据,(可选)

11. Practice code:

11.1 Table 1:

发电量统计:
select f.站点名称,g.当日,f.月累
from 
 (select nvl(s.DEPARTMENT,'油田发电量合计') as 站点名称, /* e.attr_set_id,*/sum(e.input_value)  as 月累
from ST_WATPOW_REP_ELEC_ATTR_SET s,ST_WATPOW_REP_DAILY d,ST_WATPOW_REP_ART_INP_ACT_ELEC e 
where d.daily_id=e.daily_id and d.daily_date between to_date('2020-10-01','YYYY-MM-DD') and to_date('2020-10-29','YYYY-MM-DD')
and s.attr_set_id=e.attr_set_id  and s.SET_ATTR='发电'  --or  */replace(s.SET_ATTR,chr(9),'')='外购'
group by  rollup(s.DEPARTMENT)--, e.attr_set_id
)  f  left join
(select  c.department as department,a.attr_set_id  as attr_set_id,a.input_value  as 当日
from ST_WATPOW_REP_ART_INP_ACT_ELEC a,ST_WATPOW_REP_DAILY b ,ST_WATPOW_REP_ELEC_ATTR_SET c
where a.daily_id=b.daily_id and b.daily_date=to_date('2020-10-27','YYYY-MM-DD') and c.set_attr='发电' and c.attr_set_id=a.attr_set_id
order by a.attr_set_id)  g  on  f.站点名称 =  g.department 

11.2 Process

create:

create or replace procedure P_ST_WATPOW_REP_DAILY(Name in out type, Name in out type, ...) is
begin
  
end P_ST_WATPOW_REP_DAILY;
create or replace procedure getDates(S_RQ date,RE out sys_refcursor) 
as
begin

  
end getDates;

When testing the stored procedure, an ORA-24338 error occurred. After deleting the statement that closes the cursor, it can run normally.
Insert picture description here

create or replace procedure P_getDates(S_RQ date,RE out sys_refcursor)
as
begin
  open RE for
    select f.站点名称,g.当日,f.月累
from 
 (select nvl(s.DEPARTMENT,'油田发电量合计') as 站点名称, /* e.attr_set_id,*/sum(e.input_value)  as 月累
from ST_WATPOW_REP_ELEC_ATTR_SET s,ST_WATPOW_REP_DAILY d,ST_WATPOW_REP_ART_INP_ACT_ELEC e 
where d.daily_id=e.daily_id and d.daily_date=S_RQ and s.attr_set_id=e.attr_set_id  and s.SET_ATTR='发电'  --or  */replace(s.SET_ATTR,chr(9),'')='外购'
group by  rollup(s.DEPARTMENT)--, e.attr_set_id
)  f  left join
(select  c.department as department,a.attr_set_id  as attr_set_id,a.input_value  as 当日
from ST_WATPOW_REP_ART_INP_ACT_ELEC a,ST_WATPOW_REP_DAILY b ,ST_WATPOW_REP_ELEC_ATTR_SET c
where a.daily_id=b.daily_id and b.daily_date=S_RQ and c.set_attr='发电' and c.attr_set_id=a.attr_set_id
order by a.attr_set_id)  g  on  f.站点名称 =  g.department;
end P_getDates;

11.3 Packages and inclusion process

package:

create or replace package SDGLTest is

  -- Author  : MUMU
  -- Created : 2020/10/30 10:06:16
  -- Purpose : 
  
  -- Public type declarations
  --type <TypeName> is <Datatype>;
  type cur is ref cursor;
  
  -- Public constant declarations
  --<ConstantName> constant <Datatype> := <Value>;

  -- Public variable declarations
  --<VariableName> <Datatype>;
  S_RQ date;

  -- Public function and procedure declarations
  --function <FunctionName>(<Parameter> <Datatype>) return <Datatype>;
  procedure P_getDates(S_RQ date,RE out sys_refcursor);

end SDGLTest;

Package body:

create or replace package body SDGLTest is

procedure P_getDates(S_RQ date,RE out sys_refcursor) as
begin
  open RE for
    select f.站点名称,g.当日,f.月累
from  (select nvl(s.DEPARTMENT,'油田发电量合计') as 站点名称, /* e.attr_set_id,*/sum(e.input_value)  as 月累
from ST_WATPOW_REP_ELEC_ATTR_SET s,ST_WATPOW_REP_DAILY d,ST_WATPOW_REP_ART_INP_ACT_ELEC e 
where d.daily_id=e.daily_id and d.daily_date=S_RQ and s.attr_set_id=e.attr_set_id  and s.SET_ATTR='发电'  --or  */replace(s.SET_ATTR,chr(9),'')='外购'
group by  rollup(s.DEPARTMENT)--, e.attr_set_id
)  f  left join
(select  c.department as department,a.attr_set_id  as attr_set_id,a.input_value  as 当日
from ST_WATPOW_REP_ART_INP_ACT_ELEC a,ST_WATPOW_REP_DAILY b ,ST_WATPOW_REP_ELEC_ATTR_SET c
where a.daily_id=b.daily_id and b.daily_date=S_RQ and c.set_attr='发电' and c.attr_set_id=a.attr_set_id
order by a.attr_set_id)  g  on  f.站点名称 =  g.department;
end P_getDates;


end SDGLTest;

11.4 Package body stored procedure to build a table

undone

insert into ONETEST(DEPARTMENT,INPUT_VALUE)
select s.department as DEPARTMENT,e.input_value as INPUT_VALUE
from ST_WATPOW_REP_ELEC_ATTR_SET s,ST_WATPOW_REP_ART_INP_ACT_ELEC e,ST_WATPOW_REP_DAILY d
where e.daily_id=d.daily_id and s.set_attr='外购' and s.attr_set_id=e.attr_set_id
order by e.attr_set_id;
insert into ONETEST(DEPARTMENT,INPUT_VALUE)
values ('测试',23333);

Guess you like

Origin blog.csdn.net/Wendy_i/article/details/109337526