oracle及shell相關學習記錄

  • Oracle
  • 簡單創建dblink
  • CREATE PUBLIC DATABASE LINK DBLINK_DRPMID CONNECT TO 用戶名IDENTIFIED BY "密碼" USING 'ip:1521/數據庫名'
    

     

--查看錶空間

SELECT created, log_mode, log_mode FROM v$database; 
--1G=1024MB 
--1M=1024KB 
--1K=1024Bytes 
--1M=11048576Bytes 
--1G=1024*11048576Bytes=11313741824Bytes 
SELECT a.tablespace_name "表空間名", 
total "表空間大小", 
free "表空間剩餘大小", 
(total - free) "表空間使用大小", 
total / (1024 * 1024 * 1024) "表空間大小(G)", 
free / (1024 * 1024 * 1024) "表空間剩餘大小(G)", 
(total - free) / (1024 * 1024 * 1024) "表空間使用大小(G)", 
round((total - free) / total, 4) * 100 "使用率 %" 
FROM (SELECT tablespace_name, SUM(bytes) free 
FROM dba_free_space 
GROUP BY tablespace_name) a, 
(SELECT tablespace_name, SUM(bytes) total 
FROM dba_data_files 
GROUP BY tablespace_name) b 
WHERE a.tablespace_name = b.tablespace_name 

 

1.安裝pl/sql development 遠程連接

修改tnsnames.ora   我的tnsnames.ora在D:\Program Files\PLSQL(綠色版)\PLSQL\instantclient_11_2文件夾下

 

修改爲:

orcl =

  (DESCRIPTION =

    (ADDRESS_LIST =

      (ADDRESS = (PROTOCOL = TCP)(HOST = 192.168.9.102)(PORT = 1521))

    )

    (CONNECT_DATA =

      (SERVER = DEDICATED)

      (SERVICE_NAME = orcl)

    )

  )

主要是更改連接的host和端口

    服務端修改listener.ora

我的在 D:\oracle\product\11.2.0\dbhome_1\NETWORK\ADMIN”目錄下的

ORCL =
(DESCRIPTION =
(ADDRESS = (PROTOCOL = TCP)(HOST = localhost)(PORT = 1521))
(ADDRESS = (PROTOCOL = TCP)(HOST = 192.168.9.102)(PORT = 1521))
(CONNECT_DATA =
(SERVER = DEDICATED)
(SERVICE_NAME = 
orcl)
)
)

   主要是增加一條address  將本機的ip增加監聽

 2.

創建表空間

create tablespace xcc datafile 'e:\oracle\oradata\test\data_1.dbf' size 2000M;

創建用戶

Create user xcc identified by xcc;

Create user xcc identified by xcc profile default tablespace xcc account xcc;

授權

Grant create user,drop user,alter user,create any view,drop any view,exp_full_database,imp_full_databse,dba,connect,resource,create session to xcc;

Conn xcc/xcc

建表

Create  table tt (id number(32) primary key,

Name varchar2(16),

Age integer(2),

Sex varchar(2) default ‘男’ check( sex in (‘男’,’女’)));

 

--Oracle下語法:
create table BranchAccount as select from TATA.dbo.BranchAccount
--SqlServer下語法:
select into BranchAccountfrom TATA.dbo.BranchAccount

改表名

Rename tt to student;

該字段

Alter table student modify(age integer not null);

Alter table student rename column age to age1;

其他

alter table tablename add (column datatype [default value][null/not null],….);

alter table tablename modify (column datatype [default value][null/not null],….);

alter table tablename drop (column);

alter table tablename drop constraint foreignkeyname;

外鍵

單列外鍵

create table book (id number(32) primary key,

                  name varchar2(30),

               s_id number(32),

             constraint s_id   foreign key (id) references student(id)) ;

多列

create table tb_supplier (

supplier_id number not null,

supplier_name varchar2(50) not null,

 contact_name varchar2(50),

CONSTRAINT pk_supplier PRIMARY KEY (supplier_id,supplier_name) );

 

