Difference between RowID and REF of an Object Table - oracle

I would like to know the difference between ROWID and the REF (which gives the OID) of an object Table?
like, we query:
select rowid from emp;
and
select ref(e) from XX_OBJ_TABLE e;
// here XX_OBJ_TABLE is the object table of some XX_OBJ Object Type.
and
select rowid from XX_OBJ_TABLE;
please tell me the difference.
Much Appreciated.
Thanks in Advance.

Rowid is physical row address. REF implies hidden foreign key on hidden column which identifies uniquely object record.
As I remember from the first Tom Kyte book, this column is not indexed so it might cause locking issue when you delete from master table.

Related

How to create table in Hive with specific column values from another table

I am new to Hive and have some problems. I try to find a answer here and other sites but with no luck... I also tried many different querys that come to my mind, also without success.
I have my source table and i want to create new table like this.
Were:
id would be number of distinct counties as auto increment numbers and primary key
counties as distinct names of counties (from source table)
You could follow this approach.
A CTAS(Create Table As Select)
with your example this CTAS could work
CREATE TABLE t_county
ROW FORMAT DELIMITED FIELDS TERMINATED BY ','
STORED AS TEXTFILE AS
WITH t AS(
SELECT DISTINCT county, ROW_NUMBER() OVER() AS id
FROM counties)
SELECT id, county
FROM t;
You cannot have primary key or foreign keys on Hive as you have primary key on RBDMSs like Oracle or MySql because Hive is schema on read instead of schema on write like Oracle so you cannot implement constraints of any kind on Hive.
I can not give you the exact answer because of it suppose to you must try to do it by yourself and then if you have a problem or a doubt come here and tell us. But, what i can tell you is that you can use the insertstatement to create a new table using data from another table, I.E:
create table CARS (name string);
insert table CARS select x, y from TABLE_2;
You can also use the overwrite statement if you desire to delete all the existing data that you have inside that table (CARS).
So, the operation will be
CREATE TABLE ==> INSERT OPERATION (OVERWRITE?) + QUERY OPERATION
Hive is not an RDBMS database, so there is no concept of primary key or foreign key.
But you can add auto increment column in Hive. Please try as:
Create table new_table as
select reflect("java.util.UUID", "randomUUID") id, countries from my_source_table;

How can I merge two tables using ROWID in oracle?

I know that ROWID is distinct for each row in different tables.But,I am seeing somewhere that two tables are being merged using rowid.So,I also tried to see it,but I am getting the blank output.
I have person table which looks as:
scrowid is the column which contains rowid as:
alter table ot.person
add scrowid VARCHAR2(200) PRIMARY KEY;
I populated this person table as:
insert into ot.person(id,name,age,scrowid)
select id,name, age,a.rowid from ot.per a;
After this I also created another table ot.temp_person by same steps.Both table has same table structure and datatypes.So, i wanted to see them using inner join and I tried them as:
select * from ot.person p inner join ot.temp_person tp ON p.scrowid=tp.scrowid
I got my output as empty table:
Is there is any possible way I can merge two tables using rowid? Or I have forgotten some steps?If there is any way to join these two tables using rowid then suggest me.
Define scrowid as datatype ROWID or UROWID then it may work.
However, in general the ROWID may change at any time unless you lock the record, so it would be a poor key to join your tables.
I think perhaps you misunderstood the merging of two tables via rowid, unless what you actually saw was a Union, Cross Join, or Full Outer Join. Any attempt to match rowid, requardless of you define it, doomed to fail. This results from it being an internal definition. Rowid in not just a data type it is an internal structure (That is an older version of description but Oracle doesn't link documentation versions.) Those fields are basically:
- The data object number of the object
- The data block in the datafile in which the row resides
- The position of the row in the data block (first row is 0)
- The datafile in which the row resides (first file is 1). The file
number is relative to the tablespace.
So while it's possible for different tables to have the same rowid, it would be exteremly unlikely. Thus making an inner join on them always return null.

why can't we use ROWID as primary key?

According to Oracle Documentation
You should not use ROWID as the primary key of a table. If you delete
and reinsert a row with the Import and Export utilities, for example,
then its rowid may change. If you delete a row, then Oracle may
reassign its rowid to a new row inserted later.
I didn't understand the actual reason. Does it mean, when we use Import/Export utilities, then only we shouldn't use ROWID as primary key or we should never use ROWID as primary key ?
As explained above, when we delete the row and re-insert then same ROWID may get assign but on the other side the row was already deleted, so there won't be any problem if we get same ROWID. Isn't it ? Can anyone explain this with some example ?
If you rebuild your table then the ROWID of the table may change and you dont want your primary key to be changed.
Also if you delete one record then a new record could be given that ROWID. Also you should understand that ROWID does not persist across a database EXPORT and IMPORT process.
From here
If rows are moved, the ROWID will change. Rows can move due to
maintenance operations like shrinks and table moves. As a result,
storing ROWIDs for long periods of time is a bad idea. They should
only be used in a single transaction, preferably as part of a SELECT
... FOR UPDATE, where the row is locked, preventing row movement.
We should never use ROWIDs as primary keys for permanent and business-important data.
ROWID is a technical address of a row. There are several scenarious when
a) rowid of the existing records would be changed
b) different records would have the same rowid.
For example, if you have partitioned table, updating of record's partitioning key would bring us into record's rowid changing. Such scenarious prevents of using ROWID keys unless we can to forget it without serious consequences.
ROWID keys can be used for unnecessary temporary data, such as exceptions tables, or for short-term navigation, such as in WHERE CURRENT OF clause.

