Getting DBA_USERS information - oracle

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!

Related

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.

View the contents and constraints of a table

I am working on this assignment question and it is asking me:
To create a table called (TEMP_CUST) from an existing table Customers
View the content and constraints of TEMP_CUST table
What I have done so far is I have created my table, didn't add any constraints to the table TEMP_CUST and viewed the table using the DESC command.
Here is the code for table creation
CREATE TABLE TEMP_CUST
AS
(SELECT
CUSTOMER#, LASTNAME,
FIRSTNAME, ADDRESS, CITY,
STATE, ZIP, REFERRED,
REGION, EMAIL
FROM
CUSTOMERS);
DESC TEMP_CUST;
Now that I have done that I want to view the constraints of the table. I have used this command but am not sure if it is correct.
SELECT *
FROM USER_CONSTRAINTS
WHERE TABLE_NAME = 'TEMP_CUST';
i have used this command, but not sure if it is correct.
You haven't said why you don't think it's correct so we have to guess the reason for your doubt. Perhaps it's because the set of constraints you get is smaller than the set of constraints for the original CUSTOMERS table?
That is correct. When we use CREATE TABLE ... AS SELECT the statement creates a new table with the projection, column names and datatypes of the original tables (assuming a vanilla SELECT clause) and the data (determined by the WHERE clause, if any). However, the only constraints which are created are NOT NULL constraints on the primary key column(s) and any other mandatory columns. The new table does not have primary key, foreign key or check constraints. We have to create these explicitly.
Hence, this query ...
SELECT * FROM USER_CONSTRAINTS
WHERE TABLE_NAME = 'TEMP_CUST';
... might return fewer constraints than you were expecting.

ORACLE SQL Query to fetch all table names IN DB whereever given value is treated as PK

Just want to know is this possible.
Say that if i have value 'X' and iam sure that this is referenced in some other tables as PK value but not sure about exactly which table is that, so i would like to know the list of those tables.
Pseudo query of above what i mentioned
SELECT TABLE_NAME FROM DBA_TABLES WHERE <<ATLEAST ONE OF THE TABLE ROW PK VALUE IS MATCHING EQUAL TO 'X'>>;

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

Difference between RowID and REF of an Object Table

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.

Resources