create table tb_products (

product_id number not null,

product_name varchar2(100),

supplier_name varchar2(50),

 supplier_id number not null,

 constraint fk_products_supplier foreign key (supplier_id,supplier_name) references tb_supplier(supplier_id,supplier_name) );


 
alter table tb_products add constraint fk_products_supplier  foreign key (supplier_id,supplier_name)  references tb_supplier(supplier_id,supplier_name);

 
批量插入

insert all

into book(id,name,S_ID) values('01','english','01')

into book(id,name,S_ID) values('02','math','02')

select 1 from DUAL;

 
DUAL表的用途:

Dual 是 Oracle中的一個實際存在的表,任何用戶均可讀取,常用在沒有目標表的Select語句塊中

--查看當前連接用戶

SQL> select user from dual;

--查看當前日期、時間

SQL> select sysdate from dual;

SQL> select to_char(sysdate,'yyyy-mm-dd hh24:mi:ss') from dual;

--當作計算器用

SQL> select 1+2 from dual;

--查看序列值

SQL> create sequence aaa increment by 1 start with 1;

SQL> select aaa.nextval from dual;

存儲過程
無參存儲過程
--create or replace procedure procedureName

--as聲明

--自定義變量或遊標

--;

--begin執行

--;

--exception異常

--;

--end;

create or replace procedure TheCopyTable

as

my_exception exception;

sid student.id%type;

sname student.name%type;

row_id int;

resu int;

cursor s_cur is select * from student;

begin

dbms_output.put_line('您調用了TheCopyTable這個存儲過程');

row_id := 1;

for stu in s_cur

    loop

    sid:=stu.id;

    sname:=stu.name;

    dbms_output.put_line('student表中第'||row_id||':'||sid||sname);

    row_id := row_id +1;

    select count(1) into resu from s1 where id = sid and name = sname;

    if resu = 0 then

    dbms_output.put_line('正在插入數據...');

    insert into s1 select * from student where student.id = sid;

    dbms_output.put_line('插入完成');

    else

    dbms_output.put_line('已存在記錄');

    end if;

    if row_id =3 then

    raise my_exception  ;

    end if;

    end loop;

exception

    when my_exception then

        dbms_output.put_line('已結束');

    when no_data_found then

        dbms_output.put_line('Data is not found!');

    when too_many_rows then

        dbms_output.put_line('Too many rows round!');

    when OTHERS then

        dbms_output.put_line('others error');

end;

 

--調用

--begin

--procedureName;

--end;

call  TheCopyTable();
將一個表中數據插入另一個表

insert into s1 select * from student where student.id = sid;

 

有參存儲過程

 

create or replace procedure calculator(a  in number,b in number,c out number)

as

i number;

my_exception exception;

begin

i := 10;

c := a;

while i>1

loop

c := c/b;

i := i-1;

if b = 0 then

raise my_exception;

else

dbms_output.put_line(a||'/'||b||'='||c);

end if;

end loop;

end;

 

調用

 

declare

c number(10);

begin

calculator(10,2,c);

end;

 

循環

  1. for…in…loop  一般用於遊標和數組
  2. while 各種語句 loop

數組

  1. 固定長度的數組

declare

type vararray is varray(30) of varchar2(100);

var_a vararray := vararray('a','b','c','d');

begin

for i in 1..var_a.count loop

dbms_output.put_line(var_a(i));

end loop;

end;

1..var_a.count 是指1var_a長度之間是一個範圍

也可使用var_a.first..var_a.last

  1. 未知長度

 declare

type vararray is table of varchar2(20) index by binary_integer;

var_a vararray;

begin

 for i in 1..15 loop

 var_a(i):=i+10;

 end loop;

 dbms_output.put_line(var_a.count);

end;

 

查看錶空間

