Oracle DatabaseMetadate get columns for table named USER - oracle

We are using JDBC DatabaseMetadata to discover objects in the database. DatabaseMetadata.getColumns returns more verbose information -that we really need- than those from select * from table where 1 = 2.
Now when I pass the table name -that is a reserved word in Oracle- to getColumns, it fails. If I escape the table name, it does not get results -as it doesn't see a table with that name (i.e. pass the table name "USER" instead of USER).
Is there a possible way to pass such table -named after reserved words to getColumns for Oracle database.

Related

variable table name in Spring boot

I have tables like "operation_log_202001", "operation_log_202002", ...
(I know this table structure and system structure is so bad. But I have to maintain it.)
When I try to access the "user" table, I can connect to the "user" table easily because it doesn't have any variable part in the table name.
I tried to solve this problem as below.
#Query("select * from :table_name limit 20")
Flux<ReviewData> findAll(String table_name);
But the generated query is "select * from 'operation_log_202001' limit 20". I don't wanna it to append "'" to the table name.
May I ask how to solve this problem?
Create a view, you can change it in runtime and can be mapped to an entity.
Elaborate:
A Database stores informations in tables. Those informations can be aggregated into views using custom SQL-queries. Views can act like tables, and inside their aggregation-query you are able to change the query's table-names - this way the table-name is variable.
Try with database name before table name, example :
Select * from db_log.table_name limit 20;

In what order will Oracle use synonym, view, and table objects?

Let's say that my database has the following objects:
A table on schema "B" named "mlb_players"
A view on schema "C" named "mlb_players" that is a SELECT against a table on server "DB2"
A synonym on schema "D" named "mlb_players" that points to a table on server "DB3"
All of these objects are granted to my schema "RedSoxRule"
If I execute this query, from where would the data be retrieved?
SELECT *
FROM mlb_players
In other words, if a given name (in this case "mlb_players") is applicable to different object types, and the GRANTS are equal, in what order will Oracle find the requested object?
Oracle has a nice long explanation of this.
Basically, when resolving a name, it looks:
For objects in your schema with that name
For public synonyms with that name
If the name has multiple parts (e.g. C.mlb_players, it checks to see if the first part is a qualifying schema that you have access to.
In your example, Oracle won't find any of them. They're all in different schemas from your RedSoxRule schema, none of them are public synonyms, and you didn't qualify mlb_players with a schema name.
It doesn't actually matter what the type of the object is (table, view, synonym, package, etc) - they're all treated the same.
If I execute this query, from where would the data be retrieved
Nowhere. REDSOXRULE has no object called mlb_players so the query would fail with ORA-00942: table or view does not exist.
You would need to prefix the table name with the schema you're prefixing, e.g.
SELECT *
FROM d.mlb_players;
Let's suppose you have a variation on your posted structure.
REDSOXRULE has a view mlb_players for a.mlb_players. The query select * from mlb_players would select from this view, i.e from a.mlb_players.
Instead of a view REDSOXRULE has a private synonym mlb_players for b.mlb_players. The query select * from mlb_players would select from this synonym, i.e from b.mlb_players. Note that you can't have a private synonym with the same name as a table or view in your schema.
Instead of a private synonym the database has a public synonym mlb_players for d.mlb_players. The query select * from mlb_players would select from this publicsynonym, i.e from d.mlb_players.
That is, Oracle looks first for objects owned by the schema (tables, views, private synonyms, etc). Then it looks at public synonyms. Then it looks for objects in other schemas

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'>>;

Oracle does not allow to create "USER" table

Oracle is not allowing to create USER table.
Can anyone guide me to create USER table in Oracle.?
TIA.
Can you use a different name like my_user or something else. If you are insistent about using the table name user then you will have to provide the table name in quotes.
CREATE TABLE "USER"
(
col1 NUMBER(10)
)
You, will have to use quotes and maintain the upper case when doing any operations on this table.
The following will give you an error.
select * from USER;
ORA-00903: invalid table name
However, the following will work.
select * from "USER";
That said I don't recommend this option and it would be good if you can change your table name.
USER is a reserved keyword in oracle. Thus it can't be used directly.
Here is the list of restricted keywords a.k.a reserved words.
e.g. you can't either create a table called TABLE...

The different between TABLE_NAME vs "TABLE_NAME" in Oracle?

I'm a very new Oracle SQL user and I got a problem.
If I create a table with code such as
create table TBL_NAME...
after that I can get data by
select * from TBL_NAME;
However, when I create table with Navicat (just click on button new table), then I have to add "" to table name to access my table, such as:
select * from "TBL_NAME";
So, is there have other type of table? And if I use Navicat to create table, what type of it?
When you specify the name of a table in Oracle without double quotes then Oracle converts that table name to uppercase. But if you specify the name of the table within double-quotes then Oracle will respect the lower-case letters that you may have.
So, as in your example the name of your table is already all upper-case, then there is no difference in specifying or not the double-quotes.
But for example if you create a table like this:
CREATE TABLE "my_table" ....
Then you cannot access it like this:
SELECT * FROM my_table;
as Oracle will convert that select to this:
SELECT * FROM MY_TABLE;
And there is no such table in your system.
In your case with Navicat, it just needs you to specify the name of the table as-is, but don't worry, just put the double-quotes and stick to all upper-case names and you will be fine.

Resources