will there be an unique id for each record in Oracle

In my application I have created a column with sequence to have unique id for each and every record that I add,I doubt will there be any unique row-number created by oracle itself for every record we updated in table,if yes then how to access that row number like
SELECT row-number from table where employee_name='name';
here I want to get unique row number created by oracle,
I have searched on net but haven't got proper information
Oracle does maintain a ROWID for each row; however, in my opinion using ROWID in user-written code is both poor practice and dangerous. ROWID is only guaranteed to be constant for the duration of a single transaction. ROWID is not guaranteed to be constant forever and the database can change it if and when it determines that a change is necessary. If your data does not supply a value or combination of values which are unique and unchanging I strongly suggest you learn how to create an artificial key which is automatically set using sequences and triggers. I believe 12c supplies auto-increment columns which you can use if you're using the latest version of Oracle.
Share and enjoy.
There are 2 unique identifiers in Oracle named as ROWNUM and ROWID. You can use them in such ways:-
SELECT ROWNUM
FROM table
WHERE employee_name = 'name';
and
SELECT ROWID
FROM table
WHERE employee_name = 'name';
You can read further about them.
Rownum -
http://docs.oracle.com/cd/B12037_01/server.101/b10759/pseudocolumns008.htm
Rowid - http://docs.oracle.com/cd/B19306_01/server.102/b14200/pseudocolumns008.htm

Getting DBA_USERS information

I am trying to retreive a USER id, from DBA_USERS like we can do in DBA_ROLES.
I've tryied to retreive ROWID column from DBA_ROLES, but i get this warning:
"ORA-01445: cannot select ROWID from, or sample, a join view without a key-preserved table"
From what i can understand, DBA_USERS is a Oracle generated view and it is not possible to retrieve this ROWID. Am i right?
If this is correct, how can i know from which tables this view is generated? Or how can i know the ROWID of a USER?
Kind regards!
Sam
I am trying to retrieve a USER id, from DBA_USERS
You are looking for DBA_USERS.USER_ID :
SQL> SELECT USER_ID FROM DBA_USERS WHERE USERNAME = 'SYLVAIN';
USER_ID
----------
48
I've tryied to retreive ROWID column
ROWID have nothing to do here. Those are kind of "pointers" to the row physical storage. Under some specific conditions they are subject to change. Since views don't have physical storage, ROWID is meaningless for them -- hence the error "ORA-01445" :
from oraerr:
ORA-01445: cannot select ROWID from a join
view without a key-preserved table
Cause: A SELECT statement attempted to select ROWIDs from a view
derived from a join operation. Because the rows selected in the view
do not correspond to underlying physical records, no ROWIDs can be
returned.
Action: Remove ROWID from the view selection clause, then re-execute
the statement.
What Sylvain is talking about is the rownum not the rowid. The rownum is a sequential number, whereas the rowid denotes the physical location of a row.
See here:
0:opal#spmdtz> select rowid, rownum, xxx.* from xxx;
Rowid |rownum|x |y |
------------------------------------
AAAS/3AAGAAAbmYAAA| 1|foo1|foo2|
AAAS/3AAGAAAbmYAAB| 2|bar1|bar2|
The rowid can be useful when you want to update a row. You can say where rowid= ... or in other cases where you want to refer to a row you already "have". I believe it is the fastest way to access a row.
But I don't understand why you would need the rowid in your query.
DBA_USERS is a view, a view which consists of a query on a few tables.
ORA-01445 means that Oracle cannot retrieve the ROWID you request due to the fact that you need to query the relevant table directly (or change the view SQL and query the ROWID also) to get the relevant ROWID (needless to say that if your view is created by joining a couple of tables — how can Oracle determine which ROWID you want?) .
The "main" table DBA_USERS gets data from is sys.USER$ table.
To get the ROWID, first look at the SQL behind DBA_USERS (it's very simple on most IDEs) to understand which data you want to query except the ROWIDs.
Then you can just query:
select ROWID, USER# user_id, NAME username
from sys.USER$;
(or any other column you need).
Good luck!

Resources