SELECT a.tablespace_name "表空間名", 
total "表空間大小", 
free "表空間剩餘大小", 
(total - free) "表空間使用大小", 
total / (1024 * 1024 * 1024) "表空間大小(G)", 
free / (1024 * 1024 * 1024) "表空間剩餘大小(G)", 
(total - free) / (1024 * 1024 * 1024) "表空間使用大小(G)", 
round((total - free) / total, 4) * 100 "使用率 %" 
FROM (SELECT tablespace_name, SUM(bytes) free 
FROM dba_free_space 
GROUP BY tablespace_name) a, 
(SELECT tablespace_name, SUM(bytes) total 
FROM dba_data_files 
GROUP BY tablespace_name) b 
WHERE a.tablespace_name = b.tablespace_name 

oracle查看錶行數所佔空間大小

select ut.table_name,ut.OWNER,ut.tablespace_name,num_rows,us.BYTES from dba_tables ut,
(select segment_name,sum(bytes)/1024/1024||'M' bytes from user_segments group by segment_name) us 
where ut.TABLE_NAME = us.segment_name
order by num_rows desc nulls last;

oracle查看錶空間塊大小

select tablespace_name,blocK_size,status,contents from dba_tablespaces;

增加表空間

alter tablespace PDM_DATA add datafile 'D:\APP\ADMINISTRATOR\ORADATA\PDM\PDM_DATA02.DBF' size 1000m autoextend on next 320m maxsize 30480m

oracle查找數據絕對文件號

select file#,name,status from v$datafile

一些shell腳本

linux:

#查看該目錄所在的硬盤大小

df -h [目錄]

ex: df -h /usr/local

#移動文件命令

mv [source] [target]

ex: mv ./a.txt /home

#刪除命令

rm -rf [目錄]

ex: rm -rf /home/temp/*

#查看文件所佔空間大小

du [file/directory]

-sh 

ex: du -sh /home/temp

#tgz壓縮

tar zcvf [Directory/targetFile.tgz]  [sourceFile/Directory]

ex: tar zcvf   kernel.tgz   /home/bak

#查看目錄下一層深度文件大小
du -h --max-depth=1 

[root@FineReportAppServer ~]# du -h --max-depth=1 
4.0K	./桌面
4.0K	./.abrt
4.0K	./.nautilus
4.0K	./.gnome2_private
384K	./.gstreamer-0.10
12M	./mysql-connector-java-5.1.40
260K	./.visualvm
20K	./satools
16K	./.gnote
4.0K	./公共的
12K	./.dbus
52K	./.config
4.0K	./圖片
22G	./.FineReport80
4.0K	./文檔

#刪除指定日期的文件夾
find ./ -mtime +3  -type d  -exec rm -rf {} \;
#+3 是指3天前  d是文件夾 


[root@FineReportAppServer cache]# ls -l
؜ԃ 72
drwxr-xr-x. 2 root root  4096 1Ղ  31 10:03 DATA_1548900202002_769
drwxr-xr-x. 2 root root  4096 1Ղ  31 10:04 DATA_1548900278760_21
drwxr-xr-x. 2 root root  4096 1Ղ  31 13:29 DATA_1548901256295_826
drwxr-xr-x. 2 root root  4096 2Ղ   1 15:24 DATA_1548953225620_832
drwxr-xr-x. 2 root root  4096 2Ղ   1 00:47 DATA_1548953231140_703
drwxr-xr-x. 2 root root  4096 2Ղ   1 14:40 DATA_1549003241568_18
drwxr-xr-x. 2 root root  4096 2Ղ   1 18:08 DATA_1549005970559_623
drwxr-xr-x. 2 root root  4096 2Ղ   1 17:02 DATA_1549011716648_413
drwxr-xr-x. 2 root root  4096 2Ղ   4 16:34 DATA_1549269248567_754
drwxr-xr-x. 2 root root 20480 2Ղ  12 09:48 DATA_1549935601750_579
drwxr-xr-x. 2 root root  4096 2Ղ  12 09:42 DATA_1549935761321_205
drwxr-xr-x. 2 root root  4096 2Ղ  12 09:45 DATA_1549935916375_702
drwxr-xr-x. 2 root root  4096 2Ղ  12 09:46 DATA_1549935970785_753
[root@FineReportAppServer cache]# find ./ -mtime +10  -type d  -exec rm -rf {} \;
find: ¡°./DATA_1548900278760_21¡±: ûԐҸ?þ»?¼
find: ¡°./DATA_1548901256295_826¡±: ûԐҸ?þ»?¼
find: ¡°./DATA_1548953231140_703¡±: ûԐҸ?þ»?¼
find: ¡°./DATA_1548900202002_769¡±: ûԐҸ?þ»?¼
[root@FineReportAppServer cache]# find ./ -mtime +3  -type d  -exec rm -rf {} \;
find: ¡°./DATA_1549003241568_18¡±: ûԐҸ?þ»?¼
find: ¡°./DATA_1548953225620_832¡±: ûԐҸ?þ»?¼
find: ¡°./DATA_1549011716648_413¡±: ûԐҸ?þ»?¼
find: ¡°./DATA_1549005970559_623¡±: ûԐҸ?þ»?¼
find: ¡°./DATA_1549269248567_754¡±: ûԐҸ?þ»?¼
[root@FineReportAppServer cache]# ls -l
؜ԃ 36
drwxr-xr-x. 2 root root 20480 2Ղ  12 09:48 DATA_1549935601750_579
drwxr-xr-x. 2 root root  4096 2Ղ  12 09:42 DATA_1549935761321_205
drwxr-xr-x. 2 root root  4096 2Ղ  12 09:45 DATA_1549935916375_702
drwxr-xr-x. 2 root root  4096 2Ղ  12 09:46 DATA_1549935970785_753


#添加定時任務清理finereport的cache文件,建立sh文件內容如下
[root@FineReportAppServer myshell]# cat del_old_cache.sh 
#!/usr/bin/bash
# del >2 dir
find /root/.FineReport80/cache/  -mtime +2  -type d  | xargs rm -rf
#添加定時任務
[root@FineReportAppServer myshell]# crontab -e


*/1 * * * * . /etc/profile; /home/shellScript/tomcatRestart2.0.sh
* */3 * * * /usr/sbin/ntpdate 192.168.10.12 && /usr/sbin/hwclock --systohc
0 23 * * * /home/myshell/del_old_cache.sh
#查看定時任務
[root@FineReportAppServer myshell]# crontab -l

