Oracle 相关知识

Posted Hamilton Tan

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Oracle 相关知识相关的知识,希望对你有一定的参考价值。

1.Navicat 连接Oracle 报错:

解决办法:

1).先下载oracle客户端,win64_11gR2_client.zip,下载地址:http://www.oracle.com/technetwork/database/enterprise-edition/downloads/112010-win64soft-094461.html

2).下载之后打开navicat,选择工具->选项->其他->OCI 的OCI Library(oci.dll)*:选择指定的路径:C:\\app\\test\\product\\11.2.0\\client_1\\oci.dll。如图:

 

2、oracle安装之后,创建用户名和权限

create user 用户名 identified by "密码";  
授权:grant create session to 用户名;  
            grant create table to  用户名;  
            grant create tablespace to  用户名;  
            grant create view to  用户名;

3、序列处理

--select HR_TABLE_PERSONID.nextval from dual;
--select *from dba_sequences where sequence_owner=\'TestRecord\' AND SEQUENCE_NAME IN(\'Test_DEVICEUSER_ID\',\'TEST_PROJECT_PROJECTID\');

4、oracle不足位数补零

Oracle 函数中有不足位数补空格的函数 
LPAD和RPAD分别是左补位和右补位 
repalce(lpad(字段名,共几位),‘ ’,‘0’) 
如运行 
select replace(lpad(\'123\',5),\' \',\'0\') from dual 
结果是00123 
即补足五位

select lpad(\'123\',5,\'0\') from dual 这样也可以
5、创建表
-- 流水号信息表
CREATE TABLE Test_SERIALNUMBER
(
    "ID" Number(10) primary key,
    "NAME" VARCHAR2(100),
    BILLMARK VARCHAR2(100),
    ENTITY VARCHAR2(100),
    "YEAR" VARCHAR2(100),
    "MONTH" VARCHAR2(100),
    "DAY" VARCHAR2(100),
    PLACES INT,
    MAXCODE Number(15),
    NVersion timestamp
)
LOGGING
NOCOMPRESS
NOCACHE;

-- 添加表注释
COMMENT ON TABLE Test_SERIALNUMBER IS \'流水号信息表\';

-- 添加字段注释
COMMENT ON COLUMN Test_SERIALNUMBER.ID IS \'主键ID\';
COMMENT ON COLUMN Test_SERIALNUMBER.NAME IS \'名称\';
COMMENT ON COLUMN Test_SERIALNUMBER.ENTITY IS \'实体名称\';
COMMENT ON COLUMN Test_SERIALNUMBER.BILLMARK IS \'标记\';
COMMENT ON COLUMN Test_SERIALNUMBER.YEAR IS \'\';
COMMENT ON COLUMN Test_SERIALNUMBER.MONTH IS \'\';
COMMENT ON COLUMN Test_SERIALNUMBER.DAY IS \'\';
COMMENT ON COLUMN Test_SERIALNUMBER.PLACES IS \'位数\';
COMMENT ON COLUMN Test_SERIALNUMBER.MAXCODE IS \'流水号\';
COMMENT ON COLUMN Test_SERIALNUMBER.NVersion IS \'版本号\';



-- 插入一个数据
INSERT INTO Test_SERIALNUMBER("ID","NAME",BILLMARK,ENTITY,"YEAR","MONTH","DAY",PLACES,MAXCODE,NVersion) values(1,\'个人信息\',\'KM\',\'Test_PERSONINFO\',\'2017\',\'06\',\'29\',9,\'1000000000\',to_timestamp(sysdate,\'yyyy-mm-dd hh24:mi:ss.ff\'));

6.存储过程

/*--------------------------------------------------------------------------------------------------------              
获取最大流水号
vEntity -- 实体名称(表名)
vbillCode -- 返回流水号       
--------------------------------------------------------------------------------------------------------*/        
CREATE OR REPLACE PROCEDURE UP_GETMAXCODE(vEntity IN VARCHAR,vbillCode out VARCHAR)
AS
BEGIN

DECLARE vMaxCode VARCHAR(20);
vNVersion timestamp;
vTimes INT;

BEGIN

SELECT (MAXCODE+1),NVersion INTO vMaxCode,vNVersion
FROM HR_SERIIALNUMBER
WHERE ENTITY=vEntity;

UPDATE HR_SERIIALNUMBER SET MAXCODE=MAXCODE+1
WHERE ENTITY=vEntity
AND NVersion=vNVersion;

WHILE sql%rowcount=0 LOOP
BEGIN
 vTimes:=vTimes+1;
 IF MOD(vTimes,100) = 0 THEN
 BEGIN
  --等待1秒
  --dbms_lock.sleep(1);
  IF vTimes>500 THEN
  BEGIN
   RETURN;
  END;
  END IF;
 END;
 END IF;

 SELECT (MAXCODE+1),NVersion INTO vMaxCode,vNVersion
 FROM HR_SERIIALNUMBER
 WHERE ENTITY=vEntity;

 UPDATE HR_SERIIALNUMBER SET MAXCODE=MAXCODE+1
 WHERE ENTITY=vEntity
 AND NVersion=vNVersion;

END;
END LOOP;

SELECT (NVL(BILLMARK,\'\')||NVL("YEAR",\'\')||NVL("MONTH",\'\')||NVL("DAY",\'\')|| SUBSTR(to_char(vMaxCode),-PLACES,PLACES)) INTO vbillCode
FROM HR_SERIIALNUMBER
WHERE ENTITY=vEntity;

END;
END;

 7.游标循环批量修改某个字段的值

declare 
cursor test_personno_cur
is select personid,personno,case to_char(createdate,\'yyyymmdd\') when \'00010101\' then to_char(updatedate,\'yyyymmdd\') else to_char(createdate,\'yyyymmdd\') end createdate from test_test order by personid asc for update;
vCode varchar(20):=\'00000000000\'; 
begin
  for test_row in test_personno_cur
  loop
    vCode := to_number(vCode) + 1;
    --dbms_output.put_line(\'vCode:\'||vCode);
    vCode := lpad(vCode,11,\'0\');
    --dbms_output.put_line(\'vCode:\'||vCode);
    update test_test set personno= (case to_char(createdate,\'yyyymmdd\') when \'00010101\' then to_char(updatedate,\'yyyymmdd\') else to_char(createdate,\'yyyymmdd\') end || vCode)  where current of test_personno_cur;
  end loop;
  commit;
end;

 8.错误:找不到请求的 .Net Framework Data Provider。可能没有安装. (The ADO.NET provider with invariant name \'Oracle.ManagedDataAccess.Client\' is either not registered in the machine or application config file, or could not be loaded. See the inner exception for details.)

1). C:\\Windows\\Microsoft.NET\\Framework\\v4.0.30319\\Config\\machine.config的 <system.data>-><DbProviderFactories>下添加

<add name="ODP.NET, Managed Driver" invariant="Oracle.ManagedDataAccess.Client" description="Oracle Data Provider for .NET, Managed Driver" type="Oracle.ManagedDataAccess.Client.OracleClientFactory, Oracle.ManagedDataAccess, Version=4.121.2.0, Culture=neutral, PublicKeyToken=89b483f429c47342" />
          <add name="ODP.NET, Unmanaged Driver" invariant="Oracle.DataAccess.Client" description="Oracle Data Provider for .NET, Unmanaged Driver" type="Oracle.DataAccess.Client.OracleClientFactory, Oracle.DataAccess, Version=4.121.2.0, Culture=neutral, PublicKeyToken=89b483f429c47342" />

 2).在web.config里面添加:

<system.data>
    <DbProviderFactories>
      <remove invariant="Oracle.ManagedDataAccess.Client"/>
      <add name="ODP.NET, Managed Driver" invariant="Oracle.ManagedDataAccess.Client" description="Oracle Data Provider for .NET, Managed Driver"
        type="Oracle.ManagedDataAccess.Client.OracleClientFactory, Oracle.ManagedDataAccess, Version=4.121.2.0, Culture=neutral, PublicKeyToken=89b483f429c47342"/>
    </DbProviderFactories>
  </system.data>

参考:http://www.cnblogs.com/gudi/p/6110875.html

9.如何配置pl/sql 连接远程oracle服务器

前提是电脑必须安装oracle客户端。

配置方法:

1).找到oracle的安装目录。如:C:\\oracle\\product\\10.2.0\\db_1\\network\\ADMIN

2).找到tnsnames.ora文件。

3).用文本方式打开,添加以下内容:

1
2
3
4
5
6
7
8
本地实例名 =
  (DESCRIPTION =
    (ADDRESS = (PROTOCOL = TCP)(HOST = 远程数据库IP地址)(PORT = 远程服务器端口号))
    (CONNECT_DATA =
      (SERVER = DEDICATED)
      (SERVICE_NAME = 远程数据库服务名)
    )
  )

其中中文部分是需要修改的部分,除第一个“本地实例名”外,其他需要跟远程数据库管理员咨询,本地实例名就是方便自己识别数据库的一个名字,可以自定义。

4).然后打开pl/sql就能看到自己创建的链接,如图:

10.利用plsql 客户端,执行脚本需要commit;

11.查看当前库中表的个数:

Oracle: select count(*) from user_tables

SqlServer: SELECT count(*) FROM sysobjects WHERE (xtype = \'U\'),

 mysql:SELECT COUNT(*) TABLES, table_schema FROM information_schema.TABLES  WHERE table_schema = \'ctms_demo\' 

 12.查看oracle索引:

select *from dba_sequences where sequence_owner=\'\' and sequence_name like \'%see%\';

 13.oracle 命令导出数据库

exp test/ABC23@Orcl  owner=testfile=d:\\2018.dmp log=d:\\test.txt  STATISTICS=none

 14.Oracle序列

 1).oracle创建序列(SqlServer:IDENTITY(1,1) 和Mysql可以自增长)

CREATE SEQUENCE HR_TEST_TESTID
INCREMENT BY 1
MINVALUE 1
MAXVALUE 9999999999999999999999999999
START WITH 1
CACHE 500;

---调用

select HR_TEST_TESTID.nextval from dual

2).查找所有的序列

select * from user_sequences;

15.通过命令导出oracle 数据库(.dmp文件),也可以通过oracle客户端PLSQL Developer导出

命令连接oracle 客户端的配置文件:tnsping orcl 如下图

  

 

cd C:\\app\\Ham\\product\\11.2.0\\dbhome_1\\BIN

sqlplus Test/test@orcl

exit

exp Test/test@orcl  owner=testr file=d:\\data\\data2017.dmp log=d:\\data\\test.txt  STATISTICS=none

如下图:

 16.EFProfiler 查看和跟踪EF生成的Oracle脚本

    1).下载EFProfiler https://hibernatingrhinos.com/products/EFProf  本次用的是EntityFramework.Profiler-v4.0-Build-4041

    2).注册申请证书  https://hibernatingrhinos.com/products/EFProf/buy 

    打开EFProf.exe,把.xml的证书导入,然后

    a.引用dll,HibernatingRhinos.Profiler.Appender.dll

    b.Global Application_Start  添加 HibernatingRhinos.Profiler.Appender.EntityFramework.EntityFrameworkProfiler.Initialize();

       本次是webAPI程序,在Global.asax.cs 的    Application_Start 方法里面加入HibernatingRhinos.Profiler.Appender.EntityFramework.EntityFrameworkProfiler.Initialize(); 

     c.启动EFProf.exe 程序

     参考:http://blog.csdn.net/gavin_luo/article/details/42457509

   17.oracle 转sqlserver 总结

       1).可以通过navicat 的工具->数据传输,可以转表结构和数据。然后受到修改相关字段和存储过程,视图等,这个方法碰到大数据会报错。如图:

       

     

     

    

   2).可以通过SSMA for Oracle  参考: 1. http://www.codes51.com/article/detail_122407.html  2.http://blog.csdn.net/zheng_dian/article/details/7562489

 

18.Oracle 数据库中区分大小写,建议全部使用大写,如果有有表名都有大小写,查询的时候需要加上"",全部大写或全部小写,会报错:ORA-00942: 表或视图不存在

19.Oracle可以用rownum 相当于sqlserver 的top 

select rownum,t.* from hr_test t where rownum > 0 order by rownum asc;

 20.经常用到的oracle Sql 脚本

//查看死锁
SELECT s.username,l.OBJECT_ID,l.SESSION_ID,s.SERIAL#,
l.ORACLE_USERNAME,l.OS_USER_NAME,l.PROCESS 
FROM V$LOCKED_OBJECT l,V$SESSION S WHERE l.SESSION_ID=S.SID;      

CREATE TABLESPACE HealTest LOGGING DATAFILE \'D:\\app\\oracl\\oradata\\orcl\\HealTest.dbf\' 
SIZE 100M AUTOEXTEND ON NEXT 32M MAXSIZE 500M EXTENT MANAGEMENT LOCAL;

create temporary tablespace HealTest_TEMP tempfile \'D:\\app\\oracl\\oradata\\orcl\\HealTest_TEMP.dbf\'
size 100m autoextend on next 32m maxsize 500m extent management local;

CREATE USER HealTest IDENTIFIED BY HealTest DEFAULT TABLESPACE HealTest TEMPORARY TABLESPACE HealTest_TEMP;

drop user HealTest cascade;

grant connect,resource,dba to HealTest;
grant create session to HealTest;

 22.oralce 表添加字段

ALTER TABLE TEST ADD(NAME VARCHAR2(30),TESTID VARCHAR2(200));

23.创建数据库相关的脚本

imp healthtest/healthtest  file=D:/healthtestDB/HRDB201703231950.dmp  fromuser=healthtest touser=healthtestBAK ignore=y log=D:\\test.txt
exp healthtest/healthtest@orcl  owner=healthtest file=d:\\healthtest.dmp log=d:\\healthtest.txt  STATISTICS=none

1.create tablespace healthtest_tabspace datafile \'c:\\OrclDB\\healthtest_data.dbf\' size 500M  autoextend on next 5M; 
2.create user healthtest identified by healthtest default tablespace healthtest_tabspace; 
3.grant connect,resource,dba to healthtest; 
4.ALTER PROFILE DEFAULT LIMIT PASSWORD_LIFE_TIME UNLIMITED; 

--删除表名称空间
--DROP TABLESPACE healthtest_tabspace INCLUDING CONTENTS AND DATAFILES;

 24.

  alter system set db_recovery_file_dest_size=3G scope=both;  

以上是关于Oracle 相关知识的主要内容,如果未能解决你的问题,请参考以下文章

Oracle 相关知识

数据库相关知识

android小知识点代码片段

oracle相关知识点

oracle相关的知识

oracle相关知识