Showing posts with label Oracle DB. Show all posts
Showing posts with label Oracle DB. Show all posts

Tuesday, June 23, 2015

Import/Export large data in Oracle DB

Export:

C:\Oracle\Database\product\11.2.0\dbhome_1\BIN>exp test/123456@db_name1 file=export_data.dump log=log.txt buffer=10485867 statistics=none tables=(table1,table2)


Import:

C:\Oracle\Database\product\11.2.0\dbhome_1\BIN>imp test/123456@db_name1 file=export_data.dump full=yes


Using Pump Export/Import

    Export:
    expdp scott/tiger DIRECTORY=dmpdir DUMPFILE=scott.dmp
    with "dmpdir" created by:
    CREATE DIRECTORY dmpdir AS '/opt/oracle';
    Import:
    impdp system/oracle DIRECTORY=dmpdir DUMPFILE=scott.dmp 
                        REMAP_SCHEMA=<OLD_SCHEMA>:<NEW_SCHEMA>

Monday, June 22, 2015

Increase heap memory in Oracle DB for data import/export

You may encounter "Out of memory" when copy Database or import/export data in SQL Developer tool, go to:

<oracle_product_domain>sqldeveloper\sqldeveloper\bin

Open file: sqldeveloper.conf, add more line:

AddVMOption -Xmx1024M

(defaullt is 128M)

Tuesday, March 17, 2015

Fix DB Network Adapter could not establish

Fist, make sure you configure listener.ora correctly:

# listener.ora Network Configuration File: C:\Oracle\Database\product\11.2.0\dbhome_1\network\admin\listener.ora
# Generated by Oracle configuration tools.

SID_LIST_LISTENER =
  (SID_LIST =
    (SID_DESC =
      (SID_NAME = CLRExtProc)
      (ORACLE_HOME = C:\Oracle\Database\product\11.2.0\dbhome_1)
      (PROGRAM = extproc)
      (ENVS = "EXTPROC_DLLS=ONLY:C:\Oracle\Database\product\11.2.0\dbhome_1\bin\oraclr11.dll")
    )
  )

LISTENER =
  (DESCRIPTION_LIST =
    (DESCRIPTION =
      (ADDRESS = (PROTOCOL = IPC)(KEY = EXTPROC1521))
      (ADDRESS = (PROTOCOL = TCP)(HOST = localhost)(PORT = 1521))
      (ADDRESS = (PROTOCOL = TCP)(HOST = 301KhiemTT-W7)(PORT = 1521))

    )
  )

ADR_BASE_LISTENER = C:\Oracle\Database

Run lsnrctrl start to start Listener.

Hope that help!

Thursday, March 12, 2015

Get autoId from sequence in Oracle when insert data

CREATE SEQUENCE <sequence_name> START WITH 1 INCREMENT BY 1;

INSERT INTO Customers (AutoId, CustomerName, Country)
SELECT <sequence_name>.NEXTVAL, SupplierName, Country FROM Suppliers

Tuesday, December 30, 2014

Sequence in Oracle

Create Sequence

CREATE SEQUENCE customers_seq
 START WITH     1000
 INCREMENT BY   1
 NOCACHE
 NOCYCLE;
 
In case that we already have data in DB, use this snippet:

declare
    new_id number;
begin
   select NVL(max(id),0) + 1
   into   new_id
   from   my_table;

    execute immediate 'Create sequence my_seq
                       start with ' || new_id ||
                       ' increment by 1';
end;

/

Use Sequence

In Entity object:

    /** The auto id. */
    private Long autoId;

    /**
     * Gets the auto id.
     *
     * @return the auto id
     */
    @Id
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "sq")
    @SequenceGenerator(name = "sq", sequenceName = "my_seq", allocationSize=1)
    @Column(name = "AUTOID", unique = true, nullable = false, precision = 30, scale = 0)
    public Long getAutoId() {
        return this.autoId;
    }

    /**
     * Sets the auto id.
     *
     * @param autoId the new auto id
     */
    public void setAutoId(Long autoId) {
        this.autoId = autoId;
    }



GenerationType.SEQUENCE is specific for Oracle. Because Oracle uses Sequence strategy for create AutoId.

In @SequenceGenerator, the “allocationSize” is by default set to 50
And, by default, the “increment by” is set to 1.

Note:

It’s important to note that, the sequence generator work correctly, only when the “allocationSize” and the “increment by” are same, otherwise there is a possibility of getting the “unique constraint violation” exception. therefore, it is better to explicitly define “allocationSize” and “increment by”.