*/1 * * * * . /etc/profile; /home/shellScript/tomcatRestart2.0.sh 
* */3 * * * /usr/sbin/ntpdate 192.168.10.12 && /usr/sbin/hwclock --systohc
0 23 * * * /home/myshell/del_old_cache.sh	


補充:後來發現上面寫的定時任務未執行,做了一些修改如下
[root@FineReportAppServer cache]# vim /home/myshell/del_old_cache.sh

#!/bin/bash
#. /etc/profile
source /etc/profile

find /root/.FineReport80/cache/  -mtime +1  -type d  | xargs rm -rf
保存

[root@FineReportAppServer cache]# crontab -e


*/1 * * * * . /etc/profile; /home/shellScript/tomcatRestart2.0.sh
* */3 * * * /usr/sbin/ntpdate 192.168.10.12 && /usr/sbin/hwclock --systohc
26 10  * * * .  /home/myshell/del_old_cache.sh
保存
查看執行結果
[root@FineReportAppServer cache]# tailf /var/log/cron
Feb 18 10:20:01 FineReportAppServer CROND[22050]: (root) CMD (/usr/lib64/sa/sa1 1 1)
Feb 18 10:21:01 FineReportAppServer CROND[22567]: (root) CMD (. /etc/profile; /home/shellScript/tomcatRestart2.0.sh )
Feb 18 10:22:01 FineReportAppServer CROND[22983]: (root) CMD (. /etc/profile; /home/shellScript/tomcatRestart2.0.sh )
Feb 18 10:22:01 FineReportAppServer CROND[22984]: (root) CMD (/home/myshell/del_old_cache.sh)
Feb 18 10:23:01 FineReportAppServer CROND[23380]: (root) CMD (. /etc/profile; /home/shellScript/tomcatRestart2.0.sh )
Feb 18 10:23:31 FineReportAppServer crontab[23633]: (root) BEGIN EDIT (root)
Feb 18 10:23:49 FineReportAppServer crontab[23633]: (root) REPLACE (root)
Feb 18 10:23:49 FineReportAppServer crontab[23633]: (root) END EDIT (root)
Feb 18 10:24:01 FineReportAppServer crond[2336]: (root) RELOAD (/var/spool/cron/root)
Feb 18 10:24:02 FineReportAppServer CROND[23911]: (root) CMD (. /etc/profile; /home/shellScript/tomcatRestart2.0.sh )
Feb 18 10:25:01 FineReportAppServer CROND[24414]: (root) CMD (. /etc/profile; /home/shellScript/tomcatRestart2.0.sh )
Feb 18 10:26:01 FineReportAppServer CROND[24990]: (root) CMD (.  /home/myshell/del_old_cache.sh)
查看文件是否被清除

