Wednesday, September 30, 2009

How to set unlimited quota on tablespace to oracle user

You can set the quota when you create the user like this:


create user username identified by password

  default tablespace tablespaceName

  temporary tablespace temp

  quota unlimited on tablespaceName;


Or You can alter the user after creation like this:


alter user username
   quota unlimited on tablespaceName; 


Tuesday, September 22, 2009

How to connect Oracle SQL Developer to mySql

Download the drivers you need to connect
mySql :                      mysql-connector-java-5.0.8-bin.jar
Sybase, SQL Serverjtds-1.2.jar


In the menu of SQL Developer, select Tools -> Preferences -> Database -> Third Party JDBC Drivers.
Click Add Entry, and point out your specific JAR file. 


Now you are ready to connect to your databases.

Thursday, September 17, 2009

Oracle SQL Developer even known as Raptor

Oracle SQL Developer is Oracle´s free tool for developers and dba´s that need a quick and easy way to connec t to their databases. Oracle SQL Developer, also known as Raptor, can connect to other databases to: Microsoft SQL Server, mySql and Sybase.

The link to download this product: http://www.oracle.com/technology/software/products/sql/index.html

Wednesday, September 16, 2009

Oracle: How to avoid duplicate rows in table while inserting and updating data

There are several ways to avoid storing duplicate rows in an Oracle database when inserting and updating data. In the examples I use a table with two columns that should be unique:

1. Unique keys

ALTER TABLE tableName
    ADD CONSTRAINT uniqueKeyName UNIQUE(columnName1, columnName2);

2. First select and then insert or update

procedure insertRow(p_key1 in number, p_key2 in number, p_value in number)
is
  cursor c1(v1 in number, v2 in number) is
     select '1' from tableName
       where columnName1 = v1
            and columnName2 = v2;
  dummy varchar2(1);
begin
  open c1(p_key1, p_key2);
  fetch c1 into dummy;
  if sql%found then
    update tableName
       set valueColumn = p_value
       where columnName1 = p_key1
            and columnName2 = p_key2;
   else
      insert into tableName (columnName1, columnName2, valueColumn)
             values (p_key1, p_key2, p_value);
   end if;
end;
/

3. The MERGE-statement

MERGE INTO tableName tn
  USING dual on (dual.dummy is not null
                 and tn.columnName1 = p_key1
                 and tn.columnName2 = p_key2)
WHEN MATCHED THEN
  UPDATE set tn.valueColumn = p_value
WHEN NOT MATCHED THEN
  insert (columnaName1, columnName2, valueColumn)

     values (p_key1, p_key2, p_value);

4. update and then insert

procedure insertRow(p_key1 in number, p_key2 in number, p_value in number)
is
begin


  update tableName
     set valueColumn = p_value
    where columnName1 = p_key1
        and columnName2 = p_key2;
  if sql%rowcount = 0 then
    insert into tableName (columnName1, columnName2, valueColumn)
        values (p_key1, p_key2, p_value);
  end if;
end;
/

or

update tableName
     set valueColumn = p_value
    where columnName1 = p_key1
        and columnName2 = p_key2;


insert into tableName (columnName1, columnName2, valueColumn)
      select p_key1, p_key2, p_value
         from dual
        where not exists (select '1'
                          from tableName
                         where columnName1 = p_key1
                           and columnName2 = p_key2);

5. first delete and then insert

procedure insertRow(p_key1 in number, p_key2 in number, p_value in number)
is
begin
  delete from tableName
     where columnName1 = p_key1
         and columnName2 = p_key2;


   insert into tableName (columnName1, columnName2, valueColumn)
        values (p_key1, p_key2, p_value);
end;
/




Sunday, September 6, 2009

Oracle: How to remove duplicate keys found in table

If you have been trying to add a unique constraint or a primary key to an Oracle table and you receive the annoying error:


ORA-01452:
cannot CREATE UNIQUE INDEX; duplicate keys found
Cause:
A CREATE UNIQUE INDEX statement specified one or more columns that currently contain duplicate values. All values in the indexed columns must be unique by row to create a UNIQUE INDEX.
Action:
If the entries need not be unique, remove the keyword UNIQUE from the CREATE INDEX statement, then re-execute the statement. If the entries must be unique, as in a primary key, then remove duplicate values before creating the UNIQUE index.

Now you have the following to consider:

1. Maybe you have to redesign the datamodel and tables to allow duplicate keys in the table. Maybe they are there for a reason...

2. Create the primary/unique key without enforcing validation of the old existing data in the table with:

     CREATE UNIQUE INDEX indexName
      ON tableName (columnName)
      NOVALIDATE;

   This makes the unique constraint created but the existing data in the table is not validate according to the rule of being unique.

3. Find a way to remove the duplicates

If you select to remove the duplicate rows you have several ways to do this:

3.1. single sql-statement

    delete from tableName t1
      where t1.rowid != ( select min(t2.rowid) 
                            from tableName t2
                           where t2.columnName = t1.columnName );


3.2. plsql-program


3.3. create index statement exceptions into


The exceptions table can be created with the following script:
$ORACLE_HOME/rdbms/admin/utlexcpt.sql


Now try to add the unique constraint:

   alter table tableName
      
add constraint constraintName unique (colummName1, columnName2)

      exceptions into exceptions;


The rowid's for the duplicate rows will be stored in the exceptions table and they can be used with an join to delete the dupplicate rows in the source table.


Take a "backup" of the rows which are to be deleted:


   create table tableNameCopy as select * from tableName
        where rowid in (select row_id from exceptions);


Delete the duplicate rows:


   delete tableName where rowid in (select row_id from exceptions);






Tuesday, September 1, 2009

ORA-22856: Cannot add columns to object tables

If you get the error ORA-22856: Cannot add columns to object tables when trying to add columns to a table in Oracle9i, you propably have hit a bug.

The bug makes it impossible to add columns to a table in compressed state.
The solution is to alter the table to nocompress and rebuild it with the ALTER TABLE MOVE-statement before adding the columns.