[root@FineReportAppServer cache]# ll --time-style=long-iso
؜ԃ 184
drwxr-xr-x. 2 root root 36864 2019-02-17 09:07 DATA_1550219087448_433
drwxr-xr-x. 2 root root  4096 2019-02-16 14:01 DATA_1550296886131_170
drwxr-xr-x. 2 root root  4096 2019-02-16 14:01 DATA_1550296909780_401
drwxr-xr-x. 2 root root  4096 2019-02-17 11:31 DATA_1550374260630_584
drwxr-xr-x. 2 root root  4096 2019-02-17 22:46 DATA_1550414808179_572
drwxr-xr-x. 2 root root  4096 2019-02-17 23:46 DATA_1550418394328_607
drwxr-xr-x. 2 root root 81920 2019-02-18 09:39 DATA_1550446528958_468
drwxr-xr-x. 2 root root  4096 2019-02-18 07:58 DATA_1550447926628_761
drwxr-xr-x. 2 root root  4096 2019-02-18 08:02 DATA_1550448125852_120
drwxr-xr-x. 2 root root  4096 2019-02-18 09:34 DATA_1550453633755_587
drwxr-xr-x. 2 root root  4096 2019-02-18 10:26 DATA_1550454107679_936
drwxr-xr-x. 2 root root  4096 2019-02-18 09:58 DATA_1550455091380_641
drwxr-xr-x. 2 root root  4096 2019-02-18 09:59 DATA_1550455175422_369
drwxr-xr-x. 2 root root  4096 2019-02-18 10:00 DATA_1550455252403_77
drwxr-xr-x. 2 root root  4096 2019-02-18 10:07 DATA_1550455622368_528
drwxr-xr-x. 2 root root  4096 2019-02-18 10:07 DATA_1550455669754_324
drwxr-xr-x. 2 root root  4096 2019-02-18 10:08 DATA_1550455711320_780
drwxr-xr-x. 2 root root  4096 2019-02-18 10:10 DATA_1550455836335_577
測試生效了

#從一個文件中查找error字符的內容包含後五行 A:error所在行的後五行 B:前五行 C:上下五行
[root@FineReportAppServer shellScript]#  grep  -A 5  error* TomcatMonitor.log

--
[error]頁面訪問出錯,開始重啓tomcat
Using CATALINA_BASE:   /usr/local/tomcat
Using CATALINA_HOME:   /usr/local/tomcat
Using CATALINA_TMPDIR: /usr/local/tomcat/temp
Using JRE_HOME:        /usr/java/jdk1.7.0_79
Using CLASSPATH:       /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar
--
[error]頁面訪問出錯,開始重啓tomcat
Using CATALINA_BASE:   /usr/local/tomcat
Using CATALINA_HOME:   /usr/local/tomcat
Using CATALINA_TMPDIR: /usr/local/tomcat/temp
Using JRE_HOME:        /usr/java/jdk1.7.0_79
Using CLASSPATH:       /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar
--

oracle:
用戶即將到期時通過修改密碼可以使
賬戶的到期時間重新以該天開始計算

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章